1 16 17 package org.springframework.jndi; 18 19 import java.util.HashMap ; 20 import java.util.Hashtable ; 21 import java.util.Map ; 22 23 import javax.naming.Binding ; 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 import javax.naming.NameClassPair ; 27 import javax.naming.NameNotFoundException ; 28 import javax.naming.NamingEnumeration ; 29 import javax.naming.NamingException ; 30 import javax.naming.spi.InitialContextFactory ; 31 import javax.sql.DataSource ; 32 33 import junit.framework.TestCase; 34 35 import org.springframework.jdbc.datasource.DriverManagerDataSource; 36 import org.springframework.mock.jndi.SimpleNamingContext; 37 import org.springframework.mock.jndi.SimpleNamingContextBuilder; 38 39 42 public class SimpleNamingContextTests extends TestCase { 43 44 public SimpleNamingContextTests(String s) { 45 super(s); 46 } 47 48 public void testNamingContextBuilder() throws NamingException { 49 SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder(); 50 InitialContextFactory factory = builder.createInitialContextFactory(null); 51 52 DataSource ds = new DriverManagerDataSource(); 53 builder.bind("java:comp/env/jdbc/myds", ds); 54 Object obj = new Object (); 55 builder.bind("myobject", obj); 56 57 Context context1 = factory.getInitialContext(null); 58 assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds); 59 assertTrue("Correct Object registered", context1.lookup("myobject") == obj); 60 61 Hashtable env2 = new Hashtable (); 62 env2.put("key1", "value1"); 63 Context context2 = factory.getInitialContext(env2); 64 assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds); 65 assertTrue("Correct Object registered", context2.lookup("myobject") == obj); 66 assertTrue("Correct environment", context2.getEnvironment() != env2); 67 assertTrue("Correct key1", "value1".equals(context2.getEnvironment().get("key1"))); 68 69 Integer i = new Integer (0); 70 context1.rebind("myinteger", i); 71 String s = ""; 72 context2.bind("mystring", s); 73 74 Context context3 = (Context ) context2.lookup(""); 75 context3.rename("java:comp/env/jdbc/myds", "jdbc/myds"); 76 context3.unbind("myobject"); 77 78 assertTrue("Correct environment", context3.getEnvironment() != context2.getEnvironment()); 79 context3.addToEnvironment("key2", "value2"); 80 assertTrue("key2 added", "value2".equals(context3.getEnvironment().get("key2"))); 81 context3.removeFromEnvironment("key1"); 82 assertTrue("key1 removed", context3.getEnvironment().get("key1") == null); 83 84 assertTrue("Correct DataSource registered", context1.lookup("jdbc/myds") == ds); 85 try { 86 context1.lookup("myobject"); 87 fail("Should have thrown NameNotFoundException"); 88 } 89 catch (NameNotFoundException ex) { 90 } 92 assertTrue("Correct Integer registered", context1.lookup("myinteger") == i); 93 assertTrue("Correct String registered", context1.lookup("mystring") == s); 94 95 assertTrue("Correct DataSource registered", context2.lookup("jdbc/myds") == ds); 96 try { 97 context2.lookup("myobject"); 98 fail("Should have thrown NameNotFoundException"); 99 } 100 catch (NameNotFoundException ex) { 101 } 103 assertTrue("Correct Integer registered", context2.lookup("myinteger") == i); 104 assertTrue("Correct String registered", context2.lookup("mystring") == s); 105 106 assertTrue("Correct DataSource registered", context3.lookup("jdbc/myds") == ds); 107 try { 108 context3.lookup("myobject"); 109 fail("Should have thrown NameNotFoundException"); 110 } 111 catch (NameNotFoundException ex) { 112 } 114 assertTrue("Correct Integer registered", context3.lookup("myinteger") == i); 115 assertTrue("Correct String registered", context3.lookup("mystring") == s); 116 117 Map bindingMap = new HashMap (); 118 NamingEnumeration bindingEnum = context3.listBindings(""); 119 while (bindingEnum.hasMoreElements()) { 120 Binding binding = (Binding ) bindingEnum.nextElement(); 121 bindingMap.put(binding.getName(), binding); 122 } 123 assertTrue("Correct jdbc subcontext", ((Binding ) bindingMap.get("jdbc")).getObject() instanceof Context ); 124 assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(((Binding ) bindingMap.get("jdbc")).getClassName())); 125 126 Context jdbcContext = (Context ) context3.lookup("jdbc"); 127 jdbcContext.bind("mydsX", ds); 128 Map subBindingMap = new HashMap (); 129 NamingEnumeration subBindingEnum = jdbcContext.listBindings(""); 130 while (subBindingEnum.hasMoreElements()) { 131 Binding binding = (Binding ) subBindingEnum.nextElement(); 132 subBindingMap.put(binding.getName(), binding); 133 } 134 135 assertTrue("Correct DataSource registered", ds.equals(((Binding ) subBindingMap.get("myds")).getObject())); 136 assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(((Binding ) subBindingMap.get("myds")).getClassName())); 137 assertTrue("Correct DataSource registered", ds.equals(((Binding ) subBindingMap.get("mydsX")).getObject())); 138 assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(((Binding ) subBindingMap.get("mydsX")).getClassName())); 139 assertTrue("Correct Integer registered", i.equals(((Binding ) bindingMap.get("myinteger")).getObject())); 140 assertTrue("Correct Integer registered", Integer .class.getName().equals(((Binding ) bindingMap.get("myinteger")).getClassName())); 141 assertTrue("Correct String registered", s.equals(((Binding ) bindingMap.get("mystring")).getObject())); 142 assertTrue("Correct String registered", String .class.getName().equals(((Binding ) bindingMap.get("mystring")).getClassName())); 143 144 context1.bind("jdbc/sub/subds", ds); 145 146 Map pairMap = new HashMap (); 147 NamingEnumeration pairEnum = context2.list("jdbc"); 148 while (pairEnum.hasMore()) { 149 NameClassPair pair = (NameClassPair ) pairEnum.next(); 150 pairMap.put(pair.getName(), pair.getClassName()); 151 } 152 assertTrue("Correct sub subcontext", SimpleNamingContext.class.getName().equals(pairMap.get("sub"))); 153 154 Context subContext = (Context ) context2.lookup("jdbc/sub"); 155 Map subPairMap = new HashMap (); 156 NamingEnumeration subPairEnum = subContext.list(""); 157 while (subPairEnum.hasMoreElements()) { 158 NameClassPair pair = (NameClassPair ) subPairEnum.next(); 159 subPairMap.put(pair.getName(), pair.getClassName()); 160 } 161 162 assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(subPairMap.get("subds"))); 163 assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("myds"))); 164 assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("mydsX"))); 165 166 pairMap.clear(); 167 pairEnum = context1.list("jdbc/"); 168 while (pairEnum.hasMore()) { 169 NameClassPair pair = (NameClassPair ) pairEnum.next(); 170 pairMap.put(pair.getName(), pair.getClassName()); 171 } 172 assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("myds"))); 173 assertTrue("Correct DataSource registered", DriverManagerDataSource.class.getName().equals(pairMap.get("mydsX"))); 174 } 175 176 181 public void testCreateInitialContext() throws Exception { 182 SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder(); 183 String name = "foo"; 184 Object o = new Object (); 185 builder.bind(name, o); 186 Context ctx = new InitialContext (); 188 assertTrue(ctx.lookup(name) == o); 189 ctx.unbind(name); 191 try { 192 ctx = new InitialContext (); 193 ctx.lookup(name); 194 fail("Should have thrown NamingException"); 195 } 196 catch (NamingException ex) { 197 } 199 200 builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder(); 202 try { 203 ctx = new InitialContext (); 204 ctx.lookup(name); 205 fail("Should have thrown NamingException"); 206 } 207 catch (NamingException ex) { 208 } 210 Object o2 = new Object (); 211 builder.bind(name, o2); 212 assertEquals(ctx.lookup(name), o2); 213 } 214 215 } 216 | Popular Tags |