1 16 package org.apache.commons.collections.set; 17 18 import java.util.Arrays ; 19 import java.util.Comparator ; 20 import java.util.Set ; 21 import java.util.SortedSet ; 22 import java.util.TreeSet ; 23 24 import junit.framework.Test; 25 26 import org.apache.commons.collections.BulkTest; 27 import org.apache.commons.collections.Predicate; 28 import org.apache.commons.collections.PredicateUtils; 29 import org.apache.commons.collections.map.TestPredicatedSortedMap; 30 31 40 public class TestPredicatedSortedSet extends AbstractTestSortedSet{ 41 42 public TestPredicatedSortedSet(String testName) { 43 super(testName); 44 } 45 46 public static Test suite() { 47 return BulkTest.makeSuite(TestPredicatedSortedSet.class); 48 } 49 50 public static void main(String args[]) { 51 String [] testCaseName = { TestPredicatedSortedMap.class.getName()}; 52 junit.textui.TestRunner.main(testCaseName); 53 } 54 55 57 protected Predicate truePredicate = PredicateUtils.truePredicate(); 58 59 public Set makeEmptySet() { 60 return PredicatedSortedSet.decorate(new TreeSet (), truePredicate); 61 } 62 63 public Set makeFullSet() { 64 TreeSet set = new TreeSet (); 65 set.addAll(Arrays.asList(getFullElements())); 66 return PredicatedSortedSet.decorate(set, truePredicate); 67 } 68 69 70 protected Predicate testPredicate = 72 new Predicate() { 73 public boolean evaluate(Object o) { 74 return (o instanceof String ) && (((String ) o).startsWith("A")); 75 } 76 }; 77 78 79 protected SortedSet makeTestSet() { 80 return PredicatedSortedSet.decorate(new TreeSet (), testPredicate); 81 } 82 83 public void testGetSet() { 84 SortedSet set = makeTestSet(); 85 assertTrue("returned set should not be null", 86 ((PredicatedSortedSet) set).getSet() != null); 87 } 88 89 public void testIllegalAdd() { 90 SortedSet set = makeTestSet(); 91 String testString = "B"; 92 try { 93 set.add(testString); 94 fail("Should fail string predicate."); 95 } catch (IllegalArgumentException e) { 96 } 98 assertTrue("Collection shouldn't contain illegal element", 99 !set.contains(testString)); 100 } 101 102 public void testIllegalAddAll() { 103 SortedSet set = makeTestSet(); 104 Set elements = new TreeSet (); 105 elements.add("Aone"); 106 elements.add("Atwo"); 107 elements.add("Bthree"); 108 elements.add("Afour"); 109 try { 110 set.addAll(elements); 111 fail("Should fail string predicate."); 112 } catch (IllegalArgumentException e) { 113 } 115 assertTrue("Set shouldn't contain illegal element", 116 !set.contains("Aone")); 117 assertTrue("Set shouldn't contain illegal element", 118 !set.contains("Atwo")); 119 assertTrue("Set shouldn't contain illegal element", 120 !set.contains("Bthree")); 121 assertTrue("Set shouldn't contain illegal element", 122 !set.contains("Afour")); 123 } 124 125 public void testComparator() { 126 SortedSet set = makeTestSet(); 127 Comparator c = set.comparator(); 128 assertTrue("natural order, so comparator should be null", c == null); 129 } 130 131 public String getCompatibilityVersion() { 132 return "3.1"; 133 } 134 135 142 } 143 | Popular Tags |