1 package org.sapia.archie; 2 3 import org.sapia.archie.impl.*; 4 5 import junit.framework.TestCase; 6 7 8 17 public class NameTest extends TestCase { 18 public NameTest(String name) { 19 super(name); 20 } 21 22 public void testAddPart() throws Exception { 23 Name n = new Name(); 24 n.add(new DefaultNamePart("part")); 25 super.assertEquals(1, n.count()); 26 } 27 28 public void testFirst() throws Exception { 29 Name n = new Name(); 30 n.add(new DefaultNamePart("part")); 31 super.assertEquals("part", n.first().asString()); 32 n.add(new DefaultNamePart("otherPart")); 33 super.assertEquals("part", n.first().asString()); 34 } 35 36 public void testLast() throws Exception { 37 Name n = new Name(); 38 n.add(new DefaultNamePart("part")); 39 super.assertEquals("part", n.last().asString()); 40 n.add(new DefaultNamePart("otherPart")); 41 super.assertEquals("otherPart", n.last().asString()); 42 } 43 44 public void testChopLast() throws Exception { 45 Name n = new Name(); 46 n.add(new DefaultNamePart("part")); 47 n.add(new DefaultNamePart("otherPart")); 48 super.assertEquals("otherPart", n.chopLast().asString()); 49 super.assertEquals(1, n.count()); 50 super.assertEquals("part", n.first().asString()); 51 super.assertEquals("part", n.last().asString()); 52 } 53 54 public void testIteration() throws Exception { 55 Name n = new Name(); 56 n.add(new DefaultNamePart("path1")).add(new DefaultNamePart("path2")).add(new DefaultNamePart("path3")); 57 58 String [] parts = new String [] { "path1", "path2", "path3" }; 59 60 NamePart current; 61 int count = 0; 62 63 while (n.hasNextPart()) { 64 current = n.nextPart(); 65 super.assertEquals(parts[count++], current.asString()); 66 } 67 68 super.assertEquals(3, count); 69 n.reset(); 70 super.assertEquals(0, n.getCurrentIndex()); 71 } 72 73 public void testFrom() throws Exception { 74 Name n = new Name(); 75 n.add(new DefaultNamePart("path1")).add(new DefaultNamePart("path2")).add(new DefaultNamePart("path3")); 76 77 Name from = n.getFrom(1); 78 super.assertEquals(2, from.count()); 79 super.assertEquals("path2", from.get(0).asString()); 80 super.assertEquals("path3", from.get(1).asString()); 81 } 82 83 public void testTo() throws Exception { 84 Name n = new Name(); 85 n.add(new DefaultNamePart("path1")).add(new DefaultNamePart("path2")).add(new DefaultNamePart("path3")); 86 87 Name to = n.getTo(2); 88 super.assertEquals(2, to.count()); 89 super.assertEquals("path1", to.get(0).asString()); 90 super.assertEquals("path2", to.get(1).asString()); 91 } 92 93 public void testAdd() throws Exception { 94 Name n = new Name(); 95 n.add(new DefaultNamePart("path1")).add(new DefaultNamePart("path2")); 96 97 Name n2 = new Name(); 98 n2.add(new DefaultNamePart("path3")).add(new DefaultNamePart("path4")); 99 100 n.add(n2); 101 super.assertEquals("path1", n.get(0).asString()); 102 super.assertEquals("path2", n.get(1).asString()); 103 super.assertEquals("path3", n.get(2).asString()); 104 super.assertEquals("path4", n.get(3).asString()); 105 } 106 107 public void testAddAtBeginning() throws Exception { 108 Name n = new Name(); 109 n.add(new DefaultNamePart("path1")); 110 n.addAt(0, new DefaultNamePart("path2")); 111 super.assertEquals("path1", n.get(1).asString()); 112 super.assertEquals("path2", n.get(0).asString()); 113 } 114 115 public void testAddAtEnd() throws Exception { 116 Name n = new Name(); 117 n.add(new DefaultNamePart("path1")); 118 n.addAt(1, new DefaultNamePart("path2")); 119 super.assertEquals("path1", n.get(0).asString()); 120 super.assertEquals("path2", n.get(1).asString()); 121 } 122 123 public void testRemoveAtBeginning() throws Exception { 124 Name n = new Name(); 125 n.add(new DefaultNamePart("path1")); 126 n.add(new DefaultNamePart("path2")); 127 super.assertEquals("path1", n.get(0).asString()); 128 super.assertEquals("path2", n.get(1).asString()); 129 n.removeAt(0); 130 super.assertEquals(1, n.count()); 131 super.assertEquals("path2", n.get(0).asString()); 132 } 133 134 public void testRemoveAtEnd() throws Exception { 135 Name n = new Name(); 136 n.add(new DefaultNamePart("path1")); 137 n.add(new DefaultNamePart("path2")); 138 super.assertEquals("path1", n.get(0).asString()); 139 super.assertEquals("path2", n.get(1).asString()); 140 n.removeAt(1); 141 super.assertEquals(1, n.count()); 142 super.assertEquals("path1", n.get(0).asString()); 143 } 144 145 public void testEndsWith() throws Exception { 146 Name n1 = new Name(); 147 n1.add(new DefaultNamePart("path1")); 148 n1.add(new DefaultNamePart("path2")); 149 Name n2 = new Name(); 150 n2.add(new DefaultNamePart("path2")); 151 super.assertTrue(n1.endsWith(n1)); 152 super.assertTrue(n1.endsWith(n2)); 153 } 154 155 public void testStartsWith() throws Exception { 156 Name n1 = new Name(); 157 n1.add(new DefaultNamePart("path1")); 158 n1.add(new DefaultNamePart("path2")); 159 Name n2 = new Name(); 160 n2.add(new DefaultNamePart("path2")); 161 162 Name n3 = new Name(); 163 n2.add(new DefaultNamePart("path3")); 164 super.assertTrue(n1.startsWith(n1)); 165 super.assertTrue(!n1.startsWith(n2)); 166 super.assertTrue(n1.startsWith(n3)); 167 super.assertTrue(!n3.startsWith(n1)); 168 } 169 } 170 | Popular Tags |