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