1 16 package org.apache.commons.collections.list; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 35 public class TestUnmodifiableList extends AbstractTestList { 36 37 public TestUnmodifiableList(String testName) { 38 super(testName); 39 } 40 41 public static Test suite() { 42 return new TestSuite(TestUnmodifiableList.class); 43 } 44 45 public static void main(String args[]) { 46 String [] testCaseName = { TestUnmodifiableList.class.getName()}; 47 junit.textui.TestRunner.main(testCaseName); 48 } 49 50 public List makeEmptyList() { 52 return UnmodifiableList.decorate(new ArrayList ()); 53 } 54 55 public List makeFullList() { 56 ArrayList list = new ArrayList (); 57 list.addAll(Arrays.asList(getFullElements())); 58 return UnmodifiableList.decorate(list); 59 } 60 61 public boolean isSetSupported() { 62 return false; 63 } 64 65 public boolean isAddSupported() { 66 return false; 67 } 68 69 public boolean isRemoveSupported() { 70 return false; 71 } 72 73 protected UnmodifiableList list = null; 75 protected ArrayList array = null; 76 77 protected void setupList() { 78 list = (UnmodifiableList) makeFullList(); 79 array = new ArrayList (); 80 array.add(new Integer (1)); 81 } 82 83 86 public void testUnmodifiable() { 87 setupList(); 88 verifyUnmodifiable(list); 89 verifyUnmodifiable(list.subList(0, 2)); 90 } 91 92 protected void verifyUnmodifiable(List list) { 93 try { 94 list.add(0, new Integer (0)); 95 fail("Expecting UnsupportedOperationException."); 96 } catch (UnsupportedOperationException e) { 97 } 99 try { 100 list.add(new Integer (0)); 101 fail("Expecting UnsupportedOperationException."); 102 } catch (UnsupportedOperationException e) { 103 } 105 try { 106 list.addAll(0, array); 107 fail("Expecting UnsupportedOperationException."); 108 } catch (UnsupportedOperationException e) { 109 } 111 try { 112 list.addAll(array); 113 fail("Expecting UnsupportedOperationException."); 114 } catch (UnsupportedOperationException e) { 115 } 117 try { 118 list.clear(); 119 fail("Expecting UnsupportedOperationException."); 120 } catch (UnsupportedOperationException e) { 121 } 123 try { 124 list.remove(0); 125 fail("Expecting UnsupportedOperationException."); 126 } catch (UnsupportedOperationException e) { 127 } 129 try { 130 list.remove(new Integer (0)); 131 fail("Expecting UnsupportedOperationException."); 132 } catch (UnsupportedOperationException e) { 133 } 135 try { 136 list.removeAll(array); 137 fail("Expecting UnsupportedOperationException."); 138 } catch (UnsupportedOperationException e) { 139 } 141 try { 142 list.retainAll(array); 143 fail("Expecting UnsupportedOperationException."); 144 } catch (UnsupportedOperationException e) { 145 } 147 try { 148 list.set(0, new Integer (0)); 149 fail("Expecting UnsupportedOperationException."); 150 } catch (UnsupportedOperationException e) { 151 } 153 } 154 155 158 public void testUnmodifiableIterator() { 159 setupList(); 160 Iterator iterator = list.iterator(); 161 try { 162 Object obj = iterator.next(); 163 iterator.remove(); 164 fail("Expecting UnsupportedOperationException."); 165 } catch (UnsupportedOperationException e) { 166 } 168 } 169 170 public String getCompatibilityVersion() { 171 return "3.1"; 172 } 173 174 181 } 182 | Popular Tags |