|
1
|
|
/*
|
|
2
|
|
* JBind
|
|
3
|
|
*
|
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
|
5
|
|
*
|
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
|
8
|
|
* 'Apache Software License V 1.1'.
|
|
9
|
|
*/
|
|
10
|
|
package org.jbind.xml.test;
|
|
11
|
|
|
|
12
|
|
import java.io.File;
|
|
13
|
|
import java.lang.reflect.Constructor;
|
|
14
|
|
import java.util.Iterator;
|
|
15
|
|
|
|
16
|
|
import junit.framework.Test;
|
|
17
|
|
import junit.framework.TestResult;
|
|
18
|
|
import junit.framework.TestSuite;
|
|
19
|
|
import junit.textui.TestRunner;
|
|
20
|
|
|
|
21
|
|
import org.jbind.base.Console;
|
|
22
|
|
import org.jbind.util.other.FileUtil;
|
|
23
|
|
|
|
24
|
|
public class CreateTestSuite {
|
|
25
|
|
|
|
26
|
|
public static final String TEST_DIR = "testDir";
|
|
27
|
|
public static final String CREATE_ONLY = "createOnly";
|
|
28
|
|
|
|
29
|
1
|
public static Test suite() throws Exception {
|
|
30
|
1
|
String testDir = System.getProperty(TEST_DIR);
|
|
31
|
1
|
return doCreateSuite(testDir);
|
|
32
|
|
}
|
|
33
|
|
|
|
34
|
1
|
private static Test doCreateSuite(String aTestDir) throws Exception {
|
|
35
|
1
|
if (null == aTestDir) {
|
|
36
|
0
|
Console.err("system property " + TEST_DIR + " missing");
|
|
37
|
0
|
System.exit(1);
|
|
38
|
|
}
|
|
39
|
|
|
|
40
|
1
|
File testDir = new File(aTestDir);
|
|
41
|
1
|
File scenariosDir = new File(testDir, "scenarios");
|
|
42
|
1
|
File genSrcRootDir = new File(testDir, "data");
|
|
43
|
1
|
File behaviourSrcRootDir = new File(testDir, "behaviour");
|
|
44
|
1
|
File tmpSrcRootDir = new File(testDir, "tmp");
|
|
45
|
|
|
|
46
|
1
|
if (!testDir.isDirectory()) {
|
|
47
|
0
|
Console.err("test directory '" + testDir + "' is not a directory");
|
|
48
|
0
|
System.exit(1);
|
|
49
|
|
}
|
|
50
|
1
|
if (!scenariosDir.isDirectory()) {
|
|
51
|
0
|
Console.err("missing scenario directory in test directory");
|
|
52
|
0
|
System.exit(1);
|
|
53
|
|
}
|
|
54
|
|
|
|
55
|
1
|
if (tmpSrcRootDir.exists()) {
|
|
56
|
0
|
if (!tmpSrcRootDir.isDirectory()) {
|
|
57
|
0
|
Console.err("tmpSrc directory '" + tmpSrcRootDir + "' is not a direcotory");
|
|
58
|
0
|
System.exit(1);
|
|
59
|
|
}
|
|
60
|
0
|
FileUtil.delete(tmpSrcRootDir);
|
|
61
|
|
}
|
|
62
|
1
|
tmpSrcRootDir.mkdirs();
|
|
63
|
|
|
|
64
|
1
|
TestSuite testSuite = new TestSuite();
|
|
65
|
|
|
|
66
|
1
|
String[] scenarios = scenariosDir.list();
|
|
67
|
1
|
for (int i = 0; i < scenarios.length; i++) {
|
|
68
|
25
|
String scenarioName = scenarios[i];
|
|
69
|
25
|
if (scenarioName.startsWith("X") || "CVS".equals(scenarioName)) {
|
|
70
|
1
|
continue;
|
|
71
|
|
}
|
|
72
|
24
|
File scenarioDir = new File(scenariosDir, scenarioName);
|
|
73
|
24
|
if (!scenarioDir.isDirectory()) {
|
|
74
|
0
|
continue;
|
|
75
|
|
}
|
|
76
|
24
|
TestSuite scenarioSuite = processScenario(scenarioName, scenarioDir, genSrcRootDir, behaviourSrcRootDir, tmpSrcRootDir);
|
|
77
|
24
|
testSuite.addTest(scenarioSuite);
|
|
78
|
|
}
|
|
79
|
|
|
|
80
|
1
|
return testSuite;
|
|
81
|
|
}
|
|
82
|
|
|
|
83
|
24
|
private static TestSuite processScenario(String aScenarioName, File aScenarioDir, File aGenSrcRootDir, File aBehaviourSrcRootDir, File aTmpSrcRootDir) throws Exception {
|
|
84
|
24
|
String descriptionUrl = new File(aScenarioDir, "description.xml").toURL().toString();
|
|
85
|
24
|
DescriptionInfo description = createDescriptionInfo(descriptionUrl);
|
|
86
|
|
|
|
87
|
24
|
ScenarioSuite res = new ScenarioSuite(aScenarioName);
|
|
88
|
|
|
|
89
|
24
|
boolean createOnly = "true".equals(System.getProperty(CREATE_ONLY, "false"));
|
|
90
|
|
|
|
91
|
24
|
res.addTest(new SchemaAndCodeTest(aScenarioName, aScenarioDir, aGenSrcRootDir, aBehaviourSrcRootDir, aTmpSrcRootDir, description, createOnly));
|
|
92
|
|
|
|
93
|
24
|
if (!createOnly) {
|
|
94
|
|
// Create a suite for all instance and JUnit tests.
|
|
95
|
|
// The suite is responsible for instantiating the schema and checking the code generation.
|
|
96
|
|
// After the last test in the suite is run all schemas are removed from the
|
|
97
|
|
// Schemas singleton.
|
|
98
|
24
|
for (Iterator i = description.instanceTestInfos.iterator(); i.hasNext(); ) {
|
|
99
|
34
|
InstanceTestInfo info = (InstanceTestInfo)i.next();
|
|
100
|
34
|
Test t = null;
|
|
101
|
34
|
if (null == info.className) {
|
|
102
|
21
|
t = new InstanceTest(aScenarioName, aScenarioDir, info);
|
|
103
|
|
} else {
|
|
104
|
13
|
Class clazz = Class.forName(info.className);
|
|
105
|
13
|
Constructor c = clazz.getConstructor(new Class[]{ String.class,
|
|
106
|
|
File.class,
|
|
107
|
|
InstanceTestInfo.class });
|
|
108
|
13
|
t = (Test)c.newInstance(new Object[]{ aScenarioName, aScenarioDir,
|
|
109
|
|
info });
|
|
110
|
|
}
|
|
111
|
34
|
res.addTest(t);
|
|
112
|
|
}
|
|
113
|
|
}
|
|
114
|
|
|
|
115
|
24
|
return res;
|
|
116
|
|
}
|
|
117
|
|
|
|
118
|
24
|
private static DescriptionInfo createDescriptionInfo(String aUrl) throws Exception {
|
|
119
|
24
|
javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
|
|
120
|
24
|
factory.setNamespaceAware(false);
|
|
121
|
24
|
factory.setValidating(true);
|
|
122
|
24
|
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
|
|
123
|
24
|
DescriptionBuilder descriptionBuilder = new DescriptionBuilder();
|
|
124
|
24
|
parser.parse(aUrl, descriptionBuilder);
|
|
125
|
24
|
return descriptionBuilder.getDescription();
|
|
126
|
|
}
|
|
127
|
|
|
|
128
|
0
|
private CreateTestSuite() {}
|
|
129
|
|
|
|
130
|
0
|
private static void usage() {
|
|
131
|
0
|
System.out.println("CreateTestSuite -createOnly -testDir <dir>");
|
|
132
|
|
}
|
|
133
|
|
|
|
134
|
0
|
public static void main(String[] anArgs) {
|
|
135
|
0
|
System.out.println("start");
|
|
136
|
0
|
boolean createOnly = false;
|
|
137
|
0
|
String testDir = null;
|
|
138
|
0
|
for (int i = 0; i < anArgs.length; i++) {
|
|
139
|
0
|
String a = anArgs[i];
|
|
140
|
0
|
if ("-createOnly".equals(a)) {
|
|
141
|
0
|
createOnly = true;
|
|
142
|
0
|
} else if ("-testDir".equals(a)) {
|
|
143
|
0
|
testDir = anArgs[++i];
|
|
144
|
|
} else {
|
|
145
|
0
|
usage();
|
|
146
|
0
|
System.exit(-1);
|
|
147
|
|
}
|
|
148
|
|
}
|
|
149
|
0
|
System.getProperties().setProperty(TEST_DIR, testDir);
|
|
150
|
0
|
System.getProperties().setProperty(CREATE_ONLY, String.valueOf(createOnly));
|
|
151
|
|
|
|
152
|
0
|
try {
|
|
153
|
0
|
String[] args = new String[]{ CreateTestSuite.class.getName() };
|
|
154
|
0
|
MyTestRunner testRunner = new MyTestRunner();
|
|
155
|
0
|
TestResult r = testRunner.start(args);
|
|
156
|
0
|
if (!r.wasSuccessful()) {
|
|
157
|
0
|
System.exit(-1);
|
|
158
|
|
}
|
|
159
|
0
|
System.exit(0);
|
|
160
|
|
} catch (Exception e) {
|
|
161
|
0
|
System.err.println(e.getMessage());
|
|
162
|
0
|
System.exit(-2);
|
|
163
|
|
}
|
|
164
|
|
}
|
|
165
|
|
|
|
166
|
|
private static class MyTestRunner extends TestRunner {
|
|
167
|
0
|
public MyTestRunner() {}
|
|
168
|
0
|
public TestResult start(String[] anArgs) throws Exception {
|
|
169
|
0
|
return super.start(anArgs);
|
|
170
|
|
}
|
|
171
|
|
}
|
|
172
|
|
}
|
|
173
|
|
|