cleanup
[fedora-idea.git] / platform / testFramework / src / com / intellij / TestClassesFilter.java
blobc494d2f70bdc5a217538aed375c9c3a399971c2a
1 /*
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.
16 package com.intellij;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.io.LineNumberReader;
21 import java.util.*;
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) {
50 try {
51 Map<String, List<String>> groupNameToPatternsMap = new HashMap<String, List<String>>();
52 String currentGroupName = "";
53 LineNumberReader lineNumberReader = new LineNumberReader(inputStreamReader);
54 String line;
55 while ((line = lineNumberReader.readLine()) != null) {
56 if (line.startsWith("[") && line.endsWith("]")) {
57 currentGroupName = line.substring(1, line.length() - 1);
59 else {
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()) {
77 return true;
80 return false;
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)) {
90 return !result;
92 else {
93 return result;
97 private static boolean isAllExcludeDefinedGroup(String groupName) {
98 if (groupName == null){
99 return true;
102 if (ALL_EXCLUDE_DEFINED.equals(groupName)){
103 return true;
106 return false;
109 private List<Pattern> collectPatternsFor(String groupName) {
110 if (isAllExcludeDefinedGroup(groupName)){
111 return myAllPatterns;
112 } else {
113 if (!myPatterns.containsKey(groupName)){
114 return EMPTY_LIST;
115 } else {
116 return myPatterns.get(groupName);