1   /*
2    * Copyright 1999-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.pool.impl;
18  
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  
22  import org.apache.commons.pool.ObjectPool;
23  import org.apache.commons.pool.PoolableObjectFactory;
24  import org.apache.commons.pool.TestObjectPool;
25  
26  import java.util.List;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  
30  /***
31   * @author Rodney Waldhoff
32   * @version $Revision: 383290 $ $Date: 2006-03-05 02:00:15 -0500 (Sun, 05 Mar 2006) $
33   */
34  public class TestSoftReferenceObjectPool extends TestObjectPool {
35      public TestSoftReferenceObjectPool(String testName) {
36          super(testName);
37      }
38  
39      public static Test suite() {
40          return new TestSuite(TestSoftReferenceObjectPool.class);
41      }
42  
43      protected ObjectPool makeEmptyPool(int cap) {
44          return new SoftReferenceObjectPool(
45              new PoolableObjectFactory()  {
46                  int counter = 0;
47                  public Object makeObject() { return String.valueOf(counter++); }
48                  public void destroyObject(Object obj) { }
49                  public boolean validateObject(Object obj) { return true; }
50                  public void activateObject(Object obj) { }
51                  public void passivateObject(Object obj) { }
52              }
53              );
54      }
55  
56      protected Object getNthObject(int n) {
57          return String.valueOf(n);
58      }
59  
60      private List testFactorySequenceStates = new ArrayList(5);
61      public void testFactorySequence() throws Exception {
62          // setup
63          // We need a factory that tracks method call sequence.
64          PoolableObjectFactory pof = new PoolableObjectFactory() {
65              public Object makeObject() throws Exception {
66                  testFactorySequenceStates.add("makeObject");
67                  return new Object();
68              }
69  
70              public void activateObject(Object obj) throws Exception {
71                  testFactorySequenceStates.add("activateObject");
72              }
73  
74              public boolean validateObject(Object obj) {
75                  testFactorySequenceStates.add("validateObject");
76                  return true;
77              }
78  
79              public void passivateObject(Object obj) throws Exception {
80                  testFactorySequenceStates.add("passivateObject");
81              }
82  
83              public void destroyObject(Object obj) throws Exception {
84                  testFactorySequenceStates.add("destroyObject");
85              }
86          };
87  
88          ObjectPool pool = new SoftReferenceObjectPool(pof);
89  
90          // check the order in which the factory is called during borrow
91          testFactorySequenceStates.clear();
92          Object o = pool.borrowObject();
93          List desiredSequence = Arrays.asList(new String[] {
94                  "makeObject",
95                  "activateObject",
96                  "validateObject"
97          });
98          assertEquals("Wrong sequence", desiredSequence, testFactorySequenceStates);
99  
100         // check the order in which the factory is called when returning an object
101         testFactorySequenceStates.clear();
102         pool.returnObject(o);
103         desiredSequence = Arrays.asList(new String[] {
104                 "validateObject",
105                 "passivateObject"
106         });
107         assertEquals("Wrong sequence", desiredSequence, testFactorySequenceStates);
108 
109         // check the order in which the factory is called during borrow again
110         testFactorySequenceStates.clear();
111         o = pool.borrowObject();
112         desiredSequence = Arrays.asList(new String[] {
113                 "activateObject",
114                 "validateObject"
115         });
116         assertEquals("Wrong sequence", desiredSequence, testFactorySequenceStates);
117 
118         // check the order in which the factory is called when invalidating an object
119         testFactorySequenceStates.clear();
120         pool.invalidateObject(o);
121         desiredSequence = Arrays.asList(new String[] {
122                 "destroyObject"
123         });
124         assertEquals("Wrong sequence", desiredSequence, testFactorySequenceStates);
125     }
126 
127     protected boolean isLifo() {
128         return false;
129     }
130 
131     protected boolean isFifo() {
132         return false;
133     }
134 
135 }