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  
33      private String enabled;
34  
35      private Class<?> enabledClass;
36  
37      private Pattern pattern;
38  
39      public SingleGroupMatcher( String enabled )
40      {
41          this.enabled = enabled.endsWith( ".class" ) ? enabled.substring( 0, enabled.length() - 6 ) : enabled;
42  
43          try
44          {
45              this.pattern = Pattern.compile( enabled );
46          }
47          catch ( PatternSyntaxException pse )
48          {
49              // ignore
50          }
51      }
52  
53      @Override
54      public int hashCode()
55      {
56          final int prime = 31;
57          int result = 1;
58          result = prime * result + ( ( enabled == null ) ? 0 : enabled.hashCode() );
59          return result;
60      }
61  
62      @Override
63      public boolean equals( Object obj )
64      {
65          if ( this == obj )
66          {
67              return true;
68          }
69          if ( obj == null )
70          {
71              return false;
72          }
73          if ( getClass() != obj.getClass() )
74          {
75              return false;
76          }
77          SingleGroupMatcher other = (SingleGroupMatcher) obj;
78          if ( enabled == null )
79          {
80              if ( other.enabled != null )
81              {
82                  return false;
83              }
84          }
85          else if ( !enabled.equals( other.enabled ) )
86          {
87              return false;
88          }
89          return true;
90      }
91  
92      @Override
93      public String toString()
94      {
95          return "*" + enabled;
96      }
97  
98      public boolean enabled( Class<?>... cats )
99      {
100         if ( cats != null )
101         {
102             for ( Class<?> cls : cats )
103             {
104                 if ( enabledClass != null && enabledClass.isAssignableFrom( cls ) )
105                 {
106                     return true;
107                 }
108 
109                 String name = cls.getName();
110                 if ( name.endsWith( enabled ) )
111                 {
112                     return true;
113                 }
114             }
115         }
116 
117         return false;
118     }
119 
120     public boolean enabled( String... cats )
121     {
122         if ( enabled == null )
123         {
124             return true;
125         }
126 
127         for ( String cat : cats )
128         {
129             if ( cat == null || cat.trim().length() < 1 )
130             {
131                 continue;
132             }
133 
134             // System.out.println( cat + ".endsWith(" + enabled + ")? " + ( cat.endsWith( enabled ) ) );
135             if ( cat.endsWith( enabled ) )
136             {
137                 return true;
138             }
139 
140             if ( pattern != null && pattern.matcher( cat ).matches() )
141             {
142                 return true;
143             }
144         }
145 
146         return false;
147     }
148 
149     public void loadGroupClasses( ClassLoader cloader )
150     {
151         try
152         {
153             enabledClass = cloader.loadClass( enabled );
154         }
155         catch ( ClassNotFoundException e )
156         {
157             throw new RuntimeException( "Unable to load category: " + enabled, e );
158         }
159     }
160 
161 }