KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.geronimo.testsupport.TestSupport;
29
30 /**
31  * @version $Rev: 485321 $ $Date: 2006-12-10 19:14:46 -0500 (Sun, 10 Dec 2006) $
32  */

33 public class GBeanInfoTest extends TestSupport {
34     private static final String JavaDoc CONSTRUCTOR_ARG_0 = "ConstructorArg_0";
35     private static final String JavaDoc CONSTRUCTOR_ARG_1 = "ConstructorArg_1";
36
37     public void testGetGBeanInfo() {
38         // 1. Test GBean that exists
39
GBeanInfo gbeanInfo = GBeanInfo.getGBeanInfo(MockGBean.class.getName(), MockGBean.class.getClassLoader());
40         assertNotNull(gbeanInfo);
41
42         // 2. Test GBean that doesn't exist
43
try {
44             GBeanInfo.getGBeanInfo("ClassThatDoesNotExist", this.getClass().getClassLoader());
45             fail("InvalidConfigurationException expected");
46         } catch (InvalidConfigurationException expected) {
47         }
48
49         // 3. Test GBean that exist, but doesn't declare a getGBeanInfo()
50
// method
51
try {
52             GBeanInfo.getGBeanInfo(String JavaDoc.class.getName(), this.getClass().getClassLoader());
53             fail("InvalidConfigurationException expected");
54         } catch (InvalidConfigurationException expected) {
55         }
56     }
57
58     public void testGetName() {
59         assertEquals(MockGBean.class.getName(), gbeanInfo.getName());
60     }
61
62     public void testGetClassName() {
63         assertEquals(MockGBean.class.getName(), gbeanInfo.getClassName());
64     }
65
66     public void testGetAttributeSet() {
67         Set JavaDoc attrSet = gbeanInfo.getAttributes();
68         assertEquals(6, attrSet.size());
69         assertTrue(attrSet.contains(persistentAttrInfo));
70         assertTrue(attrSet.contains(nonPersistentAttrInfo));
71     }
72
73     public void testGetPersistentAttributes() {
74         List JavaDoc attrList = gbeanInfo.getPersistentAttributes();
75         assertEquals(3, attrList.size());
76     }
77
78     public void testGetConstructor() {
79         GConstructorInfo gctorInfo = gbeanInfo.getConstructor();
80         List JavaDoc attrNameList = gctorInfo.getAttributeNames();
81         assertEquals(2, attrNameList.size());
82         assertEquals(CONSTRUCTOR_ARG_0, attrNameList.get(0));
83         assertEquals(CONSTRUCTOR_ARG_1, attrNameList.get(1));
84     }
85
86     public void testGetOperationsSet() {
87         Set JavaDoc gbeanOpSet = gbeanInfo.getOperations();
88         assertEquals(1, gbeanOpSet.size());
89         assertTrue(gbeanOpSet.contains(opInfo));
90     }
91
92     public void testGetReferencesSet() {
93         Set JavaDoc gbeanRefSet = gbeanInfo.getReferences();
94         assertEquals(1, gbeanRefSet.size());
95         GReferenceInfo newRefInfo = (GReferenceInfo) gbeanRefSet.iterator().next();
96         assertEquals(refInfo.getName(), newRefInfo.getName());
97     }
98
99     public void testToString() {
100         assertNotNull(gbeanInfo.toString());
101         assertEquals(gbeanInfo.toString(), MockGBean.getGBeanInfo().toString());
102     }
103
104     public void xtestBackwardCompatibility() throws Exception JavaDoc {
105         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(resolveFile("src/test/data/gbeaninfo/SERIALIZATION_-6198804067155550221.ser"));
106         ObjectInputStream JavaDoc is = new ObjectInputStream JavaDoc(fis);
107         GBeanInfo beanInfo = (GBeanInfo) is.readObject();
108         assertEquals(GBeanInfo.PRIORITY_NORMAL, beanInfo.getPriority());
109     }
110
111     public void testCurrentSerialization() throws Exception JavaDoc {
112         GBeanInfo beanInfo = MockGBean.GBEAN_INFO;
113
114         ByteArrayOutputStream JavaDoc memOut = new ByteArrayOutputStream JavaDoc();
115         ObjectOutputStream JavaDoc os = new ObjectOutputStream JavaDoc(memOut);
116         os.writeObject(beanInfo);
117
118         ByteArrayInputStream JavaDoc memIn = new ByteArrayInputStream JavaDoc(memOut.toByteArray());
119         ObjectInputStream JavaDoc is = new ObjectInputStream JavaDoc(memIn);
120         beanInfo = (GBeanInfo) is.readObject();
121         assertEquals(GBeanInfo.PRIORITY_CLASSLOADER, beanInfo.getPriority());
122     }
123     
124     GBeanInfo gbeanInfo;
125
126     final static String JavaDoc nonPersistentAttrName = "nonPersistentAttribute";
127
128     final static GAttributeInfo nonPersistentAttrInfo = new GAttributeInfo(nonPersistentAttrName, String JavaDoc.class.getName(), false, false, "getFoo", "setFoo");
129
130     final static String JavaDoc persistentAttrName = "persistentAttribute";
131
132     final static GAttributeInfo persistentAttrInfo = new GAttributeInfo(persistentAttrName, String JavaDoc.class.getName(), true, false, "getFoo", "setFoo");
133
134     final static GOperationInfo opInfo = new GOperationInfo("operation", "java.lang.Object");
135
136     final static GReferenceInfo refInfo = new GReferenceInfo("reference", String JavaDoc.class.getName(), String JavaDoc.class.getName(), "setReference", "Fooifier");
137
138     public void setUp() throws Exception JavaDoc {
139         super.setUp();
140         gbeanInfo = MockGBean.getGBeanInfo();
141     }
142
143     protected void tearDown() throws Exception JavaDoc {
144         gbeanInfo = null;
145         super.tearDown();
146     }
147
148     public static final class MockGBean {
149         public static final GBeanInfo GBEAN_INFO;
150
151         static {
152             GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(MockGBean.class);
153
154             infoFactory.addAttribute(nonPersistentAttrInfo);
155             infoFactory.addAttribute(persistentAttrInfo);
156
157             infoFactory.addOperation(opInfo);
158
159             infoFactory.addReference(refInfo);
160
161             infoFactory.addAttribute(CONSTRUCTOR_ARG_0, String JavaDoc.class, true);
162             infoFactory.addAttribute(CONSTRUCTOR_ARG_1, String JavaDoc.class, true);
163             infoFactory.setPriority(GBeanInfo.PRIORITY_CLASSLOADER);
164             infoFactory.setConstructor(new String JavaDoc[]{CONSTRUCTOR_ARG_0, CONSTRUCTOR_ARG_1});
165
166
167             GBEAN_INFO = infoFactory.getBeanInfo();
168         }
169
170         public static GBeanInfo getGBeanInfo() {
171             return GBEAN_INFO;
172         }
173
174         public MockGBean(String JavaDoc ConstructorArg_0, String JavaDoc ConstructorArg_1) {
175         }
176
177         public void setReference(String JavaDoc reference) {
178         }
179     }
180 }
181
Popular Tags