1 package tests;
2
3 import junit.framework.TestCase;
4 import org.apache.ws.commons.schema.*;
5
6 import javax.xml.namespace.QName;
7 import javax.xml.transform.stream.StreamSource;
8 import java.io.FileInputStream;
9 import java.io.InputStream;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.Set;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class RedefineTest extends TestCase {
33
34
35
36
37
38
39 public void testComplexTypeRedefine() throws Exception {
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 InputStream is = new FileInputStream(Resources.asURI("redefine2.xsd"));
88 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
89 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
90
91 XmlSchemaObjectTable xsot = schema.getElements();
92 assertEquals(1, xsot.getCount());
93
94 XmlSchemaElement xse = null;
95 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
96 xse = (XmlSchemaElement)i.next();
97 }
98 assertEquals("vip", xse.getName());
99 assertEquals(new QName("http://soapinterop.org/types",
100 "person"),
101 xse.getSchemaTypeName());
102
103 XmlSchemaObjectCollection xsoc = schema.getIncludes();
104 assertEquals(1, xsoc.getCount());
105
106 XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
107 xsot = xsr.getSchemaTypes();
108 assertEquals(1, xsot.getCount());
109
110 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
111 QName qname = (QName)i.next();
112 assertEquals(new QName("http://soapinterop.org/types",
113 "person"), qname);
114 }
115
116 XmlSchemaComplexType xsct = null;
117 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
118 xsct = (XmlSchemaComplexType)i.next();
119 }
120 assertNotNull(xsct);
121
122 XmlSchemaContentModel xscm = xsct.getContentModel();
123 assertNotNull(xscm);
124
125 XmlSchemaComplexContentExtension xscce =
126 (XmlSchemaComplexContentExtension)xscm.getContent();
127 assertEquals(new QName("http://soapinterop.org/types",
128 "person"),
129 xscce.getBaseTypeName());
130
131 XmlSchemaSequence xsp = (XmlSchemaSequence)xscce.getParticle();
132 assertNotNull(xsp);
133
134 XmlSchemaObjectCollection c = xsp.getItems();
135 assertEquals(1, c.getCount());
136
137 xse = null;
138 for (int i = 0; i < c.getCount(); i++) {
139 xse = (XmlSchemaElement)c.getItem(i);
140 }
141 assertEquals("id", xse.getName());
142 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
143 "string"),
144 xse.getSchemaTypeName());
145
146 }
147
148
149
150
151
152
153 public void testSimpleTypeRedefine() throws Exception {
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 InputStream is = new FileInputStream(Resources.asURI("redefine4.xsd"));
196 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
197 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
198
199 XmlSchemaObjectTable xsot = schema.getElements();
200 assertEquals(1, xsot.getCount());
201
202 XmlSchemaElement xse = null;
203 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
204 xse = (XmlSchemaElement)i.next();
205 }
206 assertEquals("childsizedrink", xse.getName());
207 assertEquals(new QName("http://soapinterop.org/types",
208 "drinksize"),
209 xse.getSchemaTypeName());
210
211 XmlSchemaObjectCollection xsoc = schema.getIncludes();
212 assertEquals(1, xsoc.getCount());
213
214 XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
215 xsot = xsr.getSchemaTypes();
216 assertEquals(1, xsot.getCount());
217
218 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
219 QName qname = (QName)i.next();
220 assertEquals(new QName("http://soapinterop.org/types",
221 "drinksize"), qname);
222 }
223
224 XmlSchemaSimpleType xsst = null;
225 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
226 xsst = (XmlSchemaSimpleType)i.next();
227 }
228 assertNotNull(xsst);
229
230 XmlSchemaSimpleTypeRestriction xsstr =
231 (XmlSchemaSimpleTypeRestriction)xsst.getContent();
232 assertEquals(new QName("http://soapinterop.org/types",
233 "drinksize"),
234 xsstr.getBaseTypeName());
235
236 xsoc = xsstr.getFacets();
237
238 Set s = new HashSet();
239 s.add(XmlSchemaMinInclusiveFacet.class.getName());
240 s.add(XmlSchemaMaxInclusiveFacet.class.getName());
241 for (Iterator i = xsoc.getIterator(); i.hasNext(); ) {
242 Object o = i.next();
243 assertTrue(s.remove(o.getClass().getName()));
244 if (o instanceof XmlSchemaMinInclusiveFacet) {
245 assertEquals("1", ((XmlSchemaMinInclusiveFacet)o).getValue());
246 } else if (o instanceof XmlSchemaMaxInclusiveFacet) {
247 assertEquals("3", ((XmlSchemaMaxInclusiveFacet)o).getValue());
248 } else {
249 fail("Unexpected object encountered: "
250 + o.getClass().getName());
251 }
252 }
253
254 assertTrue("The set should have been empty, but instead contained: "
255 + s + ".",
256 s.isEmpty());
257
258 }
259
260
261
262
263
264
265 public void testGroupRedefine() throws Exception {
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 InputStream is = new FileInputStream(Resources.asURI("redefine6.xsd"));
308 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
309 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
310
311 XmlSchemaObjectCollection xsoc = schema.getIncludes();
312 assertEquals(1, xsoc.getCount());
313
314 XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
315 XmlSchemaObjectTable xsot = xsr.getGroup();
316 assertEquals(1, xsot.getCount());
317
318 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
319 assertEquals("PrologGroup", (String)i.next());
320 }
321
322 XmlSchemaGroup xsg = null;
323 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
324 xsg = (XmlSchemaGroup)i.next();
325 }
326
327 XmlSchemaSequence xss = (XmlSchemaSequence)xsg.getParticle();
328
329 xsoc = xss.getItems();
330 assertEquals(2, xsoc.getCount());
331
332 Set s = new HashSet();
333 s.add(XmlSchemaGroupRef.class.getName());
334 s.add(XmlSchemaElement.class.getName());
335 for (Iterator i = xsoc.getIterator(); i.hasNext(); ) {
336 Object o = i.next();
337 assertTrue(s.remove(o.getClass().getName()));
338 if (o instanceof XmlSchemaGroupRef) {
339 assertEquals(new QName("http://soapinterop.org/types",
340 "PrologGroup"),
341 ((XmlSchemaGroupRef)o).getRefName());
342 } else if (o instanceof XmlSchemaElement) {
343 assertEquals("description", ((XmlSchemaElement)o).getName());
344 } else {
345 fail("Unexpected object encountered: "
346 + o.getClass().getName());
347 }
348 }
349
350 assertTrue("The set should have been empty, but instead contained: "
351 + s + ".",
352 s.isEmpty());
353
354 }
355
356
357
358
359
360
361 public void testAttributeGroupRedefine() throws Exception {
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399 InputStream is = new FileInputStream(Resources.asURI("redefine8.xsd"));
400 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
401 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
402
403 XmlSchemaObjectCollection xsoc = schema.getIncludes();
404 assertEquals(1, xsoc.getCount());
405
406 XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
407 XmlSchemaObjectTable xsot = xsr.getAttributeGroup();
408 assertEquals(1, xsot.getCount());
409
410 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
411 assertEquals("AttribGroup", (String)i.next());
412 }
413
414 XmlSchemaAttributeGroup xsag = null;
415 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
416 xsag = (XmlSchemaAttributeGroup)i.next();
417 }
418 assertEquals("AttribGroup", xsag.getName());
419 xsoc = xsag.getAttributes();
420
421 Set s = new HashSet();
422 s.add("type");
423 s.add("units");
424 for (Iterator i = xsoc.getIterator(); i.hasNext(); ) {
425 XmlSchemaAttribute xsa = (XmlSchemaAttribute)i.next();
426 assertTrue(s.remove(xsa.getName()));
427 }
428
429 assertTrue("The set should have been empty, but instead contained: "
430 + s + ".",
431 s.isEmpty());
432
433 }
434
435
436 }