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