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