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
36
37
38
39
40
41
42
43
44
45 package org.castor.xmlctf;
46
47 import java.io.FileNotFoundException;
48 import java.io.InputStream;
49 import java.net.URL;
50 import java.net.URLClassLoader;
51
52 import junit.framework.Test;
53 import junit.framework.TestSuite;
54
55 import org.castor.xmlctf.compiler.CompilationException;
56 import org.castor.xmlctf.compiler.Compiler;
57 import org.castor.xmlctf.compiler.SunJavaCompiler;
58 import org.castor.xmlctf.util.FileServices;
59 import org.exolab.castor.mapping.Mapping;
60 import org.exolab.castor.tests.framework.testDescriptor.ListenerType;
61 import org.exolab.castor.tests.framework.testDescriptor.MarshallingTest;
62 import org.exolab.castor.tests.framework.testDescriptor.RootType;
63 import org.exolab.castor.tests.framework.testDescriptor.UnitTestCase;
64 import org.xml.sax.InputSource;
65
66
67
68
69
70
71
72
73
74 public class MarshallingFrameworkTestCase extends XMLTestCase {
75
76
77
78
79
80
81 protected final MarshallingTest _marshallingConf;
82
83 protected final boolean _hasRandom;
84
85
86
87
88
89
90
91
92
93
94 public MarshallingFrameworkTestCase(final CastorTestCase test, final UnitTestCase unit,
95 final MarshallingTest marshalling) {
96 super(test, unit);
97 _marshallingConf = marshalling;
98
99 RootType rootType = _marshallingConf.getRoot_Object();
100 if (rootType == null) {
101 throw new IllegalArgumentException("You must give a root object for a Marshaling Test: "
102 + _outputRootFile + ", " + getName());
103 }
104
105 _rootClassName = rootType.getContent();
106 if (_rootClassName == null) {
107 throw new IllegalArgumentException("You must give a root object for a Marshaling Test:"
108 + _outputRootFile + ", " + getName());
109 }
110
111 _hasRandom = rootType.getRandom();
112 _hasDump = rootType.getDump();
113 }
114
115
116
117
118
119
120
121 public MarshallingFrameworkTestCase(final String name) {
122 super(name);
123 throw new IllegalArgumentException("You cannot use the name-only constructor");
124 }
125
126
127
128
129
130 public Test suite() {
131 TestSuite suite = new TestSuite(_name);
132
133 String name = getTestSuiteName();
134 name = (name != null) ? name + "#" + _name : _name;
135
136 if (_unitTest.getCustomTest() != null) {
137 suite.addTest(new TestWithCustomTest(name, this));
138 } else {
139 suite.addTest(new TestWithReferenceDocument(name, this));
140 if (_hasRandom) {
141 suite.addTest(new TestWithRandomObject(name, this));
142 }
143 }
144 return suite;
145 }
146
147
148
149
150
151
152
153
154
155
156
157 protected void setUp() throws java.lang.Exception {
158 verbose("\n================================================");
159 verbose("Test suite '" + _test.getName() + "': setting up test '" + _name + "'");
160 verbose("================================================\n");
161
162 FileServices.copySupportFiles(_test.getTestFile(), _outputRootFile);
163
164
165 if (!_test.isDirectoryCompiled()) {
166 verbose("-->Compiling any necessary source files in " + _outputRootFile);
167 Compiler compiler = new SunJavaCompiler(_outputRootFile);
168 if (_unitTest.hasJavaSourceVersion()) {
169 compiler.setJavaSourceVersion(_unitTest.getJavaSourceVersion());
170 }
171 try {
172 compiler.compileDirectory();
173 _test.setDirectoryCompiled(true);
174 } catch (CompilationException e) {
175 if (_printStack) {
176 e.printStackTrace(System.out);
177 }
178 fail("Build Failed: " + e.getMessage());
179 }
180 }
181
182
183 ClassLoader loader = _test.getClassLoader();
184 loader = new URLClassLoader(new URL[] {_outputRootFile.toURL()}, loader);
185 _test.setClassLoader(loader);
186 getXMLContext().getInternalContext().setClassLoader(loader);
187
188 verbose("Root class specified in TestDescriptor...");
189 verbose("Loading class: " + _rootClassName);
190 _rootClass = loader.loadClass(_rootClassName);
191
192
193 String mappingFilePath = null;
194 if (_unitTest.getUnitTestCaseChoice() != null) {
195 mappingFilePath = _unitTest.getUnitTestCaseChoice().getMapping_File();
196 }
197
198 if (mappingFilePath != null) {
199 configureMapping(loader, mappingFilePath);
200 } else {
201 verbose("##### TESTING INTROSPECTION #####");
202 _mapping = null;
203 }
204 }
205
206
207
208
209
210 protected void tearDown() throws java.lang.Exception {
211 verbose("\n================================================");
212 verbose("Test suite '" + _test.getName() + "': test '" + _name + "' complete.");
213 verbose("================================================\n");
214 }
215
216
217
218
219
220
221
222
223
224 private void configureMapping(final ClassLoader loader, final String mappingFilePath) throws Exception {
225 verbose("##### TESTING MAPPING #####");
226 verbose("Mapping file: " + mappingFilePath);
227 InputStream mappingFile = loader.getResourceAsStream(mappingFilePath);
228
229 if (mappingFile == null) {
230 throw new FileNotFoundException("Unable to locate the mapping file '"
231 + mappingFilePath + "' for the test '" + _test.getName() + "'");
232 }
233
234 _mapping = new Mapping(loader);
235 InputSource source = new InputSource(mappingFile);
236 source.setSystemId(mappingFilePath);
237 _mapping.loadMapping(source);
238
239 ListenerType listener = _unitTest.getListener();
240 if (listener != null) {
241 String listenerName = listener.getClassName();
242 try {
243
244 initializeListeners(listener);
245 } catch (ClassNotFoundException cnfex) {
246
247 fail("The listener '" + listenerName + "' cannot be found in the CLASSPATH");
248 } catch (InstantiationException iex) {
249 fail("The listener '" + listenerName + "' cannot be instantiated");
250 } catch (IllegalAccessException iaex) {
251 fail("Constructing a '" + listenerName + "' failed: " + iaex);
252 }
253 verbose("##### TESTING LISTENER CLASS " + listenerName + " #####");
254 }
255 }
256
257 }