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.Set;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class ConstraintsTest extends TestCase {
32
33
34
35
36
37
38
39 public void testConstraints() 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
113 "constraintTest");
114 InputStream is = new FileInputStream(Resources.asURI("constraints.xsd"));
115 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
116 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
117
118 XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
119 assertNotNull(elem);
120 assertEquals("constraintTest", elem.getName());
121 assertEquals(new QName("http://soapinterop.org/types", "constraintTest"),
122 elem.getQName());
123
124 XmlSchemaObjectCollection c = elem.getConstraints();
125 assertEquals(3, c.getCount());
126
127 Set s = new HashSet();
128 s.add(XmlSchemaKey.class.getName());
129 s.add(XmlSchemaKeyref.class.getName());
130 s.add(XmlSchemaUnique.class.getName());
131 for (int i = 0; i < c.getCount(); i++) {
132 Object o = c.getItem(i);
133 if (o instanceof XmlSchemaKey) {
134 XmlSchemaKey key = (XmlSchemaKey)o;
135 assertEquals("keyTest", key.getName());
136
137 XmlSchemaXPath selectorXpath = key.getSelector();
138 assertEquals("tns:products/tns:productName",
139 selectorXpath.getXPath());
140
141 XmlSchemaObjectCollection fields = key.getFields();
142 assertEquals(1, fields.getCount());
143 XmlSchemaXPath fieldXpath = null;
144 for (int j = 0; j < fields.getCount(); j++) {
145 fieldXpath = (XmlSchemaXPath)fields.getItem(j);
146 }
147 assertNotNull(fieldXpath);
148 assertEquals("@productId", fieldXpath.getXPath());
149 } else if (o instanceof XmlSchemaKeyref) {
150 XmlSchemaKeyref keyref = (XmlSchemaKeyref)o;
151 assertNotNull(keyref);
152 assertEquals("keyRefTest", keyref.getName());
153 assertEquals(new QName("http://soapinterop.org/types",
154 "keyTest"),
155 keyref.getRefer());
156
157 XmlSchemaXPath selectorXpath = keyref.getSelector();
158 assertEquals("tns:manufacturers/tns:location/tns:productName",
159 selectorXpath.getXPath());
160
161 XmlSchemaObjectCollection fields = keyref.getFields();
162 assertEquals(1, fields.getCount());
163 XmlSchemaXPath fieldXpath = null;
164 for (int j = 0; j < fields.getCount(); j++) {
165 fieldXpath = (XmlSchemaXPath)fields.getItem(j);
166 }
167 assertNotNull(fieldXpath);
168 assertEquals("@productId", fieldXpath.getXPath());
169 } else if (o instanceof XmlSchemaUnique) {
170 XmlSchemaUnique unique = (XmlSchemaUnique)o;
171 assertNotNull(unique);
172 assertEquals("uniqueTest", unique.getName());
173 XmlSchemaXPath selectorXpath = unique.getSelector();
174 assertEquals("tns:manufacturers/tns:location",
175 selectorXpath.getXPath());
176
177 XmlSchemaObjectCollection fields = unique.getFields();
178 assertEquals(1, fields.getCount());
179 XmlSchemaXPath fieldXpath = null;
180 for (int j = 0; j < fields.getCount(); j++) {
181 fieldXpath = (XmlSchemaXPath)fields.getItem(j);
182 }
183 assertNotNull(fieldXpath);
184 assertEquals("@district", fieldXpath.getXPath());
185 } else {
186 fail("An unexpected constraint of \""
187 + o.getClass().getName() + "\" was found.");
188 }
189 s.remove(o.getClass().getName());
190 }
191
192 assertTrue("The set should have been empty, but instead contained: "
193 + s + ".",
194 s.isEmpty());
195
196 }
197
198 }