📄 testactionservlet.java
字号:
public void testProcessFormBeanConfigClassNoExtends()
throws Exception {
moduleConfig.addFormBeanConfig(baseFormBean);
FormBeanConfig result = null;
try {
result =
actionServlet.processFormBeanConfigClass(baseFormBean,
moduleConfig);
} catch (UnavailableException e) {
fail("An exception should not be thrown when there's nothing to do");
}
assertSame("Result should be the same as the input.", baseFormBean,
result);
}
/**
* Make sure processFormBeanConfigClass() returns the same class instance
* if the base config isn't using a custom class.
*/
public void testProcessFormBeanConfigClassSubFormCustomClass()
throws Exception {
moduleConfig.addFormBeanConfig(baseFormBean);
FormBeanConfig customSub = new FormBeanConfig();
customSub.setName("customSub");
customSub.setExtends("baseForm");
moduleConfig.addFormBeanConfig(customSub);
FormBeanConfig result =
actionServlet.processFormBeanConfigClass(customSub, moduleConfig);
assertSame("The instance returned should be the param given it.",
customSub, result);
}
/**
* Make sure the code throws the correct exception when it can't create an
* instance of the base config's custom class.
*/
public void notestProcessFormBeanConfigClassError()
throws Exception {
CustomFormBeanConfigArg customBase =
new CustomFormBeanConfigArg("customBase");
moduleConfig.addFormBeanConfig(customBase);
FormBeanConfig customSub = new FormBeanConfig();
customSub.setName("customSub");
customSub.setExtends("customBase");
moduleConfig.addFormBeanConfig(customSub);
try {
actionServlet.processFormBeanConfigClass(customSub, moduleConfig);
fail("Exception should be thrown");
} catch (UnavailableException e) {
// success
} catch (Exception e) {
fail("Unexpected exception thrown.");
}
}
/**
* Test the case where the subform has already specified its own form bean
* config class. If the code still attempts to create a new instance, an
* error will be thrown.
*/
public void testProcessFormBeanConfigClassOverriddenSubFormClass()
throws Exception {
CustomFormBeanConfigArg customBase =
new CustomFormBeanConfigArg("customBase");
moduleConfig.addFormBeanConfig(customBase);
FormBeanConfig customSub = new CustomFormBeanConfigArg("customSub");
customSub.setExtends("customBase");
moduleConfig.addFormBeanConfig(customSub);
try {
actionServlet.processFormBeanConfigClass(customSub, moduleConfig);
} catch (Exception e) {
fail("Exception should not be thrown");
}
}
// -------------------------------------------------- ExceptionConfig Tests
/**
* Test that nothing fails if there are no extensions.
*/
public void testInitModuleExceptionConfigsNoExtends()
throws ServletException {
moduleConfig.addExceptionConfig(baseException);
try {
actionServlet.initModuleExceptionConfigs(moduleConfig);
} catch (Exception e) {
fail("Unexpected exception caught.");
}
}
/**
* Test that initModuleExceptionConfigs throws an exception when a handler
* with a null key is present.
*/
public void testInitModuleExceptionConfigsNullFormType()
throws ServletException {
ExceptionConfig handler = new ExceptionConfig();
handler.setType("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(handler);
try {
actionServlet.initModuleExceptionConfigs(moduleConfig);
fail("An exception should've been thrown here.");
} catch (UnavailableException e) {
// success
} catch (Exception e) {
fail("Unrecognized exception thrown: " + e);
}
}
/**
* Test that processExceptionExtension() calls processExtends()
*/
public void testProcessExceptionExtension()
throws ServletException {
CustomExceptionConfig handler = new CustomExceptionConfig();
handler.setType("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(handler);
actionServlet.processExceptionExtension(handler, moduleConfig, null);
assertTrue("processExtends() was not called",
handler.processExtendsCalled);
}
/**
* Make sure processExceptionConfigClass() returns an instance of the
* correct class if the base config is using a custom class.
*/
public void testProcessExceptionConfigClass()
throws Exception {
CustomExceptionConfig customBase = new CustomExceptionConfig();
customBase.setType("java.lang.NullPointerException");
customBase.setKey("msg.exception.npe");
moduleConfig.addExceptionConfig(customBase);
ExceptionConfig customSub = new ExceptionConfig();
customSub.setType("java.lang.IllegalStateException");
customSub.setExtends("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(customSub);
ExceptionConfig result =
actionServlet.processExceptionConfigClass(customSub, moduleConfig,
null);
assertTrue("Incorrect class of exception config",
result instanceof CustomExceptionConfig);
assertEquals("Incorrect type", customSub.getType(), result.getType());
assertEquals("Incorrect key", customSub.getKey(), result.getKey());
assertEquals("Incorrect extends", customSub.getExtends(),
result.getExtends());
assertSame("Result was not registered in the module config", result,
moduleConfig.findExceptionConfig("java.lang.IllegalStateException"));
}
/**
* Make sure processExceptionConfigClass() returns what it was given if
* the handler passed to it doesn't extend anything.
*/
public void testProcessExceptionConfigClassNoExtends()
throws Exception {
moduleConfig.addExceptionConfig(baseException);
ExceptionConfig result = null;
try {
result =
actionServlet.processExceptionConfigClass(baseException,
moduleConfig, null);
} catch (UnavailableException e) {
fail("An exception should not be thrown when there's nothing to do");
}
assertSame("Result should be the same as the input.", baseException,
result);
}
/**
* Make sure processExceptionConfigClass() returns the same class instance
* if the base config isn't using a custom class.
*/
public void testProcessExceptionConfigClassSubConfigCustomClass()
throws Exception {
moduleConfig.addExceptionConfig(baseException);
ExceptionConfig customSub = new ExceptionConfig();
customSub.setType("java.lang.IllegalStateException");
customSub.setExtends("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(customSub);
ExceptionConfig result =
actionServlet.processExceptionConfigClass(customSub, moduleConfig,
null);
assertSame("The instance returned should be the param given it.",
customSub, result);
}
/**
* Make sure the code throws the correct exception when it can't create an
* instance of the base config's custom class.
*/
public void notestProcessExceptionConfigClassError()
throws Exception {
ExceptionConfig customBase =
new CustomExceptionConfigArg("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(customBase);
ExceptionConfig customSub = new ExceptionConfig();
customSub.setType("java.lang.IllegalStateException");
customSub.setExtends("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(customSub);
try {
actionServlet.processExceptionConfigClass(customSub, moduleConfig,
null);
fail("Exception should be thrown");
} catch (UnavailableException e) {
// success
} catch (Exception e) {
fail("Unexpected exception thrown.");
}
}
/**
* Test the case where the subconfig has already specified its own config
* class. If the code still attempts to create a new instance, an error
* will be thrown.
*/
public void testProcessExceptionConfigClassOverriddenSubFormClass()
throws Exception {
moduleConfig.addExceptionConfig(baseException);
ExceptionConfig customSub =
new CustomExceptionConfigArg("java.lang.IllegalStateException");
customSub.setExtends("java.lang.NullPointerException");
moduleConfig.addExceptionConfig(customSub);
try {
actionServlet.processExceptionConfigClass(customSub, moduleConfig,
null);
} catch (Exception e) {
fail("Exception should not be thrown");
}
}
// ---------------------------------------------------- ForwardConfig Tests
/**
* Test that nothing fails if there are no extensions.
*/
public void testInitModuleForwardConfigsNoExtends()
throws ServletException {
moduleConfig.addForwardConfig(baseForward);
try {
actionServlet.initModuleForwards(moduleConfig);
} catch (Exception e) {
fail("Unexpected exception caught.");
}
}
/**
* Test that initModuleForwards throws an exception when a forward with a
* null path is present.
*/
public void testInitModuleForwardsNullFormType()
throws ServletException {
ActionForward forward = new ActionForward("success", null, false);
moduleConfig.addForwardConfig(forward);
try {
actionServlet.initModuleForwards(moduleConfig);
fail("An exception should've been thrown here.");
} catch (UnavailableException e) {
// success
} catch (Exception e) {
fail("Unrecognized exception thrown: " + e);
}
}
/**
* Test that processForwardExtension() calls processExtends()
*/
public void testProcessForwardExtension()
throws ServletException {
CustomForwardConfig forward =
new CustomForwardConfig("forward", "/forward.jsp");
moduleConfig.addForwardConfig(forward);
actionServlet.processForwardExtension(forward, moduleConfig, null);
assertTrue("processExtends() was not called",
forward.processExtendsCalled);
}
/**
* Make sure processForwardConfigClass() returns an instance of the
* correct class if the base config is using a custom class.
*/
public void testProcessForwardConfigClass()
throws Exception {
CustomForwardConfig customBase =
new CustomForwardConfig("success", "/success.jsp");
moduleConfig.addForwardConfig(customBase);
ActionForward customSub = new ActionForward();
customSub.setName("failure");
customSub.setExtends("success");
moduleConfig.addForwardConfig(customSub);
ForwardConfig result =
actionServlet.processForwardConfigClass(customSub, moduleConfig,
null);
assertTrue("Incorrect class of forward config",
result instanceof CustomForwardConfig);
assertEquals("Incorrect name", customSub.getName(), result.getName());
assertEquals("Incorrect path", customSub.getPath(), result.getPath());
assertEquals("Incorrect extends", customSub.getExtends(),
result.getExtends());
assertSame("Result was not registered in the module config", result,
moduleConfig.findForwardConfig("failure"));
}
/**
* Make sure processForwardConfigClass() returns what it was given if the
* forward passed to it doesn't extend anything.
*/
public void testProcessForwardConfigClassNoExtends()
throws Exception {
moduleConfig.addForwardConfig(baseForward);
ForwardConfig result = null;
try {
result =
actionServlet.processForwardConfigClass(baseForward,
moduleConfig, null);
} catch (UnavailableException e) {
fail("An exception should not be thrown when there's nothing to do");
}
assertSame("Result should be the same as the input.", baseForward,
result);
}
/**
* Make sure processForwardConfigClass() returns the same class instance
* if the base config isn't using a custom class.
*/
public void testProcessForwardConfigClassSubConfigCustomClass()
throws Exception {
moduleConfig.addForwardConfig(baseForward);
ForwardConfig customSub = new ActionForward();
customSub.setName("failure");
customSub.setExtends("success");
moduleConfig.addForwardConfig(customSub);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -