1 16 package org.apache.commons.pool; 17 18 import junit.framework.TestCase; 19 20 25 public abstract class TestObjectPool extends TestCase { 26 public TestObjectPool(String testName) { 27 super(testName); 28 } 29 30 37 protected abstract ObjectPool makeEmptyPool(int mincapacity); 38 39 43 protected abstract Object getNthObject(int n); 44 45 public void setUp() throws Exception { 46 } 47 48 public void tearDown() throws Exception { 49 _pool = null; 50 } 51 52 public void testBaseBorrow() throws Exception { 53 try { 54 _pool = makeEmptyPool(3); 55 } catch(IllegalArgumentException e) { 56 return; } 58 assertEquals(getNthObject(0),_pool.borrowObject()); 59 assertEquals(getNthObject(1),_pool.borrowObject()); 60 assertEquals(getNthObject(2),_pool.borrowObject()); 61 } 62 63 public void testBaseAddObject() throws Exception { 64 try { 65 _pool = makeEmptyPool(3); 66 } catch(IllegalArgumentException e) { 67 return; } 69 try { 70 assertEquals(0,_pool.getNumIdle()); 71 assertEquals(0,_pool.getNumActive()); 72 _pool.addObject(); 73 assertEquals(1,_pool.getNumIdle()); 74 assertEquals(0,_pool.getNumActive()); 75 Object obj = _pool.borrowObject(); 76 assertEquals(getNthObject(0),obj); 77 assertEquals(0,_pool.getNumIdle()); 78 assertEquals(1,_pool.getNumActive()); 79 _pool.returnObject(obj); 80 assertEquals(1,_pool.getNumIdle()); 81 assertEquals(0,_pool.getNumActive()); 82 } catch(UnsupportedOperationException e) { 83 return; } 85 } 86 87 public void testBaseBorrowReturn() throws Exception { 88 try { 89 _pool = makeEmptyPool(3); 90 } catch(IllegalArgumentException e) { 91 return; } 93 Object obj0 = _pool.borrowObject(); 94 assertEquals(getNthObject(0),obj0); 95 Object obj1 = _pool.borrowObject(); 96 assertEquals(getNthObject(1),obj1); 97 Object obj2 = _pool.borrowObject(); 98 assertEquals(getNthObject(2),obj2); 99 _pool.returnObject(obj2); 100 obj2 = _pool.borrowObject(); 101 assertEquals(getNthObject(2),obj2); 102 _pool.returnObject(obj1); 103 obj1 = _pool.borrowObject(); 104 assertEquals(getNthObject(1),obj1); 105 _pool.returnObject(obj0); 106 _pool.returnObject(obj2); 107 obj2 = _pool.borrowObject(); 108 assertEquals(getNthObject(2),obj2); 109 obj0 = _pool.borrowObject(); 110 assertEquals(getNthObject(0),obj0); 111 } 112 113 public void testBaseNumActiveNumIdle() throws Exception { 114 try { 115 _pool = makeEmptyPool(3); 116 } catch(IllegalArgumentException e) { 117 return; } 119 assertEquals(0,_pool.getNumActive()); 120 assertEquals(0,_pool.getNumIdle()); 121 Object obj0 = _pool.borrowObject(); 122 assertEquals(1,_pool.getNumActive()); 123 assertEquals(0,_pool.getNumIdle()); 124 Object obj1 = _pool.borrowObject(); 125 assertEquals(2,_pool.getNumActive()); 126 assertEquals(0,_pool.getNumIdle()); 127 _pool.returnObject(obj1); 128 assertEquals(1,_pool.getNumActive()); 129 assertEquals(1,_pool.getNumIdle()); 130 _pool.returnObject(obj0); 131 assertEquals(0,_pool.getNumActive()); 132 assertEquals(2,_pool.getNumIdle()); 133 } 134 135 public void testBaseClear() throws Exception { 136 try { 137 _pool = makeEmptyPool(3); 138 } catch(IllegalArgumentException e) { 139 return; } 141 assertEquals(0,_pool.getNumActive()); 142 assertEquals(0,_pool.getNumIdle()); 143 Object obj0 = _pool.borrowObject(); 144 Object obj1 = _pool.borrowObject(); 145 assertEquals(2,_pool.getNumActive()); 146 assertEquals(0,_pool.getNumIdle()); 147 _pool.returnObject(obj1); 148 _pool.returnObject(obj0); 149 assertEquals(0,_pool.getNumActive()); 150 assertEquals(2,_pool.getNumIdle()); 151 _pool.clear(); 152 assertEquals(0,_pool.getNumActive()); 153 assertEquals(0,_pool.getNumIdle()); 154 Object obj2 = _pool.borrowObject(); 155 assertEquals(getNthObject(2),obj2); 156 } 157 158 public void testBaseInvalidateObject() throws Exception { 159 try { 160 _pool = makeEmptyPool(3); 161 } catch(IllegalArgumentException e) { 162 return; } 164 assertEquals(0,_pool.getNumActive()); 165 assertEquals(0,_pool.getNumIdle()); 166 Object obj0 = _pool.borrowObject(); 167 Object obj1 = _pool.borrowObject(); 168 assertEquals(2,_pool.getNumActive()); 169 assertEquals(0,_pool.getNumIdle()); 170 _pool.invalidateObject(obj0); 171 assertEquals(1,_pool.getNumActive()); 172 assertEquals(0,_pool.getNumIdle()); 173 _pool.invalidateObject(obj1); 174 assertEquals(0,_pool.getNumActive()); 175 assertEquals(0,_pool.getNumIdle()); 176 } 177 178 public void testBaseClosePool() throws Exception { 179 try { 180 _pool = makeEmptyPool(3); 181 } catch(IllegalArgumentException e) { 182 return; } 184 Object obj = _pool.borrowObject(); 185 _pool.returnObject(obj); 186 187 _pool.close(); 188 try { 189 _pool.borrowObject(); 190 fail("Expected IllegalStateException"); 191 } catch(IllegalStateException e) { 192 } 194 } 195 196 public void testBaseCantCloseTwice() throws Exception { 197 try { 198 _pool = makeEmptyPool(3); 199 } catch(IllegalArgumentException e) { 200 return; } 202 Object obj = _pool.borrowObject(); 203 _pool.returnObject(obj); 204 205 _pool.close(); 206 try { 207 _pool.close(); 208 fail("Expected IllegalStateException"); 209 } catch(IllegalStateException e) { 210 } 212 } 213 214 private ObjectPool _pool = null; 215 } 216 | Popular Tags |