001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.pool.impl;
018:
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: import org.apache.commons.pool.PoolableObjectFactory;
023:
024: import java.util.Arrays;
025: import java.util.HashMap;
026:
027: /**
028: * @author Dirk Verbeeck
029: * @version $Revision: 155430 $ $Date: 2005-02-26 08:13:28 -0500 (Sat, 26 Feb 2005) $
030: */
031: public class TestSoftRefOutOfMemory extends TestCase {
032: private SoftReferenceObjectPool pool;
033:
034: public TestSoftRefOutOfMemory(String testName) {
035: super (testName);
036: }
037:
038: public static TestSuite suite() {
039: return new TestSuite(TestSoftRefOutOfMemory.class);
040: }
041:
042: public void tearDown() throws Exception {
043: if (pool != null) {
044: pool.close();
045: pool = null;
046: }
047: System.gc();
048: }
049:
050: public void testOutOfMemory() throws Exception {
051: pool = new SoftReferenceObjectPool(
052: new SmallPoolableObjectFactory());
053:
054: Object obj = pool.borrowObject();
055: assertEquals("1", obj);
056: pool.returnObject(obj);
057: obj = null;
058:
059: assertEquals(1, pool.getNumIdle());
060:
061: try {
062: HashMap map = new HashMap();
063:
064: for (int i = 0; i < 1000000; i++) {
065: map.put(new Integer(i), new String("Fred Flintstone"
066: + i));
067: }
068: } catch (OutOfMemoryError ex) {
069:
070: }
071: obj = pool.borrowObject();
072: assertEquals("2", obj);
073: pool.returnObject(obj);
074: obj = null;
075:
076: assertEquals(1, pool.getNumIdle());
077: }
078:
079: public void testOutOfMemory1000() throws Exception {
080: pool = new SoftReferenceObjectPool(
081: new SmallPoolableObjectFactory());
082:
083: for (int i = 0; i < 1000; i++) {
084: pool.addObject();
085: }
086:
087: Object obj = pool.borrowObject();
088: assertEquals("1000", obj);
089: pool.returnObject(obj);
090: obj = null;
091:
092: assertEquals(1000, pool.getNumIdle());
093:
094: try {
095: HashMap map = new HashMap();
096:
097: for (int i = 0; i < 1000000; i++) {
098: map.put(new Integer(i), new String("Fred Flintstone"
099: + i));
100: }
101: } catch (OutOfMemoryError ex) {
102: }
103:
104: obj = pool.borrowObject();
105: assertEquals("1001", obj);
106: pool.returnObject(obj);
107: obj = null;
108:
109: assertEquals(1, pool.getNumIdle());
110: }
111:
112: public void testOutOfMemoryLarge() throws Exception {
113: pool = new SoftReferenceObjectPool(
114: new LargePoolableObjectFactory(1000000));
115:
116: Object obj = pool.borrowObject();
117: assertTrue(((String) obj).startsWith("1."));
118: pool.returnObject(obj);
119: obj = null;
120:
121: assertEquals(1, pool.getNumIdle());
122:
123: try {
124: HashMap map = new HashMap();
125:
126: for (int i = 0; i < 1000000; i++) {
127: map.put(new Integer(i), new String("Fred Flintstone"
128: + i));
129: }
130: } catch (OutOfMemoryError ex) {
131: }
132:
133: obj = pool.borrowObject();
134: assertTrue(((String) obj).startsWith("2."));
135: pool.returnObject(obj);
136: obj = null;
137:
138: assertEquals(1, pool.getNumIdle());
139: }
140:
141: public void testOutOfMemoryKeepMap() throws Exception {
142: pool = new SoftReferenceObjectPool(
143: new LargePoolableObjectFactory(1000000));
144:
145: Object obj = pool.borrowObject();
146: assertTrue(((String) obj).startsWith("1."));
147: pool.returnObject(obj);
148: obj = null;
149:
150: assertEquals(1, pool.getNumIdle());
151:
152: // allocate map outside try/catch block
153: HashMap map = new HashMap();
154: try {
155: for (int i = 0; i < 1000000; i++) {
156: map.put(new Integer(i), new String("Fred Flintstone"
157: + i));
158: }
159: } catch (OutOfMemoryError ex) {
160: }
161:
162: try {
163: obj = pool.borrowObject();
164: fail("Expected out of memory");
165: } catch (OutOfMemoryError ex) {
166: }
167: }
168:
169: public static class SmallPoolableObjectFactory implements
170: PoolableObjectFactory {
171: private int counter = 0;
172:
173: public Object makeObject() {
174: counter++;
175: return String.valueOf(counter);
176: }
177:
178: public boolean validateObject(Object obj) {
179: return true;
180: }
181:
182: public void activateObject(Object obj) {
183: }
184:
185: public void passivateObject(Object obj) {
186: }
187:
188: public void destroyObject(Object obj) {
189: }
190: }
191:
192: public static class LargePoolableObjectFactory implements
193: PoolableObjectFactory {
194: private String buffer;
195: private int counter = 0;
196:
197: public LargePoolableObjectFactory(int size) {
198: char[] data = new char[size];
199: Arrays.fill(data, '.');
200: buffer = new String(data);
201: }
202:
203: public Object makeObject() {
204: counter++;
205: return String.valueOf(counter) + buffer;
206: }
207:
208: public boolean validateObject(Object obj) {
209: return true;
210: }
211:
212: public void activateObject(Object obj) {
213: }
214:
215: public void passivateObject(Object obj) {
216: }
217:
218: public void destroyObject(Object obj) {
219: }
220: }
221: }
|