KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > MockGBean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.geronimo.gbean.GBeanInfo;
26 import org.apache.geronimo.gbean.GBeanInfoBuilder;
27
28 /**
29  * @version $Rev: 485321 $ $Date: 2006-12-10 19:14:46 -0500 (Sun, 10 Dec 2006) $
30  */

31 public class MockGBean implements MockEndpoint, MockParentInterface1, MockParentInterface2, MockChildInterface1, MockChildInterface2 {
32
33     private static final GBeanInfo GBEAN_INFO;
34
35     private String JavaDoc objectName;
36
37     private ClassLoader JavaDoc classLoader;
38
39     private Kernel kernel;
40
41     private final String JavaDoc name;
42
43     private final int finalInt;
44
45     private int mutableInt;
46
47     private int exceptionMutableInt;
48
49     private String JavaDoc value;
50
51     private Object JavaDoc someObject;
52
53     private MockEndpoint endpoint;
54
55     private Collection JavaDoc endpointCollection = Collections.EMPTY_SET;
56
57     public static GBeanInfo getGBeanInfo() {
58         return GBEAN_INFO;
59     }
60
61     static {
62         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(MockGBean.class);
63         infoFactory.addAttribute("name", String JavaDoc.class, true);
64         infoFactory.addAttribute("actualObjectName", String JavaDoc.class, false);
65         infoFactory.addAttribute("objectName", String JavaDoc.class, false);
66         infoFactory.addAttribute("actualClassLoader", ClassLoader JavaDoc.class, false);
67         infoFactory.addAttribute("classLoader", ClassLoader JavaDoc.class, false);
68         infoFactory.addAttribute("actualKernel", Kernel.class, false);
69         infoFactory.addAttribute("kernel", Kernel.class, false);
70         infoFactory.addAttribute("value", String JavaDoc.class, true);
71         infoFactory.addAttribute("finalInt", Integer.TYPE, true);
72         infoFactory.addAttribute("mutableInt", Integer.TYPE, false);
73         infoFactory.addAttribute("exceptionMutableInt", Integer.TYPE, true);
74         infoFactory.addAttribute("endpointMutableInt", Integer.TYPE, false);
75         infoFactory.addAttribute("someObject", Object JavaDoc.class, true);
76
77         infoFactory.addOperation("echo", new Class JavaDoc[]{String JavaDoc.class}, "java.lang.Object");
78         infoFactory.addOperation("checkEndpoint", "java.lang.Object");
79         infoFactory.addOperation("checkEndpointCollection", "java.lang.Object");
80         infoFactory.addOperation("doSomething", new Class JavaDoc[]{String JavaDoc.class}, "java.lang.Object");
81         infoFactory.addOperation("fetchValue", "java.lang.Object");
82
83         infoFactory.addInterface(MockEndpoint.class, new String JavaDoc[]{"mutableInt"});
84         infoFactory.addInterface(MockParentInterface1.class, new String JavaDoc[]{"value"});
85         infoFactory.addInterface(MockParentInterface2.class, new String JavaDoc[]{"value"});
86         infoFactory.addInterface(MockChildInterface1.class, new String JavaDoc[]{"finalInt"});
87         infoFactory.addInterface(MockChildInterface2.class, new String JavaDoc[]{});
88
89         infoFactory.addReference("MockEndpoint", MockEndpoint.class, null);
90         infoFactory.addReference("EndpointCollection", MockEndpoint.class, null);
91
92         infoFactory.setConstructor(new String JavaDoc[]{"name", "finalInt", "objectName", "classLoader", "kernel"});
93
94         GBEAN_INFO = infoFactory.getBeanInfo();
95     }
96
97     public MockGBean() { // present so we can test creating a class-based proxy
98
name = null;
99         finalInt = 0;
100     }
101
102     public MockGBean(String JavaDoc name, int finalInt) {
103         this.name = name;
104         this.finalInt = finalInt;
105     }
106
107     public MockGBean(String JavaDoc name, int finalInt, String JavaDoc objectName, ClassLoader JavaDoc classLoader, Kernel kernel) {
108         this.name = name;
109         this.finalInt = finalInt;
110         this.objectName = objectName;
111         this.classLoader = classLoader;
112         this.kernel = kernel;
113     }
114
115     public Object JavaDoc getSomeObject() {
116         return someObject;
117     }
118
119     public void setSomeObject(Object JavaDoc someObject) {
120         this.someObject = someObject;
121     }
122
123     public String JavaDoc getActualObjectName() {
124         return objectName;
125     }
126
127     public String JavaDoc getObjectName() {
128         return objectName;
129     }
130
131     public ClassLoader JavaDoc getActualClassLoader() {
132         return classLoader;
133     }
134
135     public ClassLoader JavaDoc getClassLoader() {
136         return classLoader;
137     }
138
139     public Kernel getActualKernel() {
140         return kernel;
141     }
142
143     public Kernel getKernel() {
144         return kernel;
145     }
146
147     public void setKernel(Kernel kernel) {
148         this.kernel = kernel;
149     }
150
151     public String JavaDoc getName() {
152         return name;
153     }
154
155     public void doNothing() {
156     }
157
158     public String JavaDoc echo(String JavaDoc msg) {
159         return msg;
160     }
161
162     public int getFinalInt() {
163         return finalInt;
164     }
165
166     /**
167      * Parameter ignored
168      */

169     public void setAnotherFinalInt(int ignored) {
170     }
171
172     /**
173      * Only setter for YetAnotherFinalInt
174      */

175     public void setYetAnotherFinalInt(int ignored) {
176     }
177
178     /**
179      * @see #setYetAnotherFinalInt(int)
180      */

181     public void setCharAsYetAnotherFinalInt(char yetAnotherFinalInt) {
182         setYetAnotherFinalInt((int) yetAnotherFinalInt);
183     }
184
185     /**
186      * @see #setYetAnotherFinalInt(int)
187      */

188     public void setBooleanAsYetAnotherFinalInt(boolean yetAnotherFinalInt) {
189         setYetAnotherFinalInt((yetAnotherFinalInt ? 1 : 0));
190     }
191
192     /**
193      * @see #setYetAnotherFinalInt(int)
194      */

195     public void setByteAsYetAnotherFinalInt(byte yetAnotherFinalInt) {
196         setYetAnotherFinalInt(yetAnotherFinalInt);
197     }
198
199     /**
200      * @see #setYetAnotherFinalInt(int)
201      */

202     public void setShortAsYetAnotherFinalInt(short yetAnotherFinalInt) {
203         setYetAnotherFinalInt(yetAnotherFinalInt);
204     }
205
206     /**
207      * @see #setYetAnotherFinalInt(int)
208      */

209     public void setLongAsYetAnotherFinalInt(long yetAnotherFinalInt) {
210         setYetAnotherFinalInt((int) yetAnotherFinalInt);
211     }
212
213     /**
214      * @see #setYetAnotherFinalInt(int)
215      */

216     public void setFloatAsYetAnotherFinalInt(float yetAnotherFinalInt) {
217         setYetAnotherFinalInt((int) yetAnotherFinalInt);
218     }
219
220     /**
221      * @see #setYetAnotherFinalInt(int)
222      */

223     public void setDoubleAsYetAnotherFinalInt(double yetAnotherFinalInt) {
224         setYetAnotherFinalInt((int) yetAnotherFinalInt);
225     }
226
227     /**
228      * Getter that returns nothing
229      */

230     public void getVoidGetterOfFinalInt() {
231     }
232
233     public int getMutableInt() {
234         return mutableInt;
235     }
236
237     public void doSetMutableInt(int mutableInt) {
238         setMutableInt(mutableInt);
239     }
240
241     public void setMutableInt(int mutableInt) {
242         this.mutableInt = mutableInt;
243     }
244
245     public String JavaDoc getValue() {
246         return value;
247     }
248
249     public String JavaDoc fetchValue() {
250         return value;
251     }
252
253     public void setValue(String JavaDoc value) {
254         this.value = value;
255     }
256
257     public void setExceptionMutableInt(int exceptionMutableInt) throws InvocationTargetException JavaDoc {
258         this.exceptionMutableInt = exceptionMutableInt;
259         if (exceptionMutableInt == -1) {
260             throw new InvocationTargetException JavaDoc(new Exception JavaDoc("Thrown when -1"));
261         }
262         if (exceptionMutableInt == -2) {
263             throw new InvocationTargetException JavaDoc(new Error JavaDoc("Thrown when -2"));
264         }
265         if (exceptionMutableInt == -3) {
266             throw new InvocationTargetException JavaDoc(new Throwable JavaDoc("Thrown when -3"));
267         }
268     }
269
270     public int getExceptionMutableInt() throws InvocationTargetException JavaDoc {
271         if (this.exceptionMutableInt == -1) {
272             throw new InvocationTargetException JavaDoc(new Exception JavaDoc("Thrown when -1"));
273         }
274         if (this.exceptionMutableInt == -2) {
275             throw new InvocationTargetException JavaDoc(new Error JavaDoc("Thrown when -2"));
276         }
277         if (exceptionMutableInt == -3) {
278             throw new InvocationTargetException JavaDoc(new Throwable JavaDoc("Thrown when -3"));
279         }
280         return this.exceptionMutableInt;
281     }
282
283     public MockEndpoint getMockEndpoint() {
284         return endpoint;
285     }
286
287     public void setMockEndpoint(MockEndpoint endpoint) {
288         this.endpoint = endpoint;
289     }
290
291     public Collection JavaDoc getEndpointCollection() {
292         return endpointCollection;
293     }
294
295     public void setEndpointCollection(Collection JavaDoc endpointCollection) {
296         this.endpointCollection = endpointCollection;
297     }
298
299     public String JavaDoc doSomething(String JavaDoc name) {
300         return name;
301     }
302
303     public String JavaDoc endpointDoSomething(String JavaDoc name) {
304         return name;
305     }
306
307     public String JavaDoc checkEndpoint() {
308         if (endpoint == null) {
309             return "no endpoint";
310         }
311         return endpoint.endpointDoSomething("endpointCheck");
312     }
313
314     public int checkEndpointCollection() {
315         int successCount = 0;
316         for (Iterator JavaDoc iterator = endpointCollection.iterator(); iterator.hasNext();) {
317             MockEndpoint mockEndpoint = (MockEndpoint) iterator.next();
318             String JavaDoc result = mockEndpoint.endpointDoSomething("endpointCheck");
319             if ("endpointCheck".equals(result)) {
320                 successCount++;
321             }
322         }
323         return successCount;
324     }
325
326     public int getEndpointMutableInt() {
327         return endpoint.getMutableInt();
328     }
329
330     public void setEndpointMutableInt(int mutableInt) {
331         endpoint.setMutableInt(mutableInt);
332     }
333 }
334
Popular Tags