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.Set;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class ConstraintsTest extends TestCase {
51
52
53
54
55
56
57
58 public void testConstraints() 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
132 "constraintTest");
133 InputStream is = new FileInputStream(Resources.asURI("constraints.xsd"));
134 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
135 schemaCol.read(new StreamSource(is), null);
136
137 XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
138 assertNotNull(elem);
139 assertEquals("constraintTest", elem.getName());
140 assertEquals(new QName("http://soapinterop.org/types", "constraintTest"),
141 elem.getQName());
142
143 XmlSchemaObjectCollection c = elem.getConstraints();
144 assertEquals(3, c.getCount());
145
146 Set s = new HashSet();
147 s.add(XmlSchemaKey.class.getName());
148 s.add(XmlSchemaKeyref.class.getName());
149 s.add(XmlSchemaUnique.class.getName());
150 for (int i = 0; i < c.getCount(); i++) {
151 Object o = c.getItem(i);
152 if (o instanceof XmlSchemaKey) {
153 XmlSchemaKey key = (XmlSchemaKey)o;
154 assertEquals("keyTest", key.getName());
155
156 XmlSchemaXPath selectorXpath = key.getSelector();
157 assertEquals("tns:products/tns:productName",
158 selectorXpath.getXPath());
159
160 XmlSchemaObjectCollection fields = key.getFields();
161 assertEquals(1, fields.getCount());
162 XmlSchemaXPath fieldXpath = null;
163 for (int j = 0; j < fields.getCount(); j++) {
164 fieldXpath = (XmlSchemaXPath)fields.getItem(j);
165 }
166 assertNotNull(fieldXpath);
167 assertEquals("@productId", fieldXpath.getXPath());
168 } else if (o instanceof XmlSchemaKeyref) {
169 XmlSchemaKeyref keyref = (XmlSchemaKeyref)o;
170 assertNotNull(keyref);
171 assertEquals("keyRefTest", keyref.getName());
172 assertEquals(new QName("http://soapinterop.org/types",
173 "keyTest"),
174 keyref.getRefer());
175
176 XmlSchemaXPath selectorXpath = keyref.getSelector();
177 assertEquals("tns:manufacturers/tns:location/tns:productName",
178 selectorXpath.getXPath());
179
180 XmlSchemaObjectCollection fields = keyref.getFields();
181 assertEquals(1, fields.getCount());
182 XmlSchemaXPath fieldXpath = null;
183 for (int j = 0; j < fields.getCount(); j++) {
184 fieldXpath = (XmlSchemaXPath)fields.getItem(j);
185 }
186 assertNotNull(fieldXpath);
187 assertEquals("@productId", fieldXpath.getXPath());
188 } else if (o instanceof XmlSchemaUnique) {
189 XmlSchemaUnique unique = (XmlSchemaUnique)o;
190 assertNotNull(unique);
191 assertEquals("uniqueTest", unique.getName());
192 XmlSchemaXPath selectorXpath = unique.getSelector();
193 assertEquals("tns:manufacturers/tns:location",
194 selectorXpath.getXPath());
195
196 XmlSchemaObjectCollection fields = unique.getFields();
197 assertEquals(1, fields.getCount());
198 XmlSchemaXPath fieldXpath = null;
199 for (int j = 0; j < fields.getCount(); j++) {
200 fieldXpath = (XmlSchemaXPath)fields.getItem(j);
201 }
202 assertNotNull(fieldXpath);
203 assertEquals("@district", fieldXpath.getXPath());
204 } else {
205 fail("An unexpected constraint of \""
206 + o.getClass().getName() + "\" was found.");
207 }
208 s.remove(o.getClass().getName());
209 }
210
211 assertTrue("The set should have been empty, but instead contained: "
212 + s + ".",
213 s.isEmpty());
214
215 }
216
217 }