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 import org.xml.sax.InputSource;
25
26 import javax.xml.namespace.QName;
27 import javax.xml.transform.stream.StreamSource;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.FileInputStream;
32 import java.io.InputStream;
33 import java.util.HashSet;
34 import java.util.Set;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class AnyTest extends TestCase {
54
55
56
57
58
59
60 public void testAny() throws Exception {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
83 "department");
84 InputStream is = new FileInputStream(Resources.asURI("any.xsd"));
85 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
86 schemaCol.read(new StreamSource(is), null);
87
88 verifyAccuracy(ELEMENT_QNAME, schemaCol,5L,10L);
89
90 }
91
92 public void testAnyAttribute() throws Exception {
93 InputStream is = new FileInputStream(Resources.asURI("anyAttribute.xsd"));
94 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
95 schemaCol.read(new StreamSource(is), null);
96
97 XmlSchema[] schemas = schemaCol.getXmlSchemas();
98 XmlSchema schema = null;
99 for(int x = 0; x < schemas.length; x ++) {
100 if("http://soapinterop.org/types".equals(schemas[x].getTargetNamespace())) {
101 schema = schemas[x];
102 }
103 }
104
105 ByteArrayOutputStream baos = new ByteArrayOutputStream();
106 schema.write(baos);
107 baos.close();
108 byte[] bytes = baos.toByteArray();
109 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
110 schemaCol = new XmlSchemaCollection();
111 schemaCol.read(new InputSource(bais), null);
112 XmlSchemaType type = schemaCol.getTypeByQName(new QName("http://soapinterop.org/types",
113 "OccuringStructWithAnyAttribute"));
114 XmlSchemaComplexType complexType = (XmlSchemaComplexType) type;
115 XmlSchemaAnyAttribute aa = complexType.getAnyAttribute();
116 assertNotNull(aa);
117 }
118
119
120 public void testAnyZeroOccurs() throws Exception {
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
143 "department");
144 InputStream is = new FileInputStream(Resources.asURI("anyZero.xsd"));
145 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
146 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
147
148 ByteArrayOutputStream baos = new ByteArrayOutputStream();
149 schema.write(baos);
150
151 XmlSchemaCollection schemaCol2 = new XmlSchemaCollection();
152 schemaCol2.read(new StreamSource(new ByteArrayInputStream(baos.toByteArray())), null);
153
154 verifyAccuracy(ELEMENT_QNAME, schemaCol2,0,0);
155
156
157 }
158
159 private void verifyAccuracy(QName ELEMENT_QNAME,
160 XmlSchemaCollection schemaCol,long minCount, long maxCount) {
161 XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
162 assertNotNull(elem);
163 assertEquals("department", elem.getName());
164 assertEquals(new QName("http://soapinterop.org/types", "department"),
165 elem.getQName());
166
167 XmlSchemaComplexType type =
168 (XmlSchemaComplexType)elem.getSchemaType();
169 assertNotNull(type);
170
171 XmlSchemaSequence xss = (XmlSchemaSequence)type.getParticle();
172 assertNotNull(xss);
173
174 XmlSchemaObjectCollection c = xss.getItems();
175 assertEquals(3, c.getCount());
176
177 Set s = new HashSet();
178 s.add("id");
179 s.add("name");
180 Object o = null;
181 for (int i = 0; i < c.getCount(); i++) {
182 o = c.getItem(i);
183 if (o instanceof XmlSchemaElement) {
184 String name = ((XmlSchemaElement)o).getName();
185 if (name.equals("id")) {
186 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
187 "integer"),
188 ((XmlSchemaElement)o).getSchemaTypeName());
189 } else if (name.equals("name")) {
190 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
191 "string"),
192 ((XmlSchemaElement)o).getSchemaTypeName());
193 }
194 s.remove(name);
195 } else if (o instanceof XmlSchemaAny) {
196 XmlSchemaContentProcessing xscp =
197 ((XmlSchemaAny)o).getProcessContent();
198 assertEquals("none", xscp.toString());
199 assertEquals(minCount, ((XmlSchemaAny)o).getMinOccurs());
200 assertEquals(maxCount, ((XmlSchemaAny)o).getMaxOccurs());
201 }
202 }
203
204 assertTrue("The set should have been empty, but instead contained: "
205 + s + ".",
206 s.isEmpty());
207 }
208
209 }