-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCtlController.java
More file actions
145 lines (129 loc) · 5 KB
/
Copy pathCtlController.java
File metadata and controls
145 lines (129 loc) · 5 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package org.opengis.cite.wms13;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import org.apache.commons.io.FilenameUtils;
import org.w3c.dom.Document;
import com.occamlab.te.SetupOptions;
import com.occamlab.te.spi.ctl.CtlExecutor;
import com.occamlab.te.spi.executors.TestRunExecutor;
import com.occamlab.te.spi.jaxrs.TestSuiteController;
/**
* Main test run controller is responsible for executing the test suite.
*/
public class CtlController implements TestSuiteController {
private TestRunExecutor executor;
private Properties etsProperties = new Properties();
/**
* Constructs a controller object for this test suite. The location of the main CTL
* script is read from the "main-script" property in the ets.properties file (a
* classpath resource).
*/
public CtlController() {
SetupOptions setupOpts = new SetupOptions();
try (InputStream is = getClass().getResourceAsStream("ets.properties")) {
this.etsProperties.load(is);
String mainScriptPath = etsProperties.getProperty("main-script");
Object ctlFile = findScriptFile(URI.create(mainScriptPath));
setupOpts.addSource(ctlFile);
this.executor = new CtlExecutor(setupOpts);
}
catch (IOException ex) {
Logger.getLogger(getClass().getName())
.log(Level.SEVERE, "Failed to initialize CtlController. {0}", ex.getMessage());
}
}
/**
* A convenience method for running the test suite using a command-line interface.
* @param args Test run arguments (optional). The first argument must refer to a local
* XML properties file containing the expected set of test run arguments. If no
* argument is supplied, the file located at ${user.home}/test-run-props.xml will be
* used.
* @throws Exception If an error errors during the test run.
*/
public static void main(String[] args) throws Exception {
File propsFile;
if (args.length == 0) {
propsFile = new File(FilenameUtils.normalize(System.getProperty("user.home")), "test-run-props.xml");
}
else {
String xmlProps = args[0];
propsFile = (xmlProps.startsWith("file:")) ? new File(URI.create(xmlProps)) : new File(xmlProps);
}
if (!propsFile.isFile()) {
throw new IllegalArgumentException("Test run arguments not found at " + propsFile);
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// This is the PRIMARY defense. If DTDs (doctypes) are disallowed, almost all XML
// entity attacks are prevented
// Xerces 2 only -
// http://xerces.apache.org/xerces2-j/features.html#disallow-doctype-decl
String FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
dbf.setFeature(FEATURE, true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document testRunArgs = db.parse(propsFile);
TestSuiteController controller = new CtlController();
Source results = controller.doTestRun(testRunArgs);
Logger.getLogger(CtlController.class.getName()).log(Level.INFO, "Test results: {0}", results.getSystemId());
}
@Override
public String getCode() {
return etsProperties.getProperty("ets-code");
}
@Override
public String getVersion() {
return etsProperties.getProperty("ets-version");
}
@Override
public String getTitle() {
return etsProperties.getProperty("ets-title");
}
@Override
public Source doTestRun(Document testRunArgs) throws Exception {
Properties validArgs = TestRunArguments.validateArguments(testRunArgs);
// pass Properties directly instead?
return executor.execute(TestRunArguments.propertiesAsDocument(validArgs));
}
/**
* Creates a File from the given URI reference. If the URI is not absolute, is is
* resolved against the location of the TE_BASE/scripts directory; if a file does not
* exist at this location the URI is assumed to be a classpath reference.
* @param uri An absolute or relative URI.
* @return A File object, or null if one could not be created.
*/
final Object findScriptFile(URI uri) {
File ctlFile = null;
File baseDir = SetupOptions.getBaseConfigDirectory();
if (!uri.isAbsolute()) {
File scriptsDir = new File(baseDir, "scripts");
ctlFile = new File(scriptsDir, uri.getPath());
}
if (null == ctlFile || !ctlFile.isFile()) {
URL resource = getClass().getResource(uri.getPath());
if (resource.getProtocol().equals("jar")) {
// See https://github.com/opengeospatial/ets-wms13/issues/120 and
// https://github.com/opengeospatial/teamengine/issues/632
// will return an URL
return resource;
}
try {
ctlFile = new File(resource.toURI());
}
catch (URISyntaxException ex) {
Logger.getLogger(getClass().getName()).log(Level.INFO, "Invalid URI: {0}", ex);
}
}
Logger.getLogger(getClass().getName()).log(Level.CONFIG, "Created File object: {0}", ctlFile);
return ctlFile;
}
}