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 package org.castor.xmlctf;
46
47 import java.io.File;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.io.InputStreamReader;
51 import java.net.MalformedURLException;
52 import java.net.URL;
53 import java.net.URLClassLoader;
54 import java.util.jar.JarFile;
55
56 import junit.framework.Test;
57 import junit.framework.TestCase;
58 import junit.framework.TestSuite;
59
60 import org.castor.xmlctf.util.FileServices;
61 import org.exolab.castor.tests.framework.testDescriptor.MarshallingTest;
62 import org.exolab.castor.tests.framework.testDescriptor.OnlySourceGenerationTest;
63 import org.exolab.castor.tests.framework.testDescriptor.SchemaTest;
64 import org.exolab.castor.tests.framework.testDescriptor.SourceGeneratorTest;
65 import org.exolab.castor.tests.framework.testDescriptor.TestDescriptor;
66 import org.exolab.castor.tests.framework.testDescriptor.TestDescriptorChoice;
67 import org.exolab.castor.tests.framework.testDescriptor.UnitTestCase;
68 import org.exolab.castor.xml.MarshalException;
69 import org.exolab.castor.xml.ValidationException;
70
71
72
73
74
75
76
77
78 public class CastorTestCase extends TestCase {
79
80
81 public static final short UNKNOWN = -1;
82
83 public static final short DIRECTORY = 0;
84
85 public static final short JAR = 1;
86
87
88 public static final String TEST_DESCRIPTOR = "TestDescriptor.xml";
89
90 private static final String TEST_DESCRIPTOR_JAR = "META-INF/TestDescriptor.xml";
91
92 private static final String FILE_SEPARATOR = System.getProperty("file.separator");
93
94 private static final float JAVA_VERSION = Float.parseFloat(System.getProperty("java.specification.version"));
95
96 private static final boolean VERBOSE;
97
98 static {
99 String v = System.getProperty(TestCaseAggregator.VERBOSE_PROPERTY);
100 VERBOSE = (v != null && v.equals("true"));
101 v = null;
102 }
103
104
105
106
107
108
109 private static boolean _printStack;
110
111 static {
112 String v = System.getProperty(TestCaseAggregator.PRINT_STACK_TRACE);
113 _printStack = (v != null && v.equals("true"));
114 v = null;
115 }
116
117
118 private boolean _compiled = false;
119
120 private ClassLoader _loader;
121
122 private TestDescriptor _testDescriptor;
123
124 private final File _testFile;
125
126 private final short _type;
127
128 private final File _outputRootFile;
129
130 private final String _directoryToHere;
131
132
133
134
135
136 public CastorTestCase(final String name) {
137 super(name);
138 _testFile = null;
139 _outputRootFile = null;
140 _directoryToHere = "";
141 _type = UNKNOWN;
142 }
143
144
145
146
147
148
149
150
151
152
153
154 public CastorTestCase(final File file, final String directoryToHere, final String outputRoot) {
155 super(directoryToHere);
156 _directoryToHere = directoryToHere;
157
158 if (file.isDirectory()) {
159 _type = DIRECTORY;
160 _outputRootFile = new File(outputRoot + FILE_SEPARATOR);
161 } else {
162 _type = JAR;
163 try {
164 new JarFile(file);
165 String fileName = file.getName();
166 fileName = fileName.substring(0, fileName.lastIndexOf("."));
167 _outputRootFile = new File(outputRoot + FILE_SEPARATOR + fileName);
168 } catch (java.util.zip.ZipException e) {
169 throw new IllegalStateException(file.getAbsolutePath() + " is not a valid JAR file.");
170 } catch (java.io.IOException ie) {
171 throw new IllegalStateException(file.getAbsolutePath() + " is not a valid JAR file.");
172 }
173 }
174
175
176 try {
177 URL[] urlList = {file.toURL()};
178 _loader = new URLClassLoader(urlList, this.getClass().getClassLoader());
179 } catch (MalformedURLException urle) {
180
181 urle.printStackTrace();
182 }
183 _testFile = file;
184 _outputRootFile.mkdirs();
185 }
186
187 public ClassLoader getClassLoader() {
188 return _loader;
189 }
190
191 public File getTestFile() {
192 return _testFile;
193 }
194
195 public short getType() {
196 return _type;
197 }
198
199 public String getDirectoryToHere() {
200 return _directoryToHere;
201 }
202
203 public File getOutputRootFile() {
204 return _outputRootFile;
205 }
206
207
208
209
210
211
212
213
214 public boolean isDirectoryCompiled() {
215 return _compiled;
216 }
217
218
219
220
221
222
223 public void setClassLoader(final ClassLoader loader) {
224 _loader = loader;
225 }
226
227
228
229
230
231
232
233
234
235 public void setDirectoryCompiled(final boolean compiled) {
236 _compiled = compiled;
237 }
238
239
240
241
242
243
244 public Test suite() {
245 final InputStream descriptor;
246 if (_type == JAR) {
247 descriptor = _loader.getResourceAsStream(TEST_DESCRIPTOR_JAR);
248 } else {
249 descriptor = _loader.getResourceAsStream(TEST_DESCRIPTOR);
250 }
251
252 if (descriptor == null) {
253 verbose("test '" + _testFile.getName() + "' has no TestDescriptor.xml");
254 return null;
255 }
256
257 try {
258 _testDescriptor = TestDescriptor.unmarshal(new InputStreamReader(descriptor));
259 } catch (ValidationException ve) {
260 verbose("Error reading: " + _testFile.getAbsolutePath());
261 verbose("-> " + ve.toString());
262 if (_printStack) {
263 ve.printStackTrace(System.out);
264 }
265 fail(ve.toString());
266 } catch (MarshalException me) {
267 verbose("Error reading: " + _testFile.getAbsolutePath());
268 verbose("-> " + me.toString());
269 if (_printStack) {
270 me.printStackTrace(System.out);
271 }
272 fail(me.toString());
273 } finally {
274 try {
275 descriptor.close();
276 } catch (IOException e) {
277
278 }
279 }
280
281 if (_testDescriptor.hasMinimumJavaVersion()) {
282
283 float minVersion = _testDescriptor.getMinimumJavaVersion();
284 if (minVersion >= 5F && minVersion < 10F) {
285 minVersion = 1.0F + (minVersion / 10F);
286 }
287 if (JAVA_VERSION < minVersion) {
288 verbose("-->Test requires at minimum Java " + minVersion + ", but we are running Java " + JAVA_VERSION);
289 verbose("-->Skipping the test");
290 return null;
291 }
292 }
293
294 if (_testDescriptor.hasMaximumJavaVersion()) {
295
296 float maxVersion = _testDescriptor.getMaximumJavaVersion();
297 if (maxVersion >= 5F && maxVersion < 10F) {
298 maxVersion = 1.0F + (maxVersion / 10F);
299 }
300 if (JAVA_VERSION > maxVersion) {
301 verbose("-->Test is designed to run up to Java " + maxVersion + ", but we are running Java " + JAVA_VERSION);
302 verbose("-->Skipping the test");
303 return null;
304 }
305 }
306
307 final String suiteName = _directoryToHere + _testDescriptor.getName();
308 final TestSuite suite = new TestSuite(suiteName);
309 verbose("Creating '" + suiteName + "' test suite");
310
311 TestDescriptorChoice choice = _testDescriptor.getTestDescriptorChoice();
312 MarshallingTest marshallingTests = choice.getMarshallingTest();
313 SourceGeneratorTest sourceGenTests = choice.getSourceGeneratorTest();
314 SchemaTest schemaTests = choice.getSchemaTest();
315 OnlySourceGenerationTest genOnlyTests = choice.getOnlySourceGenerationTest();
316
317 if (marshallingTests != null) {
318 setUpMarshallingTests(suiteName, suite, marshallingTests);
319 }
320 if (sourceGenTests != null) {
321 setUpSourceGeneratorTests(suiteName, suite, sourceGenTests);
322 }
323 if (schemaTests != null) {
324 setUpSchemaTests(suiteName, suite, schemaTests);
325 }
326 if (genOnlyTests != null) {
327 setUpGenerationOnlyTests(suiteName, suite, genOnlyTests);
328 }
329
330 return suite;
331 }
332
333
334
335
336
337
338
339
340
341 private void setUpMarshallingTests(final String suiteName, final TestSuite suite,
342 final MarshallingTest mar) {
343 for (int i = 0; i < mar.getUnitTestCaseCount(); ++i) {
344 UnitTestCase tc = mar.getUnitTestCase(i);
345 MarshallingFrameworkTestCase mftc = new MarshallingFrameworkTestCase(this, tc, mar);
346 mftc._configuration = mar.getConfiguration();
347 mftc.setTestSuiteName(suiteName);
348 suite.addTest(mftc.suite());
349 }
350 }
351
352
353
354
355
356
357
358
359
360 private void setUpSourceGeneratorTests(final String suiteName, final TestSuite suite,
361 final SourceGeneratorTest sg) {
362 for (int i = 0; i < sg.getUnitTestCaseCount(); ++i) {
363 UnitTestCase tc = sg.getUnitTestCase(i);
364 SourceGeneratorTestCase sgtc = new SourceGeneratorTestCase(this, tc, sg);
365 sgtc.setTestSuiteName(suiteName);
366 suite.addTest(sgtc.suite());
367 }
368 }
369
370
371
372
373
374
375
376
377
378 private void setUpSchemaTests(final String suiteName, final TestSuite suite,
379 final SchemaTest schemaTest) {
380 for (int i = 0; i < schemaTest.getUnitTestCaseCount(); i++) {
381 UnitTestCase tc = schemaTest.getUnitTestCase(i);
382
383 String name = tc.getUnitTestCaseChoice().getSchema();
384 if (name.equals("*")) {
385 File[] list = _testFile.listFiles();
386 for (int j = 0; j < list.length; ++j) {
387 String fileName = list[j].getName();
388
389
390 if (fileName.endsWith(FileServices.XSD)) {
391 makeIndividualSchemaTest(suiteName, suite, tc, fileName);
392 }
393 }
394 } else {
395 makeIndividualSchemaTest(suiteName, suite, tc, name);
396 }
397 }
398 }
399
400
401
402
403
404
405
406
407
408 private void setUpGenerationOnlyTests(final String suiteName, final TestSuite suite,
409 final OnlySourceGenerationTest sg) {
410 for (int i = 0; i < sg.getUnitTestCaseCount(); ++i) {
411 UnitTestCase tc = sg.getUnitTestCase(i);
412 OnlySourceGenerationTestCase sgtc = new OnlySourceGenerationTestCase(this, tc, sg);
413 sgtc.setTestSuiteName(suiteName);
414 suite.addTest(sgtc.suite());
415 }
416 }
417
418
419
420
421
422
423
424
425
426 private void makeIndividualSchemaTest(final String suiteName, final TestSuite suite,
427 final UnitTestCase tc, final String name) {
428 tc.setName(suiteName + '#' + name);
429 SchemaTestCase stc = new SchemaTestCase(this, tc);
430 stc.setSchemaName(name);
431 suite.addTest(stc);
432 }
433
434
435
436
437
438
439 private void verbose(final String message) {
440 if (VERBOSE) {
441 System.out.println(message);
442 }
443 }
444
445 }