1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
116
117 System.out.println("[WARNING] Couldn't load group class '" + enabled + "' in Surefire|Failsafe plugin. "
118 + "The group class is ignored!");
119 }
120 }
121 }