1 16 package org.apache.commons.pool; 17 18 import junit.framework.TestCase; 19 20 25 public abstract class TestKeyedObjectPool extends TestCase { 26 public TestKeyedObjectPool(String testName) { 27 super(testName); 28 } 29 30 37 protected abstract KeyedObjectPool makeEmptyPool(int mincapacity); 38 39 44 protected abstract Object getNthObject(Object key, int n); 45 46 protected abstract Object makeKey(int n); 47 48 public void setUp() throws Exception { 49 } 50 51 public void tearDown() throws Exception { 52 _pool = null; 53 } 54 55 public void testBaseBorrow() throws Exception { 56 try { 57 _pool = makeEmptyPool(3); 58 } catch(IllegalArgumentException e) { 59 return; } 61 Object keya = makeKey(0); 62 Object keyb = makeKey(1); 63 assertEquals("1",getNthObject(keya,0),_pool.borrowObject(keya)); 64 assertEquals("2",getNthObject(keyb,0),_pool.borrowObject(keyb)); 65 assertEquals("3",getNthObject(keyb,1),_pool.borrowObject(keyb)); 66 assertEquals("4",getNthObject(keya,1),_pool.borrowObject(keya)); 67 assertEquals("5",getNthObject(keyb,2),_pool.borrowObject(keyb)); 68 assertEquals("6",getNthObject(keya,2),_pool.borrowObject(keya)); 69 } 70 71 public void testBaseBorrowReturn() throws Exception { 72 try { 73 _pool = makeEmptyPool(3); 74 } catch(IllegalArgumentException e) { 75 return; } 77 Object keya = makeKey(0); 78 Object obj0 = _pool.borrowObject(keya); 79 assertEquals(getNthObject(keya,0),obj0); 80 Object obj1 = _pool.borrowObject(keya); 81 assertEquals(getNthObject(keya,1),obj1); 82 Object obj2 = _pool.borrowObject(keya); 83 assertEquals(getNthObject(keya,2),obj2); 84 _pool.returnObject(keya,obj2); 85 obj2 = _pool.borrowObject(keya); 86 assertEquals(getNthObject(keya,2),obj2); 87 _pool.returnObject(keya,obj1); 88 obj1 = _pool.borrowObject(keya); 89 assertEquals(getNthObject(keya,1),obj1); 90 _pool.returnObject(keya,obj0); 91 _pool.returnObject(keya,obj2); 92 obj2 = _pool.borrowObject(keya); 93 assertEquals(getNthObject(keya,2),obj2); 94 obj0 = _pool.borrowObject(keya); 95 assertEquals(getNthObject(keya,0),obj0); 96 } 97 98 public void testBaseNumActiveNumIdle() throws Exception { 99 try { 100 _pool = makeEmptyPool(3); 101 } catch(IllegalArgumentException e) { 102 return; } 104 Object keya = makeKey(0); 105 assertEquals(0,_pool.getNumActive(keya)); 106 assertEquals(0,_pool.getNumIdle(keya)); 107 Object obj0 = _pool.borrowObject(keya); 108 assertEquals(1,_pool.getNumActive(keya)); 109 assertEquals(0,_pool.getNumIdle(keya)); 110 Object obj1 = _pool.borrowObject(keya); 111 assertEquals(2,_pool.getNumActive(keya)); 112 assertEquals(0,_pool.getNumIdle(keya)); 113 _pool.returnObject(keya,obj1); 114 assertEquals(1,_pool.getNumActive(keya)); 115 assertEquals(1,_pool.getNumIdle(keya)); 116 _pool.returnObject(keya,obj0); 117 assertEquals(0,_pool.getNumActive(keya)); 118 assertEquals(2,_pool.getNumIdle(keya)); 119 120 assertEquals(0,_pool.getNumActive("xyzzy12345")); 121 assertEquals(0,_pool.getNumIdle("xyzzy12345")); 122 } 123 124 public void testBaseNumActiveNumIdle2() throws Exception { 125 try { 126 _pool = makeEmptyPool(6); 127 } catch(IllegalArgumentException e) { 128 return; } 130 Object keya = makeKey(0); 131 Object keyb = makeKey(1); 132 assertEquals(0,_pool.getNumActive()); 133 assertEquals(0,_pool.getNumIdle()); 134 assertEquals(0,_pool.getNumActive(keya)); 135 assertEquals(0,_pool.getNumIdle(keya)); 136 assertEquals(0,_pool.getNumActive(keyb)); 137 assertEquals(0,_pool.getNumIdle(keyb)); 138 139 Object objA0 = _pool.borrowObject(keya); 140 Object objB0 = _pool.borrowObject(keyb); 141 142 assertEquals(2,_pool.getNumActive()); 143 assertEquals(0,_pool.getNumIdle()); 144 assertEquals(1,_pool.getNumActive(keya)); 145 assertEquals(0,_pool.getNumIdle(keya)); 146 assertEquals(1,_pool.getNumActive(keyb)); 147 assertEquals(0,_pool.getNumIdle(keyb)); 148 149 Object objA1 = _pool.borrowObject(keya); 150 Object objB1 = _pool.borrowObject(keyb); 151 152 assertEquals(4,_pool.getNumActive()); 153 assertEquals(0,_pool.getNumIdle()); 154 assertEquals(2,_pool.getNumActive(keya)); 155 assertEquals(0,_pool.getNumIdle(keya)); 156 assertEquals(2,_pool.getNumActive(keyb)); 157 assertEquals(0,_pool.getNumIdle(keyb)); 158 159 _pool.returnObject(keya,objA0); 160 _pool.returnObject(keyb,objB0); 161 162 assertEquals(2,_pool.getNumActive()); 163 assertEquals(2,_pool.getNumIdle()); 164 assertEquals(1,_pool.getNumActive(keya)); 165 assertEquals(1,_pool.getNumIdle(keya)); 166 assertEquals(1,_pool.getNumActive(keyb)); 167 assertEquals(1,_pool.getNumIdle(keyb)); 168 169 _pool.returnObject(keya,objA1); 170 _pool.returnObject(keyb,objB1); 171 172 assertEquals(0,_pool.getNumActive()); 173 assertEquals(4,_pool.getNumIdle()); 174 assertEquals(0,_pool.getNumActive(keya)); 175 assertEquals(2,_pool.getNumIdle(keya)); 176 assertEquals(0,_pool.getNumActive(keyb)); 177 assertEquals(2,_pool.getNumIdle(keyb)); 178 } 179 180 public void testBaseClear() throws Exception { 181 try { 182 _pool = makeEmptyPool(3); 183 } catch(IllegalArgumentException e) { 184 return; } 186 Object keya = makeKey(0); 187 assertEquals(0,_pool.getNumActive(keya)); 188 assertEquals(0,_pool.getNumIdle(keya)); 189 Object obj0 = _pool.borrowObject(keya); 190 Object obj1 = _pool.borrowObject(keya); 191 assertEquals(2,_pool.getNumActive(keya)); 192 assertEquals(0,_pool.getNumIdle(keya)); 193 _pool.returnObject(keya,obj1); 194 _pool.returnObject(keya,obj0); 195 assertEquals(0,_pool.getNumActive(keya)); 196 assertEquals(2,_pool.getNumIdle(keya)); 197 _pool.clear(keya); 198 assertEquals(0,_pool.getNumActive(keya)); 199 assertEquals(0,_pool.getNumIdle(keya)); 200 Object obj2 = _pool.borrowObject(keya); 201 assertEquals(getNthObject(keya,2),obj2); 202 } 203 204 public void testBaseInvalidateObject() throws Exception { 205 try { 206 _pool = makeEmptyPool(3); 207 } catch(IllegalArgumentException e) { 208 return; } 210 Object keya = makeKey(0); 211 assertEquals(0,_pool.getNumActive(keya)); 212 assertEquals(0,_pool.getNumIdle(keya)); 213 Object obj0 = _pool.borrowObject(keya); 214 Object obj1 = _pool.borrowObject(keya); 215 assertEquals(2,_pool.getNumActive(keya)); 216 assertEquals(0,_pool.getNumIdle(keya)); 217 _pool.invalidateObject(keya,obj0); 218 assertEquals(1,_pool.getNumActive(keya)); 219 assertEquals(0,_pool.getNumIdle(keya)); 220 _pool.invalidateObject(keya,obj1); 221 assertEquals(0,_pool.getNumActive(keya)); 222 assertEquals(0,_pool.getNumIdle(keya)); 223 } 224 225 public void testBaseAddObject() throws Exception { 226 try { 227 _pool = makeEmptyPool(3); 228 } catch(IllegalArgumentException e) { 229 return; } 231 Object key = makeKey(0); 232 try { 233 assertEquals(0,_pool.getNumIdle()); 234 assertEquals(0,_pool.getNumActive()); 235 assertEquals(0,_pool.getNumIdle(key)); 236 assertEquals(0,_pool.getNumActive(key)); 237 _pool.addObject(key); 238 assertEquals(1,_pool.getNumIdle()); 239 assertEquals(0,_pool.getNumActive()); 240 assertEquals(1,_pool.getNumIdle(key)); 241 assertEquals(0,_pool.getNumActive(key)); 242 Object obj = _pool.borrowObject(key); 243 assertEquals(getNthObject(key,0),obj); 244 assertEquals(0,_pool.getNumIdle()); 245 assertEquals(1,_pool.getNumActive()); 246 assertEquals(0,_pool.getNumIdle(key)); 247 assertEquals(1,_pool.getNumActive(key)); 248 _pool.returnObject(key,obj); 249 assertEquals(1,_pool.getNumIdle()); 250 assertEquals(0,_pool.getNumActive()); 251 assertEquals(1,_pool.getNumIdle(key)); 252 assertEquals(0,_pool.getNumActive(key)); 253 } catch(UnsupportedOperationException e) { 254 return; } 256 } 257 258 private KeyedObjectPool _pool = null; 259 } 260 | Popular Tags |