2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 import java
.io
.IOException
;
19 import java
.io
.InputStreamReader
;
20 import java
.io
.LineNumberReader
;
22 import java
.util
.regex
.Pattern
;
24 public class TestClassesFilter
{
26 private final Map
<String
, List
<Pattern
>> myPatterns
= new HashMap
<String
, List
<Pattern
>>();
27 public static final TestClassesFilter EMPTY_CLASSES_FILTER
= new TestClassesFilter(new HashMap
<String
, List
<String
>>());
28 public static final ArrayList
<Pattern
> EMPTY_LIST
= new ArrayList
<Pattern
>();
29 private final List
<Pattern
> myAllPatterns
= new ArrayList
<Pattern
>();
30 public static final String ALL_EXCLUDE_DEFINED
= "ALL_EXCLUDE_DEFINED";
32 private TestClassesFilter(Map
<String
, List
<String
>> filters
) {
34 for (String groupName
: filters
.keySet()) {
35 List
<String
> filterList
= filters
.get(groupName
);
36 ArrayList
<Pattern
> patterns
= new ArrayList
<Pattern
>();
37 myPatterns
.put(groupName
, patterns
);
38 for (String aFilter
: filterList
) {
39 String filter
= aFilter
.trim();
40 if (filter
.length() == 0) continue;
41 filter
= filter
.replaceAll("\\*", ".\\*");
42 Pattern pattern
= Pattern
.compile(filter
);
43 myAllPatterns
.add(pattern
);
44 patterns
.add(pattern
);
49 public static TestClassesFilter
createOn(InputStreamReader inputStreamReader
) {
51 Map
<String
, List
<String
>> groupNameToPatternsMap
= new HashMap
<String
, List
<String
>>();
52 String currentGroupName
= "";
53 LineNumberReader lineNumberReader
= new LineNumberReader(inputStreamReader
);
55 while ((line
= lineNumberReader
.readLine()) != null) {
56 if (line
.startsWith("[") && line
.endsWith("]")) {
57 currentGroupName
= line
.substring(1, line
.length() - 1);
60 if (!groupNameToPatternsMap
.containsKey(currentGroupName
)) {
61 groupNameToPatternsMap
.put(currentGroupName
, new ArrayList
<String
>());
63 groupNameToPatternsMap
.get(currentGroupName
).add(line
);
67 return new TestClassesFilter(groupNameToPatternsMap
);
69 catch (IOException e
) {
70 return EMPTY_CLASSES_FILTER
;
74 private static boolean matches(Collection
<Pattern
> patterns
, String className
) {
75 for (Pattern pattern
: patterns
) {
76 if (pattern
.matcher(className
).matches()) {
85 public boolean matches(String className
, String groupName
) {
86 List
<Pattern
> patterns
= collectPatternsFor(groupName
);
87 boolean result
= matches(patterns
, className
);
88 //null group means all patterns from each defined group should be excluded
89 if (isAllExcludeDefinedGroup(groupName
)) {
97 private static boolean isAllExcludeDefinedGroup(String groupName
) {
98 if (groupName
== null){
102 if (ALL_EXCLUDE_DEFINED
.equals(groupName
)){
109 private List
<Pattern
> collectPatternsFor(String groupName
) {
110 if (isAllExcludeDefinedGroup(groupName
)){
111 return myAllPatterns
;
113 if (!myPatterns
.containsKey(groupName
)){
116 return myPatterns
.get(groupName
);