1 16 package org.apache.naming; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.NoSuchElementException ; 22 23 import javax.naming.Binding ; 24 import javax.naming.CompositeName ; 25 import javax.naming.Context ; 26 import javax.naming.NameClassPair ; 27 import javax.naming.NamingEnumeration ; 28 import javax.naming.NamingException ; 29 import javax.naming.OperationNotSupportedException ; 30 31 import junit.framework.TestCase; 32 33 53 public abstract class AbstractContextTest extends TestCase { 54 55 public AbstractContextTest(String name) { 56 super(name); 57 } 58 59 61 62 protected Context initialContext; 63 64 65 protected Context firstContext; 66 67 68 protected Context secondContext; 69 70 71 protected HashMap binding; 72 73 75 76 protected String firstContextName() { 77 return "java:comp"; 78 } 79 80 81 protected String secondContextName() { 82 return "env"; 83 } 84 85 86 protected String firstBoundName() { 87 return "hello"; 88 } 89 90 91 protected Object firstBoundObject() { 92 return "Hello"; 93 } 94 95 96 protected String secondBoundName() { 97 return "world"; 98 } 99 100 101 protected Object secondBoundObject() { 102 return "World"; 103 } 104 105 107 111 protected boolean isGetNameInNamespaceSupported() { 112 return true; 113 } 114 115 119 protected boolean isWritable() { 120 return true; 121 } 122 123 126 protected abstract Context makeInitialContext(); 127 128 130 133 protected void addBindings() throws Exception { 134 secondContext.bind(firstBoundName(), firstBoundObject()); 135 secondContext.bind(secondBoundName(), secondBoundObject()); 136 binding.put(firstBoundName(), firstBoundObject()); 137 binding.put(secondBoundName(), secondBoundObject()); 138 } 139 140 143 protected void removeBindings() throws Exception { 144 secondContext.unbind(firstBoundName()); 145 secondContext.unbind(secondBoundName()); 146 binding.clear(); 147 } 148 149 protected void setUp() throws Exception { 150 super.setUp(); 151 binding = new HashMap (); 152 initialContext = makeInitialContext(); 153 if (isWritable()) { 154 firstContext = initialContext.createSubcontext(firstContextName()); 155 secondContext = firstContext.createSubcontext(secondContextName()); 156 addBindings(); 157 } 158 } 159 160 protected void tearDown() throws Exception { 161 if (isWritable()) { 162 removeBindings(); 163 firstContext.destroySubcontext(secondContextName()); 164 initialContext.destroySubcontext(firstContextName()); 165 } 166 initialContext = null; 167 } 168 169 171 176 protected void verifyLookup(Object boundObject, Object returnedObject) { 177 assertEquals(boundObject, returnedObject); 178 } 179 180 185 protected void verifyList(Map expected, Map returned) { 186 assertEquals(expected, returned); 187 } 188 189 194 protected void verifyListBindings(Map expected, Map returned) { 195 assertEquals(expected, returned); 196 } 197 198 200 public void testInitialContext() throws NamingException { 201 verifyLookup(firstBoundObject(), 202 initialContext.lookup(firstContextName() + "/" 203 + secondContextName() +"/" + firstBoundName())); 204 verifyLookup(secondBoundObject(), 205 initialContext.lookup(new CompositeName 206 (firstContextName() + "/" + secondContextName() + "/" + secondBoundName()))); 207 verifyLookup(secondContext.lookup(firstBoundName()), 208 ((Context ) secondContext.lookup("")).lookup(firstBoundName())); 209 } 210 211 public void testLookup() throws NamingException { 212 verifyLookup(firstBoundObject(), secondContext.lookup(firstBoundName())); 213 verifyLookup(firstBoundObject(), 214 firstContext.lookup(secondContextName() + "/" +firstBoundName())); 215 try { 216 secondContext.lookup("foo"); 217 fail("expecting NamingException"); 218 } catch (NamingException e) { 219 } 221 verifyLookup(firstBoundObject(), 222 secondContext.lookup(new CompositeName (firstBoundName()))); 223 verifyLookup(firstBoundObject(), 224 firstContext.lookup(new CompositeName (secondContextName() + "/" + firstBoundName()))); 225 verifyLookup(secondBoundObject(), 226 ((Context ) initialContext.lookup(firstContextName())).lookup(secondContextName() + "/" + secondBoundName())); 227 } 228 229 public void testComposeName() throws NamingException { 230 assertEquals("org/research/user/jane", 231 secondContext.composeName("user/jane", "org/research")); 232 assertEquals("research/user/jane", 233 secondContext.composeName("user/jane", "research")); 234 assertEquals(new CompositeName ("org/research/user/jane"), 235 secondContext.composeName(new CompositeName ("user/jane"), 236 new CompositeName ("org/research"))); 237 assertEquals(new CompositeName ("research/user/jane"), 238 secondContext.composeName(new CompositeName ("user/jane"), 239 new CompositeName ("research"))); 240 } 241 242 public void testList() throws NamingException { 243 NamingEnumeration enumeration; 244 Map expected; 245 Map result; 246 247 expected = new HashMap (); 248 for (Iterator i = binding.entrySet().iterator(); i.hasNext();) { 249 Map.Entry entry = (Map.Entry ) i.next(); 250 expected.put(entry.getKey(), entry.getValue().getClass().getName()); 251 } 252 enumeration = secondContext.list(""); 253 result = new HashMap (); 254 while (enumeration.hasMore()) { 255 NameClassPair pair = (NameClassPair ) enumeration.next(); 256 result.put(pair.getName(), pair.getClassName()); 257 } 258 verifyList(expected, result); 259 260 try { 261 enumeration.next(); 262 fail("Expecting NoSuchElementException"); 263 } catch (NoSuchElementException e) { 264 } 266 try { 267 enumeration.nextElement(); 268 fail("Expecting NoSuchElementException"); 269 } catch (NoSuchElementException e) { 270 } 272 } 273 274 public void testListBindings() throws NamingException { 275 NamingEnumeration enumeration; 276 Map result; 277 enumeration = secondContext.listBindings(""); 278 result = new HashMap (); 279 while (enumeration.hasMore()) { 280 Binding pair = (Binding ) enumeration.next(); 281 result.put(pair.getName(), pair.getObject()); 282 } 283 verifyListBindings(binding, result); 284 285 try { 286 enumeration.next(); 287 fail("Expecting NoSuchElementException"); 288 } catch (NoSuchElementException e) { 289 } 291 try { 292 enumeration.nextElement(); 293 fail("Expecting NoSuchElementException"); 294 } catch (NoSuchElementException e) { 295 } 297 } 298 299 303 public void testGetNameInNamespace() throws Exception { 304 if (isGetNameInNamespaceSupported()) { 305 String name = initialContext.getNameInNamespace(); 306 if (name == null) { 307 fail("Null NameInNamespace for initial context"); 308 } 309 } else { 310 try { 311 String name = firstContext.getNameInNamespace(); 312 fail("Expecting OperationNotSupportedException"); 313 } catch(OperationNotSupportedException ex) { 314 } 316 } 317 } 318 319 322 public void testRebind() throws Exception { 323 secondContext.rebind(firstBoundName(), secondBoundObject()); 324 secondContext.rebind(secondBoundName(), firstBoundObject()); 325 binding.put(firstBoundName(), secondBoundObject()); 326 binding.put(secondBoundName(), firstBoundObject()); 327 NamingEnumeration enumeration; 328 Map result; 329 enumeration = secondContext.listBindings(""); 330 result = new HashMap (); 331 while (enumeration.hasMore()) { 332 Binding pair = (Binding ) enumeration.next(); 333 result.put(pair.getName(), pair.getObject()); 334 } 335 verifyListBindings(binding, result); 336 } 337 } 338 | Popular Tags |