1 17 18 package org.apache.geronimo.naming.java; 19 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.HashSet ; 23 import java.util.List ; 24 import java.util.Set ; 25 import javax.management.ObjectName ; 26 import javax.naming.NameClassPair ; 27 import javax.naming.NamingEnumeration ; 28 import javax.naming.NamingException ; 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 42 public class ContextBuilderTest extends TestCase { 43 private ComponentContextBuilder builder; 44 45 private List proxy; 46 47 public void testEnvEntries() throws Exception { 48 String stringVal = "Hello World"; 49 Character charVal = new Character ('H'); 50 Byte byteVal = new Byte ((byte) 12); 51 Short shortVal = new Short ((short) 12345); 52 Integer intVal = new Integer (12345678); 53 Long longVal = new Long (1234567890123456L); 54 Float floatVal = new Float (123.456); 55 Double doubleVal = new Double (12345.6789); 56 Boolean booleanVal = Boolean.TRUE; 57 builder.addEnvEntry("string", String .class.getName(), stringVal, null); 58 builder.addEnvEntry("char", Character .class.getName(), charVal.toString(), null); 59 builder.addEnvEntry("byte", Byte .class.getName(), byteVal.toString(), null); 60 builder.addEnvEntry("short", Short .class.getName(), shortVal.toString(), null); 61 builder.addEnvEntry("int", Integer .class.getName(), intVal.toString(), null); 62 builder.addEnvEntry("long", Long .class.getName(), longVal.toString(), null); 63 builder.addEnvEntry("float", Float .class.getName(), floatVal.toString(), null); 64 builder.addEnvEntry("double", Double .class.getName(), doubleVal.toString(), null); 65 builder.addEnvEntry("boolean", Boolean .class.getName(), booleanVal.toString(), null); 66 67 SimpleReadOnlyContext context = new SimpleReadOnlyContext(builder.getContext()); 68 Set actual = new HashSet (); 69 for (NamingEnumeration e = context.listBindings("env"); e.hasMore();) { 70 NameClassPair pair = (NameClassPair ) e.next(); 71 actual.add(pair.getName()); 72 } 73 Set expected = new HashSet (Arrays.asList(new String []{"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 { 87 proxy = new ArrayList (); 88 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 proxyFactoryName = null; 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 o = context.lookup("env/resourceenvref"); 101 assertEquals(proxy, o); 102 } finally { 103 kernel.shutdown(); 104 } 105 } 106 107 public void testEmptyEnvironment() throws NamingException { 108 SimpleReadOnlyContext context = new SimpleReadOnlyContext(builder.getContext()); 109 try { 110 ReadOnlyContext env = (ReadOnlyContext) context.lookup("env"); 111 assertNotNull(env); 112 } catch (NamingException e) { 113 fail(); 114 } 115 } 116 117 protected void setUp() throws Exception { 118 super.setUp(); 119 builder = new ComponentContextBuilder(); 121 } 122 123 public static class TestProxyFactory { 124 125 private Object proxy; 126 127 public TestProxyFactory(Object proxy) { 128 this.proxy = proxy; 129 } 130 131 public Object getProxy() { 132 return proxy; 133 } 134 135 } 136 137 public GBeanInfo getGbeanInfo() { 138 GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(TestProxyFactory.class); 139 infoFactory.addAttribute("Content", Object .class, true); 140 infoFactory.addOperation("getProxy"); 141 infoFactory.setConstructor(new String []{"Content"}); 142 return infoFactory.getBeanInfo(); 143 } 144 } 145 | Popular Tags |