View Javadoc

1   package org.apache.maven.surefire.group.match;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  
22  public class SingleGroupMatcher
23      implements GroupMatcher
24  {
25  
26      private String enabled;
27  
28      private Class<?> enabledClass;
29  
30      public SingleGroupMatcher( String enabled )
31      {
32          this.enabled = enabled.endsWith( ".class" ) ? enabled.substring( 0, enabled.length() - 6 ) : enabled;
33      }
34  
35      @Override
36      public int hashCode()
37      {
38          final int prime = 31;
39          int result = 1;
40          result = prime * result + ( ( enabled == null ) ? 0 : enabled.hashCode() );
41          return result;
42      }
43  
44      @Override
45      public boolean equals( Object obj )
46      {
47          if ( this == obj )
48          {
49              return true;
50          }
51          if ( obj == null )
52          {
53              return false;
54          }
55          if ( getClass() != obj.getClass() )
56          {
57              return false;
58          }
59          SingleGroupMatcher other = (SingleGroupMatcher) obj;
60          if ( enabled == null )
61          {
62              if ( other.enabled != null )
63              {
64                  return false;
65              }
66          }
67          else if ( !enabled.equals( other.enabled ) )
68          {
69              return false;
70          }
71          return true;
72      }
73  
74      @Override
75      public String toString()
76      {
77          return "*" + enabled;
78      }
79  
80      public boolean enabled( Class<?>... cats )
81      {
82          if ( cats != null )
83          {
84              for ( Class<?> cls : cats )
85              {
86                  if ( enabledClass != null && enabledClass.isAssignableFrom( cls ) )
87                  {
88                      return true;
89                  }
90  
91                  String name = cls.getName();
92                  if ( name.endsWith( enabled ) )
93                  {
94                      return true;
95                  }
96              }
97          }
98  
99          return false;
100     }
101 
102     public boolean enabled( String... cats )
103     {
104         if ( enabled == null )
105         {
106             return true;
107         }
108 
109         for ( String cat : cats )
110         {
111             if ( cat == null || cat.trim().length() < 1 )
112             {
113                 continue;
114             }
115 
116             // System.out.println( cat + ".endsWith(" + enabled + ")? " + ( cat.endsWith( enabled ) ) );
117             if ( cat.endsWith( enabled ) )
118             {
119                 return true;
120             }
121         }
122 
123         return false;
124     }
125 
126     public void loadGroupClasses( ClassLoader cloader )
127     {
128         try
129         {
130             enabledClass = cloader.loadClass( enabled );
131         }
132         catch ( ClassNotFoundException e )
133         {
134             throw new RuntimeException( "Unable to load category: " + enabled, e );
135         }
136     }
137 
138 }