1 16 17 package org.apache.commons.pool.impl; 18 19 import java.util.BitSet ; 20 import java.util.NoSuchElementException ; 21 import java.util.List ; 22 import java.util.ArrayList ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 27 import org.apache.commons.pool.ObjectPool; 28 import org.apache.commons.pool.PoolableObjectFactory; 29 import org.apache.commons.pool.TestObjectPool; 30 31 36 public class TestStackObjectPool extends TestObjectPool { 37 public TestStackObjectPool(String testName) { 38 super(testName); 39 } 40 41 public static Test suite() { 42 return new TestSuite(TestStackObjectPool.class); 43 } 44 45 protected ObjectPool makeEmptyPool(int mincap) { 46 return new StackObjectPool(new SimpleFactory()); 47 } 48 49 protected Object getNthObject(int n) { 50 return String.valueOf(n); 51 } 52 53 public void testIdleCap() throws Exception { 54 ObjectPool pool = makeEmptyPool(8); 55 Object [] active = new Object [100]; 56 for(int i=0;i<100;i++) { 57 active[i] = pool.borrowObject(); 58 } 59 assertEquals(100,pool.getNumActive()); 60 assertEquals(0,pool.getNumIdle()); 61 for(int i=0;i<100;i++) { 62 pool.returnObject(active[i]); 63 assertEquals(99 - i,pool.getNumActive()); 64 assertEquals((i < 8 ? i+1 : 8),pool.getNumIdle()); 65 } 66 } 67 68 public void testPoolWithNullFactory() throws Exception { 69 ObjectPool pool = new StackObjectPool(10); 70 for(int i=0;i<10;i++) { 71 pool.returnObject(new Integer (i)); 72 } 73 for(int j=0;j<3;j++) { 74 Integer [] borrowed = new Integer [10]; 75 BitSet found = new BitSet (); 76 for(int i=0;i<10;i++) { 77 borrowed[i] = (Integer )(pool.borrowObject()); 78 assertNotNull(borrowed); 79 assertTrue(!found.get(borrowed[i].intValue())); 80 found.set(borrowed[i].intValue()); 81 } 82 for(int i=0;i<10;i++) { 83 pool.returnObject(borrowed[i]); 84 } 85 } 86 pool.invalidateObject(pool.borrowObject()); 87 pool.invalidateObject(pool.borrowObject()); 88 pool.clear(); 89 } 90 91 public void testBorrowFromEmptyPoolWithNullFactory() throws Exception { 92 ObjectPool pool = new StackObjectPool(); 93 try { 94 pool.borrowObject(); 95 fail("Expected NoSuchElementException"); 96 } catch(NoSuchElementException e) { 97 } 99 } 100 101 public void testSetFactory() throws Exception { 102 ObjectPool pool = new StackObjectPool(); 103 try { 104 pool.borrowObject(); 105 fail("Expected NoSuchElementException"); 106 } catch(NoSuchElementException e) { 107 } 109 pool.setFactory(new SimpleFactory()); 110 Object obj = pool.borrowObject(); 111 assertNotNull(obj); 112 pool.returnObject(obj); 113 } 114 115 public void testCantResetFactoryWithActiveObjects() throws Exception { 116 ObjectPool pool = new StackObjectPool(); 117 pool.setFactory(new SimpleFactory()); 118 Object obj = pool.borrowObject(); 119 assertNotNull(obj); 120 121 try { 122 pool.setFactory(new SimpleFactory()); 123 fail("Expected IllegalStateException"); 124 } catch(IllegalStateException e) { 125 } 127 } 128 129 public void testCanResetFactoryWithoutActiveObjects() throws Exception { 130 ObjectPool pool = new StackObjectPool(); 131 { 132 pool.setFactory(new SimpleFactory()); 133 Object obj = pool.borrowObject(); 134 assertNotNull(obj); 135 pool.returnObject(obj); 136 } 137 { 138 pool.setFactory(new SimpleFactory()); 139 Object obj = pool.borrowObject(); 140 assertNotNull(obj); 141 pool.returnObject(obj); 142 } 143 } 144 145 public void testBorrowReturnWithSometimesInvalidObjects() throws Exception { 146 ObjectPool pool = new StackObjectPool(20); 147 pool.setFactory( 148 new PoolableObjectFactory() { 149 int counter = 0; 150 public Object makeObject() { return new Integer (counter++); } 151 public void destroyObject(Object obj) { } 152 public boolean validateObject(Object obj) { 153 if(obj instanceof Integer ) { 154 return ((((Integer )obj).intValue() % 2) == 1); 155 } else { 156 return false; 157 } 158 } 159 public void activateObject(Object obj) { } 160 public void passivateObject(Object obj) { 161 if(obj instanceof Integer ) { 162 if((((Integer )obj).intValue() % 3) == 0) { 163 throw new RuntimeException ("Couldn't passivate"); 164 } 165 } else { 166 throw new RuntimeException ("Couldn't passivate"); 167 } 168 } 169 } 170 ); 171 172 Object [] obj = new Object [10]; 173 for(int i=0;i<10;i++) { 174 obj[i] = pool.borrowObject(); 175 } 176 for(int i=0;i<10;i++) { 177 pool.returnObject(obj[i]); 178 } 179 assertEquals(3,pool.getNumIdle()); 180 } 181 182 public void testVariousConstructors() throws Exception { 183 { 184 StackObjectPool pool = new StackObjectPool(); 185 assertNotNull(pool); 186 } 187 { 188 StackObjectPool pool = new StackObjectPool(10); 189 assertNotNull(pool); 190 } 191 { 192 StackObjectPool pool = new StackObjectPool(10,5); 193 assertNotNull(pool); 194 } 195 { 196 StackObjectPool pool = new StackObjectPool(null); 197 assertNotNull(pool); 198 } 199 { 200 StackObjectPool pool = new StackObjectPool(null,10); 201 assertNotNull(pool); 202 } 203 { 204 StackObjectPool pool = new StackObjectPool(null,10,5); 205 assertNotNull(pool); 206 } 207 } 208 209 private final List destroyed = new ArrayList (); 210 public void testReturnObjectDiscardOrder() throws Exception { 211 PoolableObjectFactory pof = new PoolableObjectFactory() { 214 int i = 0; 215 public Object makeObject() throws Exception { 216 return new Integer (i++); 217 } 218 219 public void destroyObject(Object obj) throws Exception { 220 destroyed.add(obj); 221 } 222 223 public boolean validateObject(Object obj) { 224 return obj instanceof Integer ; 225 } 226 227 public void activateObject(Object obj) throws Exception { 228 } 229 230 public void passivateObject(Object obj) throws Exception { 231 } 232 }; 233 ObjectPool pool = new StackObjectPool(pof, 3); 234 235 Integer i0 = (Integer )pool.borrowObject(); 237 Integer i1 = (Integer )pool.borrowObject(); 238 Integer i2 = (Integer )pool.borrowObject(); 239 Integer i3 = (Integer )pool.borrowObject(); 240 241 pool.returnObject(i0); 244 pool.returnObject(i1); 245 pool.returnObject(i2); 246 247 assertEquals("No returned objects should have been destroyed yet.",0, destroyed.size()); 249 250 pool.returnObject(i3); 252 assertEquals("One object should have been destroyed.", 1, destroyed.size()); 253 254 Integer d = (Integer )destroyed.get(0); 256 assertEquals("Destoryed objects should have the stalest object.", i0, d); 257 } 258 259 static class SimpleFactory implements PoolableObjectFactory { 260 int counter = 0; 261 public Object makeObject() { return String.valueOf(counter++); } 262 public void destroyObject(Object obj) { } 263 public boolean validateObject(Object obj) { return true; } 264 public void activateObject(Object obj) { } 265 public void passivateObject(Object obj) { } 266 } 267 } 268 269 | Popular Tags |