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.AbstractName;
020: import org.apache.geronimo.gbean.AbstractNameQuery;
021: import org.apache.geronimo.gbean.GBeanData;
022: import org.apache.geronimo.kernel.config.Configuration;
023: import org.apache.geronimo.kernel.config.ConfigurationData;
024: import org.apache.geronimo.kernel.config.ConfigurationUtil;
025: import org.apache.geronimo.kernel.config.EditableConfigurationManager;
026: import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
027: import org.apache.geronimo.kernel.management.State;
028: import org.apache.geronimo.kernel.repository.Artifact;
029: import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
030: import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
031:
032: /**
033: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
034: */
035: public class ConfigTest extends TestCase {
036: private Kernel kernel;
037: private AbstractName gbeanName1;
038: private AbstractName gbeanName2;
039: private ConfigurationData configurationData;
040: private EditableConfigurationManager configurationManager;
041:
042: // private final String BASE_NAME = "test:J2EEServer=geronimo";
043:
044: public void testConfigLifecycle() throws Exception {
045: Artifact configurationId = configurationData.getId();
046:
047: // load -- config should be running and gbean registered but not started
048: configurationManager.loadConfiguration(configurationData);
049: Configuration configuration = configurationManager
050: .getConfiguration(configurationId);
051: AbstractName configurationName = Configuration
052: .getConfigurationAbstractName(configurationId);
053:
054: assertEquals(State.RUNNING_INDEX, kernel
055: .getGBeanState(configurationName));
056: assertNotNull(configuration.getConfigurationClassLoader());
057:
058: assertFalse(kernel.isLoaded(gbeanName1));
059: assertFalse(kernel.isLoaded(gbeanName2));
060:
061: // start -- gbeans should now be started
062: configurationManager.startConfiguration(configurationId);
063:
064: assertTrue(kernel.isLoaded(gbeanName1));
065: assertTrue(kernel.isLoaded(gbeanName2));
066:
067: assertEquals(State.RUNNING_INDEX, kernel
068: .getGBeanState(gbeanName1));
069: assertEquals(State.RUNNING_INDEX, kernel
070: .getGBeanState(gbeanName2));
071:
072: assertEquals(new Integer(1), kernel.getAttribute(gbeanName1,
073: "finalInt"));
074: assertEquals("1234", kernel.getAttribute(gbeanName1, "value"));
075: assertEquals(new Integer(3), kernel.getAttribute(gbeanName2,
076: "finalInt"));
077:
078: kernel.setAttribute(gbeanName2, "mutableInt", new Integer(44));
079: assertEquals(new Integer(44), kernel.getAttribute(gbeanName2,
080: "mutableInt"));
081:
082: kernel.invoke(gbeanName2, "doSetMutableInt",
083: new Object[] { new Integer(55) },
084: new String[] { "int" });
085: assertEquals(new Integer(55), kernel.getAttribute(gbeanName2,
086: "mutableInt"));
087:
088: assertEquals("no endpoint", kernel.invoke(gbeanName1,
089: "checkEndpoint", null, null));
090: assertEquals("endpointCheck", kernel.invoke(gbeanName2,
091: "checkEndpoint", null, null));
092:
093: assertEquals(new Integer(0), kernel.invoke(gbeanName1,
094: "checkEndpointCollection", null, null));
095: assertEquals(new Integer(1), kernel.invoke(gbeanName2,
096: "checkEndpointCollection", null, null));
097:
098: kernel.setAttribute(gbeanName2, "endpointMutableInt",
099: new Integer(99));
100: assertEquals(new Integer(99), kernel.getAttribute(gbeanName2,
101: "endpointMutableInt"));
102: assertEquals(new Integer(99), kernel.getAttribute(gbeanName1,
103: "mutableInt"));
104:
105: // stop -- gbeans should now be started, but still registered
106: configurationManager.stopConfiguration(configurationId);
107:
108: assertFalse(kernel.isLoaded(gbeanName1));
109: assertFalse(kernel.isLoaded(gbeanName2));
110:
111: // unload -- configuration and gbeans should be unloaded
112: configurationManager.unloadConfiguration(configurationId);
113:
114: assertFalse(kernel.isLoaded(configurationName));
115: assertFalse(kernel.isLoaded(gbeanName1));
116: assertFalse(kernel.isLoaded(gbeanName2));
117:
118: }
119:
120: public void testConfigStartStopRestart() throws Exception {
121: Artifact configurationId = configurationData.getId();
122:
123: // load -- config should be running and gbean registered but not started
124: configurationManager.loadConfiguration(configurationData);
125: Configuration configuration = configurationManager
126: .getConfiguration(configurationId);
127: AbstractName configurationName = Configuration
128: .getConfigurationAbstractName(configurationId);
129:
130: assertEquals(State.RUNNING_INDEX, kernel
131: .getGBeanState(configurationName));
132: assertNotNull(configuration.getConfigurationClassLoader());
133:
134: assertFalse(kernel.isLoaded(gbeanName1));
135: assertFalse(kernel.isLoaded(gbeanName2));
136:
137: // start -- gbeans should now be started
138: configurationManager.startConfiguration(configurationId);
139:
140: assertTrue(kernel.isLoaded(gbeanName1));
141: assertTrue(kernel.isLoaded(gbeanName2));
142: assertEquals(State.RUNNING_INDEX, kernel
143: .getGBeanState(gbeanName1));
144: assertEquals(State.RUNNING_INDEX, kernel
145: .getGBeanState(gbeanName2));
146:
147: // stop -- gbeans should now be started, but still registered
148: configurationManager.stopConfiguration(configurationId);
149:
150: assertFalse(kernel.isLoaded(gbeanName1));
151: assertFalse(kernel.isLoaded(gbeanName2));
152:
153: // restart -- gbeans should now be started
154: configurationManager.startConfiguration(configurationId);
155:
156: assertTrue(kernel.isLoaded(gbeanName1));
157: assertTrue(kernel.isLoaded(gbeanName2));
158: assertEquals(State.RUNNING_INDEX, kernel
159: .getGBeanState(gbeanName1));
160: assertEquals(State.RUNNING_INDEX, kernel
161: .getGBeanState(gbeanName2));
162:
163: // unload -- configuration and gbeans should be unloaded
164: configurationManager.stopConfiguration(configurationId);
165: configurationManager.unloadConfiguration(configurationId);
166:
167: assertFalse(kernel.isLoaded(configurationName));
168: assertFalse(kernel.isLoaded(gbeanName1));
169: assertFalse(kernel.isLoaded(gbeanName2));
170:
171: }
172:
173: public void testAddToConfig() throws Exception {
174: Artifact configurationId = configurationData.getId();
175:
176: // load and start the config
177: configurationManager.loadConfiguration(configurationData);
178: Configuration configuration = configurationManager
179: .getConfiguration(configurationId);
180: assertNotNull(configuration.getConfigurationClassLoader());
181:
182: GBeanData mockBean3 = new GBeanData(MockGBean.getGBeanInfo());
183: try {
184: kernel.getGBeanState(mockBean3.getAbstractName());
185: fail("Gbean should not be found yet");
186: } catch (GBeanNotFoundException e) {
187: }
188: mockBean3.setAttribute("value", "1234");
189: mockBean3.setAttribute("name", "child");
190: mockBean3.setAttribute("finalInt", new Integer(1));
191: configurationManager.addGBeanToConfiguration(configurationId,
192: "MyMockGMBean3", mockBean3, true);
193:
194: assertEquals(State.RUNNING_INDEX, kernel
195: .getGBeanState(mockBean3.getAbstractName()));
196: assertEquals(new Integer(1), kernel.getAttribute(mockBean3
197: .getAbstractName(), "finalInt"));
198: assertEquals("1234", kernel.getAttribute(mockBean3
199: .getAbstractName(), "value"));
200: assertEquals("child", kernel.getAttribute(mockBean3
201: .getAbstractName(), "name"));
202: }
203:
204: protected void setUp() throws Exception {
205: super .setUp();
206: kernel = KernelFactory.newInstance().createKernel("test");
207: kernel.boot();
208:
209: ConfigurationData bootstrap = new ConfigurationData(
210: new Artifact("bootstrap", "bootstrap", "", "car"),
211: kernel.getNaming());
212:
213: GBeanData artifactManagerData = bootstrap.addGBean(
214: "ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
215:
216: GBeanData artifactResolverData = bootstrap.addGBean(
217: "ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
218: artifactResolverData.setReferencePattern("ArtifactManager",
219: artifactManagerData.getAbstractName());
220:
221: GBeanData configurationManagerData = bootstrap.addGBean(
222: "ConfigurationManager",
223: EditableKernelConfigurationManager.GBEAN_INFO);
224: configurationManagerData.setReferencePattern("ArtifactManager",
225: artifactManagerData.getAbstractName());
226: configurationManagerData.setReferencePattern(
227: "ArtifactResolver", artifactResolverData
228: .getAbstractName());
229:
230: ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap,
231: getClass().getClassLoader());
232:
233: configurationManager = ConfigurationUtil
234: .getEditableConfigurationManager(kernel);
235:
236: configurationData = new ConfigurationData(new Artifact("test",
237: "test", "", "car"), kernel.getNaming());
238:
239: GBeanData mockBean1 = configurationData.addGBean(
240: "MyMockGMBean1", MockGBean.getGBeanInfo());
241: gbeanName1 = mockBean1.getAbstractName();
242: mockBean1.setAttribute("value", "1234");
243: mockBean1.setAttribute("name", "child");
244: mockBean1.setAttribute("finalInt", new Integer(1));
245:
246: GBeanData mockBean2 = configurationData.addGBean(
247: "MyMockGMBean2", MockGBean.getGBeanInfo());
248: gbeanName2 = mockBean2.getAbstractName();
249: mockBean2.setAttribute("value", "5678");
250: mockBean2.setAttribute("name", "Parent");
251: mockBean2.setAttribute("finalInt", new Integer(3));
252: mockBean2.setReferencePattern("MockEndpoint", gbeanName1);
253: mockBean2.setReferencePattern("EndpointCollection",
254: new AbstractNameQuery(gbeanName1, MockGBean
255: .getGBeanInfo().getInterfaces()));
256: }
257:
258: protected void tearDown() throws Exception {
259: kernel.shutdown();
260: super.tearDown();
261: }
262: }
|