1 16 package org.apache.commons.collections.list; 17 18 import java.util.Arrays ; 19 20 29 public abstract class TestAbstractLinkedList extends AbstractTestList { 30 31 public TestAbstractLinkedList(String testName) { 32 super(testName); 33 } 34 35 public void testRemoveFirst() { 37 resetEmpty(); 38 AbstractLinkedList list = (AbstractLinkedList) collection; 39 if (isRemoveSupported() == false) { 40 try { 41 list.removeFirst(); 42 } catch (UnsupportedOperationException ex) {} 43 } 44 45 list.addAll( Arrays.asList( new String []{"value1", "value2"})); 46 assertEquals( "value1", list.removeFirst() ); 47 checkNodes(); 48 list.addLast( "value3"); 49 checkNodes(); 50 assertEquals( "value2", list.removeFirst() ); 51 assertEquals( "value3", list.removeFirst() ); 52 checkNodes(); 53 list.addLast( "value4" ); 54 checkNodes(); 55 assertEquals( "value4", list.removeFirst() ); 56 checkNodes(); 57 } 58 59 public void testRemoveLast() { 60 resetEmpty(); 61 AbstractLinkedList list = (AbstractLinkedList) collection; 62 if (isRemoveSupported() == false) { 63 try { 64 list.removeLast(); 65 } catch (UnsupportedOperationException ex) {} 66 } 67 68 list.addAll( Arrays.asList( new String []{"value1", "value2"})); 69 assertEquals( "value2", list.removeLast() ); 70 list.addFirst( "value3"); 71 checkNodes(); 72 assertEquals( "value1", list.removeLast() ); 73 assertEquals( "value3", list.removeLast() ); 74 list.addFirst( "value4" ); 75 checkNodes(); 76 assertEquals( "value4", list.removeFirst() ); 77 } 78 79 public void testAddNodeAfter() { 80 resetEmpty(); 81 AbstractLinkedList list = (AbstractLinkedList) collection; 82 if (isAddSupported() == false) { 83 try { 84 list.addFirst(null); 85 } catch (UnsupportedOperationException ex) {} 86 } 87 88 list.addFirst("value1"); 89 list.addNodeAfter(list.getNode(0,false),"value2"); 90 assertEquals("value1", list.getFirst()); 91 assertEquals("value2", list.getLast()); 92 list.removeFirst(); 93 checkNodes(); 94 list.addNodeAfter(list.getNode(0,false),"value3"); 95 checkNodes(); 96 assertEquals("value2", list.getFirst()); 97 assertEquals("value3", list.getLast()); 98 list.addNodeAfter(list.getNode(0, false),"value4"); 99 checkNodes(); 100 assertEquals("value2", list.getFirst()); 101 assertEquals("value3", list.getLast()); 102 assertEquals("value4", list.get(1)); 103 list.addNodeAfter(list.getNode(2, false), "value5"); 104 checkNodes(); 105 assertEquals("value2", list.getFirst()); 106 assertEquals("value4", list.get(1)); 107 assertEquals("value3", list.get(2)); 108 assertEquals("value5", list.getLast()); 109 } 110 111 public void testRemoveNode() { 112 resetEmpty(); 113 if (isAddSupported() == false || isRemoveSupported() == false) return; 114 AbstractLinkedList list = (AbstractLinkedList) collection; 115 116 list.addAll( Arrays.asList( new String []{"value1", "value2"})); 117 list.removeNode(list.getNode(0, false)); 118 checkNodes(); 119 assertEquals("value2", list.getFirst()); 120 assertEquals("value2", list.getLast()); 121 list.addFirst("value1"); 122 list.addFirst("value0"); 123 checkNodes(); 124 list.removeNode(list.getNode(1, false)); 125 assertEquals("value0", list.getFirst()); 126 assertEquals("value2", list.getLast()); 127 checkNodes(); 128 list.removeNode(list.getNode(1, false)); 129 assertEquals("value0", list.getFirst()); 130 assertEquals("value0", list.getLast()); 131 checkNodes(); 132 } 133 134 public void testGetNode() { 135 resetEmpty(); 136 AbstractLinkedList list = (AbstractLinkedList) collection; 137 assertEquals(list.getNode(0, true).previous, list.getNode(0, true).next); 139 try { 140 Object obj = list.getNode(0, false); 141 fail("Expecting IndexOutOfBoundsException."); 142 } catch (IndexOutOfBoundsException ex) { 143 } 145 list.addAll( Arrays.asList( new String []{"value1", "value2"})); 146 checkNodes(); 147 list.addFirst("value0"); 148 checkNodes(); 149 list.removeNode(list.getNode(1, false)); 150 checkNodes(); 151 try { 152 Object obj = list.getNode(2, false); 153 fail("Expecting IndexOutOfBoundsException."); 154 } catch (IndexOutOfBoundsException ex) { 155 } 157 try { 158 Object obj = list.getNode(-1, false); 159 fail("Expecting IndexOutOfBoundsException."); 160 } catch (IndexOutOfBoundsException ex) { 161 } 163 try { 164 Object obj = list.getNode(3, true); 165 fail("Expecting IndexOutOfBoundsException."); 166 } catch (IndexOutOfBoundsException ex) { 167 } 169 } 170 171 protected void checkNodes() { 172 AbstractLinkedList list = (AbstractLinkedList) collection; 173 for (int i = 0; i < list.size; i++) { 174 assertEquals(list.getNode(i, false).next, list.getNode(i + 1, true)); 175 if (i < list.size - 1) { 176 assertEquals(list.getNode(i + 1, false).previous, 177 list.getNode(i, false)); 178 } 179 } 180 } 181 182 } 183 | Popular Tags |