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.net.MalformedURLException;
|
12
|
import java.net.URL;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Collections;
|
15
|
import java.util.List;
|
16
|
|
17
|
import org.junit.runner.Runner;
|
18
|
import org.junit.runner.notification.RunNotifier;
|
19
|
import org.junit.runners.BlockJUnit4ClassRunner;
|
20
|
import org.junit.runners.Suite;
|
21
|
import org.junit.runners.model.FrameworkMethod;
|
22
|
import org.junit.runners.model.InitializationError;
|
23
|
import org.junit.runners.model.Statement;
|
24
|
|
25
|
import eu.etaxonomy.dataportal.DataPortalContext;
|
26
|
import eu.etaxonomy.dataportal.DataPortalContextProvider;
|
27
|
import eu.etaxonomy.dataportal.DataPortalSite;
|
28
|
import eu.etaxonomy.dataportal.DataPortalSiteContextProvider;
|
29
|
import eu.etaxonomy.dataportal.DataPortalsListContextProvider;
|
30
|
|
31
|
|
32
|
/**
|
33
|
* @author a.kohlbecker
|
34
|
*
|
35
|
*/
|
36
|
public class DataPortalContextSuite extends Suite{
|
37
|
|
38
|
public static final String SYSTEM_PROPERTY_SITE_LIST_URL = "SiteListUrl";
|
39
|
|
40
|
/**
|
41
|
* Only to be used for test classes which extend {@link CdmDataPortalTestBase}
|
42
|
*
|
43
|
* @author a.kohlbecker
|
44
|
*/
|
45
|
@Retention(RetentionPolicy.RUNTIME)
|
46
|
@Target(ElementType.TYPE)
|
47
|
@Inherited
|
48
|
public @interface DataPortalContexts {
|
49
|
|
50
|
/**
|
51
|
* @return an array of DataPortalSite to which the annotated test
|
52
|
* class is applicable
|
53
|
*/
|
54
|
DataPortalSite[] value() default {};
|
55
|
|
56
|
/**
|
57
|
* Alternative configuration option to the default {@link #value()}.
|
58
|
*
|
59
|
* In case this mode is active the system property <code>SiteListUrl</code> must contain a URL which
|
60
|
* points to a resource containing a list of Data Portals base URLs to be tested. Each dataportal URL must be in a
|
61
|
* separate line of the text returned by the URL in <code>SiteListUrl</code.
|
62
|
*/
|
63
|
boolean siteListUrl() default false;
|
64
|
}
|
65
|
|
66
|
private final List<Runner> runners = new ArrayList<Runner>();
|
67
|
|
68
|
|
69
|
private class TestClassRunnerWithDataPortalContext extends BlockJUnit4ClassRunner {
|
70
|
|
71
|
private final DataPortalContext context;
|
72
|
|
73
|
|
74
|
public TestClassRunnerWithDataPortalContext(Class<?> klass, DataPortalContext context) throws InitializationError {
|
75
|
super(klass);
|
76
|
this.context = context;
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
public Object createTest() throws Exception {
|
81
|
Object testClass = getTestClass().getOnlyConstructor().newInstance();
|
82
|
((CdmDataPortalTestBase)testClass).setContext(context);
|
83
|
return testClass;
|
84
|
}
|
85
|
|
86
|
@Override
|
87
|
protected String getName() {
|
88
|
return getTestClass().getName();
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
protected String testName(final FrameworkMethod method) {
|
93
|
return method.getName();
|
94
|
|
95
|
}
|
96
|
|
97
|
@Override
|
98
|
protected Statement classBlock(RunNotifier notifier) {
|
99
|
return childrenInvoker(notifier);
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
protected void validateZeroArgConstructor(List<Throwable> errors) {
|
104
|
super.validateZeroArgConstructor(errors);
|
105
|
validateCdmDataPortalTestBase(errors);
|
106
|
}
|
107
|
|
108
|
protected void validateCdmDataPortalTestBase(List<Throwable> errors) {
|
109
|
// constructor should have exactly one arg
|
110
|
if ( ! CdmDataPortalTestBase.class.isAssignableFrom(getTestClass().getJavaClass()) ){
|
111
|
String gripe= "Test class must be a subclass of " + CdmDataPortalTestBase.class.getName();
|
112
|
errors.add(new Exception(gripe));
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Only called reflectively. Do not use programmatically.
|
119
|
*/
|
120
|
public DataPortalContextSuite(Class<?> klass) throws InitializationError {
|
121
|
super(klass, Collections.<Runner>emptyList());
|
122
|
DataPortalContexts dataPortalContextsAnotation = getTestClass().getJavaClass().getAnnotation(DataPortalContexts.class);
|
123
|
DataPortalContextProvider contextProvider = null;
|
124
|
|
125
|
if(dataPortalContextsAnotation != null) {
|
126
|
if(dataPortalContextsAnotation.siteListUrl()){
|
127
|
String siteListUrlString = System.getProperty(SYSTEM_PROPERTY_SITE_LIST_URL);
|
128
|
if(System.getProperty(SYSTEM_PROPERTY_SITE_LIST_URL) == null) {
|
129
|
throw new RuntimeException("The system property " + SYSTEM_PROPERTY_SITE_LIST_URL + " must be set if 'siteListUrl' is enabled");
|
130
|
}
|
131
|
try {
|
132
|
contextProvider = new DataPortalsListContextProvider(new URL(siteListUrlString));
|
133
|
} catch (MalformedURLException e) {
|
134
|
throw new RuntimeException("Error parsing the provided URL", e);
|
135
|
}
|
136
|
} else {
|
137
|
contextProvider = new DataPortalSiteContextProvider(dataPortalContextsAnotation.value());
|
138
|
}
|
139
|
|
140
|
assert contextProvider != null;
|
141
|
for (DataPortalContext dataPortalContext : contextProvider.contexts()) {
|
142
|
runners.add(new TestClassRunnerWithDataPortalContext(klass, dataPortalContext));
|
143
|
}
|
144
|
}
|
145
|
}
|
146
|
|
147
|
@Override
|
148
|
protected List<Runner> getChildren() {
|
149
|
return runners;
|
150
|
}
|
151
|
|
152
|
}
|