1 16 package org.apache.commons.collections.bag; 17 18 import java.util.Comparator ; 19 20 import junit.framework.Test; 21 import junit.framework.TestSuite; 22 23 import org.apache.commons.collections.Bag; 24 import org.apache.commons.collections.Predicate; 25 import org.apache.commons.collections.PredicateUtils; 26 import org.apache.commons.collections.SortedBag; 27 28 37 public class TestPredicatedSortedBag extends AbstractTestSortedBag { 38 39 private SortedBag nullBag = null; 40 41 public TestPredicatedSortedBag(String testName) { 42 super(testName); 43 } 44 45 public static Test suite() { 46 return new TestSuite(TestPredicatedSortedBag.class); 47 } 48 49 public static void main(String args[]) { 50 String [] testCaseName = { TestPredicatedSortedBag.class.getName()}; 51 junit.textui.TestRunner.main(testCaseName); 52 } 53 54 56 protected Predicate stringPredicate() { 57 return new Predicate() { 58 public boolean evaluate(Object o) { 59 return o instanceof String ; 60 } 61 }; 62 } 63 64 protected Predicate truePredicate = PredicateUtils.truePredicate(); 65 66 protected SortedBag decorateBag(SortedBag bag, Predicate predicate) { 67 return PredicatedSortedBag.decorate(bag, predicate); 68 } 69 70 public Bag makeBag() { 71 return decorateBag(new TreeBag(), truePredicate); 72 } 73 74 protected Bag makeTestBag() { 75 return decorateBag(new TreeBag(), stringPredicate()); 76 } 77 78 80 public void testDecorate() { 81 SortedBag bag = decorateBag(new TreeBag(), stringPredicate()); 82 SortedBag bag2 = ((PredicatedSortedBag) bag).getSortedBag(); 83 try { 84 SortedBag bag3 = decorateBag(new TreeBag(), null); 85 fail("Expecting IllegalArgumentException for null predicate"); 86 } catch (IllegalArgumentException e) {} 87 try { 88 SortedBag bag4 = decorateBag(nullBag, stringPredicate()); 89 fail("Expecting IllegalArgumentException for null bag"); 90 } catch (IllegalArgumentException e) {} 91 } 92 93 public void testSortOrder() { 94 SortedBag bag = decorateBag(new TreeBag(), stringPredicate()); 95 String one = "one"; 96 String two = "two"; 97 String three = "three"; 98 bag.add(one); 99 bag.add(two); 100 bag.add(three); 101 assertEquals("first element", bag.first(), one); 102 assertEquals("last element", bag.last(), two); 103 Comparator c = bag.comparator(); 104 assertTrue("natural order, so comparator should be null", c == null); 105 } 106 107 public String getCompatibilityVersion() { 108 return "3.1"; 109 } 110 111 123 } 124 | Popular Tags |