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