1 package org.apache.maven.surefire.group.match;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.regex.Pattern;
23 import java.util.regex.PatternSyntaxException;
24
25
26
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 public boolean enabled( Class<?>... cats )
87 {
88 if ( cats != null )
89 {
90 for ( Class<?> cls : cats )
91 {
92 if ( enabledClass != null && enabledClass.isAssignableFrom( cls ) )
93 {
94 return true;
95 }
96
97 String name = cls.getName();
98 if ( name.endsWith( enabled ) )
99 {
100 return true;
101 }
102 }
103 }
104
105 return false;
106 }
107
108 public boolean enabled( String... cats )
109 {
110 for ( String cat : cats )
111 {
112 if ( cat == null || cat.trim().length() < 1 )
113 {
114 continue;
115 }
116
117 if ( cat.endsWith( enabled ) )
118 {
119 return true;
120 }
121
122 if ( pattern != null && pattern.matcher( cat ).matches() )
123 {
124 return true;
125 }
126 }
127
128 return false;
129 }
130
131 public void loadGroupClasses( ClassLoader classLoader )
132 {
133 try
134 {
135 enabledClass = classLoader.loadClass( enabled );
136 }
137 catch ( ClassNotFoundException e )
138 {
139 throw new RuntimeException( "Unable to load category: " + enabled, e );
140 }
141 }
142 }