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