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.kernel;
017:
018: import junit.framework.TestCase;
019: import org.apache.geronimo.gbean.GBeanData;
020: import org.apache.geronimo.gbean.GBeanInfo;
021: import org.apache.geronimo.gbean.GBeanInfoBuilder;
022: import org.apache.geronimo.kernel.config.ConfigurationData;
023: import org.apache.geronimo.kernel.config.ConfigurationManager;
024: import org.apache.geronimo.kernel.config.ConfigurationUtil;
025: import org.apache.geronimo.kernel.config.KernelConfigurationManager;
026: import org.apache.geronimo.kernel.repository.Artifact;
027:
028: /**
029: * @version $Rev: 558235 $ $Date: 2007-07-20 20:47:45 -0700 (Fri, 20 Jul 2007) $
030: */
031: public class SimpleGBeanTest extends TestCase {
032: public void test() throws Exception {
033: // boot the kernel
034: Kernel kernel = KernelFactory.newInstance()
035: .createKernel("test");
036: kernel.boot();
037:
038: // load the configuration manager bootstrap service
039: ConfigurationData bootstrap = new ConfigurationData(
040: new Artifact("bootstrap", "bootstrap", "", "car"),
041: kernel.getNaming());
042: bootstrap.addGBean("ConfigurationManager",
043: KernelConfigurationManager.GBEAN_INFO);
044: ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap,
045: getClass().getClassLoader());
046: ConfigurationManager configurationManager = (ConfigurationManager) kernel
047: .getGBean(ConfigurationManager.class);
048:
049: // create a configuration for our test bean
050: Artifact configurationId = new Artifact("test", "test", "",
051: "car");
052: ConfigurationData configurationData = new ConfigurationData(
053: configurationId, kernel.getNaming());
054: GBeanData mockBean1 = configurationData.addGBean("MyBean",
055: TestGBean.getGBeanInfo());
056: mockBean1.setAttribute("value", "1234");
057:
058: // load and start the configuration
059: configurationManager.loadConfiguration(configurationData);
060: configurationManager.startConfiguration(configurationId);
061:
062: // invoke GBean directly
063: TestGBean testGBean = (TestGBean) kernel.getGBean("MyBean");
064: assertEquals("1234", testGBean.getValue());
065: assertEquals("1234", testGBean.fetchValue());
066:
067: // invoke GBean by short name
068: assertEquals("1234", kernel.getAttribute("MyBean", "value"));
069: assertEquals("1234", kernel.invoke("MyBean", "fetchValue"));
070: // this does not work without addOperation
071: assertEquals("1234", kernel.invoke("MyBean", "getValue"));
072:
073: // invoke GBean by type
074: assertEquals("1234", kernel.getAttribute(TestGBean.class,
075: "value"));
076: assertEquals("1234", kernel.invoke(TestGBean.class,
077: "fetchValue"));
078:
079: // invoke GBean by name and type
080: assertEquals("1234", kernel.getAttribute("MyBean",
081: TestGBean.class, "value"));
082: assertEquals("1234", kernel.invoke("MyBean", TestGBean.class,
083: "fetchValue"));
084:
085: // stop and unload configuration
086: configurationManager.stopConfiguration(configurationId);
087: configurationManager.unloadConfiguration(configurationId);
088:
089: // stop the kernel
090: kernel.shutdown();
091: }
092:
093: public static class TestGBean {
094: private final String value;
095:
096: public TestGBean(String value) {
097: this .value = value;
098: }
099:
100: public String getValue() {
101: return value;
102: }
103:
104: public String fetchValue() {
105: return value;
106: }
107:
108: private static final GBeanInfo GBEAN_INFO;
109: static {
110: GBeanInfoBuilder infoBuilder = GBeanInfoBuilder
111: .createStatic(TestGBean.class);
112: infoBuilder.setConstructor(new String[] { "value" });
113: // FIXME : The getters/setters should have been added as an operation
114: infoBuilder.addOperation("getValue");
115: GBEAN_INFO = infoBuilder.getBeanInfo();
116: }
117:
118: public static GBeanInfo getGBeanInfo() {
119: return GBEAN_INFO;
120: }
121: }
122: }
|