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
23 import junit.framework.TestCase;
24
25 import org.exolab.castor.tests.framework.testDescriptor.FailureType;
26 import org.exolab.castor.tests.framework.testDescriptor.types.FailureStepType;
27 import org.exolab.castor.xml.XMLContext;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 class TestWithRandomObject extends TestCase {
48
49 private static final String RANDOM = "_RandomObject";
50
51
52 private final XMLTestCase _delegate;
53
54
55 protected final FailureType _failure;
56
57 protected final String _outputName;
58
59
60
61
62
63
64 TestWithRandomObject(String name) {
65 super(name+RANDOM);
66 throw new IllegalArgumentException("You cannot use the name-only constructor");
67 }
68
69
70
71
72
73
74
75 TestWithRandomObject(String name, XMLTestCase tc) {
76 super(name+RANDOM);
77 _delegate = tc;
78 _failure = tc._failure;
79 _outputName = _delegate._name.replace(' ', '_') + "-testWithRandomObject";
80 }
81
82
83
84
85
86
87 protected void setUp() throws Exception {
88 _delegate.setXMLContext(new XMLContext());
89 _delegate.setUp();
90
91
92
93
94
95 }
96
97
98
99
100
101
102 protected void tearDown() throws Exception {
103 _delegate.setUp();
104
105
106
107
108
109 }
110
111
112
113
114 public void runTest() throws Exception {
115 verbose("\n------------------------------");
116 verbose("Test with randomly generated object");
117 verbose("------------------------------\n");
118 if (_delegate._skip) {
119 verbose("-->Skipping the test");
120 return;
121 }
122
123
124 CastorTestable randomizedObject = getRandomizedReference();
125
126
127 if (_delegate._hasDump) {
128 verbose("----> Dump the object to '" + _outputName + "-ref.dump" +"'");
129 FileWriter writer = new FileWriter(new File(_delegate._outputRootFile, _outputName + "-ref.dump"));
130 writer.write(randomizedObject.dumpFields());
131 writer.close();
132 }
133
134
135 File marshal_output = _delegate.testMarshal(randomizedObject, _outputName + ".xml");
136 if (marshal_output == null) {
137 return;
138 }
139
140
141 Object unmarshaledRandomizedObject;
142 try {
143 unmarshaledRandomizedObject = _delegate.testUnmarshal(marshal_output);
144 } catch (Exception e) {
145 if (!_delegate.checkExceptionWasExpected(e, FailureStepType.SECOND_UNMARSHAL)) {
146 fail("Exception Unmarshaling from disk " + e);
147 }
148 return;
149 }
150
151 if (_failure != null && _failure.getContent() && _failure.getFailureStep() != null &&
152 _failure.getFailureStep().equals(FailureStepType.SECOND_UNMARSHAL)) {
153 fail("Unmarshaling the marshaled random document was expected to fail, but succeeded");
154 }
155
156
157 if (_delegate._hasDump) {
158 verbose("---->Dump the object to '" + _outputName + "-unmar.dump" +"'");
159 FileWriter writer = new FileWriter(new File(_delegate._outputRootFile, _outputName + "-unmar.dump"));
160 writer.write(((CastorTestable)unmarshaledRandomizedObject).dumpFields());
161 writer.close();
162 }
163
164
165 boolean result = unmarshaledRandomizedObject.equals(randomizedObject);
166 verbose("----> Compare unmarshaled document to reference object: " + ((result)?"OK":"### Failed ### "));
167
168 final FailureStepType step = _failure != null ? _failure.getFailureStep() : null;
169 final boolean expectedToFail = _failure != null && _failure.getContent()
170 && (step == null || step.equals(FailureStepType.COMPARE_TO_REFERENCE));
171
172 if (_failure == null || !_failure.getContent()) {
173 assertTrue("The initial randomized object and the one resulting of the" +
174 " marshal/unmarshal process are different", result);
175 } else if (expectedToFail) {
176 assertFalse("Comparing the random object to the marshal+unmarshaled one" +
177 " was expected to fail, but succeeded", result);
178 }
179
180 if (expectedToFail ^ result) {
181 return;
182 }
183
184 if (_failure != null && _failure.getContent()) {
185 fail("The test with random document was expected to fail, but passed");
186 }
187 }
188
189
190
191
192
193
194
195 private CastorTestable getRandomizedReference() throws InstantiationException, IllegalAccessException {
196 verbose("--> Randomize an object model for the root '" + _delegate._rootClassName + "'.");
197 CastorTestable randomizedObject = ((CastorTestable)_delegate._rootClass.newInstance());
198 assertNotNull("Randomized object model is null", randomizedObject);
199 randomizedObject.randomizeFields();
200 return randomizedObject;
201 }
202
203 private void verbose(String message) {
204 _delegate.verbose(message);
205 }
206
207 }