1 16 package org.apache.commons.collections.list; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.List ; 22 import java.util.ListIterator ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 27 import org.apache.commons.collections.collection.TestTransformedCollection; 28 29 38 public class TestTransformedList extends AbstractTestList { 39 40 public TestTransformedList(String testName) { 41 super(testName); 42 } 43 44 public static Test suite() { 45 return new TestSuite(TestTransformedList.class); 46 } 47 48 public static void main(String args[]) { 49 String [] testCaseName = { TestTransformedList.class.getName()}; 50 junit.textui.TestRunner.main(testCaseName); 51 } 52 53 public Collection makeConfirmedCollection() { 54 return new ArrayList (); 55 } 56 57 public Collection makeConfirmedFullCollection() { 58 List list = new ArrayList (); 59 list.addAll(Arrays.asList(getFullElements())); 60 return list; 61 } 62 63 public List makeEmptyList() { 64 return TransformedList.decorate(new ArrayList (), TestTransformedCollection.NOOP_TRANSFORMER); 65 } 66 67 public List makeFullList() { 68 List list = new ArrayList (); 69 list.addAll(Arrays.asList(getFullElements())); 70 return TransformedList.decorate(list, TestTransformedCollection.NOOP_TRANSFORMER); 71 } 72 73 public void testTransformedList() { 74 List list = TransformedList.decorate(new ArrayList (), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER); 75 assertEquals(0, list.size()); 76 Object [] els = new Object [] {"1", "3", "5", "7", "2", "4", "6"}; 77 for (int i = 0; i < els.length; i++) { 78 list.add(els[i]); 79 assertEquals(i + 1, list.size()); 80 assertEquals(true, list.contains(new Integer ((String ) els[i]))); 81 assertEquals(false, list.contains(els[i])); 82 } 83 84 assertEquals(false, list.remove(els[0])); 85 assertEquals(true, list.remove(new Integer ((String ) els[0]))); 86 87 list.clear(); 88 for (int i = 0; i < els.length; i++) { 89 list.add(0, els[i]); 90 assertEquals(i + 1, list.size()); 91 assertEquals(new Integer ((String ) els[i]), list.get(0)); 92 } 93 94 list.set(0, "22"); 95 assertEquals(new Integer (22), list.get(0)); 96 97 ListIterator it = list.listIterator(); 98 it.next(); 99 it.set("33"); 100 assertEquals(new Integer (33), list.get(0)); 101 it.add("44"); 102 assertEquals(new Integer (44), list.get(1)); 103 104 List adds = new ArrayList (); 105 adds.add("1"); 106 adds.add("2"); 107 list.clear(); 108 list.addAll(adds); 109 assertEquals(new Integer (1), list.get(0)); 110 assertEquals(new Integer (2), list.get(1)); 111 112 adds.clear(); 113 adds.add("3"); 114 list.addAll(1, adds); 115 assertEquals(new Integer (1), list.get(0)); 116 assertEquals(new Integer (3), list.get(1)); 117 assertEquals(new Integer (2), list.get(2)); 118 } 119 120 public String getCompatibilityVersion() { 121 return "3.1"; 122 } 123 124 131 } 132 | Popular Tags |