1 16 package org.apache.commons.collections.set; 17 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Set ; 23 24 import org.apache.commons.collections.collection.AbstractTestCollection; 25 26 45 public abstract class AbstractTestSet extends AbstractTestCollection { 46 47 52 public AbstractTestSet(String name) { 53 super(name); 54 } 55 56 60 public void verify() { 61 super.verify(); 62 63 assertEquals("Sets should be equal", confirmed, collection); 64 assertEquals("Sets should have equal hashCodes", 65 confirmed.hashCode(), collection.hashCode()); 66 Collection set = makeConfirmedCollection(); 67 Iterator iterator = collection.iterator(); 68 while (iterator.hasNext()) { 69 assertTrue("Set.iterator should only return unique elements", 70 set.add(iterator.next())); 71 } 72 } 73 74 78 public boolean isEqualsCheckable() { 79 return true; 80 } 81 82 87 public Collection makeConfirmedCollection() { 88 return new HashSet (); 89 } 90 91 96 public Collection makeConfirmedFullCollection() { 97 Collection set = makeConfirmedCollection(); 98 set.addAll(Arrays.asList(getFullElements())); 99 return set; 100 } 101 102 107 public abstract Set makeEmptySet(); 108 109 117 public Set makeFullSet() { 118 Set set = makeEmptySet(); 119 set.addAll(Arrays.asList(getFullElements())); 120 return set; 121 } 122 123 128 public final Collection makeCollection() { 129 return makeEmptySet(); 130 } 131 132 137 public final Collection makeFullCollection() { 138 return makeFullSet(); 139 } 140 141 145 public Set getSet() { 146 return (Set )collection; 147 } 148 149 152 public Set getConfirmedSet() { 153 return (Set )confirmed; 154 } 155 156 160 public void testSetEquals() { 161 resetEmpty(); 162 assertEquals("Empty sets should be equal", 163 getSet(), getConfirmedSet()); 164 verify(); 165 166 Collection set2 = makeConfirmedCollection(); 167 set2.add("foo"); 168 assertTrue("Empty set shouldn't equal nonempty set", 169 !getSet().equals(set2)); 170 171 resetFull(); 172 assertEquals("Full sets should be equal", getSet(), getConfirmedSet()); 173 verify(); 174 175 set2.clear(); 176 set2.addAll(Arrays.asList(getOtherElements())); 177 assertTrue("Sets with different contents shouldn't be equal", 178 !getSet().equals(set2)); 179 } 180 181 184 public void testSetHashCode() { 185 resetEmpty(); 186 assertEquals("Empty sets have equal hashCodes", 187 getSet().hashCode(), getConfirmedSet().hashCode()); 188 189 resetFull(); 190 assertEquals("Equal sets have equal hashCodes", 191 getSet().hashCode(), getConfirmedSet().hashCode()); 192 } 193 194 } 195 | Popular Tags |