~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/IDTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2005 Elliotte Rusty Harold
 
2
   
 
3
   This library is free software; you can redistribute it and/or modify
 
4
   it under the terms of version 2.1 of the GNU Lesser General Public 
 
5
   License as published by the Free Software Foundation.
 
6
   
 
7
   This library is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 
10
   GNU Lesser General Public License for more details.
 
11
   
 
12
   You should have received a copy of the GNU Lesser General Public
 
13
   License along with this library; if not, write to the 
 
14
   Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 
15
   Boston, MA 02111-1307  USA
 
16
   
 
17
   You can contact Elliotte Rusty Harold by sending e-mail to
 
18
   elharo@metalab.unc.edu. Please include the word "XOM" in the
 
19
   subject line. The XOM home page is located at http://www.xom.nu/
 
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
 * <p>
 
39
 * Tests for <code>xml:id</code> attributes.
 
40
 * </p>
 
41
 * 
 
42
 * @author Elliotte Rusty Harold
 
43
 * @version 1.1b2
 
44
 *
 
45
 */
 
46
public class IDTest extends XOMTestCase {
 
47
 
 
48
    
 
49
    public IDTest(String name) {
 
50
        super(name);
 
51
    }
 
52
 
 
53
    
 
54
    public void testBuilderAllowsNonNCNameXmlIdAttributes() 
 
55
      throws ParsingException, IOException {
 
56
        
 
57
        Builder builder = new Builder();
 
58
        String data = "<root xml:id='p 2'/>";
 
59
        Document doc = builder.build(data, null);
 
60
        Element root = doc.getRootElement();
 
61
        Attribute id = root.getAttribute(0);
 
62
        assertEquals("p 2", id.getValue());  
 
63
        
 
64
    }
 
65
    
 
66
    
 
67
    public void testIDMustBeNCName() {
 
68
        
 
69
        Attribute id = new Attribute("xml:id", 
 
70
          "http://www.w3.org/XML/1998/namespace", "name");
 
71
        assertEquals("name", id.getValue());
 
72
        
 
73
        try {
 
74
            id.setValue("not a name");
 
75
            fail("allowed non-NCName as value of xml:id attribute");
 
76
        }
 
77
        catch (IllegalDataException success) {
 
78
            assertNotNull(success.getMessage());
 
79
            assertEquals("not a name", success.getData());
 
80
        }
 
81
        
 
82
    }
 
83
 
 
84
    
 
85
    public void testNameSetIDMustBeNCName() {
 
86
        
 
87
        Attribute id = new Attribute("id", "not a name");
 
88
        
 
89
        try {
 
90
            id.setNamespace("xml", 
 
91
              "http://www.w3.org/XML/1998/namespace");
 
92
            fail("allowed non-NCName as value of xml:id attribute");
 
93
        }
 
94
        catch (IllegalDataException success) {
 
95
            assertNotNull(success.getMessage());
 
96
            assertEquals("not a name", success.getData());
 
97
        }
 
98
        
 
99
    }
 
100
 
 
101
    
 
102
    public void testBuilderNormalizesXmlIdAttributes() 
 
103
      throws ParsingException, IOException {
 
104
        
 
105
        Builder builder = new Builder();
 
106
        String data = "<root xml:id='  p2  '/>";
 
107
        Document doc = builder.build(data, null);
 
108
        Element root = doc.getRootElement();
 
109
        String value = root.getAttributeValue("id", 
 
110
          "http://www.w3.org/XML/1998/namespace");
 
111
        assertEquals("p2", value);
 
112
        
 
113
    }
 
114
 
 
115
    
 
116
    public void testBuilderDoesNotOverNormalizeXmlIdAttributesWithCarriageReturns() 
 
117
      throws ParsingException, IOException {
 
118
        
 
119
        Builder builder = new Builder();
 
120
        String data = "<root xml:id='&#x0D;  p2  '/>";
 
121
        Document doc = builder.build(data, null);
 
122
        Element root = doc.getRootElement();
 
123
        Attribute id = root.getAttribute(0);
 
124
        assertEquals("\r\u0020p2", id.getValue());
 
125
        
 
126
    }
 
127
 
 
128
    
 
129
    public void testBuilderDoesNotOverNormalizeXmlIdAttributesWithLineFeeds() 
 
130
      throws ParsingException, IOException {
 
131
        
 
132
        Builder builder = new Builder();
 
133
        String data = "<root xml:id='&#x0A;  p2  '/>";
 
134
        Document doc = builder.build(data, null);
 
135
        Element root = doc.getRootElement();
 
136
        Attribute id = root.getAttribute(0);
 
137
        assertEquals("\n\u0020p2", id.getValue());
 
138
        
 
139
    }
 
140
 
 
141
    
 
142
    public void testBuiltXmlIdAttributeHasTypeId() 
 
143
      throws ParsingException, IOException {
 
144
        
 
145
        Builder builder = new Builder();
 
146
        String data = "<root xml:id='  p2  '/>";
 
147
        Document doc = builder.build(data, null);
 
148
        Element root = doc.getRootElement();
 
149
        Attribute id = root.getAttribute("id", 
 
150
          "http://www.w3.org/XML/1998/namespace");
 
151
        assertEquals(Attribute.Type.ID, id.getType());
 
152
        
 
153
    }
 
154
    
 
155
    
 
156
    public void testConstructedXmlIdAttributeHasTypeId() 
 
157
      throws ParsingException, IOException {
 
158
        
 
159
        Attribute id = new Attribute("xml:id", 
 
160
          "http://www.w3.org/XML/1998/namespace", "p2");
 
161
        assertEquals(Attribute.Type.ID, id.getType());
 
162
        
 
163
    }
 
164
    
 
165
    
 
166
    public void testNamespaceSetXmlIdAttributeHasTypeId() {
 
167
        
 
168
        Attribute id = new Attribute("id", "p2");
 
169
        id.setNamespace("xml", "http://www.w3.org/XML/1998/namespace");
 
170
        assertEquals(Attribute.Type.ID, id.getType());
 
171
        
 
172
    }
 
173
 
 
174
    
 
175
    public void testNameSetXmlIdAttributeHasTypeId() {
 
176
        
 
177
        Attribute id = new Attribute("xml:space", 
 
178
          "http://www.w3.org/XML/1998/namespace", "preserve");
 
179
        id.setLocalName("id");
 
180
        assertEquals(Attribute.Type.ID, id.getType());
 
181
        
 
182
    }
 
183
 
 
184
    
 
185
    public void testNameSetXmlIdAttributeMustBeNCName() {
 
186
        
 
187
        Attribute id = new Attribute("xml:space", 
 
188
          "http://www.w3.org/XML/1998/namespace", "not a name");
 
189
        try {
 
190
            id.setLocalName("id");
 
191
            fail("Allowed non-NCNAME ID");
 
192
        }
 
193
        catch (IllegalDataException success) {
 
194
            assertNotNull(success.getMessage());
 
195
        }
 
196
        
 
197
    }
 
198
 
 
199
    
 
200
    public void testCantChangeTypeOfXMLIDAttribute() {
 
201
        
 
202
        Attribute id = new Attribute("xml:id", 
 
203
          "http://www.w3.org/XML/1998/namespace", "p2");
 
204
        
 
205
        try {
 
206
            id.setType(Attribute.Type.CDATA);
 
207
            fail("changed xml:id attribute to CDATA");
 
208
        }
 
209
        catch (IllegalDataException success) {
 
210
            assertNotNull(success.getMessage());
 
211
        }
 
212
        assertEquals(Attribute.Type.ID, id.getType());
 
213
        
 
214
    }
 
215
 
 
216
    
 
217
    public void testCantChangeValueOfXMLIDAttributeToNonNCName() {
 
218
        
 
219
        Attribute id = new Attribute("xml:id", 
 
220
          "http://www.w3.org/XML/1998/namespace", "p2");
 
221
        Attribute id2 = new Attribute(id);
 
222
        try {
 
223
            id.setValue("not a name");
 
224
            fail("Allowed non-name for xml:id");
 
225
        }
 
226
        catch (IllegalDataException success) {
 
227
            assertNotNull(success.getMessage());
 
228
        }
 
229
        
 
230
        // nothing changed
 
231
        assertEquals(id, id2);
 
232
        
 
233
    }
 
234
 
 
235
    
 
236
    public void testXPathRecognizesXmlIDAttributes() 
 
237
      throws ParsingException, IOException {
 
238
        
 
239
        Element root = new Element("root");
 
240
        Document doc = new Document(root);
 
241
        Element child = new Element("child");
 
242
        root.appendChild(child);
 
243
        Attribute id = new Attribute("id", "p2");
 
244
        child.addAttribute(id);
 
245
        id.setNamespace("xml", "http://www.w3.org/XML/1998/namespace");
 
246
        Nodes result = doc.query("id('p2')");
 
247
        assertEquals(1, result.size());
 
248
        assertEquals(child, result.get(0));
 
249
        
 
250
    }
 
251
    
 
252
    
 
253
    public void testXMLIDTestSuiteFromW3CServer() 
 
254
      throws ParsingException, IOException {
 
255
        
 
256
        URL base = new URL("http://www.w3.org/XML/2005/01/xml-id/test-suite.xml");
 
257
        Builder builder = new Builder();
 
258
        Document catalog = builder.build(base.openStream());
 
259
        Element testsuite = catalog.getRootElement();
 
260
        Elements testCatalogs = testsuite.getChildElements("test-catalog");
 
261
        for (int i = 0; i < testCatalogs.size(); i++) {
 
262
            Elements testcases = testCatalogs.get(i).getChildElements("test-case");
 
263
            for (int j = 0; j < testcases.size(); j++) {
 
264
                Element testcase = testcases.get(j);
 
265
                String features = testcase.getAttributeValue("feature");
 
266
                if (features != null && features.indexOf("xml11") >= 0) {
 
267
                    continue; // skip test
 
268
                }
 
269
                URL testURL = new URL(base, testcase.getFirstChildElement("file-path").getValue() + "/");
 
270
                Element scenario = testcase.getFirstChildElement("scenario");
 
271
                Element input = scenario.getFirstChildElement("input-file");
 
272
                URL inputFile = new URL(testURL, input.getValue());
 
273
                Elements expectedIDs = scenario.getChildElements("id");
 
274
                Document inputDoc = builder.build(inputFile.openStream());
 
275
                Nodes recognizedIDs = getIDs(inputDoc);
 
276
                assertEquals(expectedIDs.size(), recognizedIDs.size());
 
277
                for (int k = 0; k < expectedIDs.size(); k++) {
 
278
                    assertEquals(expectedIDs.get(i).getValue(), recognizedIDs.get(i).getValue());
 
279
                }
 
280
            } // end for
 
281
        }
 
282
        
 
283
    }
 
284
    
 
285
    
 
286
    public void testXMLIDTestSuite() 
 
287
      throws ParsingException, IOException {
 
288
        
 
289
        Builder builder = new Builder();
 
290
        File base = new File("data");
 
291
        base = new File(base, "xmlid");
 
292
        File catalog = new File(base, "catalog.xml");
 
293
        
 
294
        // The test suite needs to be installed separately. If we can't
 
295
        // find the catalog, we just don't run these tests.
 
296
        if (catalog.exists()) {
 
297
            Document doc = builder.build(catalog);
 
298
            Element testsuite = doc.getRootElement();
 
299
            Elements testCatalogs = testsuite.getChildElements("test-catalog");
 
300
            for (int i = 0; i < testCatalogs.size(); i++) {
 
301
                Elements testcases = testCatalogs.get(i).getChildElements("test-case");
 
302
                for (int j = 0; j < testcases.size(); j++) {
 
303
                    Element testcase = testcases.get(j);
 
304
                    String features = testcase.getAttributeValue("features");
 
305
                    if (features != null && features.indexOf("xml11") >= 0) {
 
306
                        continue; // skip test
 
307
                    }
 
308
                    File root = new File(base, testcase.getFirstChildElement("file-path").getValue());
 
309
                    File inputFile = null;
 
310
                    Element scenario = testcase.getFirstChildElement("scenario");
 
311
                    Element input = scenario.getFirstChildElement("input");
 
312
                    inputFile = new File(root, input.getValue());
 
313
                    Elements expectedIDs = scenario.getChildElements("id");
 
314
                    try {
 
315
                        Document inputDoc = builder.build(inputFile);
 
316
                        Nodes recognizedIDs = getIDs(inputDoc);
 
317
                        assertEquals(expectedIDs.size(), recognizedIDs.size());
 
318
                        for (int k = 0; k < expectedIDs.size(); k++) {
 
319
                            assertEquals(expectedIDs.get(i).getValue(), recognizedIDs.get(i).getValue());
 
320
                        }
 
321
                    }
 
322
                    catch (ParsingException ex) {
 
323
                        System.err.println(inputFile);
 
324
                        ex.printStackTrace();
 
325
                    }
 
326
                } // end for
 
327
            }
 
328
            
 
329
        } // end if 
 
330
        
 
331
    }
 
332
 
 
333
 
 
334
    private Nodes getIDs(Document doc) {
 
335
 
 
336
        Element root = doc.getRootElement();
 
337
        Nodes list = new Nodes();
 
338
        getIDs(root, list);
 
339
        return list;
 
340
    }
 
341
    
 
342
    
 
343
    private void getIDs(Element element, Nodes list) {
 
344
 
 
345
        for (int i = 0; i < element.getAttributeCount(); i++) {
 
346
            Attribute a = element.getAttribute(i);
 
347
            if (a.getType() == Attribute.Type.ID) {
 
348
                // need to sort these into specific order
 
349
                list.append(a);
 
350
            }
 
351
        }
 
352
        for (int i = 0; i < element.getChildCount(); i++) {
 
353
            Node child = element.getChild(i);
 
354
            if (child instanceof Element) {
 
355
                getIDs((Element) child, list);
 
356
            }
 
357
        }
 
358
        
 
359
    }
 
360
    
 
361
    
 
362
}