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.naming.deployment;
017:
018: import java.util.Arrays;
019: import java.util.HashMap;
020: import java.util.HashSet;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import javax.naming.Context;
025: import javax.naming.NameClassPair;
026: import javax.naming.NamingEnumeration;
027: import javax.naming.NamingException;
028:
029: import junit.framework.TestCase;
030: import org.apache.geronimo.naming.enc.EnterpriseNamingContext;
031: import org.apache.geronimo.j2ee.deployment.NamingBuilder;
032: import org.apache.xmlbeans.XmlObject;
033: import org.apache.xmlbeans.XmlCursor;
034:
035: /**
036: * @version $Rev: 535381 $ $Date: 2007-05-04 14:04:18 -0700 (Fri, 04 May 2007) $
037: */
038: public class EnvironmentEntryBuilderTest extends TestCase {
039: private Map componentContext = new HashMap();
040: private NamingBuilder environmentEntryBuilder = new EnvironmentEntryBuilder(
041: new String[] { AbstractNamingBuilder.JEE_NAMESPACE });
042:
043: private static final String TEST = "<tmp xmlns=\"http://java.sun.com/xml/ns/javaee\">"
044: + "<env-entry>"
045: + "<env-entry-name>string</env-entry-name>"
046: + "<env-entry-type>java.lang.String</env-entry-type>"
047: + "<env-entry-value>Hello World</env-entry-value>"
048: + "</env-entry>"
049: +
050:
051: "<env-entry>"
052: + "<env-entry-name>char</env-entry-name>"
053: + "<env-entry-type>java.lang.Character</env-entry-type>"
054: + "<env-entry-value>H</env-entry-value>"
055: + "</env-entry>"
056: +
057:
058: "<env-entry>"
059: + "<env-entry-name>byte</env-entry-name>"
060: + "<env-entry-type>java.lang.Byte</env-entry-type>"
061: + "<env-entry-value>12</env-entry-value>"
062: + "</env-entry>"
063: +
064:
065: "<env-entry>"
066: + "<env-entry-name>short</env-entry-name>"
067: + "<env-entry-type>java.lang.Short</env-entry-type>"
068: + "<env-entry-value>12345</env-entry-value>"
069: + "</env-entry>"
070: +
071:
072: "<env-entry>"
073: + "<env-entry-name>int</env-entry-name>"
074: + "<env-entry-type>java.lang.Integer</env-entry-type>"
075: + "<env-entry-value>12345678</env-entry-value>"
076: + "</env-entry>"
077: +
078:
079: "<env-entry>"
080: + "<env-entry-name>long</env-entry-name>"
081: + "<env-entry-type>java.lang.Long</env-entry-type>"
082: + "<env-entry-value>1234567890123456</env-entry-value>"
083: + "</env-entry>"
084: +
085:
086: "<env-entry>"
087: + "<env-entry-name>float</env-entry-name>"
088: + "<env-entry-type>java.lang.Float</env-entry-type>"
089: + "<env-entry-value>123.456</env-entry-value>"
090: + "</env-entry>"
091: +
092:
093: "<env-entry>"
094: + "<env-entry-name>double</env-entry-name>"
095: + "<env-entry-type>java.lang.Double</env-entry-type>"
096: + "<env-entry-value>12345.6789</env-entry-value>"
097: + "</env-entry>"
098: +
099:
100: "<env-entry>"
101: + "<env-entry-name>boolean</env-entry-name>"
102: + "<env-entry-type>java.lang.Boolean</env-entry-type>"
103: + "<env-entry-value>TRUE</env-entry-value>"
104: + "</env-entry>" + "</tmp>";
105:
106: public void testEnvEntries() throws Exception {
107:
108: String stringVal = "Hello World";
109: Character charVal = new Character('H');
110: Byte byteVal = new Byte((byte) 12);
111: Short shortVal = new Short((short) 12345);
112: Integer intVal = new Integer(12345678);
113: Long longVal = new Long(1234567890123456L);
114: Float floatVal = new Float(123.456);
115: Double doubleVal = new Double(12345.6789);
116: Boolean booleanVal = Boolean.TRUE;
117:
118: XmlObject doc = XmlObject.Factory.parse(TEST);
119: XmlCursor cursor = doc.newCursor();
120: try {
121: cursor.toFirstChild();
122: doc = cursor.getObject();
123: } finally {
124: cursor.dispose();
125: }
126: environmentEntryBuilder.buildNaming(doc, null, null,
127: componentContext);
128: Context context = EnterpriseNamingContext
129: .createEnterpriseNamingContext(NamingBuilder.JNDI_KEY
130: .get(componentContext));
131: Set actual = new HashSet();
132: for (NamingEnumeration e = context.listBindings("env"); e
133: .hasMore();) {
134: NameClassPair pair = (NameClassPair) e.next();
135: actual.add(pair.getName());
136: }
137: Set expected = new HashSet(Arrays.asList(new String[] {
138: "string", "char", "byte", "short", "int", "long",
139: "float", "double", "boolean" }));
140: assertEquals(expected, actual);
141: assertEquals(stringVal, context.lookup("env/string"));
142: assertEquals(charVal, context.lookup("env/char"));
143: assertEquals(byteVal, context.lookup("env/byte"));
144: assertEquals(shortVal, context.lookup("env/short"));
145: assertEquals(intVal, context.lookup("env/int"));
146: assertEquals(longVal, context.lookup("env/long"));
147: assertEquals(floatVal, context.lookup("env/float"));
148: assertEquals(doubleVal, context.lookup("env/double"));
149: assertEquals(booleanVal, context.lookup("env/boolean"));
150: }
151:
152: public void xtestEmptyEnvironment() throws NamingException {
153: Context context = EnterpriseNamingContext
154: .createEnterpriseNamingContext(componentContext);
155: try {
156: Context env = (Context) context.lookup("env");
157: assertNotNull(env);
158: } catch (NamingException e) {
159: e.printStackTrace();
160: fail();
161: }
162: }
163:
164: }
|