001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.gbean;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.FileInputStream;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.apache.geronimo.testsupport.TestSupport;
027:
028: /**
029: * @version $Rev: 558235 $ $Date: 2007-07-20 20:47:45 -0700 (Fri, 20 Jul 2007) $
030: */
031: public class GBeanInfoTest extends TestSupport {
032: private static final String CONSTRUCTOR_ARG_0 = "ConstructorArg_0";
033: private static final String CONSTRUCTOR_ARG_1 = "ConstructorArg_1";
034:
035: public void testGetGBeanInfo() {
036: // 1. Test GBean that exists
037: GBeanInfo gbeanInfo = GBeanInfo.getGBeanInfo(MockGBean.class
038: .getName(), MockGBean.class.getClassLoader());
039: assertNotNull(gbeanInfo);
040:
041: // 2. Test GBean that doesn't exist
042: try {
043: GBeanInfo.getGBeanInfo("ClassThatDoesNotExist", this
044: .getClass().getClassLoader());
045: fail("InvalidConfigurationException expected");
046: } catch (InvalidConfigurationException expected) {
047: }
048:
049: // 3. Test GBean that exist, but doesn't declare a getGBeanInfo()
050: // method
051: try {
052: GBeanInfo.getGBeanInfo(String.class.getName(), this
053: .getClass().getClassLoader());
054: fail("InvalidConfigurationException expected");
055: } catch (InvalidConfigurationException expected) {
056: }
057: }
058:
059: public void testGetName() {
060: assertEquals(MockGBean.class.getName(), gbeanInfo.getName());
061: }
062:
063: public void testGetClassName() {
064: assertEquals(MockGBean.class.getName(), gbeanInfo
065: .getClassName());
066: }
067:
068: public void testGetAttributeSet() {
069: Set attrSet = gbeanInfo.getAttributes();
070: assertEquals(6, attrSet.size());
071: assertTrue(attrSet.contains(persistentAttrInfo));
072: assertTrue(attrSet.contains(nonPersistentAttrInfo));
073: }
074:
075: public void testGetPersistentAttributes() {
076: List attrList = gbeanInfo.getPersistentAttributes();
077: assertEquals(3, attrList.size());
078: }
079:
080: public void testGetConstructor() {
081: GConstructorInfo gctorInfo = gbeanInfo.getConstructor();
082: List attrNameList = gctorInfo.getAttributeNames();
083: assertEquals(2, attrNameList.size());
084: assertEquals(CONSTRUCTOR_ARG_0, attrNameList.get(0));
085: assertEquals(CONSTRUCTOR_ARG_1, attrNameList.get(1));
086: }
087:
088: public void testGetOperationsSet() {
089: Set gbeanOpSet = gbeanInfo.getOperations();
090: assertEquals(3, gbeanOpSet.size());
091: assertTrue(gbeanOpSet.contains(opInfo));
092: }
093:
094: public void testGetReferencesSet() {
095: Set gbeanRefSet = gbeanInfo.getReferences();
096: assertEquals(1, gbeanRefSet.size());
097: GReferenceInfo newRefInfo = (GReferenceInfo) gbeanRefSet
098: .iterator().next();
099: assertEquals(refInfo.getName(), newRefInfo.getName());
100: }
101:
102: public void testToString() {
103: assertNotNull(gbeanInfo.toString());
104: assertEquals(gbeanInfo.toString(), MockGBean.getGBeanInfo()
105: .toString());
106: }
107:
108: public void testBackwardCompatibility() throws Exception {
109: FileInputStream fis = new FileInputStream(
110: resolveFile("src/test/data/gbeaninfo/SERIALIZATION_-6198804067155550221.ser"));
111: ObjectInputStream is = new ObjectInputStream(fis);
112: GBeanInfo beanInfo = (GBeanInfo) is.readObject();
113: assertEquals(GBeanInfo.PRIORITY_NORMAL, beanInfo.getPriority());
114: }
115:
116: public void testCurrentSerialization() throws Exception {
117: GBeanInfo beanInfo = MockGBean.GBEAN_INFO;
118:
119: ByteArrayOutputStream memOut = new ByteArrayOutputStream();
120: ObjectOutputStream os = new ObjectOutputStream(memOut);
121: os.writeObject(beanInfo);
122:
123: ByteArrayInputStream memIn = new ByteArrayInputStream(memOut
124: .toByteArray());
125: ObjectInputStream is = new ObjectInputStream(memIn);
126: beanInfo = (GBeanInfo) is.readObject();
127: assertEquals(GBeanInfo.PRIORITY_CLASSLOADER, beanInfo
128: .getPriority());
129: }
130:
131: GBeanInfo gbeanInfo;
132:
133: final static String nonPersistentAttrName = "nonPersistentAttribute";
134:
135: final static GAttributeInfo nonPersistentAttrInfo = new GAttributeInfo(
136: nonPersistentAttrName, String.class.getName(), false,
137: false, "getFoo", "setFoo");
138:
139: final static String persistentAttrName = "persistentAttribute";
140:
141: final static GAttributeInfo persistentAttrInfo = new GAttributeInfo(
142: persistentAttrName, String.class.getName(), true, false,
143: "getFoo", "setFoo");
144:
145: final static GOperationInfo opInfo = new GOperationInfo(
146: "operation", "java.lang.Object");
147:
148: final static GReferenceInfo refInfo = new GReferenceInfo(
149: "reference", String.class.getName(),
150: String.class.getName(), "setReference", "Fooifier");
151:
152: public void setUp() throws Exception {
153: super .setUp();
154: gbeanInfo = MockGBean.getGBeanInfo();
155: }
156:
157: protected void tearDown() throws Exception {
158: gbeanInfo = null;
159: super .tearDown();
160: }
161:
162: public static final class MockGBean {
163: public static final GBeanInfo GBEAN_INFO;
164:
165: static {
166: GBeanInfoBuilder infoFactory = GBeanInfoBuilder
167: .createStatic(MockGBean.class);
168:
169: infoFactory.addAttribute(nonPersistentAttrInfo);
170: infoFactory.addAttribute(persistentAttrInfo);
171:
172: infoFactory.addOperation(opInfo);
173: // These two operations are added automatically
174: infoFactory.addOperation("addSomething",
175: new Class[] { String.class }); // ignored
176: infoFactory.addOperation("removeSomething",
177: new Class[] { String.class }); // ignored
178: infoFactory.addReference(refInfo);
179:
180: infoFactory.addAttribute(CONSTRUCTOR_ARG_0, String.class,
181: true);
182: infoFactory.addAttribute(CONSTRUCTOR_ARG_1, String.class,
183: true);
184: infoFactory.setPriority(GBeanInfo.PRIORITY_CLASSLOADER);
185: infoFactory.setConstructor(new String[] {
186: CONSTRUCTOR_ARG_0, CONSTRUCTOR_ARG_1 });
187:
188: GBEAN_INFO = infoFactory.getBeanInfo();
189: }
190:
191: public static GBeanInfo getGBeanInfo() {
192: return GBEAN_INFO;
193: }
194:
195: public MockGBean(String ConstructorArg_0,
196: String ConstructorArg_1) {
197: }
198:
199: public void setReference(String reference) {
200: }
201:
202: public void addSomething(String something) {
203: }
204:
205: public String removeSomething(String something) {
206: return null;
207: }
208: }
209: }
|