1 16 package org.apache.commons.collections; 17 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.LinkedList ; 21 import java.util.List ; 22 import java.util.NoSuchElementException ; 23 24 import org.apache.commons.collections.list.AbstractTestList; 25 26 40 public abstract class TestLinkedList extends AbstractTestList { 41 42 public TestLinkedList(String testName) { 43 super(testName); 44 } 45 46 public List makeEmptyList() { 47 return makeEmptyLinkedList(); 48 } 49 50 public List makeFullList() { 51 return makeFullLinkedList(); 52 } 53 54 59 protected abstract LinkedList makeEmptyLinkedList(); 60 61 66 protected LinkedList makeFullLinkedList() { 67 LinkedList list = makeEmptyLinkedList(); 69 list.addAll(Arrays.asList(getFullElements())); 70 return list; 71 } 72 73 78 protected LinkedList getLinkedList() { 79 return (LinkedList )collection; 80 } 81 82 87 protected LinkedList getConfirmedLinkedList() { 88 return (LinkedList )confirmed; 89 } 90 91 94 public void testLinkedListAddFirst() { 95 if (!isAddSupported()) return; 96 Object o = "hello"; 97 98 resetEmpty(); 99 getLinkedList().addFirst(o); 100 getConfirmedLinkedList().addFirst(o); 101 verify(); 102 103 resetFull(); 104 getLinkedList().addFirst(o); 105 getConfirmedLinkedList().addFirst(o); 106 verify(); 107 } 108 109 112 public void testLinkedListAddLast() { 113 if (!isAddSupported()) return; 114 Object o = "hello"; 115 116 resetEmpty(); 117 getLinkedList().addLast(o); 118 getConfirmedLinkedList().addLast(o); 119 verify(); 120 121 resetFull(); 122 getLinkedList().addLast(o); 123 getConfirmedLinkedList().addLast(o); 124 verify(); 125 } 126 127 130 public void testLinkedListGetFirst() { 131 resetEmpty(); 132 try { 133 getLinkedList().getFirst(); 134 fail("getFirst() should throw a NoSuchElementException for an " + 135 "empty list."); 136 } catch (NoSuchElementException e) { 137 } 139 verify(); 140 141 resetFull(); 142 Object first = getLinkedList().getFirst(); 143 Object confirmedFirst = getConfirmedLinkedList().getFirst(); 144 assertEquals("Result returned by getFirst() was wrong.", 145 confirmedFirst, first); 146 verify(); 147 } 148 149 152 public void testLinkedListGetLast() { 153 resetEmpty(); 154 try { 155 getLinkedList().getLast(); 156 fail("getLast() should throw a NoSuchElementException for an " + 157 "empty list."); 158 } catch (NoSuchElementException e) { 159 } 161 verify(); 162 163 resetFull(); 164 Object last = getLinkedList().getLast(); 165 Object confirmedLast = getConfirmedLinkedList().getLast(); 166 assertEquals("Result returned by getLast() was wrong.", 167 confirmedLast, last); 168 verify(); 169 } 170 171 174 public void testLinkedListRemoveFirst() { 175 if (!isRemoveSupported()) return; 176 177 resetEmpty(); 178 try { 179 getLinkedList().removeFirst(); 180 fail("removeFirst() should throw a NoSuchElementException for " + 181 "an empty list."); 182 } catch (NoSuchElementException e) { 183 } 185 verify(); 186 187 resetFull(); 188 Object first = getLinkedList().removeFirst(); 189 Object confirmedFirst = getConfirmedLinkedList().removeFirst(); 190 assertEquals("Result returned by removeFirst() was wrong.", 191 confirmedFirst, first); 192 verify(); 193 } 194 195 198 public void testLinkedListRemoveLast() { 199 if (!isRemoveSupported()) return; 200 201 resetEmpty(); 202 try { 203 getLinkedList().removeLast(); 204 fail("removeLast() should throw a NoSuchElementException for " + 205 "an empty list."); 206 } catch (NoSuchElementException e) { 207 } 209 verify(); 210 211 resetFull(); 212 Object last = getLinkedList().removeLast(); 213 Object confirmedLast = getConfirmedLinkedList().removeLast(); 214 assertEquals("Result returned by removeLast() was wrong.", 215 confirmedLast, last); 216 verify(); 217 } 218 219 222 public Collection makeConfirmedCollection() { 223 return new LinkedList (); 224 } 225 226 229 public Collection makeConfirmedFullCollection() { 230 List list = new LinkedList (); 231 list.addAll(Arrays.asList(getFullElements())); 232 return list; 233 } 234 } 235 | Popular Tags |