001: /*
002: * Copyright (C) 2004, 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 07. March 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.io.path;
013:
014: import junit.framework.TestCase;
015:
016: public class PathTrackerTest extends TestCase {
017:
018: private PathTracker pathTracker;
019:
020: protected void setUp() throws Exception {
021: super .setUp();
022: // small initial capacity to ensure resizing works
023: pathTracker = new PathTracker(1);
024: }
025:
026: public void testExposesXpathLikeExpressionOfLocationInWriter() {
027:
028: assertEquals(new Path(""), pathTracker.getPath());
029:
030: // <root>
031: pathTracker.pushElement("root");
032: assertEquals(new Path("/root"), pathTracker.getPath());
033:
034: // <childA>
035: pathTracker.pushElement("childA");
036: assertEquals(new Path("/root/childA"), pathTracker.getPath());
037: // </childA>
038: pathTracker.popElement();
039: assertEquals(new Path("/root"), pathTracker.getPath());
040:
041: // <childB>
042: pathTracker.pushElement("childB");
043: assertEquals(new Path("/root/childB"), pathTracker.getPath());
044:
045: // <grandchild>
046: pathTracker.pushElement("grandchild");
047: assertEquals(new Path("/root/childB/grandchild"), pathTracker
048: .getPath());
049: // </grandchild>
050: pathTracker.popElement();
051: assertEquals(new Path("/root/childB"), pathTracker.getPath());
052:
053: // </childB>
054: pathTracker.popElement();
055: assertEquals(new Path("/root"), pathTracker.getPath());
056:
057: // </root>
058: pathTracker.popElement();
059: assertEquals(new Path(""), pathTracker.getPath());
060:
061: }
062:
063: public void testAddsIndexIfSiblingOfSameTypeAlreadyExists() {
064:
065: // <root>
066: pathTracker.pushElement("root");
067:
068: // <child>
069: pathTracker.pushElement("child");
070: assertEquals(new Path("/root/child"), pathTracker.getPath());
071: // </child>
072: pathTracker.popElement();
073:
074: // <child>
075: pathTracker.pushElement("child");
076: assertEquals(new Path("/root/child[2]"), pathTracker.getPath());
077: // </child>
078: pathTracker.popElement();
079:
080: // <another>
081: pathTracker.pushElement("another");
082: assertEquals(new Path("/root/another"), pathTracker.getPath());
083: // </another>
084: pathTracker.popElement();
085:
086: // <child>
087: pathTracker.pushElement("child");
088: assertEquals(new Path("/root/child[3]"), pathTracker.getPath());
089: // </child>
090: pathTracker.popElement();
091:
092: // ...
093: }
094:
095: public void testAssociatesIndexOnlyWithDirectParent() {
096:
097: // <root>
098: pathTracker.pushElement("root");
099:
100: // <child>
101: pathTracker.pushElement("child");
102:
103: // <child>
104: pathTracker.pushElement("child");
105: assertEquals(new Path("/root/child/child"), pathTracker
106: .getPath());
107: // </child>
108: pathTracker.popElement();
109:
110: // <child>
111: pathTracker.pushElement("child");
112: assertEquals(new Path("/root/child/child[2]"), pathTracker
113: .getPath());
114: // </child>
115: pathTracker.popElement();
116:
117: // </child>
118: pathTracker.popElement();
119:
120: // <child>
121: pathTracker.pushElement("child");
122:
123: // <child>
124: pathTracker.pushElement("child");
125: assertEquals(new Path("/root/child[2]/child"), pathTracker
126: .getPath());
127: // </child>
128: pathTracker.popElement();
129:
130: // <child>
131: pathTracker.pushElement("child");
132: assertEquals(new Path("/root/child[2]/child[2]"), pathTracker
133: .getPath());
134:
135: // ...
136: }
137:
138: }
|