KickJava   Java API By Example, From Geeks To Geeks.

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


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.kernel.Kernel;
21 import org.apache.geronimo.kernel.KernelFactory;
22 import org.apache.geronimo.kernel.config.ConfigurationData;
23 import org.apache.geronimo.kernel.config.ConfigurationUtil;
24 import org.apache.geronimo.kernel.config.EditableConfigurationManager;
25 import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
26 import org.apache.geronimo.kernel.repository.Artifact;
27 import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
28 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
29 import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
30 import org.apache.geronimo.naming.java.RootContext;
31 import org.apache.xbean.naming.context.ImmutableContext;
32 import org.apache.xbean.naming.global.GlobalContextManager;
33
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import javax.naming.NameNotFoundException JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42
43 /**
44  * @version $Rev$ $Date$
45  */

46 public class JavaCompGBeanTest extends AbstractContextTest {
47     private Kernel kernel;
48     private Hashtable JavaDoc contextEnv;
49
50     public void testLookupEnv() throws Exception JavaDoc {
51         Map JavaDoc javaCompBindings = new HashMap JavaDoc();
52         javaCompBindings.put("foo", "bar");
53
54         // a regular context doesn't contain env
55
RootContext.setComponentContext(new ImmutableContext(javaCompBindings));
56         try {
57             new InitialContext JavaDoc(contextEnv).lookup("java:comp/env");
58             fail("Expected NameNotFoundException");
59         } catch (NameNotFoundException JavaDoc expected) {
60             // expected
61
}
62
63         // ENC adds env if not present
64
RootContext.setComponentContext(EnterpriseNamingContext.createEnterpriseNamingContext(javaCompBindings, null, null, null));
65         new InitialContext JavaDoc(contextEnv).lookup("java:comp/env");
66     }
67
68     protected Map JavaDoc getNestedBindings(Map JavaDoc globalBindings, String JavaDoc nestedPath) {
69         HashMap JavaDoc nestedBindings = new HashMap JavaDoc();
70         for (Iterator JavaDoc iterator = globalBindings.entrySet().iterator(); iterator.hasNext();) {
71             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
72             String JavaDoc globalName = (String JavaDoc) entry.getKey();
73             Object JavaDoc value = entry.getValue();
74
75             if (globalName.startsWith(nestedPath)) {
76                 String JavaDoc nestedName = globalName.substring(nestedPath.length());
77                 nestedBindings.put(nestedName, value);
78             }
79         }
80         return nestedBindings;
81     }
82
83     protected void setUp() throws Exception JavaDoc {
84         super.setUp();
85
86         kernel = KernelFactory.newInstance().createKernel("test");
87         kernel.boot();
88
89         ConfigurationData bootstrap = new ConfigurationData(new Artifact("bootstrap", "bootstrap", "", "car"), kernel.getNaming());
90
91         GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
92
93         GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
94         artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
95
96         GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO);
97         configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
98         configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName());
99
100         ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader());
101
102         EditableConfigurationManager configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel);
103
104         ConfigurationData configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming());
105         configurationData.addGBean("GlobalContext", GlobalContextGBean.GBEAN_INFO);
106         configurationData.addGBean("JavaComp", JavaCompContextGBean.GBEAN_INFO);
107
108         configurationManager.loadConfiguration(configurationData);
109         configurationManager.startConfiguration(configurationData.getId());
110
111
112         contextEnv = new Hashtable JavaDoc();
113         contextEnv.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName());
114     }
115
116     protected void tearDown() throws Exception JavaDoc {
117         kernel.shutdown();
118         super.tearDown();
119     }
120 }
121
Popular Tags