1 17 package org.apache.geronimo.gjndi; 18 19 import junit.framework.TestCase; 20 21 import javax.naming.NamingException ; 22 import javax.naming.CompositeName ; 23 import javax.naming.CompoundName ; 24 import javax.naming.Context ; 25 import javax.naming.NamingEnumeration ; 26 import javax.naming.NameClassPair ; 27 import javax.naming.Binding ; 28 import javax.naming.LinkRef ; 29 import javax.naming.InitialContext ; 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.NoSuchElementException ; 34 import java.util.Properties ; 35 import java.util.Collections ; 36 37 import org.apache.geronimo.naming.java.RootContext; 38 import org.apache.xbean.naming.context.ImmutableContext; 39 import org.apache.xbean.naming.context.WritableContext; 40 import org.apache.xbean.naming.context.ContextAccess; 41 import org.apache.xbean.naming.global.GlobalContextManager; 42 43 46 public class JavaCompContextTest extends TestCase { 47 protected Context readOnlyContext; 48 protected Properties syntax; 49 protected Map envBinding; 50 protected Context initialContext; 51 protected Context compContext; 52 protected Context envContext; 53 54 public void testInitialContext() throws NamingException { 55 assertEquals("Hello", initialContext.lookup("java:comp/env/hello")); 56 assertEquals("Hello", initialContext.lookup(new CompositeName ("java:comp/env/hello"))); 57 } 58 59 public void testLookup() throws NamingException { 60 assertEquals("Hello", envContext.lookup("hello")); 61 assertEquals("Hello", compContext.lookup("env/hello")); 62 try { 63 envContext.lookup("foo"); 64 fail(); 65 } catch (NamingException e) { 66 } 68 assertEquals("Hello", envContext.lookup(new CompositeName ("hello"))); 69 assertEquals("Hello", compContext.lookup(new CompositeName ("env/hello"))); 70 assertEquals("Hello", envContext.lookup(new CompoundName ("hello", syntax))); 71 assertEquals("Hello", compContext.lookup(new CompoundName ("env/hello", syntax))); 72 73 assertEquals(envContext, envContext.lookup("")); 74 } 75 76 public void testSubContext() throws NamingException { 77 assertEquals("long name", initialContext.lookup("java:comp/env/here/there/anywhere")); 78 Context intermediate = (Context )initialContext.lookup("java:comp/env/here/there"); 79 assertNotNull(intermediate); 80 assertEquals("long name", intermediate.lookup("anywhere")); 81 } 82 83 public void testSchemeLookup() throws NamingException { 84 assertEquals("Hello", envContext.lookup("java:comp/env/hello")); 86 assertEquals("Hello", compContext.lookup("java:comp/env/hello")); 87 } 88 89 public void testLookupLink() throws NamingException { 90 assertEquals("Hello", envContext.lookup("link")); 91 } 92 93 public void testComposeName() throws NamingException { 94 assertEquals("org/research/user/jane", envContext.composeName("user/jane", "org/research")); 95 assertEquals("research/user/jane", envContext.composeName("user/jane", "research")); 96 assertEquals(new CompositeName ("org/research/user/jane"), envContext.composeName(new CompositeName ("user/jane"), new CompositeName ("org/research"))); 97 assertEquals(new CompositeName ("research/user/jane"), envContext.composeName(new CompositeName ("user/jane"), new CompositeName ("research"))); 98 } 99 100 public void testList() throws NamingException { 101 NamingEnumeration ne; 102 Map expected; 103 Map result; 104 105 expected = new HashMap (); 106 for (Iterator i = envBinding.entrySet().iterator(); i.hasNext();) { 107 Map.Entry entry = (Map.Entry ) i.next(); 108 expected.put(entry.getKey(), entry.getValue().getClass().getName()); 109 } 110 ne = envContext.list(""); 111 result = new HashMap (); 112 while (ne.hasMore()) { 113 NameClassPair pair = (NameClassPair ) ne.next(); 114 result.put(pair.getName(), pair.getClassName()); 115 } 116 assertEquals(expected, result); 117 118 try { 119 ne.next(); 120 fail(); 121 } catch (NoSuchElementException e) { 122 } 124 try { 125 ne.nextElement(); 126 fail(); 127 } catch (NoSuchElementException e) { 128 } 130 } 131 132 public void testListBindings() throws NamingException { 133 NamingEnumeration ne; 134 ne = envContext.listBindings(""); 135 int count = 0; 136 while (ne.hasMore()) { 137 count ++; 138 Binding pair = (Binding ) ne.next(); 139 assertTrue(envBinding.containsKey(pair.getName())); 140 if (! (envBinding.get(pair.getName()) instanceof Context )) { 141 assertEquals(pair.getObject(), envBinding.get(pair.getName())); 142 } 143 } 144 assertEquals(envBinding.size(), count); 145 146 try { 147 ne.next(); 148 fail(); 149 } catch (NoSuchElementException e) { 150 } 152 try { 153 ne.nextElement(); 154 fail(); 155 } catch (NoSuchElementException e) { 156 } 158 } 159 160 public void testSpeed() throws NamingException { 161 Context comp = (Context ) initialContext.lookup("java:comp"); 162 163 long start = System.currentTimeMillis(); 164 for (int i=0; i < 1000000; i++) { 165 comp.lookup("env/hello"); 168 } 169 170 long end = System.currentTimeMillis(); 171 System.out.println("lookup(String) milliseconds: " + (end - start)); 172 } 173 174 protected void setUp() throws Exception { 175 super.setUp(); 176 System.setProperty("java.naming.factory.initial", GlobalContextManager.class.getName()); 177 System.setProperty("java.naming.factory.url.pkgs", "org.apache.geronimo.knaming"); 178 179 LinkRef link = new LinkRef ("java:comp/env/hello"); 180 181 Map bindings = new HashMap (); 182 bindings.put("env/hello", "Hello"); 183 bindings.put("env/world", "Hello World"); 184 bindings.put("env/here/there/anywhere", "long name"); 185 bindings.put("env/link", link); 186 187 readOnlyContext = new WritableContext("", bindings, ContextAccess.UNMODIFIABLE); 188 189 envBinding = new HashMap (); 190 envBinding.put("hello", "Hello"); 191 envBinding.put("world", "Hello World"); 192 envBinding.put("here", readOnlyContext.lookup("env/here")); 193 envBinding.put("link", link); 194 195 RootContext.setComponentContext(readOnlyContext); 196 197 Context javaCompContext = new JavaCompContextGBean(); 198 Context globalContext = new ImmutableContext(Collections.singletonMap(javaCompContext.getNameInNamespace(), javaCompContext)); 199 GlobalContextManager.setGlobalContext(globalContext); 200 201 initialContext = new InitialContext (); 202 compContext = (Context ) initialContext.lookup("java:comp"); 203 envContext = (Context ) initialContext.lookup("java:comp/env"); 204 205 syntax = new Properties (); 206 } 207 } 208 | Popular Tags |