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