1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.kernel;
19
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Iterator;
24
25 import org.apache.geronimo.gbean.GBeanInfo;
26 import org.apache.geronimo.gbean.GBeanInfoBuilder;
27 import org.apache.geronimo.gbean.GBeanLifecycleController;
28
29 /**
30 * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
31 */
32 public class MockGBean implements MockEndpoint {
33
34 private static final GBeanInfo GBEAN_INFO;
35
36 private String objectName;
37
38 private ClassLoader classLoader;
39
40 private Kernel kernel;
41
42 private final String name;
43
44 private final int finalInt;
45
46 private int mutableInt;
47
48 private int exceptionMutableInt;
49
50 private String value;
51
52 private MockEndpoint endpoint;
53
54 private Collection endpointCollection = Collections.EMPTY_SET;
55 private GBeanLifecycleController gbeanLifecycleController;
56
57 public static GBeanInfo getGBeanInfo() {
58 return GBEAN_INFO;
59 }
60
61 static {
62 GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("MockGBean", MockGBean.class);
63 infoFactory.addAttribute("name", String.class, true);
64 infoFactory.addAttribute("actualObjectName", String.class, false);
65 infoFactory.addAttribute("objectName", String.class, false);
66 infoFactory.addAttribute("gbeanLifecycleController", GBeanLifecycleController.class, false);
67 infoFactory.addAttribute("actualClassLoader", ClassLoader.class, false);
68 infoFactory.addAttribute("classLoader", ClassLoader.class, false);
69 infoFactory.addAttribute("actualKernel", Kernel.class, false);
70 infoFactory.addAttribute("kernel", Kernel.class, false);
71 infoFactory.addAttribute("value", String.class, true);
72 infoFactory.addAttribute("finalInt", Integer.TYPE, true);
73 infoFactory.addAttribute("mutableInt", Integer.TYPE, false);
74 infoFactory.addAttribute("exceptionMutableInt", Integer.TYPE, true);
75 infoFactory.addAttribute("endpointMutableInt", Integer.TYPE, false);
76
77 infoFactory.addOperation("echo", new Class[]{String.class});
78 infoFactory.addOperation("checkEndpoint");
79 infoFactory.addOperation("checkEndpointCollection");
80 infoFactory.addOperation("doSomething", new Class[]{String.class});
81
82 infoFactory.addInterface(MockEndpoint.class, new String[]{"mutableInt"});
83
84 infoFactory.addReference("MockEndpoint", MockEndpoint.class, null);
85 infoFactory.addReference("EndpointCollection", MockEndpoint.class, null);
86
87 infoFactory.setConstructor(new String[]{"name", "finalInt", "objectName", "classLoader", "gbeanLifecycleController", "kernel"});
88
89 GBEAN_INFO = infoFactory.getBeanInfo();
90 }
91
92 public MockGBean(String name, int finalInt) {
93 this.name = name;
94 this.finalInt = finalInt;
95 }
96
97 public MockGBean(String name, int finalInt, String objectName, ClassLoader classLoader, GBeanLifecycleController gbeanLifecycleController, Kernel kernel) {
98 this.name = name;
99 this.finalInt = finalInt;
100 this.objectName = objectName;
101 this.classLoader = classLoader;
102 this.gbeanLifecycleController = gbeanLifecycleController;
103 this.kernel = kernel;
104 }
105
106 public String getActualObjectName() {
107 return objectName;
108 }
109
110 public String getObjectName() {
111 return objectName;
112 }
113
114 public ClassLoader getActualClassLoader() {
115 return classLoader;
116 }
117
118 public ClassLoader getClassLoader() {
119 return classLoader;
120 }
121
122 public GBeanLifecycleController getGbeanLifecycleController() {
123 return gbeanLifecycleController;
124 }
125
126 public Kernel getActualKernel() {
127 return kernel;
128 }
129
130 public Kernel getKernel() {
131 return kernel;
132 }
133
134 public void setKernel(Kernel kernel) {
135 this.kernel = kernel;
136 }
137
138 public String getName() {
139 return name;
140 }
141
142 public void doNothing() {
143 }
144
145 public String echo(String msg) {
146 return msg;
147 }
148
149 public int getFinalInt() {
150 return finalInt;
151 }
152
153 /**
154 * Parameter ignored
155 */
156 public void setAnotherFinalInt(int ignored) {
157 }
158
159 /**
160 * Only setter for YetAnotherFinalInt
161 */
162 public void setYetAnotherFinalInt(int ignored) {
163 }
164
165 /**
166 * @see #setYetAnotherFinalInt(int)
167 */
168 public void setCharAsYetAnotherFinalInt(char yetAnotherFinalInt) {
169 setYetAnotherFinalInt((int) yetAnotherFinalInt);
170 }
171
172 /**
173 * @see #setYetAnotherFinalInt(int)
174 */
175 public void setBooleanAsYetAnotherFinalInt(boolean yetAnotherFinalInt) {
176 setYetAnotherFinalInt((yetAnotherFinalInt ? 1 : 0));
177 }
178
179 /**
180 * @see #setYetAnotherFinalInt(int)
181 */
182 public void setByteAsYetAnotherFinalInt(byte yetAnotherFinalInt) {
183 setYetAnotherFinalInt(yetAnotherFinalInt);
184 }
185
186 /**
187 * @see #setYetAnotherFinalInt(int)
188 */
189 public void setShortAsYetAnotherFinalInt(short yetAnotherFinalInt) {
190 setYetAnotherFinalInt(yetAnotherFinalInt);
191 }
192
193 /**
194 * @see #setYetAnotherFinalInt(int)
195 */
196 public void setLongAsYetAnotherFinalInt(long yetAnotherFinalInt) {
197 setYetAnotherFinalInt((int) yetAnotherFinalInt);
198 }
199
200 /**
201 * @see #setYetAnotherFinalInt(int)
202 */
203 public void setFloatAsYetAnotherFinalInt(float yetAnotherFinalInt) {
204 setYetAnotherFinalInt((int) yetAnotherFinalInt);
205 }
206
207 /**
208 * @see #setYetAnotherFinalInt(int)
209 */
210 public void setDoubleAsYetAnotherFinalInt(double yetAnotherFinalInt) {
211 setYetAnotherFinalInt((int) yetAnotherFinalInt);
212 }
213
214 /**
215 * Getter that returns nothing
216 */
217 public void getVoidGetterOfFinalInt() {
218 }
219
220 public int getMutableInt() {
221 return mutableInt;
222 }
223
224 public void doSetMutableInt(int mutableInt) {
225 setMutableInt(mutableInt);
226 }
227
228 public void setMutableInt(int mutableInt) {
229 this.mutableInt = mutableInt;
230 }
231
232 public String getValue() {
233 return value;
234 }
235
236 public void setValue(String value) {
237 this.value = value;
238 }
239
240 public void setExceptionMutableInt(int exceptionMutableInt) throws InvocationTargetException {
241 this.exceptionMutableInt = exceptionMutableInt;
242 if (exceptionMutableInt == -1) {
243 throw new InvocationTargetException(new Exception("Thrown when -1"));
244 }
245 if (exceptionMutableInt == -2) {
246 throw new InvocationTargetException(new Error("Thrown when -2"));
247 }
248 if (exceptionMutableInt == -3) {
249 throw new InvocationTargetException(new Throwable("Thrown when -3"));
250 }
251 }
252
253 public int getExceptionMutableInt() throws InvocationTargetException {
254 if (this.exceptionMutableInt == -1) {
255 throw new InvocationTargetException(new Exception("Thrown when -1"));
256 }
257 if (this.exceptionMutableInt == -2) {
258 throw new InvocationTargetException(new Error("Thrown when -2"));
259 }
260 if (exceptionMutableInt == -3) {
261 throw new InvocationTargetException(new Throwable("Thrown when -3"));
262 }
263 return this.exceptionMutableInt;
264 }
265
266 public MockEndpoint getMockEndpoint() {
267 return endpoint;
268 }
269
270 public void setMockEndpoint(MockEndpoint endpoint) {
271 this.endpoint = endpoint;
272 }
273
274 public Collection getEndpointCollection() {
275 return endpointCollection;
276 }
277
278 public void setEndpointCollection(Collection endpointCollection) {
279 this.endpointCollection = endpointCollection;
280 }
281
282 public String doSomething(String name) {
283 return name;
284 }
285
286 public String endpointDoSomething(String name) {
287 return name;
288 }
289
290 public String checkEndpoint() {
291 if (endpoint == null) {
292 return "no endpoint";
293 }
294 return endpoint.endpointDoSomething("endpointCheck");
295 }
296
297 public int checkEndpointCollection() {
298 int successCount = 0;
299 for (Iterator iterator = endpointCollection.iterator(); iterator.hasNext();) {
300 MockEndpoint mockEndpoint = (MockEndpoint) iterator.next();
301 String result = mockEndpoint.endpointDoSomething("endpointCheck");
302 if ("endpointCheck".equals(result)) {
303 successCount++;
304 }
305 }
306 return successCount;
307 }
308
309 public int getEndpointMutableInt() {
310 return endpoint.getMutableInt();
311 }
312
313 public void setEndpointMutableInt(int mutableInt) {
314 endpoint.setMutableInt(mutableInt);
315 }
316 }