WW-4517 UnknownHandlers should return null instead of throwing an

exception
This commit is contained in:
Lukasz Lenart
2015-06-21 18:56:07 +02:00
parent 586f770d30
commit ce61023a4c
7 changed files with 29 additions and 13 deletions
@@ -104,7 +104,11 @@ public class DefaultUnknownHandlerManager implements UnknownHandlerManager {
}
}
return null;
if (unknownHandlers.isEmpty()) {
throw new NoSuchMethodException(String.format("No UnknownHandlers defined to handle method [%s]", methodName));
} else {
throw new NoSuchMethodException(String.format("None of defined UnknownHandlers can handle method [%s]", methodName));
}
}
/**
@@ -33,7 +33,7 @@ public interface UnknownHandler {
* @return An generated ActionConfig, can return <tt>null</tt>
* @throws XWorkException
*/
public ActionConfig handleUnknownAction(String namespace, String actionName) throws XWorkException;
ActionConfig handleUnknownAction(String namespace, String actionName) throws XWorkException;
/**
* Handles the case when a result cannot be found for an action and result code.
@@ -45,7 +45,7 @@ public interface UnknownHandler {
* @return A result to be executed, can return <tt>null</tt>
* @throws XWorkException
*/
public Result handleUnknownResult(ActionContext actionContext, String actionName, ActionConfig actionConfig, String resultCode) throws XWorkException;
Result handleUnknownResult(ActionContext actionContext, String actionName, ActionConfig actionConfig, String resultCode) throws XWorkException;
/**
* Handles the case when an action method cannot be found. This method is responsible both for finding the method and executing it.
@@ -53,11 +53,9 @@ public interface UnknownHandler {
* @since 2.1
* @param action The action object
* @param methodName The method name to call
* @return The result returned from invoking the action method, can return <tt>null</tt>
* @deprecated @throws NoSuchMethodException If the method cannot be found should return null instead,
* don't throw exception as other UnknownHandles won't be invoked
* 'throws NoSuchMethodException' signature will be removed with next
* major release
* @return The result returned from invoking the action method, can return <tt>null</tt> if this instance cannot
* handle such methodName to allow other handlers give a chance
*/
public Object handleUnknownActionMethod(Object action, String methodName) throws NoSuchMethodException;
Object handleUnknownActionMethod(Object action, String methodName);
}
@@ -25,8 +25,17 @@ import java.util.List;
* @see com.opensymphony.xwork2.DefaultUnknownHandlerManager
*/
public interface UnknownHandlerManager {
Result handleUnknownResult(ActionContext actionContext, String actionName, ActionConfig actionConfig, String resultCode);
/**
* Tries to handle passed methodName if cannot find method should re
*
* @param action Action's instance
* @param methodName method name to handle
* @return Result representing result of given action method
* @throws NoSuchMethodException if method can be handled by defined UnknownHandlers
*/
Object handleUnknownMethod(Object action, String methodName) throws NoSuchMethodException;
ActionConfig handleUnknownAction(String namespace, String actionName);
@@ -52,7 +52,7 @@ public class ActionInvocationTest extends XWorkTestCase {
public Result handleUnknownResult(ActionContext actionContext, String actionName, ActionConfig actionConfig, String resultCode) throws XWorkException {
return null;
}
public Object handleUnknownActionMethod(Object action, String methodName) throws NoSuchMethodException {
public Object handleUnknownActionMethod(Object action, String methodName) {
if (methodName.equals("unknownmethod")) {
return "found";
} else {
@@ -29,7 +29,7 @@ public class SomeUnknownHandler implements UnknownHandler{
return actionConfig;
}
public Object handleUnknownActionMethod(Object action, String methodName) throws NoSuchMethodException {
public Object handleUnknownActionMethod(Object action, String methodName) {
return actionMethodResult;
}
@@ -77,6 +77,11 @@ public class UnknownHandlerManagerTest extends ConfigurationTestBase {
//should not pick any
uh1.setActionMethodResult(null);
uh2.setActionMethodResult(null);
assertEquals(null, uhm.handleUnknownMethod(null, null));
try {
uhm.handleUnknownMethod(null, null);
fail("Should throw exception!");
} catch (NoSuchMethodException e) {
assertTrue(true);
}
}
}
@@ -385,7 +385,7 @@ public class ConventionUnknownHandler implements UnknownHandler {
/**
* Not used
*/
public Object handleUnknownActionMethod(Object action, String methodName) throws NoSuchMethodException {
public Object handleUnknownActionMethod(Object action, String methodName) {
return null;
}