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      @Override
87      public boolean enabled( Class<?>... cats )
88      {
89          if ( cats != null )
90          {
91              for ( Class<?> cls : cats )
92              {
93                  if ( enabledClass != null && enabledClass.isAssignableFrom( cls ) )
94                  {
95                      return true;
96                  }
97  
98                  String name = cls.getName();
99                  if ( name.endsWith( enabled ) )
100                 {
101                     return true;
102                 }
103             }
104         }
105 
106         return false;
107     }
108 
109     @Override
110     public boolean enabled( String... cats )
111     {
112         for ( String cat : cats )
113         {
114             if ( cat == null || cat.trim().isEmpty() )
115             {
116                 continue;
117             }
118 
119             if ( cat.equals( enabled ) )
120             {
121                 return true;
122             }
123 
124             if ( pattern != null && pattern.matcher( cat ).matches() )
125             {
126                 return true;
127             }
128         }
129 
130         return false;
131     }
132 
133     @Override
134     public void loadGroupClasses( ClassLoader classLoader )
135     {
136         try
137         {
138             enabledClass = classLoader.loadClass( enabled );
139         }
140         catch ( ClassNotFoundException e )
141         {
142             // class is not available at runtime, for instance this would happen in reactor projects
143             // in which not all modules have the required class on the classpath/module path
144             System.out.println( "[WARNING] Couldn't load group class '" + enabled + "' in Surefire|Failsafe plugin. "
145                     + "The group class is ignored!" );
146         }
147     }
148 }