KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gjndi > KernelContextGBeanTest


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.gjndi;
18
19 import org.apache.geronimo.gbean.GBeanData;
20 import org.apache.geronimo.gbean.GBeanInfo;
21 import org.apache.geronimo.gbean.GBeanInfoBuilder;
22 import org.apache.geronimo.gbean.AbstractName;
23 import org.apache.geronimo.kernel.Kernel;
24 import org.apache.geronimo.kernel.KernelFactory;
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.repository.Artifact;
30 import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
31 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
32 import org.apache.geronimo.naming.java.RootContext;
33 import org.apache.xbean.naming.context.ImmutableContext;
34 import org.apache.xbean.naming.global.GlobalContextManager;
35
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.Collections JavaDoc;
43
44 /**
45  * @version $Rev$ $Date$
46  */

47 public class KernelContextGBeanTest extends AbstractContextTest {
48     private Kernel kernel;
49     private EditableConfigurationManager configurationManager;
50     private ConfigurationData configurationData;
51     private GBeanInfo immutableContextGBeanInfo;
52     private Hashtable JavaDoc contextEnv;
53
54     public void test() throws Exception JavaDoc {
55         Map JavaDoc globalBindings = new HashMap JavaDoc();
56         globalBindings.put("java:comp/string", "foo");
57         globalBindings.put("java:comp/nested/context/string", "bar");
58         globalBindings.put("java:comp/a/b/c/d/e/string", "beer");
59         globalBindings.put("java:comp/a/b/c/d/e/one", new Integer JavaDoc(1));
60         globalBindings.put("java:comp/a/b/c/d/e/two", new Integer JavaDoc(2));
61         globalBindings.put("java:comp/a/b/c/d/e/three", new Integer JavaDoc(3));
62         globalBindings.put("test/env/foo", new Integer JavaDoc(42));
63         globalBindings.put("test/baz", "caz");
64
65         Map JavaDoc javaCompBindings = getNestedBindings(globalBindings, "java:comp/");
66         ImmutableContext javaCompContext = new ImmutableContext(javaCompBindings);
67         RootContext.setComponentContext(javaCompContext);
68
69         GBeanData javaComp = configurationData.addGBean("JavaComp", JavaCompContextGBean.GBEAN_INFO);
70         AbstractName javaCompName = javaComp.getAbstractName();
71
72         GBeanData test = configurationData.addGBean("Test", immutableContextGBeanInfo);
73         AbstractName testName = test.getAbstractName();
74         test.setAttribute("nameInNamespace", "test");
75         Map JavaDoc testBindings = getNestedBindings(globalBindings, "test/");
76         test.setAttribute("bindings", testBindings);
77
78         configurationManager.loadConfiguration(configurationData);
79         configurationManager.startConfiguration(configurationData.getId());
80
81         InitialContext JavaDoc ctx = new InitialContext JavaDoc(contextEnv);
82         assertEq(globalBindings, ctx);
83
84         //
85
// stop test context
86
//
87
kernel.stopGBean(testName);
88
89         HashMap JavaDoc javaCompOnlyBindings = new HashMap JavaDoc(globalBindings);
90         javaCompOnlyBindings.remove("test/env/foo");
91         javaCompOnlyBindings.remove("test/baz");
92         assertEq(javaCompOnlyBindings, ctx);
93
94         //
95
// stop java context
96
//
97
kernel.stopGBean(javaCompName);
98
99         assertEq(Collections.EMPTY_MAP, ctx);
100
101
102         //
103
// restart java context
104
//
105
kernel.startGBean(javaCompName);
106
107         assertEq(javaCompOnlyBindings, ctx);
108
109         //
110
// restart test context
111
//
112
kernel.startGBean(testName);
113
114         assertEq(globalBindings, ctx);
115     }
116
117     protected Map JavaDoc getNestedBindings(Map JavaDoc globalBindings, String JavaDoc nestedPath) {
118         HashMap JavaDoc nestedBindings = new HashMap JavaDoc();
119         for (Iterator JavaDoc iterator = globalBindings.entrySet().iterator(); iterator.hasNext();) {
120             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
121             String JavaDoc globalName = (String JavaDoc) entry.getKey();
122             Object JavaDoc value = entry.getValue();
123
124             if (globalName.startsWith(nestedPath)) {
125                 String JavaDoc nestedName = globalName.substring(nestedPath.length());
126                 nestedBindings.put(nestedName, value);
127             }
128         }
129         return nestedBindings;
130     }
131
132     protected void setUp() throws Exception JavaDoc {
133         super.setUp();
134
135         kernel = KernelFactory.newInstance().createKernel("test");
136         kernel.boot();
137
138         ConfigurationData bootstrap = new ConfigurationData(new Artifact("bootstrap", "bootstrap", "", "car"), kernel.getNaming());
139
140         GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
141
142         GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
143         artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
144
145         GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO);
146         configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
147         configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName());
148
149         ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader());
150
151         configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel);
152
153         configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming());
154         configurationData.addGBean("GlobalContext", GlobalContextGBean.GBEAN_INFO);
155
156         GBeanInfoBuilder builder = new GBeanInfoBuilder(ImmutableContext.class);
157         builder.setConstructor(new String JavaDoc[] {"nameInNamespace", "bindings", "cacheReferences"});
158         builder.addAttribute("nameInNamespace", String JavaDoc.class, true);
159         builder.addAttribute("bindings", Map JavaDoc.class, true);
160         builder.addAttribute("cacheReferences", boolean.class, true);
161         immutableContextGBeanInfo = builder.getBeanInfo();
162
163         contextEnv = new Hashtable JavaDoc();
164         contextEnv.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName());
165     }
166
167     protected void tearDown() throws Exception JavaDoc {
168         kernel.shutdown();
169         super.tearDown();
170     }
171 }
172
Popular Tags