1 package org.jboss.cache.pojo.collection; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jboss.cache.pojo.PojoCache; 9 import org.jboss.cache.pojo.PojoCacheFactory; 10 import org.jboss.cache.pojo.test.Address; 11 12 import java.io.PrintWriter ; 13 import java.io.StringWriter ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Collection ; 17 import java.util.Collections ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.LinkedList ; 21 import java.util.List ; 22 import java.util.NoSuchElementException ; 23 import java.util.Set ; 24 25 30 31 public class CachedSetTest extends TestCase 32 { 33 Log log = LogFactory.getLog(CachedSetTest.class); 34 PojoCache cache_; 35 Set skills; 36 37 public CachedSetTest(String name) 38 { 39 super(name); 40 } 41 42 protected void setUp() throws Exception 43 { 44 super.setUp(); 45 log.info("setUp() ...."); 46 String configFile = "META-INF/local-service.xml"; 47 boolean toStart = false; 48 cache_ = PojoCacheFactory.createCache(configFile, toStart); 49 cache_.start(); 50 51 stage(); 52 } 53 54 protected void tearDown() throws Exception 55 { 56 super.tearDown(); 57 cache_.stop(); 58 } 59 60 protected void stage() throws Exception 61 { 62 skills = new HashSet (); 63 skills.add("Java"); 64 skills.add("C++"); 65 skills.add("Perl"); 66 67 cache_.attach("/person/test7", skills); 68 skills = (Set ) cache_.find("/person/test7"); 69 int size = skills.size(); 70 assertEquals("Size is ", 3, size); 71 } 72 73 public void testClear() throws Throwable 74 { 75 int size = skills.size(); 76 assertEquals("Size is ", 3, size); 77 78 skills.clear(); 79 size = skills.size(); 80 assertEquals("Size is ", 0, size); 81 82 assertTrue("Should be empty", skills.isEmpty()); 83 } 84 85 public void testAddAndRemoveIndex() throws Throwable 86 { 87 skills.add("Golf"); 88 int size = skills.size(); 89 assertEquals("Size is ", 4, size); 90 91 skills.add("Golf"); 92 size = skills.size(); 93 assertEquals("Size is ", 4, size); 94 95 assertTrue("Skill shuold contain Golf ", skills.contains("Golf")); 96 skills.remove("C++"); 97 size = skills.size(); 98 assertEquals("Size is ", 3, size); 99 assertFalse("Skill does not contain C++ anymore ", skills.contains("C++")); 100 101 skills.add("Golf"); 102 size = skills.size(); 103 assertEquals("Size is ", 3, size); 104 skills.clear(); 105 size = skills.size(); 106 assertEquals("Size is ", 0, size); 107 108 assertTrue("Should be empty", skills.isEmpty()); 109 } 110 111 private static class DumbObject implements java.io.Serializable 112 { 113 int i; 114 DumbObject(int i) { this.i = i; } 115 public int hashCode() 116 { 117 return 0; 118 } 119 public String toString() 120 { 121 return "" + i; 122 } 123 } 124 125 public void testConflictingHash() throws Exception 126 { 127 Set set = new HashSet (); 128 String nulls = null; 129 DumbObject o1 = new DumbObject(1); 130 DumbObject o2 = new DumbObject(2); 131 DumbObject o3 = new DumbObject(3); 132 set.add(o1); 133 set.add(o2); 134 set.add(o3); 135 set.add(nulls); 136 Set set2 = Collections.unmodifiableSet(new HashSet (set)); 137 138 cache_.attach("/test", set); set = (Set ) cache_.find("/test"); 140 assertEquals("Same", set2, set); 141 assertEquals(true, set.remove(o2)); 142 assertEquals(true, set.remove(nulls)); 143 assertEquals(false, set.remove(o2)); 144 assertEquals("Same minus 2 ", set2.size()-2, set.size()); 145 assertEquals("set " + set, true, set.add(o2)); 146 assertEquals("set " + set, true, set.add(nulls)); 147 assertEquals(false, set.add(o2)); 148 assertEquals(false, set.add(nulls)); 149 assertEquals("Same", set2, set); 150 } 151 152 public void testAddAndRemoveAll() throws Throwable 153 { 154 List list = new ArrayList (); 155 list.add("Tennis"); 156 list.add("Polo"); 157 list.add("Baseball"); 158 159 skills.addAll((Collection ) list); 160 int size = skills.size(); 161 assertEquals("Size is ", 6, size); 162 assertTrue("Skill contains Polo ", skills.contains("Polo")); 163 164 skills.removeAll((Collection ) list); 165 size = skills.size(); 166 assertEquals("Size is ", 3, size); 167 assertFalse("Skill does not contain Polo ", skills.contains("Polo")); 168 assertTrue("Skill contains C++ ", skills.contains("C++")); 169 170 } 171 172 public void testRemoveAndAdd() 173 { 174 assertTrue(skills.remove("C++")); 175 assertTrue(skills.add("C++")); 176 assertEquals("removeAndAdd: size is 3", 3, skills.size()); 177 } 178 179 public void testIterator() 180 { 181 Iterator it = skills.iterator(); 182 int counter = 0; 183 while (it.hasNext()) 184 { 185 counter++; 186 it.next(); 187 it.remove(); 188 } 189 190 assertEquals("iterator: Size should be ", 3, counter); 191 assertEquals("iterator: Skills should be empty ", 0, skills.size()); 192 193 List list = new ArrayList (); 194 list.add("Tennis"); 195 list.add("Polo"); 196 list.add("Baseball"); 197 list.add("Soccer"); 198 list.add("Hockey"); 199 list.add("Lacrosse"); 200 skills.addAll(list); 201 it = skills.iterator(); 202 while (it.hasNext()) 203 { 204 counter++; 205 if ("Polo".equals(it.next())) 206 { 207 it.remove(); 208 } 209 } 210 assertFalse("iterator: item removed via iterator", skills.contains("Polo")); 211 assertTrue("iterator: item not removed via iterator", skills.contains("Lacrosse")); 212 213 list = new ArrayList (); 215 list.add("Tennis"); 216 list.add("Polo"); 217 list.add("Baseball"); 218 skills.addAll(list); 219 it = skills.iterator(); 220 while (it.hasNext()) 221 { 222 it.next(); 223 } 224 try 225 { 226 it.next(); 227 fail("iterator: Didn't fail on next() when hasNext() == false"); 228 } 229 catch (NoSuchElementException good) 230 { 231 } 232 233 it = skills.iterator(); 235 try 236 { 237 it.remove(); 238 } 239 catch (IllegalStateException good) 240 { 241 } 243 catch (Exception e) 244 { 245 StringWriter stackTrace = new StringWriter (); 246 e.printStackTrace(new PrintWriter (stackTrace)); 247 fail("iterator: failed with incorrect exception type when removing " + 248 "without first calling next() -- " + e.getClass().getName() + ". stackTrace=" + stackTrace); 249 } 250 251 252 } 253 254 public void testEquals() throws Throwable 255 { 256 Set set = (Set ) cache_.find("/person/test7"); 257 assertTrue("iterator: List should be the same ", set.equals(skills)); 258 set = new HashSet (); 259 set.add("German"); 260 set.add("test"); 261 set.add("English"); 262 assertFalse("List should not be the same ", set.equals(skills)); 263 assertFalse("List should not be the same ", skills.equals(set)); 264 } 265 266 267 public void testAttachAndDetach() throws Exception 268 { 269 Set set = new HashSet (); 270 set.add("English"); 271 set.add("French"); 272 set.add("Taiwanese"); 273 274 cache_.attach("/test", set); set = (Set ) cache_.find("/test"); 276 assertEquals("Size ", 3, set.size()); 277 278 set = (Set ) cache_.detach("/test"); 279 assertEquals("Size ", 3, set.size()); 280 281 System.out.println("**** End of cache content **** "); 282 set.remove("French"); 283 set.add("Hoklo"); 284 assertEquals("Size ", 3, set.size()); 285 assertTrue("Content ", set.contains("Hoklo")); 286 287 cache_.attach("/test", set); 289 set.remove("Taiwanese"); 290 assertEquals("Size ", 2, set.size()); 291 } 292 293 public void testPojoAttachAndDetach() throws Exception 294 { 295 Address add1 = new Address(); 296 add1.setCity("San Jose"); 297 add1.setZip(95123); 298 299 Address add2 = new Address(); 300 add2.setCity("Sunnyvale"); 301 add2.setZip(94086); 302 303 Address add3 = new Address(); 304 add3.setCity("Santa Clara"); 305 add3.setZip(951131); 306 307 Set set = new HashSet (); 308 set.add(add1); 309 set.add(add2); 310 set.add(add3); 311 312 cache_.attach("/test", set); set = (Set ) cache_.find("/test"); 314 assertEquals("Size ", 3, set.size()); 315 316 set = (Set ) cache_.detach("/test"); 317 assertEquals("Size ", 3, set.size()); 318 319 System.out.println("**** End of cache content **** "); 320 set.remove(add2); 321 set.add(add2); 322 assertEquals("Size ", 3, set.size()); 323 assertTrue("Content ", set.contains(add2)); 324 325 cache_.attach("/test", set); 327 set.remove(add2); 328 assertEquals("Size ", 2, set.size()); 329 } 330 331 public void testToArray() throws Exception 332 { 333 Object [] arr = skills.toArray(); 334 assertEquals("toArray: array length is correct", 3, arr.length); 335 testLanguagesFound(arr); 336 337 Object [] arr1 = skills.toArray(arr); 338 assertTrue("toArray: arrays match", Arrays.equals(arr, arr1)); 339 340 arr = new Object [5]; 341 arr = skills.toArray(arr); 342 assertEquals("toArray: array length is correct", 5, arr.length); 343 testLanguagesFound(arr); 344 assertEquals(null, arr[3]); 345 assertEquals(null, arr[4]); 346 347 arr = new Object [2]; 348 arr = skills.toArray(arr); 349 assertEquals("toArray: array length is correct", 3, arr.length); 350 testLanguagesFound(arr); 351 352 } 353 354 private void testLanguagesFound(Object [] arr) 355 { 356 boolean[] found = new boolean[3]; 357 for (int i = 0; i < arr.length; i++) 358 { 359 if ("Java".equals(arr[i])) 360 found[0] = true; 361 else if ("C++".equals(arr[i])) 362 found[1] = true; 363 else if ("Perl".equals(arr[i])) 364 found[2] = true; 365 } 366 assertTrue("toArray: all elements found", found[0] && found[1] && found[2]); 367 368 } 369 370 public void testRetainAll() throws Exception 371 { 372 LinkedList list2 = new LinkedList (); 373 list2.add("Java"); 374 375 assertTrue("testRetainAll", skills.retainAll(list2)); 376 assertTrue("testRetainAll, skills size should be 1 but is " + skills.size(), skills.size() == 1); 378 assertTrue("testRetainAll", skills.contains("Java")); 379 380 } 381 382 public void testContainsAll() throws Exception 383 { 384 LinkedList list2 = new LinkedList (); 385 list2.add("Java"); 386 list2.add("C++"); 387 list2.add("Perl"); 388 389 skills.clear(); 390 391 assertTrue(skills.addAll(list2)); 392 list2.remove("Java"); 393 assertTrue("testContainsAll", skills.containsAll(list2)); 394 skills.remove("C++"); 395 assertFalse("testContainsAll", skills.containsAll(list2)); 396 } 397 398 public static Test suite() throws Exception 399 { 400 return new TestSuite(CachedSetTest.class); 401 } 402 403 public static void main(String [] args) throws Exception 404 { 405 junit.textui.TestRunner.run(suite()); 406 } 407 408 409 } 410 411 | Popular Tags |