1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.pool2;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.pool2.impl.DefaultPooledObject;
24
25
26
27
28
29
30 public class MethodCallPoolableObjectFactory implements PooledObjectFactory<Object> {
31 private final List<MethodCall> methodCalls = new ArrayList<>();
32 private int count;
33 private boolean valid = true;
34 private boolean makeObjectFail;
35 private boolean activateObjectFail;
36 private boolean validateObjectFail;
37 private boolean passivateObjectFail;
38 private boolean destroyObjectFail;
39
40 @Override
41 public void activateObject(final PooledObject<Object> obj) {
42 methodCalls.add(new MethodCall("activateObject", obj.getObject()));
43 if (activateObjectFail) {
44 throw new PrivateException("activateObject");
45 }
46 }
47
48 @Override
49 public void destroyObject(final PooledObject<Object> obj) {
50 methodCalls.add(new MethodCall("destroyObject", obj.getObject()));
51 if (destroyObjectFail) {
52 throw new PrivateException("destroyObject");
53 }
54 }
55
56 public int getCurrentCount() {
57 return count;
58 }
59
60 public List<MethodCall> getMethodCalls() {
61 return methodCalls;
62 }
63
64 public boolean isActivateObjectFail() {
65 return activateObjectFail;
66 }
67
68 public boolean isDestroyObjectFail() {
69 return destroyObjectFail;
70 }
71
72 public boolean isMakeObjectFail() {
73 return makeObjectFail;
74 }
75
76 public boolean isPassivateObjectFail() {
77 return passivateObjectFail;
78 }
79
80 public boolean isValid() {
81 return valid;
82 }
83
84 public boolean isValidateObjectFail() {
85 return validateObjectFail;
86 }
87
88 @Override
89 public PooledObject<Object> makeObject() {
90 final MethodCall call = new MethodCall("makeObject");
91 methodCalls.add(call);
92 final int originalCount = this.count++;
93 if (makeObjectFail) {
94 throw new PrivateException("makeObject");
95 }
96
97 final Integer obj = Integer.valueOf(originalCount);
98 call.setReturned(obj);
99 return new DefaultPooledObject<>(obj);
100 }
101
102 @Override
103 public void passivateObject(final PooledObject<Object> obj) {
104 methodCalls.add(new MethodCall("passivateObject", obj.getObject()));
105 if (passivateObjectFail) {
106 throw new PrivateException("passivateObject");
107 }
108 }
109
110 public void reset() {
111 count = 0;
112 getMethodCalls().clear();
113 setMakeObjectFail(false);
114 setActivateObjectFail(false);
115 setValid(true);
116 setValidateObjectFail(false);
117 setPassivateObjectFail(false);
118 setDestroyObjectFail(false);
119 }
120
121 public void setActivateObjectFail(final boolean activateObjectFail) {
122 this.activateObjectFail = activateObjectFail;
123 }
124
125 public void setCurrentCount(final int count) {
126 this.count = count;
127 }
128
129 public void setDestroyObjectFail(final boolean destroyObjectFail) {
130 this.destroyObjectFail = destroyObjectFail;
131 }
132
133 public void setMakeObjectFail(final boolean makeObjectFail) {
134 this.makeObjectFail = makeObjectFail;
135 }
136
137 public void setPassivateObjectFail(final boolean passivateObjectFail) {
138 this.passivateObjectFail = passivateObjectFail;
139 }
140
141 public void setValid(final boolean valid) {
142 this.valid = valid;
143 }
144
145 public void setValidateObjectFail(final boolean validateObjectFail) {
146 this.validateObjectFail = validateObjectFail;
147 }
148
149 @Override
150 public boolean validateObject(final PooledObject<Object> obj) {
151 final MethodCall call = new MethodCall("validateObject", obj.getObject());
152 methodCalls.add(call);
153 if (validateObjectFail) {
154 throw new PrivateException("validateObject");
155 }
156 final boolean r = valid;
157 call.returned(Boolean.valueOf(r));
158 return r;
159 }
160 }