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.Serializable;
019: import java.util.Collections;
020: import java.util.Set;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
026: */
027: public class GBeanInfoFactoryTest extends TestCase {
028: /*
029: * void GBeanInfoBuilder(Class)
030: */
031: public void testGBeanInfoFactoryClass() {
032: assertNotNull(new GBeanInfoBuilder(String.class));
033: try {
034: new GBeanInfoBuilder(null);
035: fail("IllegalArgumentException expected");
036: } catch (IllegalArgumentException expected) {
037: }
038:
039: final Class className = String.class;
040: GBeanInfoBuilder gbeanInfoFactory = new GBeanInfoBuilder(
041: className);
042: assertEquals(className.getName(), gbeanInfoFactory
043: .getBeanInfo().getName());
044: assertEquals(className.getName(), gbeanInfoFactory
045: .getBeanInfo().getClassName());
046: }
047:
048: /*
049: * test for void GBeanInfoBuilder(Class, String)
050: */
051: public void testGBeanInfoFactoryClassString() {
052: try {
053: new GBeanInfoBuilder((Class) null, (String) null);
054: fail("IllegalArgumentException expected");
055: } catch (IllegalArgumentException expected) {
056: }
057: }
058:
059: /*
060: * test for void GBeanInfoBuilder(Class, GBeanInfo)
061: */
062: public void testGBeanInfoFactoryClassGBeanInfo() {
063: try {
064: new GBeanInfoBuilder((Class) null, (GBeanInfo) null);
065: fail("IllegalArgumentException expected");
066: } catch (IllegalArgumentException expected) {
067: }
068:
069: GBeanInfoBuilder gbeanInfoFactory = new GBeanInfoBuilder(
070: MockGBean.class, MockGBean.getGBeanInfo());
071: assertEquals(MockGBean.class.getName(), gbeanInfoFactory
072: .getBeanInfo().getName());
073: assertEquals(MockGBean.class.getName(), gbeanInfoFactory
074: .getBeanInfo().getClassName());
075: assertEquals(5, gbeanInfoFactory.getBeanInfo().getAttributes()
076: .size());
077: assertEquals(3, gbeanInfoFactory.getBeanInfo().getOperations()
078: .size());
079: }
080:
081: /*
082: * Class to test for void addInterface(Class)
083: */
084: public void testAddInterfaceClass() {
085: GBeanInfoBuilder gbeanInfoFactory;
086:
087: gbeanInfoFactory = new GBeanInfoBuilder(MockGBean.class);
088: gbeanInfoFactory.addInterface(Serializable.class);
089: assertEquals(3, gbeanInfoFactory.getBeanInfo().getAttributes()
090: .size());
091: assertEquals(3, gbeanInfoFactory.getBeanInfo().getOperations()
092: .size());
093:
094: gbeanInfoFactory = new GBeanInfoBuilder(MockGBean.class);
095: gbeanInfoFactory.addInterface(GBeanLifecycle.class);
096: GBeanInfo gbeanInfo = gbeanInfoFactory.getBeanInfo();
097: assertEquals(3, gbeanInfoFactory.getBeanInfo().getAttributes()
098: .size());
099: assertEquals(3, gbeanInfoFactory.getBeanInfo().getOperations()
100: .size());
101:
102: gbeanInfoFactory = new GBeanInfoBuilder(MockGBean.class);
103: gbeanInfoFactory.addInterface(SetterOnlyInterface.class);
104: gbeanInfo = gbeanInfoFactory.getBeanInfo();
105: assertEquals(3, gbeanInfo.getAttributes().size());
106: GAttributeInfo gattrInfo = gbeanInfo.getAttribute("int");
107: assertEquals("int", gattrInfo.getName());
108: assertEquals("setInt", gattrInfo.getSetterName());
109: assertEquals("getInt", gattrInfo.getGetterName());
110:
111: Set opsSet = gbeanInfo.getOperations();
112: assertEquals(3, opsSet.size());
113: }
114:
115: private static interface SetterOnlyInterface {
116: public void setInt(int i);
117: }
118:
119: private static interface GetterOnlyInterface {
120: public int getInt();
121: }
122:
123: final static GNotificationInfo notificationInfo = new GNotificationInfo(
124: "notification", Collections.singleton(null));
125:
126: public static final class MockGBean implements GBeanLifecycle,
127: SetterOnlyInterface, GetterOnlyInterface {
128:
129: public static final GBeanInfo GBEAN_INFO;
130:
131: static {
132: GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(
133: MockGBean.class);
134: infoFactory.setConstructor(new String[] { "foo", "bar" });
135: infoFactory.addAttribute("foo", String.class, false);
136: infoFactory.addAttribute("bar", String.class, false);
137: infoFactory.addReference("reference", String.class, null);
138: GBEAN_INFO = infoFactory.getBeanInfo();
139: }
140:
141: public static GBeanInfo getGBeanInfo() {
142: return GBEAN_INFO;
143: }
144:
145: public MockGBean() {
146: }
147:
148: public MockGBean(String foo, String bar) {
149: }
150:
151: public void setReference(String reference) {
152: }
153:
154: public void setInt(int i) {
155: }
156:
157: public int getInt() {
158: return 0;
159: }
160:
161: public void doStart() {
162: }
163:
164: public void doStop() {
165: }
166:
167: public void doFail() {
168: }
169: }
170:
171: }
|