1 16 package org.apache.naming.resources; 17 18 import java.io.ByteArrayInputStream ; 19 import java.util.HashMap ; 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.Name ; 28 import javax.naming.NameClassPair ; 29 import javax.naming.NamingEnumeration ; 30 import javax.naming.NamingException ; 31 import javax.naming.NameParser ; 32 import javax.naming.OperationNotSupportedException ; 33 import javax.naming.directory.DirContext ; 34 import javax.naming.directory.Attribute ; 35 import javax.naming.directory.Attributes ; 36 import javax.naming.directory.BasicAttribute ; 37 import javax.naming.directory.ModificationItem ; 38 39 import junit.framework.TestCase; 40 41 56 public abstract class AbstractDirContextTest extends TestCase { 57 public AbstractDirContextTest(String name) { 58 super(name); 59 } 60 62 63 protected Context initialContext; 64 65 66 protected Context firstContext; 67 68 69 protected Context secondContext; 70 71 72 protected HashMap binding; 73 74 76 80 protected boolean isGetNameInNamespaceSupported() { 81 return true; 82 } 83 84 88 protected boolean isWritable() { 89 return true; 90 } 91 92 95 protected abstract Context makeInitialContext(); 96 97 99 100 protected String firstContextName() { 101 return "firstDir"; 102 } 103 104 105 protected String secondContextName() { 106 return "secondDir"; 107 } 108 109 110 protected String firstBoundName() { 111 return "test1"; 112 } 113 114 115 protected Object firstBoundObject() { 116 return new Resource(new ByteArrayInputStream (bytes)); 117 } 118 119 120 protected String secondBoundName() { 121 return "test2"; 122 } 123 124 125 protected Object secondBoundObject() { 126 return new Resource(new ByteArrayInputStream (bytes)); 127 } 128 129 131 134 protected void addBindings() throws Exception { 135 secondContext.bind(firstBoundName(), firstBoundObject()); 136 secondContext.bind(secondBoundName(), secondBoundObject()); 137 binding.put(firstBoundName(), firstBoundObject()); 138 binding.put(secondBoundName(), secondBoundObject()); 139 } 140 141 144 protected void removeBindings() throws Exception { 145 secondContext.unbind(firstBoundName()); 146 secondContext.unbind(secondBoundName()); 147 binding.clear(); 148 } 149 150 protected void setUp() throws Exception { 151 super.setUp(); 152 binding = new HashMap (); 153 initialContext = makeInitialContext(); 154 if (isWritable()) { 155 firstContext = initialContext.createSubcontext(firstContextName()); 156 secondContext = firstContext.createSubcontext(secondContextName()); 157 addBindings(); 158 } 159 nameParser = initialContext.getNameParser(""); 160 } 161 162 protected void tearDown() throws Exception { 163 if (isWritable()) { 164 removeBindings(); 165 firstContext.destroySubcontext(secondContextName()); 166 initialContext.destroySubcontext(firstContextName()); 167 } 168 initialContext = null; 169 } 170 171 173 174 protected byte[] bytes = {'a', 'b', 'c'}; 175 176 179 protected Attributes expectedAttributes() { 180 return null; 181 } 182 183 184 protected NameParser nameParser = null; 185 186 188 192 protected boolean isSchemaSupported() { 193 return true; 194 } 195 196 200 protected boolean isAttributeModificationSupported() { 201 return true; 202 } 203 204 210 protected String replaceAttributeName() { 211 return null; 212 } 213 214 218 protected Object replaceAttributeValue() { 219 return null; 220 } 221 222 227 protected String addAttributeName() { 228 return null; 229 } 230 231 235 protected Object addAttributeValue() { 236 return null; 237 } 238 239 244 protected String removeAttributeName() { 245 return null; 246 } 247 248 250 255 protected void verifyLookup(Object boundObject, Object returnedObject) { 256 assertEquals(boundObject, returnedObject); 257 } 258 259 263 protected void verifyList(Map expected, Map returned) { 264 assertEquals(expected.keySet(), returned.keySet()); 265 Iterator iterator = returned.values().iterator(); 266 while (iterator.hasNext()) { 267 assertTrue(((String ) iterator.next()).length() > 0); 268 } 269 } 270 271 274 protected void verifyListBindings(Map expected, Map returned) { 275 assertEquals(expected.keySet(), returned.keySet()); 276 } 277 278 282 protected void verifyAttribute(DirContext context, String name, String attributeName, 283 Object attributeValue) throws Exception { 284 String attrIds[] = new String [1]; 285 attrIds[0] = attributeName; 286 Attributes attrs = context.getAttributes(name, attrIds); 287 assertTrue(attrs.get(attributeName).contains(attributeValue)); 288 } 289 290 294 protected void verifyAttributeMissing(DirContext context, String name, String attributeName) throws Exception { 295 String attrIds[] = new String [1]; 296 attrIds[0] = attributeName; 297 Attributes attrs = context.getAttributes(name, attrIds); 298 assertEquals(0, attrs.size()); 299 } 300 301 303 public void testInitialContext() throws NamingException { 304 verifyLookup(firstBoundObject(), 305 initialContext.lookup(firstContextName() + "/" 306 + secondContextName() +"/" + firstBoundName())); 307 verifyLookup(secondBoundObject(), 308 initialContext.lookup(new CompositeName 309 (firstContextName() + "/" + secondContextName() + "/" + secondBoundName()))); 310 verifyLookup(secondContext.lookup(firstBoundName()), 311 ((Context ) secondContext.lookup("")).lookup(firstBoundName())); 312 } 313 314 public void testLookup() throws NamingException { 315 verifyLookup(firstBoundObject(), secondContext.lookup(firstBoundName())); 316 verifyLookup(firstBoundObject(), 317 firstContext.lookup(secondContextName() + "/" +firstBoundName())); 318 try { 319 secondContext.lookup("foo"); 320 fail("expecting NamingException"); 321 } catch (NamingException e) { 322 } 324 verifyLookup(firstBoundObject(), 325 secondContext.lookup(new CompositeName (firstBoundName()))); 326 verifyLookup(firstBoundObject(), 327 firstContext.lookup(new CompositeName (secondContextName() + "/" + firstBoundName()))); 328 verifyLookup(secondBoundObject(), 329 ((Context ) initialContext.lookup(firstContextName())).lookup(secondContextName() + "/" + secondBoundName())); 330 } 331 332 public void testComposeName() throws NamingException { 333 assertEquals("org/research/user/jane", 334 secondContext.composeName("user/jane", "org/research")); 335 assertEquals("research/user/jane", 336 secondContext.composeName("user/jane", "research")); 337 assertEquals(new CompositeName ("org/research/user/jane"), 338 secondContext.composeName(new CompositeName ("user/jane"), 339 new CompositeName ("org/research"))); 340 assertEquals(new CompositeName ("research/user/jane"), 341 secondContext.composeName(new CompositeName ("user/jane"), 342 new CompositeName ("research"))); 343 } 344 345 public void testList() throws NamingException { 346 NamingEnumeration enumeration; 347 Map expected; 348 Map result; 349 350 expected = new HashMap (); 351 for (Iterator i = binding.entrySet().iterator(); i.hasNext();) { 352 Map.Entry entry = (Map.Entry ) i.next(); 353 expected.put(entry.getKey(), entry.getValue().getClass().getName()); 354 } 355 enumeration = secondContext.list(""); 356 result = new HashMap (); 357 while (enumeration.hasMore()) { 358 NameClassPair pair = (NameClassPair ) enumeration.next(); 359 result.put(pair.getName(), pair.getClassName()); 360 } 361 verifyList(expected, result); 362 363 try { 364 enumeration.next(); 365 fail("Expecting NoSuchElementException"); 366 } catch (NoSuchElementException e) { 367 } 369 try { 370 enumeration.nextElement(); 371 fail("Expecting NoSuchElementException"); 372 } catch (NoSuchElementException e) { 373 } 375 } 376 377 public void testListBindings() throws NamingException { 378 NamingEnumeration enumeration; 379 Map result; 380 enumeration = secondContext.listBindings(""); 381 result = new HashMap (); 382 while (enumeration.hasMore()) { 383 Binding pair = (Binding ) enumeration.next(); 384 result.put(pair.getName(), pair.getObject()); 385 } 386 verifyListBindings(binding, result); 387 388 try { 389 enumeration.next(); 390 fail("Expecting NoSuchElementException"); 391 } catch (NoSuchElementException e) { 392 } 394 try { 395 enumeration.nextElement(); 396 fail("Expecting NoSuchElementException"); 397 } catch (NoSuchElementException e) { 398 } 400 } 401 402 406 public void testGetNameInNamespace() throws Exception { 407 if (isGetNameInNamespaceSupported()) { 408 String name = initialContext.getNameInNamespace(); 409 if (name == null) { 410 fail("Null NameInNamespace for initial context"); 411 } 412 } else { 413 try { 414 String name = firstContext.getNameInNamespace(); 415 fail("Expecting OperationNotSupportedException"); 416 } catch(OperationNotSupportedException ex) { 417 } 419 } 420 } 421 422 425 public void testRebind() throws Exception { 426 secondContext.rebind(firstBoundName(), secondBoundObject()); 427 secondContext.rebind(secondBoundName(), firstBoundObject()); 428 binding.put(firstBoundName(), secondBoundObject()); 429 binding.put(secondBoundName(), firstBoundObject()); 430 NamingEnumeration enumeration; 431 Map result; 432 enumeration = secondContext.listBindings(""); 433 result = new HashMap (); 434 while (enumeration.hasMore()) { 435 Binding pair = (Binding ) enumeration.next(); 436 result.put(pair.getName(), pair.getObject()); 437 } 438 verifyListBindings(binding, result); 439 } 440 441 446 public void testAttributes() throws Exception { 447 DirContext context = (DirContext ) initialContext.lookup(firstContextName()+ "/" + secondContextName()); 448 Attributes attrs = (Attributes ) context.getAttributes(firstBoundName()); 449 NamingEnumeration enumeration = attrs.getAll(); 450 while (enumeration.hasMoreElements()) { 451 assertTrue(enumeration.nextElement() instanceof Attribute ); 452 } 453 Attributes expected = expectedAttributes(); 454 if (expected != null) { 455 assertEquals(expected, attrs); 456 } 457 } 458 459 464 public void testGetSchema() throws Exception { 465 if (!isSchemaSupported()) { 466 try { 467 ((DirContext ) initialContext.lookup("")).getSchema(""); 468 fail("Expecting OperationNotSupportedException"); 469 } catch (OperationNotSupportedException ex) { 470 } 472 return; 473 } 474 DirContext context = (DirContext ) initialContext.lookup(""); 475 DirContext schemaContext = context.getSchema(""); 476 assertNotNull(schemaContext); 477 Name name = nameParser.parse(firstContext.getNameInNamespace()); 478 schemaContext = context.getSchema(name); 479 assertNotNull(schemaContext); 480 schemaContext = context.getSchema(firstBoundName()); 481 assertNotNull(schemaContext); 482 schemaContext = context.getSchemaClassDefinition(name); 483 assertNotNull(schemaContext); 484 schemaContext = context.getSchemaClassDefinition(firstBoundName()); 485 assertNotNull(schemaContext); 486 } 487 488 495 public void testAttributeModification() throws Exception { 496 if (!isAttributeModificationSupported()) { 497 return; 498 } 499 ModificationItem [] modifications = new ModificationItem [1]; 500 DirContext context = (DirContext ) initialContext.lookup(firstContextName() + "/" 501 + secondContextName() +"/" + firstBoundName()); 502 503 if(addAttributeName() != null) { 504 Attribute addModification = new BasicAttribute (addAttributeName(), addAttributeValue()); 505 modifications[0] = new ModificationItem (DirContext.ADD_ATTRIBUTE, addModification); 506 context.modifyAttributes("", modifications); 507 verifyAttribute(context, "", addAttributeName(), addAttributeValue()); 508 } 509 510 if(replaceAttributeName() != null) { 511 Attribute replaceModification = new BasicAttribute (replaceAttributeName(), replaceAttributeValue()); 512 modifications[0] = new ModificationItem (DirContext.REPLACE_ATTRIBUTE, replaceModification); 513 context.modifyAttributes("", modifications); 514 verifyAttribute(context, "", replaceAttributeName(), replaceAttributeValue()); 515 } 516 517 if(removeAttributeName() != null) { 518 Attribute removeModification = new BasicAttribute (removeAttributeName()); 519 modifications[0] = new ModificationItem (DirContext.REMOVE_ATTRIBUTE, removeModification); 520 context.modifyAttributes("", modifications); 521 verifyAttributeMissing(context, "", removeAttributeName()); 522 } 523 } 524 525 } 526 | Popular Tags |