update copyright
[fedora-idea.git] / plugins / junit_rt / src / com / intellij / junit4 / JUnit4TestRunnerUtil.java
blob106362f2ec0eaaaaf56d48567ea5162a805bddfa
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.junit4;
18 import org.junit.runner.Description;
19 import org.junit.runner.Request;
20 import org.junit.runner.manipulation.Filter;
22 import java.io.BufferedReader;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.text.MessageFormat;
26 import java.util.*;
28 public class JUnit4TestRunnerUtil {
29 /**
30 * @noinspection HardCodedStringLiteral
32 private static final ResourceBundle ourBundle = ResourceBundle.getBundle("RuntimeBundle");
34 public static Request buildRequest(String[] suiteClassNames) {
35 if (suiteClassNames.length == 0) {
36 return null;
38 Vector result = new Vector();
39 for (int i = 0; i < suiteClassNames.length; i++) {
40 String suiteClassName = suiteClassNames[i];
41 if (suiteClassName.charAt(0) == '@') {
42 // all tests in the package specified
43 try {
44 final Map classMethods = new HashMap();
45 BufferedReader reader = new BufferedReader(new FileReader(suiteClassName.substring(1)));
46 try {
47 reader.readLine(); //skip junit4/junit3 parameter
48 final String packageName = reader.readLine();
49 String line;
51 while ((line = reader.readLine()) != null) {
52 String className = line;
53 final int idx = line.indexOf(',');
54 if (idx != -1) {
55 className = line.substring(0, idx);
56 Set methodNames = (Set)classMethods.get(className);
57 if (methodNames == null) {
58 methodNames = new HashSet();
59 classMethods.put(className, methodNames);
61 methodNames.add(line.substring(idx + 1));
64 appendTestClass(result, className);
66 String suiteName = packageName.length() == 0 ? "<default package>": packageName;
67 Class[] classes = getArrayOfClasses(result);
68 Request allClasses;
69 try {
70 Class.forName("org.junit.runner.Computer");
71 allClasses = JUnit46ClassesRequestBuilder.getClassesRequest(suiteName, classes);
73 catch (ClassNotFoundException e) {
74 try {
75 Class.forName("org.junit.internal.requests.ClassesRequest");
76 allClasses = JUnit4ClassesRequestBuilder.getClassesRequest(suiteName, classes);
78 catch (ClassNotFoundException e1) {
79 allClasses = JUnit45ClassesRequestBuilder.getClassesRequest(suiteName, classes);
83 return classMethods.isEmpty() ? allClasses : allClasses.filterWith(new Filter() {
84 public boolean shouldRun(Description description) {
85 if (description.isTest()) {
86 final Set methods = (Set)classMethods.get(JUnit4ReflectionUtil.getClassName(description));
87 return methods == null || methods.contains(JUnit4ReflectionUtil.getMethodName(description));
89 return true;
92 public String describe() {
93 return "Failed tests";
95 });
97 finally {
98 reader.close();
101 catch (IOException e) {
102 e.printStackTrace();
103 System.exit(1);
106 else {
107 int index = suiteClassName.indexOf(',');
108 if (index != -1) {
109 return Request.method(loadTestClass(suiteClassName.substring(0, index)), suiteClassName.substring(index + 1));
111 appendTestClass(result, suiteClassName);
115 return result.size() == 1 ? Request.aClass((Class)result.get(0)) : Request.classes(getArrayOfClasses(result));
118 private static void appendTestClass(Vector result, String className) {
119 final Class aClass = loadTestClass(className);
120 if (!result.contains(aClass)) { //do not append classes twice: rerun failed tests from one test suite
121 result.addElement(aClass);
125 private static Class[] getArrayOfClasses(Vector result) {
126 Class[] classes = new Class[result.size()];
127 for (int i = 0; i < result.size(); i++) {
128 classes[i] = (Class)result.get(i);
130 return classes;
133 private static Class loadTestClass(String suiteClassName) {
134 try {
135 return Class.forName(suiteClassName);
137 catch (ClassNotFoundException e) {
138 String clazz = e.getMessage();
139 if (clazz == null) {
140 clazz = suiteClassName;
142 System.err.print(MessageFormat.format(ourBundle.getString("junit.class.not.found"), new Object[]{clazz}));
143 System.exit(1);
145 catch (Exception e) {
146 System.err.println(MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[]{e.toString()}));
147 System.exit(1);
149 return null;
152 public static String testsFoundInPackageMesage(int testCount, String name) {
153 return MessageFormat.format(ourBundle.getString("tests.found.in.package"), new Object[]{new Integer(testCount), name});