KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > SimpleGBeanTest


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.kernel;
18
19 import junit.framework.TestCase;
20 import org.apache.geronimo.gbean.GBeanData;
21 import org.apache.geronimo.gbean.GBeanInfo;
22 import org.apache.geronimo.gbean.GBeanInfoBuilder;
23 import org.apache.geronimo.kernel.config.ConfigurationData;
24 import org.apache.geronimo.kernel.config.ConfigurationManager;
25 import org.apache.geronimo.kernel.config.ConfigurationUtil;
26 import org.apache.geronimo.kernel.config.KernelConfigurationManager;
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 SimpleGBeanTest extends TestCase {
33     public void test() throws Exception JavaDoc {
34         // boot the kernel
35
Kernel kernel = KernelFactory.newInstance().createKernel("test");
36         kernel.boot();
37
38         // load the configuration manager bootstrap service
39
ConfigurationData bootstrap = new ConfigurationData(new Artifact("bootstrap", "bootstrap", "", "car"), kernel.getNaming());
40         bootstrap.addGBean("ConfigurationManager", KernelConfigurationManager.GBEAN_INFO);
41         ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader());
42         ConfigurationManager configurationManager = (ConfigurationManager) kernel.getGBean(ConfigurationManager.class);
43
44         // create a configuration for our test bean
45
Artifact configurationId = new Artifact("test", "test", "", "car");
46         ConfigurationData configurationData = new ConfigurationData(configurationId, kernel.getNaming());
47         GBeanData mockBean1 = configurationData.addGBean("MyBean", TestGBean.getGBeanInfo());
48         mockBean1.setAttribute("value", "1234");
49
50         // load and start the configuration
51
configurationManager.loadConfiguration(configurationData);
52         configurationManager.startConfiguration(configurationId);
53
54         // invoke GBean directly
55
TestGBean testGBean = (TestGBean) kernel.getGBean("MyBean");
56         assertEquals("1234", testGBean.getValue());
57         assertEquals("1234", testGBean.fetchValue());
58
59         // invoke GBean by short name
60
assertEquals("1234", kernel.getAttribute("MyBean", "value"));
61         assertEquals("1234", kernel.invoke("MyBean", "fetchValue"));
62
63         // invoke GBean by type
64
assertEquals("1234", kernel.getAttribute(TestGBean.class, "value"));
65         assertEquals("1234", kernel.invoke(TestGBean.class, "fetchValue"));
66
67         // invoke GBean by name and type
68
assertEquals("1234", kernel.getAttribute("MyBean", TestGBean.class, "value"));
69         assertEquals("1234", kernel.invoke("MyBean", TestGBean.class, "fetchValue"));
70
71         // stop and unload configuration
72
configurationManager.stopConfiguration(configurationId);
73         configurationManager.unloadConfiguration(configurationId);
74
75         // stop the kernel
76
kernel.shutdown();
77     }
78
79     public static class TestGBean {
80         private final String JavaDoc value;
81
82         public TestGBean(String JavaDoc value) {
83             this.value = value;
84         }
85
86         public String JavaDoc getValue() {
87             return value;
88         }
89
90         public String JavaDoc fetchValue() {
91             return value;
92         }
93
94
95         private static final GBeanInfo GBEAN_INFO;
96         static {
97             GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(TestGBean.class);
98             infoBuilder.setConstructor(new String JavaDoc[] {"value"});
99             GBEAN_INFO = infoBuilder.getBeanInfo();
100         }
101         public static GBeanInfo getGBeanInfo() {
102             return GBEAN_INFO;
103         }
104     }
105 }
106
Popular Tags