1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
63
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
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
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
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
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 }