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