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