1 16 17 18 package org.apache.naming.resources; 19 20 import java.util.Date ; 21 22 import javax.naming.Binding ; 23 import javax.naming.NameClassPair ; 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NamingException ; 26 27 import javax.naming.directory.Attribute ; 28 import javax.naming.directory.Attributes ; 29 import javax.naming.directory.DirContext ; 30 31 import junit.framework.Test; 32 import junit.framework.TestCase; 33 import junit.framework.TestSuite; 34 35 36 63 64 public abstract class BaseDirContextTestCase extends TestCase { 65 66 67 69 70 73 protected DirContext context = null; 74 75 76 79 protected String docBase = System.getProperty("doc.base"); 80 81 82 86 protected static final String dirContextNames[] = 87 { "classes", "images", "jsp", "lib", "META-INF", "WEB-INF" }; 88 89 90 94 protected static final String topLevelNames[] = 95 { "index.jsp", "jakarta-banner.gif", "tomcat.gif", "tomcat-power.gif", "META-INF", "WEB-INF" }; 96 97 101 protected static final String webInfNames[] = 102 { "classes", "jsp", "lib", "web.xml" }; 103 104 105 108 protected static final String webInfAttrs[] = 109 { "creationdate", "displayname", "getcontentlength", "getlastmodified", 110 "resourcetype" }; 111 112 113 116 protected static final String webXmlAttrs[] = 117 { "creationdate", "displayname", "getcontentlength", "getlastmodified", 118 "resourcetype" }; 119 120 121 123 124 129 public BaseDirContextTestCase(String name) { 130 131 super(name); 132 133 } 134 135 136 138 139 143 public abstract void setUp(); 144 145 146 150 152 153 157 public abstract void tearDown(); 158 159 160 162 163 166 public abstract void testGetAttributesWebInf(); 167 168 169 173 public abstract void testGetAttributesWebXml(); 174 175 176 180 public void testListTopLevel() { 181 182 try { 183 checkList(context.list(""), topLevelNames); 184 } catch (NamingException e) { 185 fail("NamingException: " + e); 186 } 187 188 } 189 190 191 195 public void testListWebInfDirect() { 196 197 try { 198 199 Object webInfEntry = context.lookup("WEB-INF"); 201 assertNotNull("Found WEB-INF entry", webInfEntry); 202 assertTrue("WEB-INF entry is a DirContext", 203 webInfEntry instanceof DirContext ); 204 DirContext webInfContext = (DirContext ) webInfEntry; 205 206 checkList(webInfContext.list(""), webInfNames); 208 209 } catch (NamingException e) { 210 fail("NamingException: " + e); 211 } 212 213 214 } 215 216 217 221 public void testListWebInfIndirect() { 222 223 try { 224 checkList(context.list("WEB-INF"), webInfNames); 225 } catch (NamingException e) { 226 fail("NamingException: " + e); 227 } 228 229 } 230 231 232 236 public void testListBindingsTopLevel() { 237 238 try { 239 checkListBindings(context.listBindings(""), topLevelNames); 240 } catch (NamingException e) { 241 fail("NamingException: " + e); 242 } 243 244 } 245 246 247 251 public void testListBindingsWebInfDirect() { 252 253 try { 254 255 Object webInfEntry = context.lookup("WEB-INF"); 257 assertNotNull("Found WEB-INF entry", webInfEntry); 258 assertTrue("WEB-INF entry is a DirContext", 259 webInfEntry instanceof DirContext ); 260 DirContext webInfContext = (DirContext ) webInfEntry; 261 262 checkListBindings(webInfContext.listBindings(""), webInfNames); 264 265 } catch (NamingException e) { 266 fail("NamingException: " + e); 267 } 268 269 270 } 271 272 273 277 public void testListBindingsWebInfIndirect() { 278 279 try { 280 checkListBindings(context.listBindings("WEB-INF"), webInfNames); 281 } catch (NamingException e) { 282 fail("NamingException: " + e); 283 } 284 285 } 286 287 288 290 291 299 protected void checkList(NamingEnumeration ne, String list[]) 300 throws NamingException { 301 302 String contextClassName = context.getClass().getName(); 303 304 assertNotNull("NamingEnumeration is not null", ne); 305 while (ne.hasMore()) { 306 307 Object next = ne.next(); 308 assertTrue("list() returns NameClassPair instances", 309 next instanceof NameClassPair ); 310 NameClassPair ncp = (NameClassPair ) next; 311 312 assertTrue("Name '" + ncp.getName() + "' is expected", 313 isListed(ncp.getName(), list)); 314 315 if (isDirContext(ncp.getName())) { 316 assertTrue("Class '" + ncp.getClassName() + "' is '" + 317 contextClassName + "'", 318 contextClassName.equals(ncp.getClassName())); 319 } 320 321 assertTrue("Relative is 'true'", ncp.isRelative()); 322 323 } 324 325 } 326 327 328 337 protected void checkListBindings(NamingEnumeration ne, String list[]) 338 throws NamingException { 339 340 String contextClassName = context.getClass().getName(); 341 342 assertNotNull("NamingEnumeration is not null", ne); 343 while (ne.hasMore()) { 344 345 Object next = ne.next(); 346 assertTrue("listBindings() returns Binding instances", 347 next instanceof Binding ); 348 Binding b = (Binding ) next; 349 350 assertTrue("Name '" + b.getName() + "' is expected", 351 isListed(b.getName(), list)); 352 353 if (isDirContext(b.getName())) { 354 assertTrue("Class '" + b.getClassName() + "' is '" + 355 contextClassName + "'", 356 contextClassName.equals(b.getClassName())); 357 } 358 359 assertTrue("Relative is 'true'", b.isRelative()); 360 361 Object object = b.getObject(); 362 assertNotNull("Name '" + b.getName() + "' has a non-null object", 363 object); 364 if(isDirContext(b.getName())) { 365 assertTrue("Entry '" + b.getName() + "' is a DirContext", 366 object instanceof DirContext ); 367 } else { 368 assertTrue("Entry '" + b.getName() + "' is a Resource", 369 object instanceof Resource); 370 } 371 372 } 373 374 } 375 376 377 386 protected void checkWebInfAttributes(Attributes attrs, 387 Date creationDate, 388 long contentLength, 389 String displayName, 390 Date lastModifiedDate) 391 throws NamingException { 392 393 assertNotNull("getAttributes() returned non-null", attrs); 394 395 NamingEnumeration ne = attrs.getAll(); 396 assertNotNull("getAll() returned non-null", ne); 397 while (ne.hasMore()) { 398 399 Object next = ne.next(); 400 assertTrue("getAll() returns Attribute instances", 401 next instanceof Attribute ); 402 Attribute attr = (Attribute ) next; 403 String name = attr.getID(); 404 int index = getIndex(name, webInfAttrs); 405 assertTrue("WEB-INF attribute '" + name + "' is expected", 406 index >= 0); 407 Object value = attr.get(); 408 assertNotNull("get() returned non-null", value); 409 410 if (name.equals("creationdate")) { 411 assertTrue("Creation date is a date", 412 value instanceof Date ); 413 assertTrue("Creation date equals " + creationDate, 414 creationDate.equals((Date ) value)); 415 } else if (name.equals("displayname")) { 416 assertTrue("Display name is a string", 417 value instanceof String ); 418 assertTrue("Display name equals " + displayName, 419 displayName.equals((String ) value)); 420 } else if (name.equals("getcontentlength")) { 421 assertTrue("Content length is a long", 422 value instanceof Long ); 423 assertTrue("Content length equals " + contentLength, 424 contentLength == ((Long ) value).longValue()); 425 } else if (name.equals("getlastmodified")) { 426 assertTrue("Last modified date is a date", 427 value instanceof Date ); 428 assertTrue("Last modified date is " + lastModifiedDate, 429 lastModifiedDate.equals((Date ) value)); 430 } 431 432 } 433 434 } 435 436 437 446 protected void checkWebXmlAttributes(Attributes attrs, 447 Date creationDate, 448 long contentLength, 449 String displayName, 450 Date lastModifiedDate) 451 throws NamingException { 452 453 assertNotNull("getAttributes() returned non-null", attrs); 454 455 NamingEnumeration ne = attrs.getAll(); 456 assertNotNull("getAll() returned non-null", ne); 457 while (ne.hasMore()) { 458 459 Object next = ne.next(); 460 assertTrue("getAll() returns Attribute instances", 461 next instanceof Attribute ); 462 Attribute attr = (Attribute ) next; 463 String name = attr.getID(); 464 int index = getIndex(name, webXmlAttrs); 465 assertTrue("WEB-INF/web.xml attribute '" + name + "' is expected", 466 index >= 0); 467 Object value = attr.get(); 468 assertNotNull("get() returned non-null", value); 469 470 if (name.equals("creationdate")) { 471 assertTrue("Creation date is a date", 472 value instanceof Date ); 473 assertTrue("Creation date equals " + creationDate, 474 creationDate.equals((Date ) value)); 475 } else if (name.equals("displayname")) { 476 assertTrue("Display name is a string", 477 value instanceof String ); 478 assertTrue("Display name equals " + displayName, 479 displayName.equals((String ) value)); 480 } else if (name.equals("getcontentlength")) { 481 assertTrue("Content length is a long", 482 value instanceof Long ); 483 assertTrue("Content length equals " + contentLength, 484 contentLength == ((Long ) value).longValue()); 485 } else if (name.equals("getlastmodified")) { 486 assertTrue("Last modified date is a date", 487 value instanceof Date ); 488 assertTrue("Last modified date is " + lastModifiedDate, 489 lastModifiedDate.equals((Date ) value)); 490 } 491 492 } 493 494 } 495 496 497 504 protected int getIndex(String name, String list[]) { 505 506 for (int i = 0; i < list.length; i++) { 507 if (name.equals(list[i])) 508 return (i); 509 } 510 return (-1); 511 512 } 513 514 515 520 protected boolean isDirContext(String name) { 521 522 return (isListed(name, dirContextNames)); 523 524 } 525 526 527 534 protected boolean isListed(String name, String list[]) { 535 536 return (getIndex(name, list) >= 0); 537 538 } 539 540 541 } 542 | Popular Tags |