1 16 package org.apache.commons.collections.collection; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.List ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 import org.apache.commons.collections.Transformer; 27 import org.apache.commons.collections.TransformerUtils; 28 29 38 public class TestTransformedCollection extends AbstractTestCollection { 39 40 private static class StringToInteger implements Transformer { 41 public Object transform(Object input) { 42 return new Integer ((String ) input); 43 } 44 } 45 46 public static final Transformer NOOP_TRANSFORMER = TransformerUtils.nopTransformer(); 47 public static final Transformer STRING_TO_INTEGER_TRANSFORMER = new StringToInteger(); 48 49 public TestTransformedCollection(String testName) { 50 super(testName); 51 } 52 53 public static Test suite() { 54 return new TestSuite(TestTransformedCollection.class); 55 } 56 57 public static void main(String args[]) { 58 String [] testCaseName = { TestTransformedCollection.class.getName()}; 59 junit.textui.TestRunner.main(testCaseName); 60 } 61 62 public Collection makeConfirmedCollection() { 64 return new ArrayList (); 65 } 66 67 public Collection makeConfirmedFullCollection() { 68 List list = new ArrayList (); 69 list.addAll(Arrays.asList(getFullElements())); 70 return list; 71 } 72 73 public Collection makeCollection() { 74 return TransformedCollection.decorate(new ArrayList (), NOOP_TRANSFORMER); 75 } 76 77 public Collection makeFullCollection() { 78 List list = new ArrayList (); 79 list.addAll(Arrays.asList(getFullElements())); 80 return TransformedCollection.decorate(list, NOOP_TRANSFORMER); 81 } 82 83 public Object [] getFullElements() { 85 return new Object [] {"1", "3", "5", "7", "2", "4", "6"}; 86 } 87 88 public Object [] getOtherElements() { 89 return new Object [] {"9", "88", "678", "87", "98", "78", "99"}; 90 } 91 92 public void testTransformedCollection() { 94 Collection coll = TransformedCollection.decorate(new ArrayList (), STRING_TO_INTEGER_TRANSFORMER); 95 assertEquals(0, coll.size()); 96 Object [] els = getFullElements(); 97 for (int i = 0; i < els.length; i++) { 98 coll.add(els[i]); 99 assertEquals(i + 1, coll.size()); 100 assertEquals(true, coll.contains(new Integer ((String ) els[i]))); 101 assertEquals(false, coll.contains(els[i])); 102 } 103 104 assertEquals(true, coll.remove(new Integer ((String ) els[0]))); 105 } 106 107 public String getCompatibilityVersion() { 108 return "3.1"; 109 } 110 111 118 } 119 | Popular Tags |