KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > runtime > GBeanAttributeTest


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 package org.apache.geronimo.gbean.runtime;
18
19 import junit.framework.TestCase;
20 import org.apache.geronimo.gbean.AbstractName;
21 import org.apache.geronimo.gbean.GAttributeInfo;
22 import org.apache.geronimo.gbean.GBeanData;
23 import org.apache.geronimo.gbean.InvalidConfigurationException;
24 import org.apache.geronimo.kernel.Kernel;
25 import org.apache.geronimo.kernel.KernelFactory;
26 import org.apache.geronimo.kernel.MockGBean;
27 import org.apache.geronimo.kernel.repository.Artifact;
28
29 /**
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class GBeanAttributeTest extends TestCase {
33
34     private static final String JavaDoc attributeName = "Name";
35
36     private static final String JavaDoc persistentPrimitiveAttributeName = "MutableInt";
37
38     /**
39      * Wraps GBean
40      */

41     private GBeanInstance gbeanInstance = null;
42
43     private MethodInvoker getInvoker = null;
44
45     private MethodInvoker setInvoker = null;
46
47     private GAttributeInfo persistentPrimitiveAttributeInfo = null;
48     private GAttributeInfo attributeInfo = null;
49     private Kernel kernel;
50
51     public final void testGBeanAttributStringClassMethodInvokerMethodInvoker() {
52         try {
53             GBeanAttribute.createFrameworkAttribute(null, null, null, null);
54             fail("IllegalArgumentException expected");
55         } catch (IllegalArgumentException JavaDoc expected) {
56         }
57
58         GBeanAttribute attribute;
59         attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String JavaDoc.class, getInvoker);
60         assertEquals(String JavaDoc.class, attribute.getType());
61         assertEquals(attributeName, attribute.getName());
62         assertTrue(attribute.isReadable());
63         assertFalse(attribute.isWritable());
64         assertFalse(attribute.isPersistent());
65         attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String JavaDoc.class, null, setInvoker, false, null, false);
66         assertEquals(String JavaDoc.class, attribute.getType());
67         assertEquals(attributeName, attribute.getName());
68         assertFalse(attribute.isReadable());
69         assertTrue(attribute.isWritable());
70         assertFalse(attribute.isPersistent());
71         attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String JavaDoc.class, getInvoker, setInvoker, false, null, false);
72         assertEquals(String JavaDoc.class, attribute.getType());
73         assertEquals(attributeName, attribute.getName());
74         assertTrue(attribute.isReadable());
75         assertTrue(attribute.isWritable());
76         assertFalse(attribute.isPersistent());
77     }
78
79     public final void testGBeanAttributeInfoClass() {
80         try {
81             new GBeanAttribute(null, null, false);
82             fail("IllegalArgumentException expected");
83         } catch (IllegalArgumentException JavaDoc expected) {
84         }
85
86         // 2. @todo BUG An attribute must be readable, writable, or persistent
87
// GBeanAttribute ctor doesn't check if readable/writable are
88
// null's
89
try {
90             new GBeanAttribute(gbeanInstance, attributeInfo, false);
91             // till Dain sorts out the question of ctor
92
// fail("InvalidConfigurationException expected");
93
} catch (InvalidConfigurationException expected) {
94         }
95
96         try {
97             GAttributeInfo invalidAttributeInfo = new GAttributeInfo(attributeName, String JavaDoc.class.getName(), false, false, null, null);
98
99             new GBeanAttribute(gbeanInstance, invalidAttributeInfo, false);
100             fail("InvalidConfigurationException expected");
101         } catch (InvalidConfigurationException expected) {
102         }
103
104         {
105             final GAttributeInfo attributeInfo = new GAttributeInfo(attributeName, String JavaDoc.class.getName(), false, false, true, false, null, null);
106             GBeanAttribute attribute = new GBeanAttribute(gbeanInstance, attributeInfo, false);
107             assertTrue(attribute.isReadable());
108             assertFalse(attribute.isWritable());
109         }
110
111         {
112             final GAttributeInfo attributeInfo = new GAttributeInfo(persistentPrimitiveAttributeName, int.class.getName(), false, false, false, true, null, null);
113             GBeanAttribute attribute = new GBeanAttribute(gbeanInstance, attributeInfo, false);
114             assertFalse(attribute.isReadable());
115             assertTrue(attribute.isWritable());
116         }
117
118         {
119             // the attribute name and getter name are different, yet both
120
// exist.
121
// getYetAnotherFinalInt doesn't exist
122
final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, "getFinalInt", null);
123             GBeanAttribute attribute = new GBeanAttribute(gbeanInstance, attributeInfo, false);
124             assertNotNull(attribute);
125         }
126
127         {
128             final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setCharAsYetAnotherFinalInt");
129             try {
130                 new GBeanAttribute(gbeanInstance, attributeInfo, false);
131                 fail("Expected InvalidConfigurationException due to invalid setter parameter type");
132             } catch (InvalidConfigurationException expected) {
133             }
134         }
135
136         {
137             final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setBooleanAsYetAnotherFinalInt");
138             try {
139                 new GBeanAttribute(gbeanInstance, attributeInfo, false);
140                 fail("Expected InvalidConfigurationException due to invalid setter parameter type");
141             } catch (InvalidConfigurationException expected) {
142             }
143         }
144
145         {
146             final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setByteAsYetAnotherFinalInt");
147             try {
148                 new GBeanAttribute(gbeanInstance, attributeInfo, false);
149                 fail("Expected InvalidConfigurationException due to invalid setter parameter type");
150             } catch (InvalidConfigurationException expected) {
151             }
152         }
153
154         {
155             final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setShortAsYetAnotherFinalInt");
156             try {
157                 new GBeanAttribute(gbeanInstance, attributeInfo, false);
158                 fail("Expected InvalidConfigurationException due to invalid setter parameter type");
159             } catch (InvalidConfigurationException expected) {
160             }
161         }
162
163         {
164             final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setLongAsYetAnotherFinalInt");
165             try {
166                 new GBeanAttribute(gbeanInstance, attributeInfo, false);
167                 fail("Expected InvalidConfigurationException due to invalid setter parameter type");
168             } catch (InvalidConfigurationException expected) {
169             }
170         }
171
172         {
173             final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setFloatAsYetAnotherFinalInt");
174             try {
175                 new GBeanAttribute(gbeanInstance, attributeInfo, false);
176                 fail("Expected InvalidConfigurationException due to invalid setter parameter type");
177             } catch (InvalidConfigurationException expected) {
178             }
179         }
180
181         {
182             final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setDoubleAsYetAnotherFinalInt");
183             try {
184                 new GBeanAttribute(gbeanInstance, attributeInfo, false);
185                 fail("Expected InvalidConfigurationException due to invalid setter parameter type");
186             } catch (InvalidConfigurationException expected) {
187             }
188         }
189
190         {
191             final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, "getVoidGetterOfFinalInt", null);
192             try {
193                 new GBeanAttribute(gbeanInstance, attributeInfo, false);
194                 fail("Getter method not found on target; InvalidConfigurationException expected");
195             } catch (InvalidConfigurationException expected) {
196             }
197         }
198
199         {
200             final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, false, null, "setThatDoesntExist");
201             try {
202                 new GBeanAttribute(gbeanInstance, attributeInfo, false);
203                 fail("Setter method not found on target; InvalidConfigurationException expected");
204             } catch (InvalidConfigurationException expected) {
205             }
206         }
207     }
208
209     public final void testGetValue() throws Exception JavaDoc {
210         {
211             // attribute that isn't readable and persistent
212
final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String JavaDoc.class, null, setInvoker, false, null, false);
213             try {
214                 attribute.getValue(gbeanInstance);
215                 fail("Only persistent attributes can be accessed while offline; exception expected");
216             } catch (/* IllegalState */Exception JavaDoc expected) {
217             }
218         }
219
220         {
221             final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String JavaDoc.class, null, setInvoker, false, null, false);
222
223             try {
224                 gbeanInstance.start();
225
226                 attribute.getValue(gbeanInstance);
227                 fail("This attribute is not readable; exception expected");
228             } catch (/* IllegalArgument */Throwable JavaDoc expected) {
229             } finally {
230                 gbeanInstance.stop();
231             }
232         }
233     }
234
235     public final void testSetValue() throws Exception JavaDoc {
236
237         // 1. (offline) attribute that isn't readable and persistent
238
{
239             final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String JavaDoc.class, null, setInvoker, false, null, false);
240             try {
241                 attribute.setValue(gbeanInstance, null);
242                 fail("Only persistent attributes can be modified while offline; exception expected");
243             } catch (/* IllegalState */Exception JavaDoc expected) {
244             }
245         }
246
247         // 2. (offline) attribute that is of primitive type, writable and
248
// persistent, but not readable
249
{
250             final GBeanAttribute persistentAttribute = new GBeanAttribute(gbeanInstance, persistentPrimitiveAttributeInfo, false);
251             try {
252                 persistentAttribute.setValue(gbeanInstance, null);
253                 fail("Cannot assign null to a primitive attribute; exception expected");
254             } catch (/* IllegalArgument */Exception JavaDoc expected) {
255             }
256         }
257
258         // 3. (online) attribute that is immutable and not persistent
259
{
260             final GBeanAttribute immutableAttribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String JavaDoc.class, getInvoker);
261
262             try {
263                 gbeanInstance.start();
264
265                 immutableAttribute.setValue(gbeanInstance, null);
266                 fail("This attribute is not writable; exception expected");
267             } catch (/* IllegalArgument */Exception JavaDoc expected) {
268             } finally {
269                 gbeanInstance.stop();
270             }
271         }
272
273         // 4. (online) attribute that is mutable and of primitive type
274
{
275             final GBeanAttribute mutablePersistentAttribute = new GBeanAttribute(gbeanInstance, persistentPrimitiveAttributeInfo, false);
276
277             try {
278                 gbeanInstance.start();
279
280                 mutablePersistentAttribute.setValue(gbeanInstance, null);
281                 fail("Cannot assign null to a primitive attribute; exception expected");
282             } catch (/* IllegalArgument */Exception JavaDoc expected) {
283             } finally {
284                 gbeanInstance.stop();
285             }
286         }
287
288         // 4a. @todo BUG: It's possible to set a value to a persistent
289
// attribute while online; IllegalStateException expected
290
{
291             final GBeanAttribute mutablePersistentAttribute = new GBeanAttribute(gbeanInstance, persistentPrimitiveAttributeInfo, false);
292
293             try {
294                 gbeanInstance.start();
295
296                 mutablePersistentAttribute.setValue(gbeanInstance, new Integer JavaDoc(4));
297                 //fail("Cannot assign a value to a persistent attribute while
298
// online; exception expected");
299
} catch (/* IllegalState */Exception JavaDoc expected) {
300             } finally {
301                 gbeanInstance.stop();
302             }
303         }
304
305         // 5. Invoke setValue so that exception is thrown
306
{
307             final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance,
308                     attributeName,
309                     int.class,
310                     null,
311                     setInvoker,
312                     false,
313                     null,
314                     false);
315
316             try {
317                 gbeanInstance.start();
318
319                 attribute.setValue(gbeanInstance, new Integer JavaDoc(4));
320                 fail("Exception expected upon setValue's call");
321             } catch (/* IllegalState */Exception JavaDoc expected) {
322             } finally {
323                 gbeanInstance.stop();
324             }
325         }
326     }
327
328     protected void setUp() throws Exception JavaDoc {
329         super.setUp();
330         kernel = KernelFactory.newInstance().createKernel("test");
331         kernel.boot();
332
333         AbstractName name = kernel.getNaming().createRootName(new Artifact("test", "foo", "1", "car"), "test", "test");
334         gbeanInstance = new GBeanInstance(new GBeanData(name, MockGBean.getGBeanInfo()),
335                 kernel,
336                 kernel.getDependencyManager(),
337                 new MyLifecycleBroadcaster(),
338                 MockGBean.class.getClassLoader());
339
340         getInvoker = new MethodInvoker() {
341             public Object JavaDoc invoke(Object JavaDoc target, Object JavaDoc[] arguments) throws Exception JavaDoc {
342                 throw new UnsupportedOperationException JavaDoc("Throws exception to rise test coverage");
343             }
344         };
345
346         setInvoker = new MethodInvoker() {
347             public Object JavaDoc invoke(Object JavaDoc target, Object JavaDoc[] arguments) throws Exception JavaDoc {
348                 throw new UnsupportedOperationException JavaDoc("Throws exception to rise test coverage");
349             }
350         };
351
352         attributeInfo = new GAttributeInfo(attributeName, String JavaDoc.class.getName(), false, false, "getName", "setName");
353         persistentPrimitiveAttributeInfo = new GAttributeInfo(persistentPrimitiveAttributeName, int.class.getName(), true, false, "getMutableInt", "setMutableInt");
354     }
355
356     protected void tearDown() throws Exception JavaDoc {
357         kernel.shutdown();
358         gbeanInstance = null;
359         super.tearDown();
360     }
361
362     private static class MyLifecycleBroadcaster implements LifecycleBroadcaster {
363         public void fireLoadedEvent() {
364         }
365
366         public void fireStartingEvent() {
367         }
368
369         public void fireRunningEvent() {
370         }
371
372         public void fireStoppingEvent() {
373         }
374
375         public void fireStoppedEvent() {
376         }
377
378         public void fireFailedEvent() {
379         }
380
381         public void fireUnloadedEvent() {
382         }
383     }
384 }
385
Popular Tags