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.binding;
017:
018: import org.apache.geronimo.gbean.AbstractName;
019: import org.apache.geronimo.gbean.AbstractNameQuery;
020: import org.apache.geronimo.gbean.GBeanData;
021: import org.apache.geronimo.gjndi.AbstractContextTest;
022: import org.apache.geronimo.gjndi.GlobalContextGBean;
023: import org.apache.geronimo.gjndi.WritableContextGBean;
024: import org.apache.geronimo.kernel.Kernel;
025: import org.apache.geronimo.kernel.KernelFactory;
026: import org.apache.geronimo.kernel.config.ConfigurationData;
027: import org.apache.geronimo.kernel.config.ConfigurationUtil;
028: import org.apache.geronimo.kernel.config.EditableConfigurationManager;
029: import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
030: import org.apache.geronimo.kernel.repository.Artifact;
031: import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
032: import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
033: import org.apache.xbean.naming.global.GlobalContextManager;
034:
035: import javax.naming.Context;
036: import javax.naming.InitialContext;
037: import javax.sql.DataSource;
038: import java.util.Collections;
039: import java.util.HashMap;
040: import java.util.Hashtable;
041: import java.util.Iterator;
042: import java.util.Map;
043:
044: /**
045: * @version $Rev$ $Date$
046: */
047: public class GBeanBindingTest extends AbstractContextTest {
048: private Kernel kernel;
049:
050: private Hashtable contextEnv;
051: private Map globalBindings;
052: private AbstractName ds1Name;
053: private AbstractName ds2Name;
054:
055: public void testBasics() throws Exception {
056: InitialContext ctx = new InitialContext(contextEnv);
057: assertEq(globalBindings, ctx);
058:
059: //
060: // stop ds2
061: //
062: kernel.stopGBean(ds2Name);
063: globalBindings.remove("writable/ds2");
064: assertEq(globalBindings, ctx);
065:
066: //
067: // restart ds2
068: //
069: kernel.startGBean(ds2Name);
070: DataSource ds2 = (DataSource) kernel.getGBean(ds2Name);
071: globalBindings.put("writable/ds2", ds2);
072: assertEq(globalBindings, ctx);
073: }
074:
075: protected Map getNestedBindings(Map globalBindings,
076: String nestedPath) {
077: HashMap nestedBindings = new HashMap();
078: for (Iterator iterator = globalBindings.entrySet().iterator(); iterator
079: .hasNext();) {
080: Map.Entry entry = (Map.Entry) iterator.next();
081: String globalName = (String) entry.getKey();
082: Object value = entry.getValue();
083:
084: if (globalName.startsWith(nestedPath)) {
085: String nestedName = globalName.substring(nestedPath
086: .length());
087: nestedBindings.put(nestedName, value);
088: }
089: }
090: return nestedBindings;
091: }
092:
093: protected void setUp() throws Exception {
094: super .setUp();
095:
096: kernel = KernelFactory.newInstance().createKernel("test");
097: kernel.boot();
098:
099: ConfigurationData bootstrap = new ConfigurationData(
100: new Artifact("bootstrap", "bootstrap", "", "car"),
101: kernel.getNaming());
102:
103: GBeanData artifactManagerData = bootstrap.addGBean(
104: "ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
105:
106: GBeanData artifactResolverData = bootstrap.addGBean(
107: "ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
108: artifactResolverData.setReferencePattern("ArtifactManager",
109: artifactManagerData.getAbstractName());
110:
111: GBeanData configurationManagerData = bootstrap.addGBean(
112: "ConfigurationManager",
113: EditableKernelConfigurationManager.GBEAN_INFO);
114: configurationManagerData.setReferencePattern("ArtifactManager",
115: artifactManagerData.getAbstractName());
116: configurationManagerData.setReferencePattern(
117: "ArtifactResolver", artifactResolverData
118: .getAbstractName());
119:
120: ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap,
121: getClass().getClassLoader());
122:
123: EditableConfigurationManager configurationManager = ConfigurationUtil
124: .getEditableConfigurationManager(kernel);
125:
126: ConfigurationData configurationData = new ConfigurationData(
127: new Artifact("test", "test", "", "car"), kernel
128: .getNaming());
129: configurationData.addGBean("GlobalContext",
130: GlobalContextGBean.GBEAN_INFO);
131:
132: contextEnv = new Hashtable();
133: contextEnv.put(Context.INITIAL_CONTEXT_FACTORY,
134: GlobalContextManager.class.getName());
135:
136: // dataSources
137: GBeanData ds1GBean = configurationData.addGBean("ds1",
138: MockDataSource.GBEAN_INFO);
139: ds1Name = ds1GBean.getAbstractName();
140:
141: GBeanData ds2GBean = configurationData.addGBean("ds2",
142: MockDataSource.GBEAN_INFO);
143: ds2Name = ds2GBean.getAbstractName();
144:
145: // bindings
146: GBeanData writableGBean = configurationData.addGBean(
147: "writable", WritableContextGBean.GBEAN_INFO);
148: AbstractName writableName = writableGBean.getAbstractName();
149: writableGBean.setAttribute("nameInNamespace", "writable");
150:
151: GBeanData dsBinding = configurationData.addGBean("dsBinding",
152: GBeanBinding.GBEAN_INFO);
153: dsBinding.setReferencePattern("Context", writableName);
154: dsBinding.setAttribute("name", "ds");
155: dsBinding.setAttribute("abstractNameQuery",
156: new AbstractNameQuery(null, Collections.singletonMap(
157: "name", "ds1"), DataSource.class.getName()));
158:
159: GBeanData ds1Binding = configurationData.addGBean("ds1Binding",
160: GBeanBinding.GBEAN_INFO);
161: ds1Binding.setReferencePattern("Context", writableName);
162: ds1Binding.setAttribute("name", "ds1");
163: ds1Binding.setAttribute("abstractNameQuery",
164: new AbstractNameQuery(null, Collections.singletonMap(
165: "name", "ds1"), DataSource.class.getName()));
166:
167: GBeanData ds2Binding = configurationData.addGBean("ds2Binding",
168: GBeanBinding.GBEAN_INFO);
169: ds2Binding.setReferencePattern("Context", writableName);
170: ds2Binding.setAttribute("name", "ds2");
171: ds2Binding.setAttribute("abstractNameQuery",
172: new AbstractNameQuery(null, Collections.singletonMap(
173: "name", "ds2"), DataSource.class.getName()));
174:
175: configurationManager.loadConfiguration(configurationData);
176: configurationManager.startConfiguration(configurationData
177: .getId());
178:
179: DataSource ds1 = (DataSource) kernel.getGBean(ds1Name);
180: DataSource ds2 = (DataSource) kernel.getGBean(ds2Name);
181:
182: // global bindings
183: globalBindings = new HashMap();
184: globalBindings.put("writable/ds", ds1);
185: globalBindings.put("writable/ds1", ds1);
186: globalBindings.put("writable/ds2", ds2);
187: }
188:
189: protected void tearDown() throws Exception {
190: kernel.shutdown();
191: super.tearDown();
192: }
193: }
|