suite presentation/navigation fixed (DEA-24794)
[fedora-idea.git] / plugins / junit_rt / src / com / intellij / junit3 / TestRunnerUtil.java
blob51d8f6c03488d61993a1b59a2520347747e41fd8
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.junit3;
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestResult;
21 import junit.framework.TestSuite;
22 import junit.runner.BaseTestRunner;
24 import java.io.BufferedReader;
25 import java.io.FileReader;
26 import java.lang.reflect.Constructor;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import java.lang.reflect.Modifier;
30 import java.text.MessageFormat;
31 import java.util.ResourceBundle;
32 import java.util.Vector;
34 public class TestRunnerUtil {
35 /** @noinspection HardCodedStringLiteral*/
36 private static final ResourceBundle ourBundle = ResourceBundle.getBundle("RuntimeBundle");
38 public static Test getTestSuite(JUnit3IdeaTestRunner runner, String[] suiteClassNames){
39 if (suiteClassNames.length == 0) {
40 return null;
42 Vector result = new Vector();
43 for (int i = 0; i < suiteClassNames.length; i++) {
44 String suiteClassName = suiteClassNames[i];
45 Test test;
46 if (suiteClassName.charAt(0) == '@') {
47 // all tests in the package specified
48 String[] classNames;
49 String suiteName;
50 try {
51 BufferedReader reader = new BufferedReader(new FileReader(suiteClassName.substring(1)));
52 Vector vector;
53 try {
54 reader.readLine(); //skip junit4/junit3 parameter
55 suiteName = reader.readLine();
56 vector = new Vector();
57 String line;
58 while ((line = reader.readLine()) != null) {
59 vector.addElement(line);
62 finally {
63 reader.close();
66 // toArray cannot be used here because the class must be compilable with 1.1
67 classNames = new String[vector.size()];
68 for (int j = 0; j < classNames.length; j++) {
69 classNames[j] = (String)vector.elementAt(j);
72 catch (Exception e) {
73 runner.runFailed(MessageFormat.format(ourBundle.getString("junit.runner.error"), new Object[] {e.toString()}));
74 return null;
76 test = new TestAllInPackage2(runner, suiteName, classNames);
78 else {
79 test = createClassOrMethodSuite(runner, suiteClassName);
80 if (test == null) return null;
82 result.addElement(test);
84 if (result.size() == 1) {
85 return (Test)result.elementAt(0);
87 else {
88 TestSuite suite = new TestSuite();
89 for (int i = 0; i < result.size(); i++) {
90 final Test test = (Test)result.elementAt(i);
91 suite.addTest(test);
93 return suite;
97 public static Test createClassOrMethodSuite(JUnit3IdeaTestRunner runner, String suiteClassName) {
98 String methodName = null;
99 int index = suiteClassName.indexOf(',');
100 if (index != -1) {
101 methodName = suiteClassName.substring(index + 1);
102 suiteClassName = suiteClassName.substring(0, index);
105 Class testClass = loadTestClass(runner, suiteClassName);
106 if (testClass == null) return null;
107 Test test = null;
108 if (methodName == null) {
109 if (test == null) {
110 try {
111 Method suiteMethod = testClass.getMethod(BaseTestRunner.SUITE_METHODNAME, new Class[0]);
112 if (!Modifier.isStatic(suiteMethod.getModifiers())) {
113 String message = MessageFormat.format(ourBundle.getString("junit.suite.must.be.static"), new Object[]{testClass.getName()});
114 System.err.println(message);
115 //runFailed(message);
116 return null;
118 try {
119 test = (Test)suiteMethod.invoke(null, new Class[0]); // static method
120 test = new SuiteMethodWrapper(test, suiteClassName);
122 catch (final InvocationTargetException e) {
123 final String message = MessageFormat.format(ourBundle.getString("junit.failed.to.invoke.suite"), new Object[]{testClass + " " + e.getTargetException().toString()});
124 //System.err.println(message);
125 //runner.runFailed(message);
126 runner.clearStatus();
127 return new FailedTestCase(testClass, BaseTestRunner.SUITE_METHODNAME, message, e);
129 catch (IllegalAccessException e) {
130 String message = MessageFormat.format(ourBundle.getString("junit.failed.to.invoke.suite"), new Object[]{testClass + " " + e.toString()});
131 //System.err.println(message);
132 //runner.runFailed(message);
133 return new FailedTestCase(testClass, BaseTestRunner.SUITE_METHODNAME, message, e);
136 catch (Throwable e) {
137 // try to extract a test suite automatically
138 runner.clearStatus();
139 test = new TestSuite(testClass);
143 else {
144 test = createMethodSuite(runner, testClass, methodName);
146 return test;
149 private static Class loadTestClass(JUnit3IdeaTestRunner runner, String suiteClassName) {
150 try {
151 return Class.forName(suiteClassName);
153 catch (ClassNotFoundException e) {
154 String clazz = e.getMessage();
155 if (clazz == null) {
156 clazz = suiteClassName;
158 runner.runFailed(MessageFormat.format(ourBundle.getString("junit.class.not.found"), new Object[] {clazz}));
160 catch (Throwable e) {
161 runner.runFailed(MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[]{e.toString()}));
163 return null;
166 private static Test createMethodSuite(JUnit3IdeaTestRunner runner, Class testClass, String methodName) {
167 runner.clearStatus();
168 try {
169 Constructor constructor = testClass.getConstructor(new Class[]{String.class});
170 return (Test)constructor.newInstance(new Object[]{methodName});
172 catch (NoSuchMethodException e) {
173 try {
174 Constructor constructor = testClass.getConstructor(new Class[0]);
175 TestCase test = (TestCase)constructor.newInstance(new Object[0]);
176 test.setName(methodName);
177 return test;
179 catch(ClassCastException e1) {
180 boolean methodExists;
181 try {
182 testClass.getMethod(methodName, new Class[0]);
183 methodExists = true;
185 catch (NoSuchMethodException e2) {
186 methodExists = false;
188 if (!methodExists) {
189 String error = MessageFormat.format(ourBundle.getString("junit.method.not.found"), new Object[]{methodName});
190 String message = MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[]{error});
191 return new FailedTestCase(testClass, methodName, message, null);
193 runner.runFailed(MessageFormat.format(ourBundle.getString("junit.class.not.derived"), new Object[]{testClass.getName()}));
194 return null;
196 catch (Exception e1) {
197 String message = MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[]{e1.toString()});
198 return new FailedTestCase(testClass, methodName, message, e1);
201 catch (Throwable e) {
202 String message = MessageFormat.format(ourBundle.getString("junit.cannot.instantiate.tests"), new Object[]{e.toString()});
203 return new FailedTestCase(testClass, methodName, message, e);
207 public static String testsFoundInPackageMesage(int testCount, String name) {
208 return MessageFormat.format(ourBundle.getString("tests.found.in.package"), new Object[]{new Integer(testCount), name});
211 public static class FailedTestCase extends TestCase {
212 private final String myMethodName;
213 private final String myMessage;
214 private final Throwable myThrowable;
216 public FailedTestCase(final Class testClass, final String methodName, final String message, final Throwable e) {
217 super(testClass.getName());
218 myMethodName = methodName;
219 myMessage = message;
220 myThrowable = e;
223 public String getMethodName() {
224 return myMethodName;
227 public String getMessage() {
228 return myMessage;
231 protected void runTest() throws Throwable {
232 try {
233 throw new RuntimeException(myMessage, myThrowable);
235 catch (NoSuchMethodError e) {
236 throw new RuntimeException(myMessage);
241 public static class SuiteMethodWrapper implements Test {
242 private Test mySuite;
243 private String myClassName;
245 public SuiteMethodWrapper(Test suite, String className) {
246 mySuite = suite;
247 myClassName = className;
250 public String getClassName() {
251 return myClassName;
254 public int countTestCases() {
255 return mySuite.countTestCases();
258 public void run(TestResult result) {
259 mySuite.run(result);
262 public Test getSuite() {
263 return mySuite;