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  public class SingleGroupMatcher implements GroupMatcher {
28      private final String enabled;
29      private final Pattern pattern;
30  
31      private Class<?> enabledClass;
32  
33      public SingleGroupMatcher(String enabled) {
34          this.enabled = enabled.endsWith(".class") ? enabled.substring(0, enabled.length() - 6) : enabled;
35          Pattern p;
36          try {
37              p = Pattern.compile(enabled);
38          } catch (PatternSyntaxException e) {
39              p = null;
40          }
41          pattern = p;
42      }
43  
44      @Override
45      public int hashCode() {
46          final int prime = 31;
47          int result = 1;
48          result = prime * result + enabled.hashCode();
49          return result;
50      }
51  
52      @Override
53      public boolean equals(Object obj) {
54          if (this == obj) {
55              return true;
56          }
57          if (obj == null) {
58              return false;
59          }
60          if (getClass() != obj.getClass()) {
61              return false;
62          }
63          SingleGroupMatcher other = (SingleGroupMatcher) obj;
64          return enabled.equals(other.enabled);
65      }
66  
67      @Override
68      public String toString() {
69          return "*" + enabled;
70      }
71  
72      @Override
73      public boolean enabled(Class<?>... cats) {
74          if (cats != null) {
75              for (Class<?> cls : cats) {
76                  if (enabledClass != null && enabledClass.isAssignableFrom(cls)) {
77                      return true;
78                  }
79  
80                  String name = cls.getName();
81                  if (name.endsWith(enabled)) {
82                      return true;
83                  }
84              }
85          }
86  
87          return false;
88      }
89  
90      @Override
91      public boolean enabled(String... cats) {
92          for (String cat : cats) {
93              if (cat == null || cat.trim().isEmpty()) {
94                  continue;
95              }
96  
97              if (cat.equals(enabled)) {
98                  return true;
99              }
100 
101             if (pattern != null && pattern.matcher(cat).matches()) {
102                 return true;
103             }
104         }
105 
106         return false;
107     }
108 
109     @Override
110     public void loadGroupClasses(ClassLoader classLoader) {
111         try {
112             enabledClass = classLoader.loadClass(enabled);
113         } catch (ClassNotFoundException e) {
114             // class is not available at runtime, for instance this would happen in reactor projects
115             // in which not all modules have the required class on the classpath/module path
116             System.out.println("[WARNING] Couldn't load group class '" + enabled + "' in Surefire|Failsafe plugin. "
117                     + "The group class is ignored!");
118         }
119     }
120 }