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.Test;
020: import junit.framework.TestSuite;
021:
022: import org.apache.commons.pool.ObjectPool;
023: import org.apache.commons.pool.PoolableObjectFactory;
024: import org.apache.commons.pool.TestObjectPool;
025:
026: import java.util.List;
027: import java.util.ArrayList;
028: import java.util.Arrays;
029:
030: /**
031: * @author Rodney Waldhoff
032: * @version $Revision: 383290 $ $Date: 2006-03-05 02:00:15 -0500 (Sun, 05 Mar 2006) $
033: */
034: public class TestSoftReferenceObjectPool extends TestObjectPool {
035: public TestSoftReferenceObjectPool(String testName) {
036: super (testName);
037: }
038:
039: public static Test suite() {
040: return new TestSuite(TestSoftReferenceObjectPool.class);
041: }
042:
043: protected ObjectPool makeEmptyPool(int cap) {
044: return new SoftReferenceObjectPool(new PoolableObjectFactory() {
045: int counter = 0;
046:
047: public Object makeObject() {
048: return String.valueOf(counter++);
049: }
050:
051: public void destroyObject(Object obj) {
052: }
053:
054: public boolean validateObject(Object obj) {
055: return true;
056: }
057:
058: public void activateObject(Object obj) {
059: }
060:
061: public void passivateObject(Object obj) {
062: }
063: });
064: }
065:
066: protected Object getNthObject(int n) {
067: return String.valueOf(n);
068: }
069:
070: private List testFactorySequenceStates = new ArrayList(5);
071:
072: public void testFactorySequence() throws Exception {
073: // setup
074: // We need a factory that tracks method call sequence.
075: PoolableObjectFactory pof = new PoolableObjectFactory() {
076: public Object makeObject() throws Exception {
077: testFactorySequenceStates.add("makeObject");
078: return new Object();
079: }
080:
081: public void activateObject(Object obj) throws Exception {
082: testFactorySequenceStates.add("activateObject");
083: }
084:
085: public boolean validateObject(Object obj) {
086: testFactorySequenceStates.add("validateObject");
087: return true;
088: }
089:
090: public void passivateObject(Object obj) throws Exception {
091: testFactorySequenceStates.add("passivateObject");
092: }
093:
094: public void destroyObject(Object obj) throws Exception {
095: testFactorySequenceStates.add("destroyObject");
096: }
097: };
098:
099: ObjectPool pool = new SoftReferenceObjectPool(pof);
100:
101: // check the order in which the factory is called during borrow
102: testFactorySequenceStates.clear();
103: Object o = pool.borrowObject();
104: List desiredSequence = Arrays.asList(new String[] {
105: "makeObject", "activateObject", "validateObject" });
106: assertEquals("Wrong sequence", desiredSequence,
107: testFactorySequenceStates);
108:
109: // check the order in which the factory is called when returning an object
110: testFactorySequenceStates.clear();
111: pool.returnObject(o);
112: desiredSequence = Arrays.asList(new String[] {
113: "validateObject", "passivateObject" });
114: assertEquals("Wrong sequence", desiredSequence,
115: testFactorySequenceStates);
116:
117: // check the order in which the factory is called during borrow again
118: testFactorySequenceStates.clear();
119: o = pool.borrowObject();
120: desiredSequence = Arrays.asList(new String[] {
121: "activateObject", "validateObject" });
122: assertEquals("Wrong sequence", desiredSequence,
123: testFactorySequenceStates);
124:
125: // check the order in which the factory is called when invalidating an object
126: testFactorySequenceStates.clear();
127: pool.invalidateObject(o);
128: desiredSequence = Arrays
129: .asList(new String[] { "destroyObject" });
130: assertEquals("Wrong sequence", desiredSequence,
131: testFactorySequenceStates);
132: }
133:
134: protected boolean isLifo() {
135: return false;
136: }
137:
138: protected boolean isFifo() {
139: return false;
140: }
141:
142: }
|