1 /** 2 * 3 * Copyright 2003-2004 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 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 /** 34 * Unit tests for basic ops on an {@link javax.naming.InitialContext}. 35 * 36 * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $ 37 */ 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 // OK 53 } 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 // envContext.lookup("dns:apache.org"); 71 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 // ok 109 } 110 try { 111 ne.nextElement(); 112 fail(); 113 } catch (NoSuchElementException e) { 114 // ok 115 } 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 // ok 137 } 138 try { 139 ne.nextElement(); 140 fail(); 141 } catch (NoSuchElementException e) { 142 // ok 143 } 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 // initialContext.lookup("java:comp/hello"); // this is sloooow due to scheme resolution 152 // envContext.lookup("hello"); 153 comp.lookup("env/hello"); 154 } 155 156 long end = System.currentTimeMillis(); 157 System.out.println("lookup(String) milliseconds: " + (end - start)); 158 } 159 160 }