1 package tests;
2
3 import junit.framework.TestCase;
4 import org.apache.ws.commons.schema.*;
5 import org.w3c.dom.Node;
6 import org.w3c.dom.NodeList;
7
8 import javax.xml.namespace.QName;
9 import javax.xml.transform.stream.StreamSource;
10 import java.io.FileInputStream;
11 import java.io.InputStream;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class NotationTest extends TestCase {
35
36
37
38
39
40
41 public void testNotation() throws Exception {
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 QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
80 "demoNotation");
81 QName notationName = new QName("http://soapinterop.org/types",
82 "teamLogo");
83
84
85
86 InputStream is = new FileInputStream(Resources.asURI("notation.xsd"));
87 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
88 XmlSchema schema = schemaCol.read(new StreamSource(is), null);
89
90 XmlSchemaObjectTable notations = schema.getNotations();
91 assertNotNull(notations.getItem(notationName));
92
93 XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
94 assertNotNull(elem);
95 assertEquals("demoNotation", elem.getName());
96 assertEquals(new QName("http://soapinterop.org/types", "demoNotation"),
97 elem.getQName());
98
99 XmlSchemaSimpleType type =
100 (XmlSchemaSimpleType)elem.getSchemaType();
101 assertNotNull(type);
102
103 XmlSchemaSimpleTypeRestriction xsstc =
104 (XmlSchemaSimpleTypeRestriction)type.getContent();
105 assertEquals(new QName("http://www.w3.org/2001/XMLSchema","NOTATION"),
106 xsstc.getBaseTypeName());
107
108 XmlSchemaObjectCollection xsoc = xsstc.getFacets();
109 assertEquals(2, xsoc.getCount());
110 Set s = new HashSet();
111 s.add("tns:teamLogo");
112 s.add("tns:teamMascot");
113 for (int i = 0; i < xsoc.getCount(); i++) {
114 XmlSchemaEnumerationFacet xsef =
115 (XmlSchemaEnumerationFacet)xsoc.getItem(i);
116 String value = (String)xsef.getValue();
117 if (!(value.equals("tns:teamLogo")
118 || value.equals("tns:teamMascot"))) {
119 fail("An unexpected value of \"" + value
120 + "\" was found.");
121 }
122 assertTrue(s.remove(value));
123 }
124 assertTrue("The set should have been empty, but instead contained: "
125 + s + ".",
126 s.isEmpty());
127
128 XmlSchemaObjectTable xsot = schema.getNotations();
129 assertEquals(2, xsot.getCount());
130
131 s.clear();
132 s.add("teamMascot");
133 s.add("teamLogo");
134 for (Iterator i = xsot.getNames(); i.hasNext(); ) {
135 String name = ((QName)i.next()).getLocalPart();
136 if (!(name.equals("teamLogo")
137 || name.equals("teamMascot"))) {
138 fail("An unexpected name of \"" + name
139 + "\" was found.");
140 }
141 assertTrue(s.remove(name));
142 }
143 assertTrue("The set should have been empty, but instead contained: "
144 + s + ".",
145 s.isEmpty());
146
147 s.clear();
148 s.add("teamMascot");
149 s.add("teamLogo");
150 for (Iterator i = xsot.getValues(); i.hasNext(); ) {
151 XmlSchemaNotation xsn = (XmlSchemaNotation)i.next();
152 String name = xsn.getName();
153 XmlSchemaAnnotation xsa = xsn.getAnnotation();
154 XmlSchemaObjectCollection col = xsa.getItems();
155 assertEquals(1, col.getCount());
156 XmlSchemaDocumentation xsd = null;
157 for (int k = 0; k < col.getCount(); k++) {
158 xsd = (XmlSchemaDocumentation)col.getItem(k);
159 }
160 if (name.equals("teamMascot")) {
161 assertEquals("http://www.team.com/graphics/teamMascot",
162 xsn.getPublic());
163 assertEquals("com/team/graphics/teamMascot",
164 xsn.getSystem());
165 assertEquals("notation.teamMascot", xsn.getId());
166 assertEquals("en", xsd.getLanguage());
167 NodeList nl = xsd.getMarkup();
168 for (int j = 0; j < nl.getLength(); j++) {
169 Node n = nl.item(j);
170 if (n.getNodeType() == Node.TEXT_NODE) {
171 assertEquals("Location of the corporate mascot.",
172 n.getNodeValue());
173 }
174 }
175 } else if (name.equals("teamLogo")) {
176 assertEquals("http://www.team.com/graphics/teamLogo",
177 xsn.getPublic());
178 assertEquals("com/team/graphics/teamLogo",
179 xsn.getSystem());
180 assertEquals("notation.teamLogo", xsn.getId());
181 assertEquals("en", xsd.getLanguage());
182 NodeList nl = xsd.getMarkup();
183 for (int j = 0; j < nl.getLength(); j++) {
184 Node n = nl.item(j);
185 if (n.getNodeType() == Node.TEXT_NODE) {
186 assertEquals("Location of the corporate logo.",
187 n.getNodeValue());
188 }
189 }
190 } else {
191 fail("An unexpected name of \"" + name
192 + "\" was found.");
193 }
194 assertTrue(s.remove(name));
195 }
196 assertTrue("The set should have been empty, but instead contained: "
197 + s + ".",
198 s.isEmpty());
199
200 }
201
202 }