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 public class ChoiceTest extends TestCase {
51
52
53
54
55
56
57 public void testChoice() throws Exception {
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 QName computerElementQname = new QName("http://soapinterop.org/types",
78 "computer");
79 QName mbElementQname = new QName("http://soapinterop.org/types",
80 "motherboard");
81
82 InputStream is = new FileInputStream(Resources.asURI("choice.xsd"));
83 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
84 schemaCol.read(new StreamSource(is), null);
85
86 QName WRONG_QNAME = new QName("http://soapinterop.org/types",
87 "machine");
88 XmlSchemaElement elem = schemaCol.getElementByQName(WRONG_QNAME);
89 assertNull(elem);
90 elem = schemaCol.getElementByQName(computerElementQname);
91 assertEquals("computer", elem.getName());
92 assertEquals(new QName("http://soapinterop.org/types", "computer"),
93 elem.getQName());
94
95 XmlSchemaComplexType cType = (XmlSchemaComplexType)elem.getSchemaType();
96 assertNotNull(cType);
97
98 XmlSchemaChoice choice = (XmlSchemaChoice)cType.getParticle();
99 assertNotNull(choice);
100
101 Set s = new HashSet();
102 s.add("desktop");
103 s.add("laptop");
104 XmlSchemaObjectCollection items = choice.getItems();
105 Iterator iterator = items.getIterator();
106 while (iterator.hasNext()) {
107 XmlSchemaElement e = (XmlSchemaElement)iterator.next();
108 String eName = e.getName();
109 if (eName.equals("desktop")) {
110 assertEquals(new QName("", "desktop"), e.getQName());
111 assertEquals(e.getName(), "desktop");
112 } else if (eName.equals("laptop")) {
113 assertEquals(new QName("", "laptop"), e.getQName());
114 assertEquals(e.getName(), "laptop");
115 } else {
116 fail("Should have had a name of desktop or laptop, but"
117 + " instead had " + eName);
118 }
119 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
120 "string"), e.getSchemaTypeName());
121 assertTrue(s.remove(e.getName()));
122 }
123 assertTrue("The set should have been empty, but instead contained: "
124 + s + ".",
125 s.isEmpty());
126
127
128 elem = schemaCol.getElementByQName(mbElementQname);
129 assertEquals("motherboard", elem.getName());
130 assertEquals(new QName("http://soapinterop.org/types", "motherboard"),
131 elem.getQName());
132
133 cType = (XmlSchemaComplexType)elem.getSchemaType();
134 assertNotNull(cType);
135
136 choice = (XmlSchemaChoice)cType.getParticle();
137 assertNotNull(choice);
138
139
140 assertEquals(choice.getMinOccurs(),1);
141 assertEquals(choice.getMaxOccurs(),6);
142
143 }
144
145 }