1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package tests;
21
22 import junit.framework.TestCase;
23 import org.apache.ws.commons.schema.*;
24
25 import javax.xml.namespace.QName;
26 import javax.xml.transform.stream.StreamSource;
27 import java.io.FileInputStream;
28 import java.io.InputStream;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.Set;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class RedefineTest extends TestCase {
52
53
54
55
56
57
58 public void testComplexTypeRedefine() throws Exception {
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 InputStream is = new FileInputStream(Resources.asURI("redefine2.xsd"));
107 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
108 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
109
110 XmlSchemaObjectTable xsot = schema.getElements();
111 assertEquals(1, xsot.getCount());
112
113 XmlSchemaElement xse = null;
114 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
115 xse = (XmlSchemaElement)i.next();
116 }
117 assertEquals("vip", xse.getName());
118 assertEquals(new QName("http://soapinterop.org/types",
119 "person"),
120 xse.getSchemaTypeName());
121
122 XmlSchemaObjectCollection xsoc = schema.getIncludes();
123 assertEquals(1, xsoc.getCount());
124
125 XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
126 xsot = xsr.getSchemaTypes();
127 assertEquals(1, xsot.getCount());
128
129 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
130 QName qname = (QName)i.next();
131 assertEquals(new QName("http://soapinterop.org/types",
132 "person"), qname);
133 }
134
135 XmlSchemaComplexType xsct = null;
136 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
137 xsct = (XmlSchemaComplexType)i.next();
138 }
139 assertNotNull(xsct);
140
141 XmlSchemaContentModel xscm = xsct.getContentModel();
142 assertNotNull(xscm);
143
144 XmlSchemaComplexContentExtension xscce =
145 (XmlSchemaComplexContentExtension)xscm.getContent();
146 assertEquals(new QName("http://soapinterop.org/types",
147 "person"),
148 xscce.getBaseTypeName());
149
150 XmlSchemaSequence xsp = (XmlSchemaSequence)xscce.getParticle();
151 assertNotNull(xsp);
152
153 XmlSchemaObjectCollection c = xsp.getItems();
154 assertEquals(1, c.getCount());
155
156 xse = null;
157 for (int i = 0; i < c.getCount(); i++) {
158 xse = (XmlSchemaElement)c.getItem(i);
159 }
160 assertEquals("id", xse.getName());
161 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
162 "string"),
163 xse.getSchemaTypeName());
164
165 }
166
167
168
169
170
171
172 public void testSimpleTypeRedefine() throws Exception {
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 InputStream is = new FileInputStream(Resources.asURI("redefine4.xsd"));
215 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
216 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
217
218 XmlSchemaObjectTable xsot = schema.getElements();
219 assertEquals(1, xsot.getCount());
220
221 XmlSchemaElement xse = null;
222 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
223 xse = (XmlSchemaElement)i.next();
224 }
225 assertEquals("childsizedrink", xse.getName());
226 assertEquals(new QName("http://soapinterop.org/types",
227 "drinksize"),
228 xse.getSchemaTypeName());
229
230 XmlSchemaObjectCollection xsoc = schema.getIncludes();
231 assertEquals(1, xsoc.getCount());
232
233 XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
234 xsot = xsr.getSchemaTypes();
235 assertEquals(1, xsot.getCount());
236
237 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
238 QName qname = (QName)i.next();
239 assertEquals(new QName("http://soapinterop.org/types",
240 "drinksize"), qname);
241 }
242
243 XmlSchemaSimpleType xsst = null;
244 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
245 xsst = (XmlSchemaSimpleType)i.next();
246 }
247 assertNotNull(xsst);
248
249 XmlSchemaSimpleTypeRestriction xsstr =
250 (XmlSchemaSimpleTypeRestriction)xsst.getContent();
251 assertEquals(new QName("http://soapinterop.org/types",
252 "drinksize"),
253 xsstr.getBaseTypeName());
254
255 xsoc = xsstr.getFacets();
256
257 Set s = new HashSet();
258 s.add(XmlSchemaMinInclusiveFacet.class.getName());
259 s.add(XmlSchemaMaxInclusiveFacet.class.getName());
260 for (Iterator i = xsoc.getIterator(); i.hasNext(); ) {
261 Object o = i.next();
262 assertTrue(s.remove(o.getClass().getName()));
263 if (o instanceof XmlSchemaMinInclusiveFacet) {
264 assertEquals("1", ((XmlSchemaMinInclusiveFacet)o).getValue());
265 } else if (o instanceof XmlSchemaMaxInclusiveFacet) {
266 assertEquals("3", ((XmlSchemaMaxInclusiveFacet)o).getValue());
267 } else {
268 fail("Unexpected object encountered: "
269 + o.getClass().getName());
270 }
271 }
272
273 assertTrue("The set should have been empty, but instead contained: "
274 + s + ".",
275 s.isEmpty());
276
277 }
278
279
280
281
282
283
284 public void testGroupRedefine() throws Exception {
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326 InputStream is = new FileInputStream(Resources.asURI("redefine6.xsd"));
327 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
328 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
329
330 XmlSchemaObjectCollection xsoc = schema.getIncludes();
331 assertEquals(1, xsoc.getCount());
332
333 XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
334 XmlSchemaObjectTable xsot = xsr.getGroup();
335 assertEquals(1, xsot.getCount());
336
337 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
338 assertEquals("PrologGroup", ((QName)i.next()).getLocalPart());
339 }
340
341 XmlSchemaGroup xsg = null;
342 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
343 xsg = (XmlSchemaGroup)i.next();
344 }
345
346 XmlSchemaSequence xss = (XmlSchemaSequence)xsg.getParticle();
347
348 xsoc = xss.getItems();
349 assertEquals(2, xsoc.getCount());
350
351 Set s = new HashSet();
352 s.add(XmlSchemaGroupRef.class.getName());
353 s.add(XmlSchemaElement.class.getName());
354 for (Iterator i = xsoc.getIterator(); i.hasNext(); ) {
355 Object o = i.next();
356 assertTrue(s.remove(o.getClass().getName()));
357 if (o instanceof XmlSchemaGroupRef) {
358 assertEquals(new QName("http://soapinterop.org/types",
359 "PrologGroup"),
360 ((XmlSchemaGroupRef)o).getRefName());
361 } else if (o instanceof XmlSchemaElement) {
362 assertEquals("description", ((XmlSchemaElement)o).getName());
363 } else {
364 fail("Unexpected object encountered: "
365 + o.getClass().getName());
366 }
367 }
368
369 assertTrue("The set should have been empty, but instead contained: "
370 + s + ".",
371 s.isEmpty());
372
373 }
374
375
376
377
378
379
380 public void testAttributeGroupRedefine() throws Exception {
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418 InputStream is = new FileInputStream(Resources.asURI("redefine8.xsd"));
419 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
420 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
421
422 XmlSchemaObjectCollection xsoc = schema.getIncludes();
423 assertEquals(1, xsoc.getCount());
424
425 XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
426 XmlSchemaObjectTable xsot = xsr.getAttributeGroup();
427 assertEquals(1, xsot.getCount());
428
429 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
430 assertEquals("AttribGroup", ((QName)i.next()).getLocalPart());
431 }
432
433 XmlSchemaAttributeGroup xsag = null;
434 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
435 xsag = (XmlSchemaAttributeGroup)i.next();
436 }
437
438 assertNotNull(xsag);
439 assertEquals("AttribGroup", (xsag.getName()).getLocalPart());
440 xsoc = xsag.getAttributes();
441
442 Set s = new HashSet();
443 s.add("type");
444 s.add("units");
445 for (Iterator i = xsoc.getIterator(); i.hasNext(); ) {
446 XmlSchemaAttribute xsa = (XmlSchemaAttribute)i.next();
447 assertTrue(s.remove(xsa.getName()));
448 }
449
450 assertTrue("The set should have been empty, but instead contained: "
451 + s + ".",
452 s.isEmpty());
453
454 }
455
456
457
458
459
460
461
462
463 public void testComplexTypeRedefineWithRelativeImports() throws Exception {
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 InputStream is = new FileInputStream(Resources.asURI("redefine10.xsd"));
543 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
544 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
545
546 XmlSchemaObjectTable xsot = schema.getElements();
547 assertEquals(1, xsot.getCount());
548
549 XmlSchemaElement xse = null;
550 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
551 xse = (XmlSchemaElement)i.next();
552 }
553 assertEquals("vip", xse.getName());
554 assertEquals(new QName("http://soapinterop.org/types",
555 "person"),
556 xse.getSchemaTypeName());
557
558 XmlSchemaObjectCollection xsoc = schema.getIncludes();
559 assertEquals(1, xsoc.getCount());
560
561 XmlSchemaRedefine xsr = (XmlSchemaRedefine)xsoc.getItem(0);
562 xsot = xsr.getSchemaTypes();
563 assertEquals(1, xsot.getCount());
564
565 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
566 QName qname = (QName)i.next();
567 assertEquals(new QName("http://soapinterop.org/types",
568 "person"), qname);
569 }
570
571 XmlSchemaComplexType xsct = null;
572 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
573 xsct = (XmlSchemaComplexType)i.next();
574 }
575 assertNotNull(xsct);
576
577 XmlSchemaContentModel xscm = xsct.getContentModel();
578 assertNotNull(xscm);
579
580 XmlSchemaComplexContentExtension xscce =
581 (XmlSchemaComplexContentExtension)xscm.getContent();
582 assertEquals(new QName("http://soapinterop.org/types",
583 "person"),
584 xscce.getBaseTypeName());
585
586 XmlSchemaSequence xsp = (XmlSchemaSequence)xscce.getParticle();
587 assertNotNull(xsp);
588
589 XmlSchemaObjectCollection c = xsp.getItems();
590 assertEquals(1, c.getCount());
591
592 xse = null;
593 for (int i = 0; i < c.getCount(); i++) {
594 xse = (XmlSchemaElement)c.getItem(i);
595 }
596 assertEquals("id", xse.getName());
597 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
598 "string"),
599 xse.getSchemaTypeName());
600
601 }
602 }