1 16 package org.apache.commons.collections; 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 25 import org.apache.commons.collections.list.PredicatedList; 26 27 36 public class TestListUtils extends BulkTest { 37 38 public TestListUtils(String name) { 39 super(name); 40 } 41 42 public static Test suite() { 43 return BulkTest.makeSuite(TestListUtils.class); 44 } 45 46 public void testNothing() { 47 } 48 49 public void testpredicatedList() { 50 Predicate predicate = new Predicate() { 51 public boolean evaluate(Object o) { 52 return o instanceof String ; 53 } 54 }; 55 List list = 56 ListUtils.predicatedList(new ArrayStack(), predicate); 57 assertTrue("returned object should be a PredicatedList", 58 list instanceof PredicatedList); 59 try { 60 list = 61 ListUtils.predicatedList(new ArrayStack(), null); 62 fail("Expecting IllegalArgumentException for null predicate."); 63 } catch (IllegalArgumentException ex) { 64 } 66 try { 67 list = 68 ListUtils.predicatedList(null, predicate); 69 fail("Expecting IllegalArgumentException for null list."); 70 } catch (IllegalArgumentException ex) { 71 } 73 } 74 75 public void testLazyList() { 76 List list = ListUtils.lazyList(new ArrayList (), new Factory() { 77 78 private int index; 79 80 public Object create() { 81 index++; 82 return new Integer (index); 83 } 84 }); 85 86 assertNotNull((Integer )list.get(5)); 87 assertEquals(6, list.size()); 88 89 assertNotNull((Integer )list.get(5)); 90 assertEquals(6, list.size()); 91 } 92 93 public void testEquals() { 94 Collection data = Arrays.asList( new String [] { "a", "b", "c" }); 95 96 List a = new ArrayList ( data ); 97 List b = new ArrayList ( data ); 98 99 assertEquals(true, a.equals(b)); 100 assertEquals(true, ListUtils.isEqualList(a, b)); 101 a.clear(); 102 assertEquals(false, ListUtils.isEqualList(a, b)); 103 assertEquals(false, ListUtils.isEqualList(a, null)); 104 assertEquals(false, ListUtils.isEqualList(null, b)); 105 assertEquals(true, ListUtils.isEqualList(null, null)); 106 } 107 108 public void testHashCode() { 109 Collection data = Arrays.asList( new String [] { "a", "b", "c" }); 110 111 List a = new ArrayList ( data ); 112 List b = new ArrayList ( data ); 113 114 assertEquals(true, a.hashCode() == b.hashCode()); 115 assertEquals(true, a.hashCode() == ListUtils.hashCodeForList(a)); 116 assertEquals(true, b.hashCode() == ListUtils.hashCodeForList(b)); 117 assertEquals(true, ListUtils.hashCodeForList(a) == ListUtils.hashCodeForList(b)); 118 a.clear(); 119 assertEquals(false, ListUtils.hashCodeForList(a) == ListUtils.hashCodeForList(b)); 120 assertEquals(0, ListUtils.hashCodeForList(null)); 121 } 122 123 } 124 | Popular Tags |