1 20 21 package org.jacorb.test.notification.util; 22 23 import java.util.HashSet ; 24 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import org.jacorb.notification.util.AbstractObjectPool; 30 31 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 32 33 36 37 public class ObjectPoolTest extends TestCase 38 { 39 public ObjectPoolTest(String name) 40 { 41 super(name); 42 } 43 44 public static Test suite() 45 { 46 TestSuite suite = new TestSuite(ObjectPoolTest.class); 47 48 return suite; 49 } 50 51 public void testGetReturnsUniqueObject() throws Exception 52 { 53 final SynchronizedInt counter = new SynchronizedInt(0); 54 55 int testSize = 10; 56 57 AbstractObjectPool pool = new AbstractObjectPool("Test", testSize, 1, 2, 20, 0) 58 { 59 public Object newInstance() 60 { 61 return new Integer (counter.increment()); 62 } 63 }; 64 65 pool.configure(null); 66 67 HashSet unique = new HashSet (); 68 69 for (int x = 0; x < testSize * 2; ++x) 70 { 71 Integer i = (Integer ) pool.lendObject(); 72 assertFalse(unique.contains(i)); 73 unique.add(i); 74 } 75 } 76 77 public void testMaximumElements() throws Exception 78 { 79 final SynchronizedInt counter = new SynchronizedInt(0); 80 81 AbstractObjectPool pool = new AbstractObjectPool("Test", 0, 0, 0, 0, 1) 82 { 83 public Object newInstance() 84 { 85 return new Integer (counter.increment()); 86 } 87 }; 88 89 pool.configure(null); 90 91 Object lended = pool.lendObject(); 92 93 try 94 { 95 pool.lendObject(); 96 fail(); 97 } catch (RuntimeException e) 98 { 99 } 101 102 try 103 { 104 pool.returnObject(new Integer (10)); 105 fail(); 106 } catch (RuntimeException e) 107 { 108 } 110 111 try 112 { 113 pool.lendObject(); 114 fail(); 115 } catch (RuntimeException e) 116 { 117 } 119 120 pool.returnObject(lended); 121 122 pool.lendObject(); 123 } 124 } 125 | Popular Tags |