1 17 18 package org.apache.geronimo.naming.java; 19 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.NoSuchElementException ; 24 25 import javax.naming.Binding ; 26 import javax.naming.CompositeName ; 27 import javax.naming.CompoundName ; 28 import javax.naming.Context ; 29 import javax.naming.NameClassPair ; 30 import javax.naming.NamingEnumeration ; 31 import javax.naming.NamingException ; 32 33 38 public class BasicContextTest extends AbstractContextTest { 39 40 public void testInitialContext() throws NamingException { 41 assertEquals("Hello", initialContext.lookup("java:comp/env/hello")); 42 assertEquals("Hello", initialContext.lookup(new CompositeName ("java:comp/env/hello"))); 43 } 44 45 public void testLookup() throws NamingException { 46 assertEquals("Hello", envContext.lookup("hello")); 47 assertEquals("Hello", compContext.lookup("env/hello")); 48 try { 49 envContext.lookup("foo"); 50 fail(); 51 } catch (NamingException e) { 52 } 54 assertEquals("Hello", envContext.lookup(new CompositeName ("hello"))); 55 assertEquals("Hello", compContext.lookup(new CompositeName ("env/hello"))); 56 assertEquals("Hello", envContext.lookup(new CompoundName ("hello", syntax))); 57 assertEquals("Hello", compContext.lookup(new CompoundName ("env/hello", syntax))); 58 59 assertEquals(envContext, envContext.lookup("")); 60 } 61 62 public void testSubContext() throws NamingException { 63 assertEquals("long name", initialContext.lookup("java:comp/env/here/there/anywhere")); 64 Context intermediate = (Context )initialContext.lookup("java:comp/env/here/there"); 65 assertNotNull(intermediate); 66 assertEquals("long name", intermediate.lookup("anywhere")); 67 } 68 69 public void testSchemeLookup() throws NamingException { 70 assertEquals("Hello", envContext.lookup("java:comp/env/hello")); 72 assertEquals("Hello", compContext.lookup("java:comp/env/hello")); 73 } 74 75 public void testLookupLink() throws NamingException { 76 assertEquals("Hello", envContext.lookup("link")); 77 } 78 79 public void testComposeName() throws NamingException { 80 assertEquals("org/research/user/jane", envContext.composeName("user/jane", "org/research")); 81 assertEquals("research/user/jane", envContext.composeName("user/jane", "research")); 82 assertEquals(new CompositeName ("org/research/user/jane"), envContext.composeName(new CompositeName ("user/jane"), new CompositeName ("org/research"))); 83 assertEquals(new CompositeName ("research/user/jane"), envContext.composeName(new CompositeName ("user/jane"), new CompositeName ("research"))); 84 } 85 86 public void testList() throws NamingException { 87 NamingEnumeration ne; 88 Map expected; 89 Map result; 90 91 expected = new HashMap (); 92 for (Iterator i = envBinding.entrySet().iterator(); i.hasNext();) { 93 Map.Entry entry = (Map.Entry ) i.next(); 94 expected.put(entry.getKey(), entry.getValue().getClass().getName()); 95 } 96 ne = envContext.list(""); 97 result = new HashMap (); 98 while (ne.hasMore()) { 99 NameClassPair pair = (NameClassPair ) ne.next(); 100 result.put(pair.getName(), pair.getClassName()); 101 } 102 assertEquals(expected, result); 103 104 try { 105 ne.next(); 106 fail(); 107 } catch (NoSuchElementException e) { 108 } 110 try { 111 ne.nextElement(); 112 fail(); 113 } catch (NoSuchElementException e) { 114 } 116 } 117 118 public void testListBindings() throws NamingException { 119 NamingEnumeration ne; 120 ne = envContext.listBindings(""); 121 int count = 0; 122 while (ne.hasMore()) { 123 count ++; 124 Binding pair = (Binding ) ne.next(); 125 assertTrue(envBinding.containsKey(pair.getName())); 126 if (! (envBinding.get(pair.getName()) instanceof ReadOnlyContext)) { 127 assertEquals(pair.getObject(), envBinding.get(pair.getName())); 128 } 129 } 130 assertEquals(envBinding.size(), count); 131 132 try { 133 ne.next(); 134 fail(); 135 } catch (NoSuchElementException e) { 136 } 138 try { 139 ne.nextElement(); 140 fail(); 141 } catch (NoSuchElementException e) { 142 } 144 } 145 146 public void testSpeed() throws NamingException { 147 Context comp = (Context ) initialContext.lookup("java:comp"); 148 149 long start = System.currentTimeMillis(); 150 for (int i=0; i < 1000000; i++) { 151 comp.lookup("env/hello"); 154 } 155 156 long end = System.currentTimeMillis(); 157 System.out.println("lookup(String) milliseconds: " + (end - start)); 158 } 159 160 } 161 | Popular Tags |