1 package com.thoughtworks.xstream.io.path; 2 3 import junit.framework.TestCase; 4 5 public class PathTrackerTest extends TestCase { 6 7 private PathTracker pathTracker; 8 9 protected void setUp() throws Exception { 10 super.setUp(); 11 pathTracker = new PathTracker(1); 13 } 14 15 public void testExposesXpathLikeExpressionOfLocationInWriter() { 16 17 assertEquals("", pathTracker.getCurrentPath()); 18 19 pathTracker.pushElement("root"); 21 assertEquals("/root", pathTracker.getCurrentPath()); 22 23 pathTracker.pushElement("childA"); 25 assertEquals("/root/childA", pathTracker.getCurrentPath()); 26 pathTracker.popElement(); 28 assertEquals("/root", pathTracker.getCurrentPath()); 29 30 pathTracker.pushElement("childB"); 32 assertEquals("/root/childB", pathTracker.getCurrentPath()); 33 34 pathTracker.pushElement("grandchild"); 36 assertEquals("/root/childB/grandchild", pathTracker.getCurrentPath()); 37 pathTracker.popElement(); 39 assertEquals("/root/childB", pathTracker.getCurrentPath()); 40 41 pathTracker.popElement(); 43 assertEquals("/root", pathTracker.getCurrentPath()); 44 45 pathTracker.popElement(); 47 assertEquals("", pathTracker.getCurrentPath()); 48 49 } 50 51 public void testAddsIndexIfSiblingOfSameTypeAlreadyExists() { 52 53 pathTracker.pushElement("root"); 55 56 pathTracker.pushElement("child"); 58 assertEquals("/root/child", pathTracker.getCurrentPath()); 59 pathTracker.popElement(); 61 62 pathTracker.pushElement("child"); 64 assertEquals("/root/child[2]", pathTracker.getCurrentPath()); 65 pathTracker.popElement(); 67 68 pathTracker.pushElement("another"); 70 assertEquals("/root/another", pathTracker.getCurrentPath()); 71 pathTracker.popElement(); 73 74 pathTracker.pushElement("child"); 76 assertEquals("/root/child[3]", pathTracker.getCurrentPath()); 77 pathTracker.popElement(); 79 80 } 82 83 public void testAssociatesIndexOnlyWithDirectParent() { 84 85 pathTracker.pushElement("root"); 87 88 pathTracker.pushElement("child"); 90 91 pathTracker.pushElement("child"); 93 assertEquals("/root/child/child", pathTracker.getCurrentPath()); 94 pathTracker.popElement(); 96 97 pathTracker.pushElement("child"); 99 assertEquals("/root/child/child[2]", pathTracker.getCurrentPath()); 100 pathTracker.popElement(); 102 103 pathTracker.popElement(); 105 106 pathTracker.pushElement("child"); 108 109 pathTracker.pushElement("child"); 111 assertEquals("/root/child[2]/child", pathTracker.getCurrentPath()); 112 pathTracker.popElement(); 114 115 pathTracker.pushElement("child"); 117 assertEquals("/root/child[2]/child[2]", pathTracker.getCurrentPath()); 118 119 } 121 122 } 123 | Popular Tags |