|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| package nu.xom.tests; |
|
22 |
| |
|
23 |
| import java.io.File; |
|
24 |
| import java.io.IOException; |
|
25 |
| import java.net.URL; |
|
26 |
| |
|
27 |
| import nu.xom.Attribute; |
|
28 |
| import nu.xom.Builder; |
|
29 |
| import nu.xom.Document; |
|
30 |
| import nu.xom.Element; |
|
31 |
| import nu.xom.Elements; |
|
32 |
| import nu.xom.IllegalDataException; |
|
33 |
| import nu.xom.Node; |
|
34 |
| import nu.xom.Nodes; |
|
35 |
| import nu.xom.ParsingException; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| public class IDTest extends XOMTestCase { |
|
47 |
| |
|
48 |
| |
|
49 |
16
| public IDTest(String name) {
|
|
50 |
16
| super(name);
|
|
51 |
| } |
|
52 |
| |
|
53 |
| |
|
54 |
1
| public void testBuilderAllowsNonNCNameXmlIdAttributes()
|
|
55 |
| throws ParsingException, IOException { |
|
56 |
| |
|
57 |
1
| Builder builder = new Builder();
|
|
58 |
1
| String data = "<root xml:id='p 2'/>";
|
|
59 |
1
| Document doc = builder.build(data, null);
|
|
60 |
1
| Element root = doc.getRootElement();
|
|
61 |
1
| Attribute id = root.getAttribute(0);
|
|
62 |
1
| assertEquals("p 2", id.getValue());
|
|
63 |
| |
|
64 |
| } |
|
65 |
| |
|
66 |
| |
|
67 |
1
| public void testIDMustBeNCName() {
|
|
68 |
| |
|
69 |
1
| Attribute id = new Attribute("xml:id",
|
|
70 |
| "http://www.w3.org/XML/1998/namespace", "name"); |
|
71 |
1
| assertEquals("name", id.getValue());
|
|
72 |
| |
|
73 |
1
| try {
|
|
74 |
1
| id.setValue("not a name");
|
|
75 |
0
| fail("allowed non-NCName as value of xml:id attribute");
|
|
76 |
| } |
|
77 |
| catch (IllegalDataException success) { |
|
78 |
1
| assertNotNull(success.getMessage());
|
|
79 |
1
| assertEquals("not a name", success.getData());
|
|
80 |
| } |
|
81 |
| |
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
1
| public void testNameSetIDMustBeNCName() {
|
|
86 |
| |
|
87 |
1
| Attribute id = new Attribute("id", "not a name");
|
|
88 |
| |
|
89 |
1
| try {
|
|
90 |
1
| id.setNamespace("xml",
|
|
91 |
| "http://www.w3.org/XML/1998/namespace"); |
|
92 |
0
| fail("allowed non-NCName as value of xml:id attribute");
|
|
93 |
| } |
|
94 |
| catch (IllegalDataException success) { |
|
95 |
1
| assertNotNull(success.getMessage());
|
|
96 |
1
| assertEquals("not a name", success.getData());
|
|
97 |
| } |
|
98 |
| |
|
99 |
| } |
|
100 |
| |
|
101 |
| |
|
102 |
1
| public void testBuilderNormalizesXmlIdAttributes()
|
|
103 |
| throws ParsingException, IOException { |
|
104 |
| |
|
105 |
1
| Builder builder = new Builder();
|
|
106 |
1
| String data = "<root xml:id=' p2 '/>";
|
|
107 |
1
| Document doc = builder.build(data, null);
|
|
108 |
1
| Element root = doc.getRootElement();
|
|
109 |
1
| String value = root.getAttributeValue("id",
|
|
110 |
| "http://www.w3.org/XML/1998/namespace"); |
|
111 |
1
| assertEquals("p2", value);
|
|
112 |
| |
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
1
| public void testBuilderDoesNotOverNormalizeXmlIdAttributesWithCarriageReturns()
|
|
117 |
| throws ParsingException, IOException { |
|
118 |
| |
|
119 |
1
| Builder builder = new Builder();
|
|
120 |
1
| String data = "<root xml:id='
 p2 '/>";
|
|
121 |
1
| Document doc = builder.build(data, null);
|
|
122 |
1
| Element root = doc.getRootElement();
|
|
123 |
1
| Attribute id = root.getAttribute(0);
|
|
124 |
1
| assertEquals("\r\u0020p2", id.getValue());
|
|
125 |
| |
|
126 |
| } |
|
127 |
| |
|
128 |
| |
|
129 |
1
| public void testBuilderDoesNotOverNormalizeXmlIdAttributesWithLineFeeds()
|
|
130 |
| throws ParsingException, IOException { |
|
131 |
| |
|
132 |
1
| Builder builder = new Builder();
|
|
133 |
1
| String data = "<root xml:id='
 p2 '/>";
|
|
134 |
1
| Document doc = builder.build(data, null);
|
|
135 |
1
| Element root = doc.getRootElement();
|
|
136 |
1
| Attribute id = root.getAttribute(0);
|
|
137 |
1
| assertEquals("\n\u0020p2", id.getValue());
|
|
138 |
| |
|
139 |
| } |
|
140 |
| |
|
141 |
| |
|
142 |
1
| public void testBuiltXmlIdAttributeHasTypeId()
|
|
143 |
| throws ParsingException, IOException { |
|
144 |
| |
|
145 |
1
| Builder builder = new Builder();
|
|
146 |
1
| String data = "<root xml:id=' p2 '/>";
|
|
147 |
1
| Document doc = builder.build(data, null);
|
|
148 |
1
| Element root = doc.getRootElement();
|
|
149 |
1
| Attribute id = root.getAttribute("id",
|
|
150 |
| "http://www.w3.org/XML/1998/namespace"); |
|
151 |
1
| assertEquals(Attribute.Type.ID, id.getType());
|
|
152 |
| |
|
153 |
| } |
|
154 |
| |
|
155 |
| |
|
156 |
1
| public void testConstructedXmlIdAttributeHasTypeId()
|
|
157 |
| throws ParsingException, IOException { |
|
158 |
| |
|
159 |
1
| Attribute id = new Attribute("xml:id",
|
|
160 |
| "http://www.w3.org/XML/1998/namespace", "p2"); |
|
161 |
1
| assertEquals(Attribute.Type.ID, id.getType());
|
|
162 |
| |
|
163 |
| } |
|
164 |
| |
|
165 |
| |
|
166 |
1
| public void testNamespaceSetXmlIdAttributeHasTypeId() {
|
|
167 |
| |
|
168 |
1
| Attribute id = new Attribute("id", "p2");
|
|
169 |
1
| id.setNamespace("xml", "http://www.w3.org/XML/1998/namespace");
|
|
170 |
1
| assertEquals(Attribute.Type.ID, id.getType());
|
|
171 |
| |
|
172 |
| } |
|
173 |
| |
|
174 |
| |
|
175 |
1
| public void testNameSetXmlIdAttributeHasTypeId() {
|
|
176 |
| |
|
177 |
1
| Attribute id = new Attribute("xml:space",
|
|
178 |
| "http://www.w3.org/XML/1998/namespace", "preserve"); |
|
179 |
1
| id.setLocalName("id");
|
|
180 |
1
| assertEquals(Attribute.Type.ID, id.getType());
|
|
181 |
| |
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
1
| public void testNameSetXmlIdAttributeMustBeNCName() {
|
|
186 |
| |
|
187 |
1
| Attribute id = new Attribute("xml:space",
|
|
188 |
| "http://www.w3.org/XML/1998/namespace", "not a name"); |
|
189 |
1
| try {
|
|
190 |
1
| id.setLocalName("id");
|
|
191 |
0
| fail("Allowed non-NCNAME ID");
|
|
192 |
| } |
|
193 |
| catch (IllegalDataException success) { |
|
194 |
1
| assertNotNull(success.getMessage());
|
|
195 |
| } |
|
196 |
| |
|
197 |
| } |
|
198 |
| |
|
199 |
| |
|
200 |
1
| public void testCantChangeTypeOfXMLIDAttribute() {
|
|
201 |
| |
|
202 |
1
| Attribute id = new Attribute("xml:id",
|
|
203 |
| "http://www.w3.org/XML/1998/namespace", "p2"); |
|
204 |
| |
|
205 |
1
| try {
|
|
206 |
1
| id.setType(Attribute.Type.CDATA);
|
|
207 |
0
| fail("changed xml:id attribute to CDATA");
|
|
208 |
| } |
|
209 |
| catch (IllegalDataException success) { |
|
210 |
1
| assertNotNull(success.getMessage());
|
|
211 |
| } |
|
212 |
1
| assertEquals(Attribute.Type.ID, id.getType());
|
|
213 |
| |
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
1
| public void testCantChangeValueOfXMLIDAttributeToNonNCName() {
|
|
218 |
| |
|
219 |
1
| Attribute id = new Attribute("xml:id",
|
|
220 |
| "http://www.w3.org/XML/1998/namespace", "p2"); |
|
221 |
1
| Attribute id2 = new Attribute(id);
|
|
222 |
1
| try {
|
|
223 |
1
| id.setValue("not a name");
|
|
224 |
0
| fail("Allowed non-name for xml:id");
|
|
225 |
| } |
|
226 |
| catch (IllegalDataException success) { |
|
227 |
1
| assertNotNull(success.getMessage());
|
|
228 |
| } |
|
229 |
| |
|
230 |
| |
|
231 |
1
| assertEquals(id, id2);
|
|
232 |
| |
|
233 |
| } |
|
234 |
| |
|
235 |
| |
|
236 |
1
| public void testXPathRecognizesXmlIDAttributes()
|
|
237 |
| throws ParsingException, IOException { |
|
238 |
| |
|
239 |
1
| Element root = new Element("root");
|
|
240 |
1
| Document doc = new Document(root);
|
|
241 |
1
| Element child = new Element("child");
|
|
242 |
1
| root.appendChild(child);
|
|
243 |
1
| Attribute id = new Attribute("id", "p2");
|
|
244 |
1
| child.addAttribute(id);
|
|
245 |
1
| id.setNamespace("xml", "http://www.w3.org/XML/1998/namespace");
|
|
246 |
1
| Nodes result = doc.query("id('p2')");
|
|
247 |
1
| assertEquals(1, result.size());
|
|
248 |
1
| assertEquals(child, result.get(0));
|
|
249 |
| |
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
1
| public void testXMLIDTestSuiteFromW3CServer()
|
|
254 |
| throws ParsingException, IOException { |
|
255 |
| |
|
256 |
1
| URL base = new URL("http://www.w3.org/XML/2005/01/xml-id/test-suite.xml");
|
|
257 |
1
| Builder builder = new Builder();
|
|
258 |
1
| Document catalog = builder.build(base.openStream());
|
|
259 |
1
| Element testsuite = catalog.getRootElement();
|
|
260 |
1
| Elements testCatalogs = testsuite.getChildElements("test-catalog");
|
|
261 |
1
| for (int i = 0; i < testCatalogs.size(); i++) {
|
|
262 |
1
| Elements testcases = testCatalogs.get(i).getChildElements("test-case");
|
|
263 |
1
| for (int j = 0; j < testcases.size(); j++) {
|
|
264 |
13
| Element testcase = testcases.get(j);
|
|
265 |
13
| String features = testcase.getAttributeValue("feature");
|
|
266 |
13
| if (features != null && features.indexOf("xml11") >= 0) {
|
|
267 |
1
| continue;
|
|
268 |
| } |
|
269 |
12
| URL testURL = new URL(base, testcase.getFirstChildElement("file-path").getValue() + "/");
|
|
270 |
12
| Element scenario = testcase.getFirstChildElement("scenario");
|
|
271 |
12
| Element input = scenario.getFirstChildElement("input-file");
|
|
272 |
12
| URL inputFile = new URL(testURL, input.getValue());
|
|
273 |
12
| Elements expectedIDs = scenario.getChildElements("id");
|
|
274 |
12
| Document inputDoc = builder.build(inputFile.openStream());
|
|
275 |
12
| Nodes recognizedIDs = getIDs(inputDoc);
|
|
276 |
12
| assertEquals(expectedIDs.size(), recognizedIDs.size());
|
|
277 |
12
| for (int k = 0; k < expectedIDs.size(); k++) {
|
|
278 |
15
| assertEquals(expectedIDs.get(i).getValue(), recognizedIDs.get(i).getValue());
|
|
279 |
| } |
|
280 |
| } |
|
281 |
| } |
|
282 |
| |
|
283 |
| } |
|
284 |
| |
|
285 |
| |
|
286 |
1
| public void testXMLIDTestSuite()
|
|
287 |
| throws ParsingException, IOException { |
|
288 |
| |
|
289 |
1
| Builder builder = new Builder();
|
|
290 |
1
| File base = new File("data");
|
|
291 |
1
| base = new File(base, "xmlid");
|
|
292 |
1
| File catalog = new File(base, "catalog.xml");
|
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
1
| if (catalog.exists()) {
|
|
297 |
1
| Document doc = builder.build(catalog);
|
|
298 |
1
| Element testsuite = doc.getRootElement();
|
|
299 |
1
| Elements testCatalogs = testsuite.getChildElements("test-catalog");
|
|
300 |
1
| for (int i = 0; i < testCatalogs.size(); i++) {
|
|
301 |
1
| Elements testcases = testCatalogs.get(i).getChildElements("test-case");
|
|
302 |
1
| for (int j = 0; j < testcases.size(); j++) {
|
|
303 |
12
| Element testcase = testcases.get(j);
|
|
304 |
12
| String features = testcase.getAttributeValue("features");
|
|
305 |
12
| if (features != null && features.indexOf("xml11") >= 0) {
|
|
306 |
1
| continue;
|
|
307 |
| } |
|
308 |
11
| File root = new File(base, testcase.getFirstChildElement("file-path").getValue());
|
|
309 |
11
| File inputFile = null;
|
|
310 |
11
| Element scenario = testcase.getFirstChildElement("scenario");
|
|
311 |
11
| Element input = scenario.getFirstChildElement("input");
|
|
312 |
11
| inputFile = new File(root, input.getValue());
|
|
313 |
11
| Elements expectedIDs = scenario.getChildElements("id");
|
|
314 |
11
| try {
|
|
315 |
11
| Document inputDoc = builder.build(inputFile);
|
|
316 |
11
| Nodes recognizedIDs = getIDs(inputDoc);
|
|
317 |
11
| assertEquals(expectedIDs.size(), recognizedIDs.size());
|
|
318 |
11
| for (int k = 0; k < expectedIDs.size(); k++) {
|
|
319 |
14
| assertEquals(expectedIDs.get(i).getValue(), recognizedIDs.get(i).getValue());
|
|
320 |
| } |
|
321 |
| } |
|
322 |
| catch (ParsingException ex) { |
|
323 |
0
| System.err.println(inputFile);
|
|
324 |
0
| ex.printStackTrace();
|
|
325 |
| } |
|
326 |
| } |
|
327 |
| } |
|
328 |
| |
|
329 |
| } |
|
330 |
| |
|
331 |
| } |
|
332 |
| |
|
333 |
| |
|
334 |
23
| private Nodes getIDs(Document doc) {
|
|
335 |
| |
|
336 |
23
| Element root = doc.getRootElement();
|
|
337 |
23
| Nodes list = new Nodes();
|
|
338 |
23
| getIDs(root, list);
|
|
339 |
23
| return list;
|
|
340 |
| } |
|
341 |
| |
|
342 |
| |
|
343 |
51
| private void getIDs(Element element, Nodes list) {
|
|
344 |
| |
|
345 |
51
| for (int i = 0; i < element.getAttributeCount(); i++) {
|
|
346 |
36
| Attribute a = element.getAttribute(i);
|
|
347 |
36
| if (a.getType() == Attribute.Type.ID) {
|
|
348 |
| |
|
349 |
29
| list.append(a);
|
|
350 |
| } |
|
351 |
| } |
|
352 |
51
| for (int i = 0; i < element.getChildCount(); i++) {
|
|
353 |
96
| Node child = element.getChild(i);
|
|
354 |
96
| if (child instanceof Element) {
|
|
355 |
28
| getIDs((Element) child, list);
|
|
356 |
| } |
|
357 |
| } |
|
358 |
| |
|
359 |
| } |
|
360 |
| |
|
361 |
| |
|
362 |
| } |