1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.castor.xmlctf;
17
18 import java.io.File;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import junit.framework.TestCase;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.castor.xmlctf.util.CTFUtils;
28 import org.exolab.castor.tests.framework.testDescriptor.FailureType;
29 import org.exolab.castor.tests.framework.testDescriptor.types.FailureStepType;
30 import org.exolab.castor.xml.XMLContext;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 class TestWithReferenceDocument extends TestCase {
53
54 private static final Log LOG = LogFactory.getLog(TestWithReferenceDocument.class);
55
56
57 private static final String REFERENCE = "_ReferenceDocument";
58
59
60 private final XMLTestCase _delegate;
61
62 protected final CastorTestCase _test;
63
64 protected final FailureType _failure;
65
66 protected final String _builderClassName;
67
68 protected final String _outputName;
69
70 protected final String _inputName;
71
72 protected final String _goldFileName;
73
74
75
76
77
78
79
80 TestWithReferenceDocument(String name) {
81 super(name + REFERENCE);
82 throw new IllegalArgumentException("You cannot use the name-only constructor");
83 }
84
85
86
87
88
89
90
91 TestWithReferenceDocument(String name, XMLTestCase tc) {
92 super(name + REFERENCE);
93 _delegate = tc;
94 _test = tc._test;
95 _failure = tc._failure;
96 _outputName = tc._name.replace(' ', '_') + "-testWithReferenceDocument.xml";
97 _builderClassName = tc._unitTest.getObjectBuilder();
98 _inputName = tc._unitTest.getInput();
99
100
101
102
103 if (tc._unitTest.getGoldFile() != null) {
104 _goldFileName = tc._unitTest.getGoldFile();
105 } else {
106 _goldFileName = _inputName;
107 }
108 }
109
110
111
112
113
114
115
116 protected void setUp() throws Exception {
117 if (_delegate == null) {
118 throw new IllegalStateException("No test specified to set up.");
119 }
120
121 _delegate.setXMLContext(new XMLContext());
122 _delegate.setUp();
123
124
125
126
127
128 }
129
130
131
132
133
134
135
136 protected void tearDown() throws Exception {
137 if (_delegate == null) {
138 throw new IllegalStateException("No test specified to tear down.");
139 }
140 _delegate.tearDown();
141
142
143
144
145
146 }
147
148
149
150
151
152
153
154
155 public void runTest() throws Exception {
156 if (_delegate == null) {
157 throw new IllegalStateException("No test specified to be run.");
158 }
159
160 verbose("\n------------------------------");
161 verbose("Test with reference documents");
162 verbose("------------------------------\n");
163 if (_delegate._skip) {
164 verbose("-->Skipping the test");
165 return;
166 }
167
168
169 Object refUnmarshal;
170 try {
171 refUnmarshal = getUnmarshaledReference();
172 } catch (Exception e) {
173 if (_delegate._verbose) {
174 LOG.error("Problem unmarshalling input file from disk", e);
175 }
176 if (!_delegate.checkExceptionWasExpected(e, FailureStepType.UNMARSHAL_REFERENCE)) {
177 fail("Exception Unmarshaling from disk " + e);
178 }
179 return;
180 }
181
182
183 Object refGenerated = getBuilderReference();
184
185
186 if (refUnmarshal != null && refGenerated != null) {
187 compareReferenceObjects(refUnmarshal, refGenerated);
188 }
189
190
191 final Object ref = (refUnmarshal != null) ? refUnmarshal : refGenerated;
192 if (ref == null) {
193 throw new Exception(
194 "There is no valid input file or hardcoded object in '" + _delegate._name + "'");
195 }
196
197
198 File marshal_output;
199 try {
200 marshal_output = _delegate.testMarshal(ref, _outputName);
201 } catch (Exception e) {
202 if (!_delegate.checkExceptionWasExpected(e, FailureStepType.MARSHAL_TO_DISK)) {
203 fail("Exception Unmarshaling from disk " + e);
204 }
205 return;
206 }
207
208 if (_failure != null && _failure.getContent() && _failure.getFailureStep() != null
209 && _failure.getFailureStep().equals(FailureStepType.MARSHAL_TO_DISK)) {
210 fail("Marshaling the reference document to disk was expected to fail, but succeeded");
211 return;
212 }
213
214
215 if (_goldFileName != null && _goldFileName.length() > 0) {
216 int result = CTFUtils.compare(_delegate._outputRootFile + "/" + _goldFileName,
217 marshal_output.getAbsolutePath());
218 verbose("----> Compare marshaled document to gold file '" + _goldFileName + "': "
219 + ((result == 0) ? "OK" : "### Failed ### "));
220
221 final boolean expectedToFail =
222 _failure != null && _failure.getContent() && _failure.getFailureStep() != null
223 && _failure.getFailureStep().equals(FailureStepType.COMPARE_TO_REFERENCE);
224
225 verbose("----> expectedToFail when comparing to reference: " + expectedToFail);
226
227 if (_failure == null || !_failure.getContent()) {
228 assertEquals("The Marshaled object differs from the gold file", 0, result);
229 } else if (expectedToFail) {
230 assertTrue(
231 "The Marshaled object was expected to differ from the" + " gold file, but did not",
232 result > 0);
233 }
234 }
235
236
237 compareListenerToItsGoldFile();
238
239
240 Object unmarshaledOutput = null;
241 if (_builderClassName != null) {
242 try {
243 unmarshaledOutput = _delegate.testUnmarshal(marshal_output);
244 } catch (Exception e) {
245 LOG.error("Problem unmarshalling output from marshalling step", e);
246 if (!_delegate.checkExceptionWasExpected(e, FailureStepType.SECOND_UNMARSHAL)) {
247 fail("Exception Unmarshaling from disk " + e);
248 }
249 return;
250 }
251
252 if (_failure != null && _failure.getContent() && _failure.getFailureStep() != null
253 && _failure.getFailureStep().equals(FailureStepType.SECOND_UNMARSHAL)) {
254 fail("Second unmarshaling was expected to fail, but succeeded");
255 return;
256 }
257
258 }
259
260
261
262
263
264
265 if (_builderClassName != null && unmarshaledOutput != null) {
266
267 boolean result = unmarshaledOutput.equals(ref);
268 if (result == false) {
269 verbose("Make sure the reference object model overrides Object#equals");
270 }
271 verbose("Compare to reference object: " + ((result) ? "OK" : " ### Failed ### "));
272
273 final FailureStepType step = _failure != null ? _failure.getFailureStep() : null;
274 final boolean expectedToFail = _failure != null && _failure.getContent()
275 && (step == null || step.equals(FailureStepType.SECOND_COMPARE));
276
277 if (_failure == null || !_failure.getContent()) {
278 assertTrue("The initial reference object and the one resulting of the "
279 + "marshal/unmarshal process are different", result);
280 } else if (expectedToFail) {
281 assertFalse("Comparing the reference object to the marshal+unmarshaled "
282 + "one was expected to fail, but succeeded", result);
283 }
284
285 if (expectedToFail ^ result) {
286 return;
287 }
288 }
289
290 if (_builderClassName != null) {
291 if (_failure != null && _failure.getContent()) {
292 fail("The test with reference document was expected to fail, but passed");
293 }
294 }
295 }
296
297
298
299
300
301
302
303
304 private Object getUnmarshaledReference() throws Exception {
305 InputStream _input = null;
306 if (_inputName != null) {
307 _input = _test.getClassLoader().getResourceAsStream(_inputName);
308 assertNotNull("The input file '" + _inputName + "' cannot be found.", _input);
309 }
310
311 verbose("--> Unmarshaling '" + _inputName + "'\n");
312
313 Object refUnmarshal = null;
314 if (_input != null) {
315 refUnmarshal = _delegate.testUnmarshal(_input);
316 _input.close();
317 }
318
319
320
321 if (_failure != null && _failure.getContent() && _failure.getFailureStep() != null
322 && _failure.getFailureStep().equals(FailureStepType.UNMARSHAL_REFERENCE)) {
323 fail("Unmarshaling the reference document was expected to fail, but succeeded");
324 }
325
326 assertNotNull("Unmarshaling '" + _inputName + "' results in a NULL object.", refUnmarshal);
327
328 return refUnmarshal;
329 }
330
331
332
333
334
335
336
337
338 private Object getBuilderReference() throws Exception {
339 Object generated = null;
340 if (_builderClassName != null) {
341 generated = _delegate.buildObjectModel(_builderClassName);
342 assertNotNull("The generated object with '" + _builderClassName + "' is null", generated);
343 }
344 return generated;
345 }
346
347
348
349
350
351
352
353
354
355 private void compareReferenceObjects(Object refUnmarshal, Object refGenerated)
356 throws IOException {
357
358 boolean result = refGenerated.equals(refUnmarshal);
359 verbose("----> Compare unmarshaled document to reference object: "
360 + ((result) ? "OK" : "### Failed ### "));
361 if (result == false) {
362 verbose("Make sure the reference object model overrides Object#equals");
363 }
364
365 if (result == false && refGenerated instanceof CastorTestable) {
366
367 FileWriter writer =
368 new FileWriter(new File(_delegate._outputRootFile, _outputName + "-refModel.dump"));
369 writer.write(((CastorTestable) refGenerated).dumpFields());
370 writer.close();
371
372 writer =
373 new FileWriter(new File(_delegate._outputRootFile, _outputName + "-refUnmarshal.dump"));
374 writer.write(((CastorTestable) refUnmarshal).dumpFields());
375 writer.close();
376 }
377
378 assertTrue("The unmarshaled reference object differs from the hardcoded reference object.",
379 result);
380 }
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 private void compareListenerToItsGoldFile() throws Exception {
396 if (_delegate._listenerGoldFile == null || _delegate._listener == null) {
397 return;
398 }
399
400 verbose("Compare listener to gold file: " + _delegate._listenerGoldFile);
401
402
403 Object listener = _delegate._listener;
404
405
406 _delegate._listener = null;
407
408 File outputFile;
409 try {
410 outputFile = _delegate.testMarshal(listener, "Listener-" + _outputName);
411 } catch (Exception e) {
412 if (!_delegate.checkExceptionWasExpected(e, FailureStepType.LISTENER_COMPARISON)) {
413 fail("Exception Unmarshaling from disk " + e);
414 }
415 return;
416 }
417
418 int result = CTFUtils.compare(_delegate._outputRootFile + "/" + _delegate._listenerGoldFile,
419 outputFile.getAbsolutePath());
420 verbose("----> Compare marshaled document to gold file '" + _delegate._listenerGoldFile + "': "
421 + ((result == 0) ? "OK" : "### Failed ### "));
422
423 if (_failure != null && _failure.getContent()) {
424
425 if (_failure.getFailureStep() != null
426 && _failure.getFailureStep().equals(FailureStepType.LISTENER_COMPARISON)) {
427 assertTrue("The Marshaled Listener is supposed to differ from its gold file", result != 0);
428 }
429 } else {
430 assertEquals("The Marshaled Listener differs from its gold file", 0, result);
431 }
432 }
433
434 private void verbose(String message) {
435 _delegate.verbose(message);
436 }
437
438 }