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
46
47
48
49
50
51
52
53
54 package org.castor.xmlctf;
55
56 import java.io.File;
57 import java.io.FileWriter;
58 import java.io.IOException;
59 import java.io.PrintWriter;
60
61 import junit.framework.Test;
62 import junit.framework.TestSuite;
63
64 import org.castor.xmlctf.util.CTFUtils;
65 import org.castor.xmlctf.util.FileServices;
66 import org.exolab.castor.tests.framework.testDescriptor.SchemaDifferences;
67 import org.exolab.castor.tests.framework.testDescriptor.UnitTestCase;
68 import org.exolab.castor.tests.framework.testDescriptor.types.FailureStepType;
69 import org.exolab.castor.xml.XMLContext;
70 import org.exolab.castor.xml.schema.Schema;
71 import org.exolab.castor.xml.schema.SchemaContextImpl;
72 import org.exolab.castor.xml.schema.reader.SchemaReader;
73 import org.exolab.castor.xml.schema.writer.SchemaWriter;
74 import org.xml.sax.InputSource;
75
76
77
78
79
80
81
82
83 public class SchemaTestCase extends XMLTestCase {
84
85
86 private String _schemaName;
87
88 private final int _differenceCountReference;
89
90
91 protected final String _goldFileName;
92
93
94
95
96
97 public SchemaTestCase(final String name) {
98 super(name);
99 throw new IllegalArgumentException("You cannot use the name-only constructor");
100 }
101
102
103
104
105
106
107 public SchemaTestCase(final CastorTestCase castorTc, final UnitTestCase tc) {
108 super(castorTc, tc);
109 _differenceCountReference = getSchemaDifferenceCount(tc, FailureStepType.COMPARE_TO_REFERENCE);
110 _goldFileName = tc.getGoldFile();
111 }
112
113
114
115
116
117
118
119 private int getSchemaDifferenceCount(final UnitTestCase tc, final FailureStepType step) {
120 SchemaDifferences[] diff = tc.getSchemaDifferences();
121 for (int i = 0; i < diff.length; i++) {
122 if (diff[i].getFailureStep().equals(step)) {
123 return diff[i].getContent();
124 }
125 }
126 return 0;
127 }
128
129
130
131
132
133
134 public void setSchemaName(final String name) {
135 _schemaName = name;
136 }
137
138 public static Test suite() {
139 return new TestSuite();
140 }
141
142 protected void setUp() throws Exception {
143 verbose("\n================================================");
144 verbose("Test suite '"+_test.getName()+"': setting up test '" + _name+"'");
145 verbose("================================================\n");
146
147 try {
148 FileServices.copySupportFiles(_test.getTestFile(), _outputRootFile);
149 } catch (IOException e) {
150 fail("IOException copying support files " + e);
151 }
152 if (getXMLContext() == null) {
153
154 setXMLContext(new XMLContext());
155 }
156 }
157
158
159
160
161
162 protected void tearDown() throws Exception {
163 verbose("\n================================================");
164 verbose("Test suite '"+_test.getName()+"': test '" + _name+"' complete.");
165 verbose("================================================\n");
166 }
167
168
169
170
171
172
173 public void runTest() throws Throwable {
174 if (_skip) {
175 verbose("-->Skipping the test");
176 return;
177 }
178
179 File schemaFile = new File(_test.getTestFile() + "/" + _schemaName);
180 String schemaURL = schemaFile.toURL().toString();
181
182 Schema schema = testReadingSchema(schemaURL);
183 if (schema == null) {
184 return;
185 }
186 testWritingSchema(schemaURL, schema);
187
188
189 compareSchemaFiles(schemaFile);
190
191 if (_failure != null && _failure.getContent()) {
192 fail("The schema test was expected to fail, but passed");
193 }
194 }
195
196 private void compareSchemaFiles(final File schemaFile) throws IOException {
197 File file = new File(_outputRootFile, _schemaName.substring(0,_schemaName.lastIndexOf('.'))
198 + "-output" + FileServices.XSD);
199
200 String goldFileName = (_goldFileName != null) ? _outputRootFile + "/" + _goldFileName
201 : schemaFile.getAbsolutePath();
202
203 int result = CTFUtils.compare(goldFileName, file.getAbsolutePath());
204 verbose("----> Compare marshaled schema to gold file '" + _goldFileName + "': " + ((result == 0)?"OK":"### Failed ### "));
205
206 final FailureStepType step = _failure != null ? _failure.getFailureStep() : null;
207 final boolean expectedToFail= _failure != null && _failure.getContent()
208 && (step == null || step.equals(FailureStepType.COMPARE_TO_REFERENCE));
209
210 if (_failure == null || !_failure.getContent()) {
211 assertEquals("The Marshaled schema differs from the gold file", _differenceCountReference, result);
212 } else if (expectedToFail) {
213 assertTrue("The Marshaled schema was expected to differ from the" +
214 " gold file, but did not", result != _differenceCountReference);
215 }
216 }
217
218
219
220
221
222
223
224 private Schema testReadingSchema(final String url) {
225 verbose("--> Reading XML Schema: " + url);
226 try {
227 SchemaReader reader = new SchemaReader();
228
229 reader.setSchemaContext(new SchemaContextImpl());
230 reader.setInputSource(new InputSource(url));
231
232 Schema returnValue = reader.read();
233 if (_failure != null && _failure.getContent() && _failure.getFailureStep() != null &&
234 _failure.getFailureStep().equals(FailureStepType.PARSE_SCHEMA)) {
235 fail("Reading/Parsing the schema was expected to fail, but succeeded");
236 }
237 return returnValue;
238 } catch (Exception e) {
239 if (!checkExceptionWasExpected(e, FailureStepType.PARSE_SCHEMA)) {
240 fail("Unable to read Schema '" + url + "': " + e.toString());
241 }
242 }
243
244 return null;
245 }
246
247
248
249
250
251
252
253 private void testWritingSchema(final String url, final Schema schema) {
254
255 try {
256 String fileName = _schemaName.substring(0,_schemaName.lastIndexOf('.'))
257 + "-output" + FileServices.XSD;
258 verbose("--> Writing XML Schema: " + fileName);
259
260 File output = new File(_outputRootFile, fileName);
261 FileWriter writer = new FileWriter(output);
262
263 SchemaWriter schemaWriter = new SchemaWriter();
264 schemaWriter.setSchemaContext(new SchemaContextImpl());
265 schemaWriter.setDocumentHandler(new PrintWriter(writer, true));
266
267 schemaWriter.write(schema);
268 writer.close();
269 } catch (Exception e) {
270 if (!checkExceptionWasExpected(e, FailureStepType.WRITE_SCHEMA)) {
271 fail("Failed to write Schema '" + url + "' to disk: " + e.toString());
272 }
273 return;
274 }
275
276 if (_failure != null && _failure.getContent() && _failure.getFailureStep() != null &&
277 _failure.getFailureStep().equals(FailureStepType.WRITE_SCHEMA)) {
278 fail("Writing the schema was expected to fail, but succeeded");
279 }
280 }
281
282 }