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 public class GroupTest extends TestCase {
32
33
34
35
36
37
38 public void testGroup() throws Exception {
39
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 QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
74 "price");
75 InputStream is = new FileInputStream(Resources.asURI("group.xsd"));
76 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
77 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
78
79 XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
80 assertNotNull(elem);
81 assertEquals("price", elem.getName());
82 assertEquals(new QName("http://soapinterop.org/types", "price"),
83 elem.getQName());
84
85 XmlSchemaComplexType cType = (XmlSchemaComplexType)elem.getSchemaType();
86 assertNotNull(cType);
87
88 XmlSchemaGroupRef ref = (XmlSchemaGroupRef)cType.getParticle();
89 assertEquals(new QName("http://soapinterop.org/types", "priceGroup"),
90 ref.getRefName());
91
92 XmlSchemaObjectTable t = schema.getGroups();
93 assertEquals(1, t.getCount());
94
95 XmlSchemaObject o = t.getItem(ref.getRefName());
96
97 Set s = new HashSet();
98 s.add("priceGroup");
99 for (Iterator i = t.getNames(); i.hasNext(); ) {
100 String name = ((QName)i.next()).getLocalPart();
101 assertEquals("priceGroup", name);
102 s.remove(name);
103 }
104 assertTrue("The set should have been empty, but instead contained: "
105 + s + ".",
106 s.isEmpty());
107
108 s.clear();
109 s.add("org.apache.ws.commons.schema.XmlSchemaGroup");
110 XmlSchemaGroup xsg = null;
111 for (Iterator i = t.getValues(); i.hasNext(); ) {
112 xsg = (XmlSchemaGroup)i.next();
113 s.remove(xsg.getClass().getName());
114 }
115 assertTrue("The set should have been empty, but instead contained: "
116 + s + ".",
117 s.isEmpty());
118
119 assertEquals("priceGroup", xsg.getName());
120
121 XmlSchemaChoice xsc = (XmlSchemaChoice)xsg.getParticle();
122 assertNotNull(xsc);
123
124 s.clear();
125 s.add("fullPrice");
126 s.add("salePrice");
127 s.add("clearancePrice");
128 s.add("freePrice");
129 XmlSchemaObjectCollection items = xsc.getItems();
130 Iterator iterator = items.getIterator();
131 while (iterator.hasNext()) {
132 XmlSchemaElement e = (XmlSchemaElement)iterator.next();
133 String eName = e.getName();
134 if (eName.equals("fullPrice")) {
135 assertEquals(new QName("", "fullPrice"), e.getQName());
136 } else if (eName.equals("salePrice")) {
137 assertEquals(new QName("", "salePrice"), e.getQName());
138 } else if (eName.equals("clearancePrice")) {
139 assertEquals(new QName("", "clearancePrice"), e.getQName());
140 } else if (eName.equals("freePrice")) {
141 assertEquals(new QName("", "freePrice"), e.getQName());
142 } else {
143 fail("The name \"" + eName + "\" was found but shouldn't "
144 + "have been found.");
145 }
146 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
147 "decimal"), e.getSchemaTypeName());
148 assertTrue(s.remove(e.getName()));
149 }
150 assertTrue("The set should have been empty, but instead contained: "
151 + s + ".",
152 s.isEmpty());
153
154 }
155
156 }