01: /*
02: * Copyright (C) 2004, 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 07. March 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.xml;
13:
14: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
15: import com.thoughtworks.xstream.io.xml.xppdom.Xpp3Dom;
16: import com.thoughtworks.xstream.io.xml.xppdom.Xpp3DomBuilder;
17:
18: import java.io.StringReader;
19: import java.util.HashMap;
20: import java.util.Map;
21:
22: public class XppDomReaderTest extends AbstractXMLReaderTest {
23: protected HierarchicalStreamReader createReader(String xml)
24: throws Exception {
25: return new XppDomDriver().createReader(new StringReader(xml));
26: }
27:
28: public void testCanReadFromElementOfLargerDocument()
29: throws Exception {
30: String xml = "<big>" + " <small>" + " <tiny/>"
31: + " </small>" + " <small-two>" + " </small-two>"
32: + "</big>";
33:
34: Xpp3Dom document = Xpp3DomBuilder.build(new StringReader(xml));
35:
36: Xpp3Dom small = document.getChild("small");
37:
38: HierarchicalStreamReader xmlReader = new XppDomReader(small);
39:
40: assertEquals("small", xmlReader.getNodeName());
41:
42: xmlReader.moveDown();
43:
44: assertEquals("tiny", xmlReader.getNodeName());
45: }
46:
47: public void testExposesAttributesKeysAndValuesByIndex()
48: throws Exception {
49:
50: // overrides test in superclass, because XppDom does not retain order of actualAttributes.
51:
52: HierarchicalStreamReader xmlReader = createReader("<node hello='world' a='b' c='d'><empty/></node>");
53:
54: assertEquals(3, xmlReader.getAttributeCount());
55:
56: Map expectedAttributes = new HashMap();
57: expectedAttributes.put("hello", "world");
58: expectedAttributes.put("a", "b");
59: expectedAttributes.put("c", "d");
60:
61: Map actualAttributes = new HashMap();
62: for (int i = 0; i < xmlReader.getAttributeCount(); i++) {
63: String name = xmlReader.getAttributeName(i);
64: String value = xmlReader.getAttribute(i);
65: actualAttributes.put(name, value);
66: }
67:
68: assertEquals(expectedAttributes, actualAttributes);
69:
70: xmlReader.moveDown();
71: assertEquals("empty", xmlReader.getNodeName());
72: assertEquals(0, xmlReader.getAttributeCount());
73: }
74:
75: }
|