1 16 package org.apache.naming; 17 18 import java.util.HashMap ; 19 import java.util.Hashtable ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.NoSuchElementException ; 23 24 import javax.naming.Binding ; 25 import javax.naming.CompositeName ; 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.naming.NameClassPair ; 29 import javax.naming.NamingEnumeration ; 30 import javax.naming.NamingException ; 31 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 import junit.textui.TestRunner; 36 37 import org.apache.naming.java.javaURLContextFactory; 38 39 45 public class BasicContextTest extends TestCase { 46 private HashMap envBinding; 47 private Context initialContext; 48 private Context compContext; 49 private Context envContext; 50 51 public BasicContextTest(String name) { 52 super(name); 53 } 54 55 public static void main(String [] args) { 56 TestRunner.run(suite()); 57 } 58 59 public static Test suite() { 60 TestSuite suite = new TestSuite(BasicContextTest.class); 61 suite.setName("Basic Context Tests"); 62 return suite; 63 } 64 65 protected void setUp() throws Exception { 66 super.setUp(); 67 Hashtable env = new Hashtable (); 68 envBinding = new HashMap (); 69 env.put(Context.INITIAL_CONTEXT_FACTORY, 70 "org.apache.naming.java.javaURLContextFactory"); 71 env.put(Context.URL_PKG_PREFIXES,"org.apache.naming"); 72 initialContext = new InitialContext (env); 73 compContext = initialContext.createSubcontext("java:comp"); 74 envContext = compContext.createSubcontext("env"); 75 envContext.bind("hello", "Hello"); 76 envContext.bind("world", "World"); 77 envBinding.put("hello", "Hello"); 78 envBinding.put("world", "World"); 79 ContextAccessController.setReadOnly(javaURLContextFactory.MAIN); 80 ContextAccessController.setSecurityToken(javaURLContextFactory.MAIN,"x"); 81 } 82 83 protected void tearDown() throws Exception { 84 ContextAccessController.setWritable(javaURLContextFactory.MAIN,"x"); 85 compContext.destroySubcontext("env"); 86 initialContext.destroySubcontext("java:comp"); 87 initialContext = null; 88 } 89 90 public void testInitialContext() throws NamingException { 91 assertEquals("Hello", initialContext.lookup("java:comp/env/hello")); 92 assertEquals("World", initialContext.lookup(new CompositeName ("java:comp/env/world"))); 93 assertEquals(envContext.lookup("hello"), 94 ((Context ) envContext.lookup("")).lookup("hello")); 95 } 96 97 public void testLookup() throws NamingException { 98 assertEquals("Hello", envContext.lookup("hello")); 99 assertEquals("Hello", compContext.lookup("env/hello")); 100 try { 101 envContext.lookup("foo"); 102 fail("expecting NamingException"); 103 } catch (NamingException e) { 104 } 106 assertEquals("Hello", envContext.lookup(new CompositeName ("hello"))); 107 assertEquals("Hello", compContext.lookup(new CompositeName ("env/hello"))); 108 assertEquals("World", 109 ((Context ) initialContext.lookup("java:comp")).lookup("env/world")); 110 } 111 112 113 public void testComposeName() throws NamingException { 114 assertEquals("org/research/user/jane", 115 envContext.composeName("user/jane", "org/research")); 116 assertEquals("research/user/jane", 117 envContext.composeName("user/jane", "research")); 118 assertEquals(new CompositeName ("org/research/user/jane"), 119 envContext.composeName(new CompositeName ("user/jane"), 120 new CompositeName ("org/research"))); 121 assertEquals(new CompositeName ("research/user/jane"), 122 envContext.composeName(new CompositeName ("user/jane"), 123 new CompositeName ("research"))); 124 } 125 126 public void testList() throws NamingException { 127 NamingEnumeration enumeration; 128 Map expected; 129 Map result; 130 131 expected = new HashMap (); 132 for (Iterator i = envBinding.entrySet().iterator(); i.hasNext();) { 133 Map.Entry entry = (Map.Entry ) i.next(); 134 expected.put(entry.getKey(), entry.getValue().getClass().getName()); 135 } 136 enumeration = envContext.list(""); 137 result = new HashMap (); 138 while (enumeration.hasMore()) { 139 NameClassPair pair = (NameClassPair ) enumeration.next(); 140 result.put(pair.getName(), pair.getClassName()); 141 } 142 assertEquals(expected, result); 143 144 try { 145 enumeration.next(); 146 fail(); 147 } catch (NoSuchElementException e) { 148 } 150 try { 151 enumeration.nextElement(); 152 fail(); 153 } catch (NoSuchElementException e) { 154 } 156 } 157 158 public void testListBindings() throws NamingException { 159 NamingEnumeration enumeration; 160 Map result; 161 enumeration = envContext.listBindings(""); 162 result = new HashMap (); 163 while (enumeration.hasMore()) { 164 Binding pair = (Binding ) enumeration.next(); 165 result.put(pair.getName(), pair.getObject()); 166 } 167 assertEquals(envBinding, result); 168 169 try { 170 enumeration.next(); 171 fail(); 172 } catch (NoSuchElementException e) { 173 } 175 try { 176 enumeration.nextElement(); 177 fail(); 178 } catch (NoSuchElementException e) { 179 } 181 } 182 183 public void testAccess() throws NamingException { 184 try { 185 envContext.bind("goodbye", "Goodbye"); 186 fail("expecting NamingException"); } catch (NamingException ex) {} 188 ContextAccessController.setWritable(javaURLContextFactory.MAIN,"x"); 189 envContext.bind("goodbye", "Goodbye"); } 191 } 192 | Popular Tags |