1 17 package org.apache.commons.collections.primitives.decorators; 18 19 import junit.framework.TestCase; 20 21 import org.apache.commons.collections.primitives.ArrayLongList; 22 import org.apache.commons.collections.primitives.LongIterator; 23 import org.apache.commons.collections.primitives.LongList; 24 import org.apache.commons.collections.primitives.LongListIterator; 25 26 30 public abstract class BaseUnmodifiableLongListTest extends TestCase { 31 32 35 public BaseUnmodifiableLongListTest(String testName) { 36 super(testName); 37 } 38 39 42 protected abstract LongList makeUnmodifiableLongList(); 43 44 protected LongList makeLongList() { 45 LongList list = new ArrayLongList(); 46 for(int i=0;i<10;i++) { 47 list.add(i); 48 } 49 return list; 50 } 51 52 55 public final void testNotModifiable() throws Exception { 56 assertListNotModifiable(makeUnmodifiableLongList()); 57 } 58 59 public final void testSublistNotModifiable() throws Exception { 60 LongList list = makeUnmodifiableLongList(); 61 assertListNotModifiable(list.subList(0,list.size()-2)); 62 } 63 64 public final void testIteratorNotModifiable() throws Exception { 65 LongList list = makeUnmodifiableLongList(); 66 assertIteratorNotModifiable(list.iterator()); 67 assertIteratorNotModifiable(list.subList(0,list.size()-2).iterator()); 68 } 69 70 public final void testListIteratorNotModifiable() throws Exception { 71 LongList list = makeUnmodifiableLongList(); 72 assertListIteratorNotModifiable(list.listIterator()); 73 assertListIteratorNotModifiable(list.subList(0,list.size()-2).listIterator()); 74 assertListIteratorNotModifiable(list.listIterator(1)); 75 assertListIteratorNotModifiable(list.subList(0,list.size()-2).listIterator(1)); 76 } 77 78 81 private void assertListIteratorNotModifiable(LongListIterator iter) throws Exception { 82 assertIteratorNotModifiable(iter); 83 84 assertTrue(iter.hasPrevious()); 85 86 try { 87 iter.set(2); 88 fail("Expected UnsupportedOperationException"); 89 } catch(UnsupportedOperationException e) { 90 } 92 93 try { 94 iter.add(2); 95 fail("Expected UnsupportedOperationException"); 96 } catch(UnsupportedOperationException e) { 97 } 99 } 100 101 private void assertIteratorNotModifiable(LongIterator iter) throws Exception { 102 assertTrue(iter.hasNext()); 103 iter.next(); 104 105 try { 106 iter.remove(); 107 fail("Expected UnsupportedOperationException"); 108 } catch(UnsupportedOperationException e) { 109 } 111 } 112 113 private void assertListNotModifiable(LongList list) throws Exception { 114 try { 115 list.add(1); 116 fail("Expected UnsupportedOperationException"); 117 } catch(UnsupportedOperationException e) { 118 } 120 121 try { 122 list.add(1,2); 123 fail("Expected UnsupportedOperationException"); 124 } catch(UnsupportedOperationException e) { 125 } 127 128 try { 129 list.addAll(makeLongList()); 130 fail("Expected UnsupportedOperationException"); 131 } catch(UnsupportedOperationException e) { 132 } 134 135 try { 136 list.addAll(1,makeLongList()); 137 fail("Expected UnsupportedOperationException"); 138 } catch(UnsupportedOperationException e) { 139 } 141 142 try { 143 list.removeElementAt(1); 144 fail("Expected UnsupportedOperationException"); 145 } catch(UnsupportedOperationException e) { 146 } 148 149 try { 150 list.removeElement(1); 151 fail("Expected UnsupportedOperationException"); 152 } catch(UnsupportedOperationException e) { 153 } 155 156 try { 157 list.removeAll(makeLongList()); 158 fail("Expected UnsupportedOperationException"); 159 } catch(UnsupportedOperationException e) { 160 } 162 163 try { 164 list.retainAll(makeLongList()); 165 fail("Expected UnsupportedOperationException"); 166 } catch(UnsupportedOperationException e) { 167 } 169 170 try { 171 list.clear(); 172 fail("Expected UnsupportedOperationException"); 173 } catch(UnsupportedOperationException e) { 174 } 176 177 try { 178 list.set(1,2); 179 fail("Expected UnsupportedOperationException"); 180 } catch(UnsupportedOperationException e) { 181 } 183 } 184 } | Popular Tags |