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.gjndi;
017:
018: import org.apache.geronimo.gbean.GBeanData;
019: import org.apache.geronimo.kernel.Kernel;
020: import org.apache.geronimo.kernel.KernelFactory;
021: import org.apache.geronimo.kernel.config.ConfigurationData;
022: import org.apache.geronimo.kernel.config.ConfigurationUtil;
023: import org.apache.geronimo.kernel.config.EditableConfigurationManager;
024: import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
025: import org.apache.geronimo.kernel.repository.Artifact;
026: import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
027: import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
028: import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
029: import org.apache.geronimo.naming.java.RootContext;
030: import org.apache.xbean.naming.context.ImmutableContext;
031: import org.apache.xbean.naming.global.GlobalContextManager;
032:
033: import javax.naming.Context;
034: import javax.naming.InitialContext;
035: import javax.naming.NamingException;
036: import javax.naming.NameNotFoundException;
037: import java.util.HashMap;
038: import java.util.Hashtable;
039: import java.util.Iterator;
040: import java.util.Map;
041:
042: /**
043: * @version $Rev$ $Date$
044: */
045: public class JavaCompGBeanTest extends AbstractContextTest {
046: private Kernel kernel;
047: private Hashtable contextEnv;
048:
049: public void testLookupEnv() throws Exception {
050: Map javaCompBindings = new HashMap();
051: javaCompBindings.put("foo", "bar");
052:
053: // a regular context doesn't contain env
054: RootContext.setComponentContext(new ImmutableContext(
055: javaCompBindings));
056: try {
057: new InitialContext(contextEnv).lookup("java:comp/env");
058: fail("Expected NameNotFoundException");
059: } catch (NameNotFoundException expected) {
060: // expected
061: }
062:
063: // ENC adds env if not present
064: RootContext.setComponentContext(EnterpriseNamingContext
065: .createEnterpriseNamingContext(javaCompBindings, null,
066: null, null));
067: new InitialContext(contextEnv).lookup("java:comp/env");
068: }
069:
070: protected Map getNestedBindings(Map globalBindings,
071: String nestedPath) {
072: HashMap nestedBindings = new HashMap();
073: for (Iterator iterator = globalBindings.entrySet().iterator(); iterator
074: .hasNext();) {
075: Map.Entry entry = (Map.Entry) iterator.next();
076: String globalName = (String) entry.getKey();
077: Object value = entry.getValue();
078:
079: if (globalName.startsWith(nestedPath)) {
080: String nestedName = globalName.substring(nestedPath
081: .length());
082: nestedBindings.put(nestedName, value);
083: }
084: }
085: return nestedBindings;
086: }
087:
088: protected void setUp() throws Exception {
089: super .setUp();
090:
091: kernel = KernelFactory.newInstance().createKernel("test");
092: kernel.boot();
093:
094: ConfigurationData bootstrap = new ConfigurationData(
095: new Artifact("bootstrap", "bootstrap", "", "car"),
096: kernel.getNaming());
097:
098: GBeanData artifactManagerData = bootstrap.addGBean(
099: "ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
100:
101: GBeanData artifactResolverData = bootstrap.addGBean(
102: "ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
103: artifactResolverData.setReferencePattern("ArtifactManager",
104: artifactManagerData.getAbstractName());
105:
106: GBeanData configurationManagerData = bootstrap.addGBean(
107: "ConfigurationManager",
108: EditableKernelConfigurationManager.GBEAN_INFO);
109: configurationManagerData.setReferencePattern("ArtifactManager",
110: artifactManagerData.getAbstractName());
111: configurationManagerData.setReferencePattern(
112: "ArtifactResolver", artifactResolverData
113: .getAbstractName());
114:
115: ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap,
116: getClass().getClassLoader());
117:
118: EditableConfigurationManager configurationManager = ConfigurationUtil
119: .getEditableConfigurationManager(kernel);
120:
121: ConfigurationData configurationData = new ConfigurationData(
122: new Artifact("test", "test", "", "car"), kernel
123: .getNaming());
124: configurationData.addGBean("GlobalContext",
125: GlobalContextGBean.GBEAN_INFO);
126: configurationData.addGBean("JavaComp",
127: JavaCompContextGBean.GBEAN_INFO);
128:
129: configurationManager.loadConfiguration(configurationData);
130: configurationManager.startConfiguration(configurationData
131: .getId());
132:
133: contextEnv = new Hashtable();
134: contextEnv.put(Context.INITIAL_CONTEXT_FACTORY,
135: GlobalContextManager.class.getName());
136: }
137:
138: protected void tearDown() throws Exception {
139: kernel.shutdown();
140: super.tearDown();
141: }
142: }
|