KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > java > ContextBuilderTest


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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
18 package org.apache.geronimo.naming.java;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import javax.naming.NameClassPair JavaDoc;
27 import javax.naming.NamingEnumeration JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.apache.geronimo.gbean.GBeanData;
33 import org.apache.geronimo.gbean.GBeanInfo;
34 import org.apache.geronimo.gbean.GBeanInfoBuilder;
35 import org.apache.geronimo.kernel.KernelRegistry;
36 import org.apache.geronimo.kernel.KernelFactory;
37 import org.apache.geronimo.kernel.Kernel;
38
39 /**
40  * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
41  */

42 public class ContextBuilderTest extends TestCase {
43     private ComponentContextBuilder builder;
44
45     private List JavaDoc proxy;
46
47     public void testEnvEntries() throws Exception JavaDoc {
48         String JavaDoc stringVal = "Hello World";
49         Character JavaDoc charVal = new Character JavaDoc('H');
50         Byte JavaDoc byteVal = new Byte JavaDoc((byte) 12);
51         Short JavaDoc shortVal = new Short JavaDoc((short) 12345);
52         Integer JavaDoc intVal = new Integer JavaDoc(12345678);
53         Long JavaDoc longVal = new Long JavaDoc(1234567890123456L);
54         Float JavaDoc floatVal = new Float JavaDoc(123.456);
55         Double JavaDoc doubleVal = new Double JavaDoc(12345.6789);
56         Boolean JavaDoc booleanVal = Boolean.TRUE;
57         builder.addEnvEntry("string", String JavaDoc.class.getName(), stringVal, null);
58         builder.addEnvEntry("char", Character JavaDoc.class.getName(), charVal.toString(), null);
59         builder.addEnvEntry("byte", Byte JavaDoc.class.getName(), byteVal.toString(), null);
60         builder.addEnvEntry("short", Short JavaDoc.class.getName(), shortVal.toString(), null);
61         builder.addEnvEntry("int", Integer JavaDoc.class.getName(), intVal.toString(), null);
62         builder.addEnvEntry("long", Long JavaDoc.class.getName(), longVal.toString(), null);
63         builder.addEnvEntry("float", Float JavaDoc.class.getName(), floatVal.toString(), null);
64         builder.addEnvEntry("double", Double JavaDoc.class.getName(), doubleVal.toString(), null);
65         builder.addEnvEntry("boolean", Boolean JavaDoc.class.getName(), booleanVal.toString(), null);
66
67         SimpleReadOnlyContext context = new SimpleReadOnlyContext(builder.getContext());
68         Set JavaDoc actual = new HashSet JavaDoc();
69         for (NamingEnumeration JavaDoc e = context.listBindings("env"); e.hasMore();) {
70             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) e.next();
71             actual.add(pair.getName());
72         }
73         Set JavaDoc expected = new HashSet JavaDoc(Arrays.asList(new String JavaDoc[]{"string", "char", "byte", "short", "int", "long", "float", "double", "boolean"}));
74         assertEquals(expected, actual);
75         assertEquals(stringVal, context.lookup("env/string"));
76         assertEquals(charVal, context.lookup("env/char"));
77         assertEquals(byteVal, context.lookup("env/byte"));
78         assertEquals(shortVal, context.lookup("env/short"));
79         assertEquals(intVal, context.lookup("env/int"));
80         assertEquals(longVal, context.lookup("env/long"));
81         assertEquals(floatVal, context.lookup("env/float"));
82         assertEquals(doubleVal, context.lookup("env/double"));
83         assertEquals(booleanVal, context.lookup("env/boolean"));
84     }
85
86     public void xtestResourceEnv() throws Exception JavaDoc {
87         proxy = new ArrayList JavaDoc();
88 // builder.addResourceEnvRef("resourceenvref", List.class, localRef);
89

90         SimpleReadOnlyContext context = new SimpleReadOnlyContext(builder.getContext());
91         Kernel kernel = KernelFactory.newInstance().createKernel("test.kernel");
92         kernel.boot();
93         try {
94             assertEquals(kernel, KernelRegistry.getKernel("test.kernel"));
95             ObjectName JavaDoc proxyFactoryName = null;//referenceFactory.createAdminObjectObjectName("testAdminObject");
96
GBeanData gbean = new GBeanData(proxyFactoryName, getGbeanInfo());
97             gbean.setAttribute("Content", proxy);
98             kernel.loadGBean(gbean, Class.forName(gbean.getGBeanInfo().getClassName()).getClassLoader());
99             kernel.startGBean(proxyFactoryName);
100             Object JavaDoc o = context.lookup("env/resourceenvref");
101             assertEquals(proxy, o);
102         } finally {
103             kernel.shutdown();
104         }
105     }
106
107     public void testEmptyEnvironment() throws NamingException JavaDoc {
108         SimpleReadOnlyContext context = new SimpleReadOnlyContext(builder.getContext());
109         try {
110             ReadOnlyContext env = (ReadOnlyContext) context.lookup("env");
111             assertNotNull(env);
112         } catch (NamingException JavaDoc e) {
113             fail();
114         }
115     }
116
117     protected void setUp() throws Exception JavaDoc {
118         super.setUp();
119 // referenceFactory = new JMXReferenceFactory("geronimo.server", "geronimo");
120
builder = new ComponentContextBuilder();
121     }
122
123     public static class TestProxyFactory {
124
125         private Object JavaDoc proxy;
126
127         public TestProxyFactory(Object JavaDoc proxy) {
128             this.proxy = proxy;
129         }
130
131         public Object JavaDoc getProxy() {
132             return proxy;
133         }
134
135     }
136
137     public GBeanInfo getGbeanInfo() {
138         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(TestProxyFactory.class);
139         infoFactory.addAttribute("Content", Object JavaDoc.class, true);
140         infoFactory.addOperation("getProxy");
141         infoFactory.setConstructor(new String JavaDoc[]{"Content"});
142         return infoFactory.getBeanInfo();
143     }
144 }
145
Popular Tags