KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > naming > deployment > EnvironmentEntryBuilderTest


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
18 package org.apache.geronimo.naming.deployment;
19
20 import java.util.Arrays JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.NameClassPair JavaDoc;
28 import javax.naming.NamingEnumeration JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 import junit.framework.TestCase;
32 import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
33 import org.apache.geronimo.j2ee.deployment.NamingBuilder;
34 import org.apache.xmlbeans.XmlObject;
35 import org.apache.xmlbeans.XmlCursor;
36
37 /**
38  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
39  */

40 public class EnvironmentEntryBuilderTest extends TestCase {
41     private Map JavaDoc componentContext = new HashMap JavaDoc();
42     private NamingBuilder environmentEntryBuilder = new EnvironmentEntryBuilder(new String JavaDoc[] {AbstractNamingBuilder.J2EE_NAMESPACE});
43
44     private static final String JavaDoc TEST = "<tmp xmlns=\"http://java.sun.com/xml/ns/j2ee\">" +
45             "<env-entry>" +
46             "<env-entry-name>string</env-entry-name>" +
47             "<env-entry-type>java.lang.String</env-entry-type>" +
48             "<env-entry-value>Hello World</env-entry-value>" +
49             "</env-entry>" +
50
51             "<env-entry>" +
52             "<env-entry-name>char</env-entry-name>" +
53             "<env-entry-type>java.lang.Character</env-entry-type>" +
54             "<env-entry-value>H</env-entry-value>" +
55             "</env-entry>" +
56
57             "<env-entry>" +
58             "<env-entry-name>byte</env-entry-name>" +
59             "<env-entry-type>java.lang.Byte</env-entry-type>" +
60             "<env-entry-value>12</env-entry-value>" +
61             "</env-entry>" +
62
63             "<env-entry>" +
64             "<env-entry-name>short</env-entry-name>" +
65             "<env-entry-type>java.lang.Short</env-entry-type>" +
66             "<env-entry-value>12345</env-entry-value>" +
67             "</env-entry>" +
68
69             "<env-entry>" +
70             "<env-entry-name>int</env-entry-name>" +
71             "<env-entry-type>java.lang.Integer</env-entry-type>" +
72             "<env-entry-value>12345678</env-entry-value>" +
73             "</env-entry>" +
74
75             "<env-entry>" +
76             "<env-entry-name>long</env-entry-name>" +
77             "<env-entry-type>java.lang.Long</env-entry-type>" +
78             "<env-entry-value>1234567890123456</env-entry-value>" +
79             "</env-entry>" +
80
81             "<env-entry>" +
82             "<env-entry-name>float</env-entry-name>" +
83             "<env-entry-type>java.lang.Float</env-entry-type>" +
84             "<env-entry-value>123.456</env-entry-value>" +
85             "</env-entry>" +
86
87             "<env-entry>" +
88             "<env-entry-name>double</env-entry-name>" +
89             "<env-entry-type>java.lang.Double</env-entry-type>" +
90             "<env-entry-value>12345.6789</env-entry-value>" +
91             "</env-entry>" +
92
93             "<env-entry>" +
94             "<env-entry-name>boolean</env-entry-name>" +
95             "<env-entry-type>java.lang.Boolean</env-entry-type>" +
96             "<env-entry-value>TRUE</env-entry-value>" +
97             "</env-entry>" +
98             "</tmp>";
99
100     public void testEnvEntries() throws Exception JavaDoc {
101         componentContext.put(NamingBuilder.JNDI_KEY, new HashMap JavaDoc());
102
103         String JavaDoc stringVal = "Hello World";
104         Character JavaDoc charVal = new Character JavaDoc('H');
105         Byte JavaDoc byteVal = new Byte JavaDoc((byte) 12);
106         Short JavaDoc shortVal = new Short JavaDoc((short) 12345);
107         Integer JavaDoc intVal = new Integer JavaDoc(12345678);
108         Long JavaDoc longVal = new Long JavaDoc(1234567890123456L);
109         Float JavaDoc floatVal = new Float JavaDoc(123.456);
110         Double JavaDoc doubleVal = new Double JavaDoc(12345.6789);
111         Boolean JavaDoc booleanVal = Boolean.TRUE;
112
113         XmlObject doc = XmlObject.Factory.parse(TEST);
114         XmlCursor cursor = doc.newCursor();
115         try {
116             cursor.toFirstChild();
117             doc = cursor.getObject();
118         } finally {
119             cursor.dispose();
120         }
121         environmentEntryBuilder.buildNaming(doc, null, null, null, null, componentContext);
122         Context JavaDoc context = EnterpriseNamingContext.createEnterpriseNamingContext((Map JavaDoc) componentContext.get(NamingBuilder.JNDI_KEY));
123         Set JavaDoc actual = new HashSet JavaDoc();
124         for (NamingEnumeration JavaDoc e = context.listBindings("env"); e.hasMore();) {
125             NameClassPair JavaDoc pair = (NameClassPair JavaDoc) e.next();
126             actual.add(pair.getName());
127         }
128         Set JavaDoc expected = new HashSet JavaDoc(Arrays.asList(new String JavaDoc[]{"string", "char", "byte", "short", "int", "long", "float", "double", "boolean"}));
129         assertEquals(expected, actual);
130         assertEquals(stringVal, context.lookup("env/string"));
131         assertEquals(charVal, context.lookup("env/char"));
132         assertEquals(byteVal, context.lookup("env/byte"));
133         assertEquals(shortVal, context.lookup("env/short"));
134         assertEquals(intVal, context.lookup("env/int"));
135         assertEquals(longVal, context.lookup("env/long"));
136         assertEquals(floatVal, context.lookup("env/float"));
137         assertEquals(doubleVal, context.lookup("env/double"));
138         assertEquals(booleanVal, context.lookup("env/boolean"));
139     }
140
141     public void xtestEmptyEnvironment() throws NamingException JavaDoc {
142         Context JavaDoc context = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext);
143         try {
144             Context JavaDoc env = (Context JavaDoc) context.lookup("env");
145             assertNotNull(env);
146         } catch (NamingException JavaDoc e) {
147             e.printStackTrace();
148             fail();
149         }
150     }
151
152 }
153
Popular Tags