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 GroupTest extends TestCase {
51
52
53
54
55
56
57 public void testGroup() throws Exception {
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
88
89
90
91
92 QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
93 "price");
94 InputStream is = new FileInputStream(Resources.asURI("group.xsd"));
95 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
96 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
97
98 XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
99 assertNotNull(elem);
100 assertEquals("price", elem.getName());
101 assertEquals(new QName("http://soapinterop.org/types", "price"),
102 elem.getQName());
103
104 XmlSchemaComplexType cType = (XmlSchemaComplexType)elem.getSchemaType();
105 assertNotNull(cType);
106
107 XmlSchemaGroupRef ref = (XmlSchemaGroupRef)cType.getParticle();
108 assertEquals(new QName("http://soapinterop.org/types", "priceGroup"),
109 ref.getRefName());
110
111 XmlSchemaObjectTable t = schema.getGroups();
112 assertEquals(1, t.getCount());
113
114 Set s = new HashSet();
115 s.add("priceGroup");
116 for (Iterator i = t.getNames(); i.hasNext(); ) {
117 String name = ((QName)i.next()).getLocalPart();
118 assertEquals("priceGroup", name);
119 s.remove(name);
120 }
121 assertTrue("The set should have been empty, but instead contained: "
122 + s + ".",
123 s.isEmpty());
124
125 s.clear();
126 s.add("org.apache.ws.commons.schema.XmlSchemaGroup");
127 XmlSchemaGroup xsg = null;
128 for (Iterator i = t.getValues(); i.hasNext(); ) {
129 xsg = (XmlSchemaGroup)i.next();
130 s.remove(xsg.getClass().getName());
131 }
132 assertTrue("The set should have been empty, but instead contained: "
133 + s + ".",
134 s.isEmpty());
135
136 assertNotNull(xsg);
137 assertEquals("priceGroup", xsg.getName().getLocalPart());
138
139 XmlSchemaChoice xsc = (XmlSchemaChoice)xsg.getParticle();
140 assertNotNull(xsc);
141
142 s.clear();
143 s.add("fullPrice");
144 s.add("salePrice");
145 s.add("clearancePrice");
146 s.add("freePrice");
147 XmlSchemaObjectCollection items = xsc.getItems();
148 Iterator iterator = items.getIterator();
149 while (iterator.hasNext()) {
150 XmlSchemaElement e = (XmlSchemaElement)iterator.next();
151 String eName = e.getName();
152 if (eName.equals("fullPrice")) {
153 assertEquals(new QName("", "fullPrice"), e.getQName());
154 } else if (eName.equals("salePrice")) {
155 assertEquals(new QName("", "salePrice"), e.getQName());
156 } else if (eName.equals("clearancePrice")) {
157 assertEquals(new QName("", "clearancePrice"), e.getQName());
158 } else if (eName.equals("freePrice")) {
159 assertEquals(new QName("", "freePrice"), e.getQName());
160 } else {
161 fail("The name \"" + eName + "\" was found but shouldn't "
162 + "have been found.");
163 }
164 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
165 "decimal"), e.getSchemaTypeName());
166 assertTrue(s.remove(e.getName()));
167 }
168 assertTrue("The set should have been empty, but instead contained: "
169 + s + ".",
170 s.isEmpty());
171
172 }
173
174 }