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 import java.util.regex.Pattern;
22 import java.util.regex.PatternSyntaxException;
23
24 public class SingleGroupMatcher
25 implements GroupMatcher
26 {
27
28 private String enabled;
29
30 private Class<?> enabledClass;
31
32 private Pattern pattern;
33
34 public SingleGroupMatcher( String enabled )
35 {
36 this.enabled = enabled.endsWith( ".class" ) ? enabled.substring( 0, enabled.length() - 6 ) : enabled;
37
38 try
39 {
40 this.pattern = Pattern.compile( enabled );
41 }
42 catch ( PatternSyntaxException pse )
43 {
44
45 }
46 }
47
48 @Override
49 public int hashCode()
50 {
51 final int prime = 31;
52 int result = 1;
53 result = prime * result + ( ( enabled == null ) ? 0 : enabled.hashCode() );
54 return result;
55 }
56
57 @Override
58 public boolean equals( Object obj )
59 {
60 if ( this == obj )
61 {
62 return true;
63 }
64 if ( obj == null )
65 {
66 return false;
67 }
68 if ( getClass() != obj.getClass() )
69 {
70 return false;
71 }
72 SingleGroupMatcher other = (SingleGroupMatcher) obj;
73 if ( enabled == null )
74 {
75 if ( other.enabled != null )
76 {
77 return false;
78 }
79 }
80 else if ( !enabled.equals( other.enabled ) )
81 {
82 return false;
83 }
84 return true;
85 }
86
87 @Override
88 public String toString()
89 {
90 return "*" + enabled;
91 }
92
93 public boolean enabled( Class<?>... cats )
94 {
95 if ( cats != null )
96 {
97 for ( Class<?> cls : cats )
98 {
99 if ( enabledClass != null && enabledClass.isAssignableFrom( cls ) )
100 {
101 return true;
102 }
103
104 String name = cls.getName();
105 if ( name.endsWith( enabled ) )
106 {
107 return true;
108 }
109 }
110 }
111
112 return false;
113 }
114
115 public boolean enabled( String... cats )
116 {
117 if ( enabled == null )
118 {
119 return true;
120 }
121
122 for ( String cat : cats )
123 {
124 if ( cat == null || cat.trim().length() < 1 )
125 {
126 continue;
127 }
128
129
130 if ( cat.endsWith( enabled ) )
131 {
132 return true;
133 }
134
135 if ( pattern != null && pattern.matcher( cat ).matches() )
136 {
137 return true;
138 }
139 }
140
141 return false;
142 }
143
144 public void loadGroupClasses( ClassLoader cloader )
145 {
146 try
147 {
148 enabledClass = cloader.loadClass( enabled );
149 }
150 catch ( ClassNotFoundException e )
151 {
152 throw new RuntimeException( "Unable to load category: " + enabled, e );
153 }
154 }
155
156 }