001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.pool; 018 019 020 /** 021 * @author Rodney Waldhoff 022 * @author Sandy McArthur 023 * @version $Revision: 901944 $ $Date: 2010-01-21 17:27:04 -0700 (Thu, 21 Jan 2010) $ 024 */ 025 public class TestBaseObjectPool extends TestObjectPool { 026 private ObjectPool _pool = null; 027 028 public TestBaseObjectPool(String testName) { 029 super(testName); 030 } 031 032 protected ObjectPool makeEmptyPool(int mincapacity) { 033 if (this.getClass() != TestBaseObjectPool.class) { 034 fail("Subclasses of TestBaseObjectPool must reimplement this method."); 035 } 036 throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation."); 037 } 038 039 protected ObjectPool makeEmptyPool(final PoolableObjectFactory factory) { 040 if (this.getClass() != TestBaseObjectPool.class) { 041 fail("Subclasses of TestBaseObjectPool must reimplement this method."); 042 } 043 throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation."); 044 } 045 046 protected Object getNthObject(final int n) { 047 if (this.getClass() != TestBaseObjectPool.class) { 048 fail("Subclasses of TestBaseObjectPool must reimplement this method."); 049 } 050 throw new UnsupportedOperationException("BaseObjectPool isn't a complete implementation."); 051 } 052 053 protected boolean isLifo() { 054 if (this.getClass() != TestBaseObjectPool.class) { 055 fail("Subclasses of TestBaseObjectPool must reimplement this method."); 056 } 057 return false; 058 } 059 060 protected boolean isFifo() { 061 if (this.getClass() != TestBaseObjectPool.class) { 062 fail("Subclasses of TestBaseObjectPool must reimplement this method."); 063 } 064 return false; 065 } 066 067 // tests 068 public void testUnsupportedOperations() throws Exception { 069 if (!getClass().equals(TestBaseObjectPool.class)) { 070 return; // skip redundant tests 071 } 072 ObjectPool pool = new BaseObjectPool() { 073 public Object borrowObject() { 074 return null; 075 } 076 public void returnObject(Object obj) { 077 } 078 public void invalidateObject(Object obj) { 079 } 080 }; 081 082 assertTrue("Negative expected.", pool.getNumIdle() < 0); 083 assertTrue("Negative expected.", pool.getNumActive() < 0); 084 085 try { 086 pool.clear(); 087 fail("Expected UnsupportedOperationException"); 088 } catch(UnsupportedOperationException e) { 089 // expected 090 } 091 092 try { 093 pool.addObject(); 094 fail("Expected UnsupportedOperationException"); 095 } catch(UnsupportedOperationException e) { 096 // expected 097 } 098 099 try { 100 pool.setFactory(null); 101 fail("Expected UnsupportedOperationException"); 102 } catch(UnsupportedOperationException e) { 103 // expected 104 } 105 } 106 107 public void testClose() throws Exception { 108 ObjectPool pool = new BaseObjectPool() { 109 public Object borrowObject() { 110 return null; 111 } 112 public void returnObject(Object obj) { 113 } 114 public void invalidateObject(Object obj) { 115 } 116 }; 117 118 pool.close(); 119 pool.close(); // should not error as of Pool 2.0. 120 } 121 122 public void testBaseBorrow() throws Exception { 123 try { 124 _pool = makeEmptyPool(3); 125 } catch(UnsupportedOperationException e) { 126 return; // skip this test if unsupported 127 } 128 assertEquals(getNthObject(0),_pool.borrowObject()); 129 assertEquals(getNthObject(1),_pool.borrowObject()); 130 assertEquals(getNthObject(2),_pool.borrowObject()); 131 } 132 133 public void testBaseAddObject() throws Exception { 134 try { 135 _pool = makeEmptyPool(3); 136 } catch(UnsupportedOperationException e) { 137 return; // skip this test if unsupported 138 } 139 try { 140 assertEquals(0,_pool.getNumIdle()); 141 assertEquals(0,_pool.getNumActive()); 142 _pool.addObject(); 143 assertEquals(1,_pool.getNumIdle()); 144 assertEquals(0,_pool.getNumActive()); 145 Object obj = _pool.borrowObject(); 146 assertEquals(getNthObject(0),obj); 147 assertEquals(0,_pool.getNumIdle()); 148 assertEquals(1,_pool.getNumActive()); 149 _pool.returnObject(obj); 150 assertEquals(1,_pool.getNumIdle()); 151 assertEquals(0,_pool.getNumActive()); 152 } catch(UnsupportedOperationException e) { 153 return; // skip this test if one of those calls is unsupported 154 } 155 } 156 157 public void testBaseBorrowReturn() throws Exception { 158 try { 159 _pool = makeEmptyPool(3); 160 } catch(UnsupportedOperationException e) { 161 return; // skip this test if unsupported 162 } 163 Object obj0 = _pool.borrowObject(); 164 assertEquals(getNthObject(0),obj0); 165 Object obj1 = _pool.borrowObject(); 166 assertEquals(getNthObject(1),obj1); 167 Object obj2 = _pool.borrowObject(); 168 assertEquals(getNthObject(2),obj2); 169 _pool.returnObject(obj2); 170 obj2 = _pool.borrowObject(); 171 assertEquals(getNthObject(2),obj2); 172 _pool.returnObject(obj1); 173 obj1 = _pool.borrowObject(); 174 assertEquals(getNthObject(1),obj1); 175 _pool.returnObject(obj0); 176 _pool.returnObject(obj2); 177 obj2 = _pool.borrowObject(); 178 if (isLifo()) { 179 assertEquals(getNthObject(2),obj2); 180 } 181 if (isFifo()) { 182 assertEquals(getNthObject(0),obj2); 183 } 184 185 obj0 = _pool.borrowObject(); 186 if (isLifo()) { 187 assertEquals(getNthObject(0),obj0); 188 } 189 if (isFifo()) { 190 assertEquals(getNthObject(2),obj0); 191 } 192 } 193 194 public void testBaseNumActiveNumIdle() throws Exception { 195 try { 196 _pool = makeEmptyPool(3); 197 } catch(UnsupportedOperationException e) { 198 return; // skip this test if unsupported 199 } 200 assertEquals(0,_pool.getNumActive()); 201 assertEquals(0,_pool.getNumIdle()); 202 Object obj0 = _pool.borrowObject(); 203 assertEquals(1,_pool.getNumActive()); 204 assertEquals(0,_pool.getNumIdle()); 205 Object obj1 = _pool.borrowObject(); 206 assertEquals(2,_pool.getNumActive()); 207 assertEquals(0,_pool.getNumIdle()); 208 _pool.returnObject(obj1); 209 assertEquals(1,_pool.getNumActive()); 210 assertEquals(1,_pool.getNumIdle()); 211 _pool.returnObject(obj0); 212 assertEquals(0,_pool.getNumActive()); 213 assertEquals(2,_pool.getNumIdle()); 214 } 215 216 public void testBaseClear() throws Exception { 217 try { 218 _pool = makeEmptyPool(3); 219 } catch(UnsupportedOperationException e) { 220 return; // skip this test if unsupported 221 } 222 assertEquals(0,_pool.getNumActive()); 223 assertEquals(0,_pool.getNumIdle()); 224 Object obj0 = _pool.borrowObject(); 225 Object obj1 = _pool.borrowObject(); 226 assertEquals(2,_pool.getNumActive()); 227 assertEquals(0,_pool.getNumIdle()); 228 _pool.returnObject(obj1); 229 _pool.returnObject(obj0); 230 assertEquals(0,_pool.getNumActive()); 231 assertEquals(2,_pool.getNumIdle()); 232 _pool.clear(); 233 assertEquals(0,_pool.getNumActive()); 234 assertEquals(0,_pool.getNumIdle()); 235 Object obj2 = _pool.borrowObject(); 236 assertEquals(getNthObject(2),obj2); 237 } 238 239 public void testBaseInvalidateObject() throws Exception { 240 try { 241 _pool = makeEmptyPool(3); 242 } catch(UnsupportedOperationException e) { 243 return; // skip this test if unsupported 244 } 245 assertEquals(0,_pool.getNumActive()); 246 assertEquals(0,_pool.getNumIdle()); 247 Object obj0 = _pool.borrowObject(); 248 Object obj1 = _pool.borrowObject(); 249 assertEquals(2,_pool.getNumActive()); 250 assertEquals(0,_pool.getNumIdle()); 251 _pool.invalidateObject(obj0); 252 assertEquals(1,_pool.getNumActive()); 253 assertEquals(0,_pool.getNumIdle()); 254 _pool.invalidateObject(obj1); 255 assertEquals(0,_pool.getNumActive()); 256 assertEquals(0,_pool.getNumIdle()); 257 } 258 259 public void testBaseClosePool() throws Exception { 260 try { 261 _pool = makeEmptyPool(3); 262 } catch(UnsupportedOperationException e) { 263 return; // skip this test if unsupported 264 } 265 Object obj = _pool.borrowObject(); 266 _pool.returnObject(obj); 267 268 _pool.close(); 269 try { 270 _pool.borrowObject(); 271 fail("Expected IllegalStateException"); 272 } catch(IllegalStateException e) { 273 // expected 274 } 275 } 276 }