1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.dataportal.junit;
|
5
|
|
6
|
import java.lang.annotation.ElementType;
|
7
|
import java.lang.annotation.Inherited;
|
8
|
import java.lang.annotation.Retention;
|
9
|
import java.lang.annotation.RetentionPolicy;
|
10
|
import java.lang.annotation.Target;
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collections;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.junit.runner.Runner;
|
16
|
import org.junit.runner.notification.RunNotifier;
|
17
|
import org.junit.runners.BlockJUnit4ClassRunner;
|
18
|
import org.junit.runners.Suite;
|
19
|
import org.junit.runners.model.FrameworkMethod;
|
20
|
import org.junit.runners.model.InitializationError;
|
21
|
import org.junit.runners.model.Statement;
|
22
|
|
23
|
import eu.etaxonomy.dataportal.DataPortalContext;
|
24
|
|
25
|
|
26
|
/**
|
27
|
* @author a.kohlbecker
|
28
|
*
|
29
|
*/
|
30
|
public class DataPortalContextSuite extends Suite{
|
31
|
|
32
|
/**
|
33
|
* Only to be used for test classes which extend {@link CdmDataPortalTestBase}
|
34
|
*
|
35
|
* @author a.kohlbecker
|
36
|
*/
|
37
|
@Retention(RetentionPolicy.RUNTIME)
|
38
|
@Target(ElementType.TYPE)
|
39
|
@Inherited
|
40
|
public @interface DataPortalContexts {
|
41
|
/**
|
42
|
* @return an array of DataPortalContext to which the annotated test
|
43
|
* class is applicable
|
44
|
*/
|
45
|
DataPortalContext[] value();
|
46
|
}
|
47
|
|
48
|
private final List<Runner> runners = new ArrayList<Runner>();
|
49
|
|
50
|
|
51
|
private class TestClassRunnerWithDataPortalContext extends BlockJUnit4ClassRunner {
|
52
|
|
53
|
private final DataPortalContext context;
|
54
|
|
55
|
/**
|
56
|
* @param klass
|
57
|
* @throws InitializationError
|
58
|
*/
|
59
|
public TestClassRunnerWithDataPortalContext(Class<?> klass, DataPortalContext context) throws InitializationError {
|
60
|
super(klass);
|
61
|
this.context = context;
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public Object createTest() throws Exception {
|
66
|
Object testClass = getTestClass().getOnlyConstructor().newInstance();
|
67
|
((CdmDataPortalTestBase)testClass).setContext(context);
|
68
|
return testClass;
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
protected String getName() {
|
73
|
return String.format("%s@%s", getTestClass().getName(), context.name());
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
protected String testName(final FrameworkMethod method) {
|
78
|
return String.format("%s@%s", method.getName(), context.name());
|
79
|
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
protected Statement classBlock(RunNotifier notifier) {
|
84
|
return childrenInvoker(notifier);
|
85
|
}
|
86
|
|
87
|
@Override
|
88
|
protected void validateZeroArgConstructor(List<Throwable> errors) {
|
89
|
super.validateZeroArgConstructor(errors);
|
90
|
validateCdmDataPortalTestBase(errors);
|
91
|
}
|
92
|
|
93
|
protected void validateCdmDataPortalTestBase(List<Throwable> errors) {
|
94
|
// constructor should have exactly one arg
|
95
|
if ( ! CdmDataPortalTestBase.class.isAssignableFrom(getTestClass().getJavaClass()) ){
|
96
|
String gripe= "Test class must be a subclass of " + CdmDataPortalTestBase.class.getName();
|
97
|
errors.add(new Exception(gripe));
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Only called reflectively. Do not use programmatically.
|
104
|
*/
|
105
|
public DataPortalContextSuite(Class<?> klass) throws InitializationError {
|
106
|
super(klass, Collections.<Runner>emptyList());
|
107
|
DataPortalContexts dataPortalContextsAnotation = getTestClass().getJavaClass().getAnnotation(DataPortalContexts.class);
|
108
|
for (DataPortalContext cntxt : dataPortalContextsAnotation.value()) {
|
109
|
runners.add(new TestClassRunnerWithDataPortalContext(klass, cntxt));
|
110
|
}
|
111
|
}
|
112
|
|
113
|
@Override
|
114
|
protected List<Runner> getChildren() {
|
115
|
return runners;
|
116
|
}
|
117
|
|
118
|
}
|