KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > GBeanInfoFactoryTest


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.gbean;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import junit.framework.TestCase;
25
26 /**
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
28  */

29 public class GBeanInfoFactoryTest extends TestCase {
30     /*
31      * void GBeanInfoBuilder(Class)
32      */

33     public void testGBeanInfoFactoryClass() {
34         assertNotNull(new GBeanInfoBuilder(String JavaDoc.class));
35         try {
36             new GBeanInfoBuilder(null);
37             fail("IllegalArgumentException expected");
38         } catch (IllegalArgumentException JavaDoc expected) {
39         }
40
41         final Class JavaDoc className = String JavaDoc.class;
42         GBeanInfoBuilder gbeanInfoFactory = new GBeanInfoBuilder(className);
43         assertEquals(className.getName(), gbeanInfoFactory.getBeanInfo().getName());
44         assertEquals(className.getName(), gbeanInfoFactory.getBeanInfo().getClassName());
45     }
46
47     /*
48      * test for void GBeanInfoBuilder(Class, String)
49      */

50     public void testGBeanInfoFactoryClassString() {
51         try {
52             new GBeanInfoBuilder((Class JavaDoc) null, (String JavaDoc) null);
53             fail("IllegalArgumentException expected");
54         } catch (IllegalArgumentException JavaDoc expected) {
55         }
56     }
57
58     /*
59      * test for void GBeanInfoBuilder(Class, GBeanInfo)
60      */

61     public void testGBeanInfoFactoryClassGBeanInfo() {
62         try {
63             new GBeanInfoBuilder((Class JavaDoc) null, (GBeanInfo) null);
64             fail("IllegalArgumentException expected");
65         } catch (IllegalArgumentException JavaDoc expected) {
66         }
67
68         GBeanInfoBuilder gbeanInfoFactory = new GBeanInfoBuilder(MockGBean.class, MockGBean.getGBeanInfo());
69         assertEquals(MockGBean.class.getName(), gbeanInfoFactory.getBeanInfo().getName());
70         assertEquals(MockGBean.class.getName(), gbeanInfoFactory.getBeanInfo().getClassName());
71         assertEquals(5, gbeanInfoFactory.getBeanInfo().getAttributes().size());
72         assertEquals(3, gbeanInfoFactory.getBeanInfo().getOperations().size());
73     }
74
75     /*
76      * Class to test for void addInterface(Class)
77      */

78     public void testAddInterfaceClass() {
79         GBeanInfoBuilder gbeanInfoFactory;
80
81         gbeanInfoFactory = new GBeanInfoBuilder(MockGBean.class);
82         gbeanInfoFactory.addInterface(Serializable JavaDoc.class);
83         assertEquals(3, gbeanInfoFactory.getBeanInfo().getAttributes().size());
84         assertEquals(3, gbeanInfoFactory.getBeanInfo().getOperations().size());
85
86         gbeanInfoFactory = new GBeanInfoBuilder(MockGBean.class);
87         gbeanInfoFactory.addInterface(GBeanLifecycle.class);
88         GBeanInfo gbeanInfo = gbeanInfoFactory.getBeanInfo();
89         assertEquals(3, gbeanInfoFactory.getBeanInfo().getAttributes().size());
90         assertEquals(3, gbeanInfoFactory.getBeanInfo().getOperations().size());
91
92         gbeanInfoFactory = new GBeanInfoBuilder(MockGBean.class);
93         gbeanInfoFactory.addInterface(SetterOnlyInterface.class);
94         gbeanInfo = gbeanInfoFactory.getBeanInfo();
95         assertEquals(3, gbeanInfo.getAttributes().size());
96         GAttributeInfo gattrInfo = gbeanInfo.getAttribute("int");
97         assertEquals("int", gattrInfo.getName());
98         assertEquals("setInt", gattrInfo.getSetterName());
99         assertEquals("getInt", gattrInfo.getGetterName());
100
101         Set JavaDoc opsSet = gbeanInfo.getOperations();
102         assertEquals(3, opsSet.size());
103     }
104
105     private static interface SetterOnlyInterface {
106         public void setInt(int i);
107     }
108
109     private static interface GetterOnlyInterface {
110         public int getInt();
111     }
112
113     final static GNotificationInfo notificationInfo = new GNotificationInfo("notification", Collections.singleton(null));
114
115     public static final class MockGBean implements GBeanLifecycle, SetterOnlyInterface, GetterOnlyInterface {
116
117         public static final GBeanInfo GBEAN_INFO;
118
119         static {
120             GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(MockGBean.class);
121             infoFactory.setConstructor(new String JavaDoc[]{"foo", "bar"});
122             infoFactory.addAttribute("foo", String JavaDoc.class, false);
123             infoFactory.addAttribute("bar", String JavaDoc.class, false);
124             infoFactory.addReference("reference", String JavaDoc.class, null);
125             GBEAN_INFO = infoFactory.getBeanInfo();
126         }
127
128         public static GBeanInfo getGBeanInfo() {
129             return GBEAN_INFO;
130         }
131
132         public MockGBean() {
133         }
134         
135         public MockGBean(String JavaDoc foo, String JavaDoc bar) {
136         }
137
138         public void setReference(String JavaDoc reference) {
139         }
140
141         public void setInt(int i) {
142         }
143
144         public int getInt() {
145             return 0;
146         }
147
148         public void doStart() {
149         }
150
151         public void doStop() {
152         }
153
154         public void doFail() {
155         }
156     }
157
158 }
159
Popular Tags