1 17 18 package org.apache.geronimo.naming.deployment; 19 20 import java.util.Arrays ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.naming.Context ; 27 import javax.naming.NameClassPair ; 28 import javax.naming.NamingEnumeration ; 29 import javax.naming.NamingException ; 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 40 public class EnvironmentEntryBuilderTest extends TestCase { 41 private Map componentContext = new HashMap (); 42 private NamingBuilder environmentEntryBuilder = new EnvironmentEntryBuilder(new String [] {AbstractNamingBuilder.J2EE_NAMESPACE}); 43 44 private static final String 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 { 101 componentContext.put(NamingBuilder.JNDI_KEY, new HashMap ()); 102 103 String stringVal = "Hello World"; 104 Character charVal = new Character ('H'); 105 Byte byteVal = new Byte ((byte) 12); 106 Short shortVal = new Short ((short) 12345); 107 Integer intVal = new Integer (12345678); 108 Long longVal = new Long (1234567890123456L); 109 Float floatVal = new Float (123.456); 110 Double doubleVal = new Double (12345.6789); 111 Boolean 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 context = EnterpriseNamingContext.createEnterpriseNamingContext((Map ) componentContext.get(NamingBuilder.JNDI_KEY)); 123 Set actual = new HashSet (); 124 for (NamingEnumeration e = context.listBindings("env"); e.hasMore();) { 125 NameClassPair pair = (NameClassPair ) e.next(); 126 actual.add(pair.getName()); 127 } 128 Set expected = new HashSet (Arrays.asList(new String []{"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 { 142 Context context = EnterpriseNamingContext.createEnterpriseNamingContext(componentContext); 143 try { 144 Context env = (Context ) context.lookup("env"); 145 assertNotNull(env); 146 } catch (NamingException e) { 147 e.printStackTrace(); 148 fail(); 149 } 150 } 151 152 } 153 | Popular Tags |