KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gjndi > binding > GBeanBindingTest


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.binding;
18
19 import org.apache.geronimo.gbean.AbstractName;
20 import org.apache.geronimo.gbean.AbstractNameQuery;
21 import org.apache.geronimo.gbean.GBeanData;
22 import org.apache.geronimo.gjndi.AbstractContextTest;
23 import org.apache.geronimo.gjndi.GlobalContextGBean;
24 import org.apache.geronimo.gjndi.WritableContextGBean;
25 import org.apache.geronimo.kernel.Kernel;
26 import org.apache.geronimo.kernel.KernelFactory;
27 import org.apache.geronimo.kernel.config.ConfigurationData;
28 import org.apache.geronimo.kernel.config.ConfigurationUtil;
29 import org.apache.geronimo.kernel.config.EditableConfigurationManager;
30 import org.apache.geronimo.kernel.config.EditableKernelConfigurationManager;
31 import org.apache.geronimo.kernel.repository.Artifact;
32 import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
33 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
34 import org.apache.xbean.naming.global.GlobalContextManager;
35
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.sql.DataSource JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Map JavaDoc;
44
45 /**
46  * @version $Rev$ $Date$
47  */

48 public class GBeanBindingTest extends AbstractContextTest {
49     private Kernel kernel;
50
51     private Hashtable JavaDoc contextEnv;
52     private Map JavaDoc globalBindings;
53     private AbstractName ds1Name;
54     private AbstractName ds2Name;
55
56     public void testBasics() throws Exception JavaDoc {
57         InitialContext JavaDoc ctx = new InitialContext JavaDoc(contextEnv);
58         assertEq(globalBindings, ctx);
59
60         //
61
// stop ds1
62
//
63
kernel.stopGBean(ds1Name);
64         DataSource JavaDoc ds2 = (DataSource JavaDoc) kernel.getGBean(ds2Name);
65         globalBindings.put("writable/ds", ds2);
66         globalBindings.remove("writable/ds1");
67         assertEq(globalBindings, ctx);
68
69         //
70
// restart ds1
71
//
72
kernel.startGBean(ds1Name);
73         DataSource JavaDoc ds1 = (DataSource JavaDoc) kernel.getGBean(ds1Name);
74         globalBindings.put("writable/ds1", ds1);
75         assertEq(globalBindings, ctx);
76     }
77
78     protected Map JavaDoc getNestedBindings(Map JavaDoc globalBindings, String JavaDoc nestedPath) {
79         HashMap JavaDoc nestedBindings = new HashMap JavaDoc();
80         for (Iterator JavaDoc iterator = globalBindings.entrySet().iterator(); iterator.hasNext();) {
81             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
82             String JavaDoc globalName = (String JavaDoc) entry.getKey();
83             Object JavaDoc value = entry.getValue();
84
85             if (globalName.startsWith(nestedPath)) {
86                 String JavaDoc nestedName = globalName.substring(nestedPath.length());
87                 nestedBindings.put(nestedName, value);
88             }
89         }
90         return nestedBindings;
91     }
92
93     protected void setUp() throws Exception JavaDoc {
94         super.setUp();
95
96         kernel = KernelFactory.newInstance().createKernel("test");
97         kernel.boot();
98
99         ConfigurationData bootstrap = new ConfigurationData(new Artifact("bootstrap", "bootstrap", "", "car"), kernel.getNaming());
100
101         GBeanData artifactManagerData = bootstrap.addGBean("ArtifactManager", DefaultArtifactManager.GBEAN_INFO);
102
103         GBeanData artifactResolverData = bootstrap.addGBean("ArtifactResolver", DefaultArtifactResolver.GBEAN_INFO);
104         artifactResolverData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
105
106         GBeanData configurationManagerData = bootstrap.addGBean("ConfigurationManager", EditableKernelConfigurationManager.GBEAN_INFO);
107         configurationManagerData.setReferencePattern("ArtifactManager", artifactManagerData.getAbstractName());
108         configurationManagerData.setReferencePattern("ArtifactResolver", artifactResolverData.getAbstractName());
109
110         ConfigurationUtil.loadBootstrapConfiguration(kernel, bootstrap, getClass().getClassLoader());
111
112         EditableConfigurationManager configurationManager = ConfigurationUtil.getEditableConfigurationManager(kernel);
113
114         ConfigurationData configurationData = new ConfigurationData(new Artifact("test", "test", "", "car"), kernel.getNaming());
115         configurationData.addGBean("GlobalContext", GlobalContextGBean.GBEAN_INFO);
116
117         contextEnv = new Hashtable JavaDoc();
118         contextEnv.put(Context.INITIAL_CONTEXT_FACTORY, GlobalContextManager.class.getName());
119
120         // dataSources
121
GBeanData ds1GBean = configurationData.addGBean("ds1", MockDataSource.GBEAN_INFO);
122         ds1Name = ds1GBean.getAbstractName();
123
124         GBeanData ds2GBean = configurationData.addGBean("ds2", MockDataSource.GBEAN_INFO);
125         ds2Name = ds2GBean.getAbstractName();
126
127         // bindings
128
GBeanData writableGBean = configurationData.addGBean("writable", WritableContextGBean.GBEAN_INFO);
129         AbstractName writableName = writableGBean.getAbstractName();
130         writableGBean.setAttribute("nameInNamespace", "writable");
131
132         GBeanData dsBinding = configurationData.addGBean("dsBinding", GBeanBinding.GBEAN_INFO);
133         dsBinding.setReferencePattern("Context", writableName);
134         dsBinding.setAttribute("name", "ds");
135         dsBinding.setAttribute("abstractNameQuery", new AbstractNameQuery(null, Collections.EMPTY_MAP, DataSource JavaDoc.class.getName()));
136
137         GBeanData ds1Binding = configurationData.addGBean("ds1Binding", GBeanBinding.GBEAN_INFO);
138         ds1Binding.setReferencePattern("Context", writableName);
139         ds1Binding.setAttribute("name", "ds1");
140         ds1Binding.setAttribute("abstractNameQuery", new AbstractNameQuery(null,
141                 Collections.singletonMap("name", "ds1"),
142                 DataSource JavaDoc.class.getName()));
143
144         GBeanData ds2Binding = configurationData.addGBean("ds2Binding", GBeanBinding.GBEAN_INFO);
145         ds2Binding.setReferencePattern("Context", writableName);
146         ds2Binding.setAttribute("name", "ds2");
147         ds2Binding.setAttribute("abstractNameQuery", new AbstractNameQuery(null,
148                 Collections.singletonMap("name", "ds2"),
149                 DataSource JavaDoc.class.getName()));
150
151         configurationManager.loadConfiguration(configurationData);
152         configurationManager.startConfiguration(configurationData.getId());
153
154         DataSource JavaDoc ds1 = (DataSource JavaDoc) kernel.getGBean(ds1Name);
155         DataSource JavaDoc ds2 = (DataSource JavaDoc) kernel.getGBean(ds2Name);
156
157         // global bindings
158
globalBindings = new HashMap JavaDoc();
159         globalBindings.put("writable/ds", ds1);
160         globalBindings.put("writable/ds1", ds1);
161         globalBindings.put("writable/ds2", ds2);
162     }
163
164     protected void tearDown() throws Exception JavaDoc {
165         kernel.shutdown();
166         super.tearDown();
167     }
168 }
169
Popular Tags