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.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26
27 import javax.xml.namespace.QName;
28 import javax.xml.transform.stream.StreamSource;
29 import java.io.FileInputStream;
30 import java.io.InputStream;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.Set;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class NotationTest extends TestCase {
54
55
56
57
58
59
60 public void testNotation() throws Exception {
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 QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
99 "demoNotation");
100 QName notationName = new QName("http://soapinterop.org/types",
101 "teamLogo");
102
103
104
105 InputStream is = new FileInputStream(Resources.asURI("notation.xsd"));
106 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
107 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
108
109 XmlSchemaObjectTable notations = schema.getNotations();
110 assertNotNull(notations.getItem(notationName));
111
112 XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
113 assertNotNull(elem);
114 assertEquals("demoNotation", elem.getName());
115 assertEquals(new QName("http://soapinterop.org/types", "demoNotation"),
116 elem.getQName());
117
118 XmlSchemaSimpleType type =
119 (XmlSchemaSimpleType)elem.getSchemaType();
120 assertNotNull(type);
121
122 XmlSchemaSimpleTypeRestriction xsstc =
123 (XmlSchemaSimpleTypeRestriction)type.getContent();
124 assertEquals(new QName("http://www.w3.org/2001/XMLSchema","NOTATION"),
125 xsstc.getBaseTypeName());
126
127 XmlSchemaObjectCollection xsoc = xsstc.getFacets();
128 assertEquals(2, xsoc.getCount());
129 Set s = new HashSet();
130 s.add("tns:teamLogo");
131 s.add("tns:teamMascot");
132 for (int i = 0; i < xsoc.getCount(); i++) {
133 XmlSchemaEnumerationFacet xsef =
134 (XmlSchemaEnumerationFacet)xsoc.getItem(i);
135 String value = (String)xsef.getValue();
136 if (!(value.equals("tns:teamLogo")
137 || value.equals("tns:teamMascot"))) {
138 fail("An unexpected value of \"" + value
139 + "\" was found.");
140 }
141 assertTrue(s.remove(value));
142 }
143 assertTrue("The set should have been empty, but instead contained: "
144 + s + ".",
145 s.isEmpty());
146
147 XmlSchemaObjectTable xsot = schema.getNotations();
148 assertEquals(2, xsot.getCount());
149
150 s.clear();
151 s.add("teamMascot");
152 s.add("teamLogo");
153 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
154 String name = ((QName)i.next()).getLocalPart();
155 if (!(name.equals("teamLogo")
156 || name.equals("teamMascot"))) {
157 fail("An unexpected name of \"" + name
158 + "\" was found.");
159 }
160 assertTrue(s.remove(name));
161 }
162 assertTrue("The set should have been empty, but instead contained: "
163 + s + ".",
164 s.isEmpty());
165
166 s.clear();
167 s.add("teamMascot");
168 s.add("teamLogo");
169 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
170 XmlSchemaNotation xsn = (XmlSchemaNotation)i.next();
171 String name = xsn.getName();
172 XmlSchemaAnnotation xsa = xsn.getAnnotation();
173 XmlSchemaObjectCollection col = xsa.getItems();
174 assertEquals(1, col.getCount());
175 XmlSchemaDocumentation xsd = null;
176 for (int k = 0; k < col.getCount(); k++) {
177 xsd = (XmlSchemaDocumentation)col.getItem(k);
178 }
179 if (name.equals("teamMascot")) {
180 assertEquals("http://www.team.com/graphics/teamMascot",
181 xsn.getPublic());
182 assertEquals("com/team/graphics/teamMascot",
183 xsn.getSystem());
184 assertEquals("notation.teamMascot", xsn.getId());
185 assertEquals("en", xsd.getLanguage());
186 NodeList nl = xsd.getMarkup();
187 for (int j = 0; j < nl.getLength(); j++) {
188 Node n = nl.item(j);
189 if (n.getNodeType() == Node.TEXT_NODE) {
190 assertEquals("Location of the corporate mascot.",
191 n.getNodeValue());
192 }
193 }
194 } else if (name.equals("teamLogo")) {
195 assertEquals("http://www.team.com/graphics/teamLogo",
196 xsn.getPublic());
197 assertEquals("com/team/graphics/teamLogo",
198 xsn.getSystem());
199 assertEquals("notation.teamLogo", xsn.getId());
200 assertEquals("en", xsd.getLanguage());
201 NodeList nl = xsd.getMarkup();
202 for (int j = 0; j < nl.getLength(); j++) {
203 Node n = nl.item(j);
204 if (n.getNodeType() == Node.TEXT_NODE) {
205 assertEquals("Location of the corporate logo.",
206 n.getNodeValue());
207 }
208 }
209 } else {
210 fail("An unexpected name of \"" + name
211 + "\" was found.");
212 }
213 assertTrue(s.remove(name));
214 }
215 assertTrue("The set should have been empty, but instead contained: "
216 + s + ".",
217 s.isEmpty());
218
219 }
220
221 }