Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aaf8055b601b92a591886a21634a38acc6206611 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package org.eclipse.emf.ecore.xcore.tests.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.Suite;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;

public class LabelledParameterized extends Suite
{
	@Retention(RetentionPolicy.RUNTIME)
	@Target(ElementType.METHOD)
	public static @interface LabelledParameters
	{
	}

	private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner
	{
		private final int parameterSetNumber;

		private final List<Object[]> parameters;

		TestClassRunnerForParameters(Class<?> type, List<Object[]> parameterList, int i) throws InitializationError
		{
			super(type);
			parameters = parameterList;
			parameterSetNumber = i;
		}

		@Override
		public Object createTest() throws Exception
		{
			return getTestClass().getOnlyConstructor().newInstance(computeParams());
		}

		protected Object[] computeParams() throws Exception
		{
			try
			{
				return parameters.get(parameterSetNumber);
			} catch (ClassCastException e)
			{
				throw new Exception(String.format("%s.%s() must return a Collection of arrays.", getTestClass().getName(),
				    getParametersMethod(getTestClass()).getName()));
			}
		}

		@Override
		protected String getName()
		{
			return String.format("%s", parameters.get(parameterSetNumber)[0]);
		}

		@Override
		protected String testName(final FrameworkMethod method)
		{
			return String.format("%s -> %s", parameters.get(parameterSetNumber)[0], method.getName());
		}

		@Override
		protected void validateZeroArgConstructor(List<Throwable> errors)
		{
			// super.validateZeroArgConstructor(errors);
		}

		@Override
		protected Statement classBlock(RunNotifier notifier)
		{
			return childrenInvoker(notifier);
		}
	}

	protected final ArrayList<Runner> runners = new ArrayList<Runner>();

	/**
	 * Only called reflectively. Do not use programmatically.
	 */
	public LabelledParameterized(Class<?> klass) throws Throwable
	{
		super(klass, Collections.<Runner> emptyList());
		List<Object[]> parametersList = getParametersList(getTestClass());
		for (int i = 0; i < parametersList.size(); i++)
			runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(), parametersList, i));
	}

	@Override
	protected List<Runner> getChildren()
	{
		return runners;
	}

	@SuppressWarnings("unchecked")
	protected List<Object[]> getParametersList(TestClass klass) throws Throwable
	{
		return (List<Object[]>) getParametersMethod(klass).invokeExplosively(null);
	}

	protected FrameworkMethod getParametersMethod(TestClass testClass) throws Exception
	{
		List<FrameworkMethod> methods = testClass.getAnnotatedMethods(LabelledParameters.class);
		for (FrameworkMethod each : methods)
		{
			int modifiers = each.getMethod().getModifiers();
			if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
				return each;
		}

		throw new Exception("No public static parameters method on class " + testClass.getName());
	}

}

Back to the top