KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 package org.apache.geronimo.kernel;
19
20 import junit.framework.TestCase;
21 import org.apache.geronimo.gbean.AbstractName;
22 import org.apache.geronimo.gbean.AbstractNameQuery;
23 import org.apache.geronimo.gbean.GBeanData;
24 import org.apache.geronimo.kernel.config.Configuration;
25 import org.apache.geronimo.kernel.config.ConfigurationData;
26 import org.apache.geronimo.kernel.config.ConfigurationUtil;
27 import org.apache.geronimo.kernel.config.EditableConfigurationManager;
28 import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
29 import org.apache.geronimo.kernel.management.State;
30 import org.apache.geronimo.kernel.repository.Artifact;
31 import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
32 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class ConfigTest extends TestCase {
38     private Kernel kernel;
39     private AbstractName gbeanName1;
40     private AbstractName gbeanName2;
41     private ConfigurationData configurationData;
42     private EditableConfigurationManager configurationManager;
43 // private final String BASE_NAME = "test:J2EEServer=geronimo";
44

45     public void testConfigLifecycle() throws Exception JavaDoc {
46         Artifact configurationId = configurationData.getId();
47
48         // load -- config should be running and gbean registered but not started
49
configurationManager.loadConfiguration(configurationData);
50         Configuration configuration = configurationManager.getConfiguration(configurationId);
51         AbstractName configurationName = Configuration.getConfigurationAbstractName(configurationId);
52
53         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(configurationName));
54         assertNotNull(configuration.getConfigurationClassLoader());
55
56         assertFalse(kernel.isLoaded(gbeanName1));
57         assertFalse(kernel.isLoaded(gbeanName2));
58
59         // start -- gbeans should now be started
60
configurationManager.startConfiguration(configurationId);
61
62         assertTrue(kernel.isLoaded(gbeanName1));
63         assertTrue(kernel.isLoaded(gbeanName2));
64
65         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbeanName1));
66         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbeanName2));
67
68         assertEquals(new Integer JavaDoc(1), kernel.getAttribute(gbeanName1, "finalInt"));
69         assertEquals("1234", kernel.getAttribute(gbeanName1, "value"));
70         assertEquals(new Integer JavaDoc(3), kernel.getAttribute(gbeanName2, "finalInt"));
71
72         kernel.setAttribute(gbeanName2, "mutableInt", new Integer JavaDoc(44));
73         assertEquals(new Integer JavaDoc(44), kernel.getAttribute(gbeanName2, "mutableInt"));
74
75         kernel.invoke(gbeanName2, "doSetMutableInt", new Object JavaDoc[]{new Integer JavaDoc(55)}, new String JavaDoc[]{"int"});
76         assertEquals(new Integer JavaDoc(55), kernel.getAttribute(gbeanName2, "mutableInt"));
77
78         assertEquals("no endpoint", kernel.invoke(gbeanName1, "checkEndpoint", null, null));
79         assertEquals("endpointCheck", kernel.invoke(gbeanName2, "checkEndpoint", null, null));
80
81         assertEquals(new Integer JavaDoc(0), kernel.invoke(gbeanName1, "checkEndpointCollection", null, null));
82         assertEquals(new Integer JavaDoc(1), kernel.invoke(gbeanName2, "checkEndpointCollection", null, null));
83
84         kernel.setAttribute(gbeanName2, "endpointMutableInt", new Integer JavaDoc(99));
85         assertEquals(new Integer JavaDoc(99), kernel.getAttribute(gbeanName2, "endpointMutableInt"));
86         assertEquals(new Integer JavaDoc(99), kernel.getAttribute(gbeanName1, "mutableInt"));
87
88
89         // stop -- gbeans should now be started, but still registered
90
configurationManager.stopConfiguration(configurationId);
91
92         assertFalse(kernel.isLoaded(gbeanName1));
93         assertFalse(kernel.isLoaded(gbeanName2));
94
95
96         // unload -- configuration and gbeans should be unloaded
97
configurationManager.unloadConfiguration(configurationId);
98
99         assertFalse(kernel.isLoaded(configurationName));
100         assertFalse(kernel.isLoaded(gbeanName1));
101         assertFalse(kernel.isLoaded(gbeanName2));
102
103     }
104
105     public void testConfigStartStopRestart() throws Exception JavaDoc {
106         Artifact configurationId = configurationData.getId();
107
108         // load -- config should be running and gbean registered but not started
109
configurationManager.loadConfiguration(configurationData);
110         Configuration configuration = configurationManager.getConfiguration(configurationId);
111         AbstractName configurationName = Configuration.getConfigurationAbstractName(configurationId);
112
113         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(configurationName));
114         assertNotNull(configuration.getConfigurationClassLoader());
115
116         assertFalse(kernel.isLoaded(gbeanName1));
117         assertFalse(kernel.isLoaded(gbeanName2));
118
119
120         // start -- gbeans should now be started
121
configurationManager.startConfiguration(configurationId);
122
123         assertTrue(kernel.isLoaded(gbeanName1));
124         assertTrue(kernel.isLoaded(gbeanName2));
125         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbeanName1));
126         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbeanName2));
127
128
129         // stop -- gbeans should now be started, but still registered
130
configurationManager.stopConfiguration(configurationId);
131
132         assertFalse(kernel.isLoaded(gbeanName1));
133         assertFalse(kernel.isLoaded(gbeanName2));
134
135
136         // restart -- gbeans should now be started
137
configurationManager.startConfiguration(configurationId);
138
139         assertTrue(kernel.isLoaded(gbeanName1));
140         assertTrue(kernel.isLoaded(gbeanName2));
141         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbeanName1));
142         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbeanName2));
143
144         // unload -- configuration and gbeans should be unloaded
145
configurationManager.stopConfiguration(configurationId);
146         configurationManager.unloadConfiguration(configurationId);
147
148         assertFalse(kernel.isLoaded(configurationName));
149         assertFalse(kernel.isLoaded(gbeanName1));
150         assertFalse(kernel.isLoaded(gbeanName2));
151
152     }
153
154     public void testAddToConfig() throws Exception JavaDoc {
155         Artifact configurationId = configurationData.getId();
156
157         // load and start the config
158
configurationManager.loadConfiguration(configurationData);
159         Configuration configuration = configurationManager.getConfiguration(configurationId);
160         assertNotNull(configuration.getConfigurationClassLoader());
161
162         GBeanData mockBean3 = new GBeanData(MockGBean.getGBeanInfo());
163         try {
164             kernel.getGBeanState(mockBean3.getAbstractName());
165             fail("Gbean should not be found yet");
166         } catch (GBeanNotFoundException e) {
167         }
168         mockBean3.setAttribute("value", "1234");
169         mockBean3.setAttribute("name", "child");
170         mockBean3.setAttribute("finalInt", new Integer JavaDoc(1));
171         configurationManager.addGBeanToConfiguration(configurationId, "MyMockGMBean3", mockBean3, true);
172
173         assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(mockBean3.getAbstractName()));
174         assertEquals(new Integer JavaDoc(1), kernel.getAttribute(mockBean3.getAbstractName(), "finalInt"));
175         assertEquals("1234", kernel.getAttribute(mockBean3.getAbstractName(), "value"));
176         assertEquals("child", kernel.getAttribute(mockBean3.getAbstractName(), "name"));
177     }
178
179     protected void setUp() throws Exception JavaDoc {
180         super.setUp();
181         kernel = KernelFactory.newInstance().createKernel("test");
182         kernel.boot();
183
184         ConfigurationData bootstrap = new ConfigurationData(new Artifact("bootstrap", "bootstrap", "", "car"), kernel.getNaming());
185
186         GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
187
188         GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
189         artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
190
191         GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO);
192         configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
193         configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName());
194
195         ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader());
196
197         configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel);
198
199
200         configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming());
201
202         GBeanData mockBean1 = configurationData.addGBean("MyMockGMBean1", MockGBean.getGBeanInfo());
203         gbeanName1 = mockBean1.getAbstractName();
204         mockBean1.setAttribute("value", "1234");
205         mockBean1.setAttribute("name", "child");
206         mockBean1.setAttribute("finalInt", new Integer JavaDoc(1));
207
208         GBeanData mockBean2 = configurationData.addGBean("MyMockGMBean2", MockGBean.getGBeanInfo());
209         gbeanName2 = mockBean2.getAbstractName();
210         mockBean2.setAttribute("value", "5678");
211         mockBean2.setAttribute("name", "Parent");
212         mockBean2.setAttribute("finalInt", new Integer JavaDoc(3));
213         mockBean2.setReferencePattern("MockEndpoint", gbeanName1);
214         mockBean2.setReferencePattern("EndpointCollection", new AbstractNameQuery(gbeanName1, MockGBean.getGBeanInfo().getInterfaces()));
215     }
216
217     protected void tearDown() throws Exception JavaDoc {
218         kernel.shutdown();
219         super.tearDown();
220     }
221 }
222
Popular Tags