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