📄 basedircontexttestcase.java
字号:
* and locate some entries we know are there.
*/
public void testListBindingsWebInfDirect() {
try {
// Look up the WEB-INF entry
Object webInfEntry = context.lookup("WEB-INF");
assertNotNull("Found WEB-INF entry", webInfEntry);
assertTrue("WEB-INF entry is a DirContext",
webInfEntry instanceof DirContext);
DirContext webInfContext = (DirContext) webInfEntry;
// Check the bindings of the WEB-INF context directly
checkListBindings(webInfContext.listBindings(""), webInfNames);
} catch (NamingException e) {
fail("NamingException: " + e);
}
}
/**
* We should be able to list the bindings of the WEB-INF entry,
* and locate some entries we know are there.
*/
public void testListBindingsWebInfIndirect() {
try {
checkListBindings(context.listBindings("WEB-INF"), webInfNames);
} catch (NamingException e) {
fail("NamingException: " + e);
}
}
// -------------------------------------------------------- Support Methods
/**
* Check the results of a list() call against the specified entry list.
*
* @param enum The naming enumeration we are checking
* @param list The list of entry names we are expecting
*
* @exception NamingException if a naming exception occurs
*/
protected void checkList(NamingEnumeration ne, String list[])
throws NamingException {
String contextClassName = context.getClass().getName();
assertNotNull("NamingEnumeration is not null", ne);
while (ne.hasMore()) {
Object next = ne.next();
assertTrue("list() returns NameClassPair instances",
next instanceof NameClassPair);
NameClassPair ncp = (NameClassPair) next;
assertTrue("Name '" + ncp.getName() + "' is expected",
isListed(ncp.getName(), list));
if (isDirContext(ncp.getName())) {
assertTrue("Class '" + ncp.getClassName() + "' is '" +
contextClassName + "'",
contextClassName.equals(ncp.getClassName()));
}
assertTrue("Relative is 'true'", ncp.isRelative());
}
}
/**
* Check the results of a listBindings() call against the
* specified entry list.
*
* @param enum The naming enumeration we are checking
* @param list The list of entry names we are expecting
*
* @exception NamingException if a naming exception occurs
*/
protected void checkListBindings(NamingEnumeration ne, String list[])
throws NamingException {
String contextClassName = context.getClass().getName();
assertNotNull("NamingEnumeration is not null", ne);
while (ne.hasMore()) {
Object next = ne.next();
assertTrue("listBindings() returns Binding instances",
next instanceof Binding);
Binding b = (Binding) next;
assertTrue("Name '" + b.getName() + "' is expected",
isListed(b.getName(), list));
if (isDirContext(b.getName())) {
assertTrue("Class '" + b.getClassName() + "' is '" +
contextClassName + "'",
contextClassName.equals(b.getClassName()));
}
assertTrue("Relative is 'true'", b.isRelative());
Object object = b.getObject();
assertNotNull("Name '" + b.getName() + "' has a non-null object",
object);
if(isDirContext(b.getName())) {
assertTrue("Entry '" + b.getName() + "' is a DirContext",
object instanceof DirContext);
} else {
assertTrue("Entry '" + b.getName() + "' is a Resource",
object instanceof Resource);
}
}
}
/**
* Check the attributes associated with a WEB-INF entry.
*
* @param attrs The attributes for this entry
* @param creationDate The creation date for this entry
* @param contentLength The content length for this entry
* @param displayName The display name for this entry
* @param lastModifiedDate The last modified date for this entry
*/
protected void checkWebInfAttributes(Attributes attrs,
Date creationDate,
long contentLength,
String displayName,
Date lastModifiedDate)
throws NamingException {
assertNotNull("getAttributes() returned non-null", attrs);
NamingEnumeration ne = attrs.getAll();
assertNotNull("getAll() returned non-null", ne);
while (ne.hasMore()) {
Object next = ne.next();
assertTrue("getAll() returns Attribute instances",
next instanceof Attribute);
Attribute attr = (Attribute) next;
String name = attr.getID();
int index = getIndex(name, webInfAttrs);
assertTrue("WEB-INF attribute '" + name + "' is expected",
index >= 0);
Object value = attr.get();
assertNotNull("get() returned non-null", value);
if (name.equals("creationdate")) {
assertTrue("Creation date is a date",
value instanceof Date);
assertTrue("Creation date equals " + creationDate,
creationDate.equals((Date) value));
} else if (name.equals("displayname")) {
assertTrue("Display name is a string",
value instanceof String);
assertTrue("Display name equals " + displayName,
displayName.equals((String) value));
} else if (name.equals("getcontentlength")) {
assertTrue("Content length is a long",
value instanceof Long);
assertTrue("Content length equals " + contentLength,
contentLength == ((Long) value).longValue());
} else if (name.equals("getlastmodified")) {
assertTrue("Last modified date is a date",
value instanceof Date);
assertTrue("Last modified date is " + lastModifiedDate,
lastModifiedDate.equals((Date) value));
}
}
}
/**
* Check the attributes associated with a WEB-INF/web.xml entry.
*
* @param attrs The attributes for this entry
* @param creationDate The creation date for this entry
* @param contentLength The content length for this entry
* @param displayName The display name for this entry
* @param lastModifiedDate The last modified date for this entry
*/
protected void checkWebXmlAttributes(Attributes attrs,
Date creationDate,
long contentLength,
String displayName,
Date lastModifiedDate)
throws NamingException {
assertNotNull("getAttributes() returned non-null", attrs);
NamingEnumeration ne = attrs.getAll();
assertNotNull("getAll() returned non-null", ne);
while (ne.hasMore()) {
Object next = ne.next();
assertTrue("getAll() returns Attribute instances",
next instanceof Attribute);
Attribute attr = (Attribute) next;
String name = attr.getID();
int index = getIndex(name, webXmlAttrs);
assertTrue("WEB-INF/web.xml attribute '" + name + "' is expected",
index >= 0);
Object value = attr.get();
assertNotNull("get() returned non-null", value);
if (name.equals("creationdate")) {
assertTrue("Creation date is a date",
value instanceof Date);
assertTrue("Creation date equals " + creationDate,
creationDate.equals((Date) value));
} else if (name.equals("displayname")) {
assertTrue("Display name is a string",
value instanceof String);
assertTrue("Display name equals " + displayName,
displayName.equals((String) value));
} else if (name.equals("getcontentlength")) {
assertTrue("Content length is a long",
value instanceof Long);
assertTrue("Content length equals " + contentLength,
contentLength == ((Long) value).longValue());
} else if (name.equals("getlastmodified")) {
assertTrue("Last modified date is a date",
value instanceof Date);
assertTrue("Last modified date is " + lastModifiedDate,
lastModifiedDate.equals((Date) value));
}
}
}
/**
* Return the index of the specified name in the specified list, or
* -1 if the name is not present.
*
* @param name Name to be looked up
* @param list List of names to be checked
*/
protected int getIndex(String name, String list[]) {
for (int i = 0; i < list.length; i++) {
if (name.equals(list[i]))
return (i);
}
return (-1);
}
/**
* Is an entry of the specified name supposed to be a DirContext?
*
* @param name Name to be checked
*/
protected boolean isDirContext(String name) {
return (isListed(name, dirContextNames));
}
/**
* Returns <code>true</code> if the specified name is found in the
* specified list.
*
* @param name Name to be looked up
* @param list List to be looked up into
*/
protected boolean isListed(String name, String list[]) {
return (getIndex(name, list) >= 0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -