Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f7b32e833a57ad4de79c3be78db8dd990568cce1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*****************************************************************************
 * Copyright (c) 2010, 2015 CEA LIST, Christian W. Damus, and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bugs 402525, 323802, 431953, 433310, 434993
 *  Christian W. Damus - bug 399859
 *  Christian W. Damus - bug 451230
 *
 *****************************************************************************/

package org.eclipse.papyrus.junit.framework.runner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.junit.runners.Suite;
import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.TestClass;

/**
 * Finds and runs tests.
 */
public class AllTestsRunner extends Suite {

	/**
	 * Constructor.
	 *
	 * @param clazz
	 *            the suite class � AllTests2
	 * @throws InitializationError
	 *             if there's a problem
	 * @throws org.junit.runners.model.InitializationError
	 */
	public AllTestsRunner(final Class<?> clazz) throws InitializationError {
		super(clazz, getSuites(clazz));
	}

	/**
	 * Returns the list of test classes
	 *
	 * @return the list of test classes
	 */
	static Class<?>[] getSuites(final Class<?> clazz) {
		// retrieve all test suites.
		final Collection<Class<?>> suites = new ArrayList<Class<?>>();

		TestClass testClass = new TestClass(clazz);
		List<ITestSuiteClass> suiteClassSpecs = new ArrayList<ITestSuiteClass>();
		List<FrameworkField> suiteSpots = testClass.getAnnotatedFields(SuiteSpot.class);
		for (FrameworkField spot : suiteSpots) {
			if (spot.isStatic()) {
				try {
					Object value = spot.get(null);
					Iterable<?> listValue;
					if (value instanceof Object[]) {
						listValue = Arrays.asList((Object[]) value);
					} else if (value instanceof Iterable<?>) {
						listValue = (Iterable<?>) value;
					} else {
						listValue = Collections.emptyList();
					}
					for (Object next : listValue) {
						if (next instanceof ITestSuiteClass) {
							suiteClassSpecs.add((ITestSuiteClass) next);
						}
					}
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		if (suiteClassSpecs.isEmpty()) {
			System.err.println("No suite classes specified in class " + clazz.getName());
		} else {
			for (final ITestSuiteClass testSuiteClass : suiteClassSpecs) {
				final Class<?> class_ = testSuiteClass.getMainTestSuiteClass();
				if (class_ != null) {
					suites.add(class_);
				} else {
					System.err.println(testSuiteClass + " does not give a correct test suite class");
				}
			}
		}
		return suites.toArray(new Class<?>[] {});
	}
}

Back to the top