Skip to content

Commit

Permalink
Fixes #1100 add support for non-static @before and @after methods in @…
Browse files Browse the repository at this point in the history
…with class
asolntsev committed Feb 10, 2017
1 parent 81e92c7 commit 27b1ae3
Showing 2 changed files with 52 additions and 7 deletions.
6 changes: 5 additions & 1 deletion framework/src/play/mvc/ActionInvoker.java
Original file line number Diff line number Diff line change
@@ -464,7 +464,11 @@ public static Object invokeControllerMethod(Method method, Object[] forceArgs) t
}
}

return invoke(method, request.controllerInstance, args);
Object methodClassInstance = isStatic ? null :
(method.getDeclaringClass().equals(request.controllerClass)) ? request.controllerInstance :
method.getDeclaringClass().newInstance();

return invoke(method, methodClassInstance, args);
}

static Object invoke(Method method, Object instance, Object ... realArgs) throws Exception {
53 changes: 47 additions & 6 deletions framework/test-src/play/mvc/ActionInvokerTest.java
Original file line number Diff line number Diff line change
@@ -3,29 +3,38 @@
import org.junit.Before;
import org.junit.Test;
import play.Play;
import play.PlayBuilder;
import play.classloading.ApplicationClasses;
import play.data.binding.CachedBoundActionMethodArgs;
import play.exceptions.JavaExecutionException;
import play.exceptions.PlayException;
import play.exceptions.UnexpectedException;
import play.mvc.results.Forbidden;
import play.mvc.results.Result;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static play.mvc.ActionInvokerTest.TestInterceptor.aftersCounter;
import static play.mvc.ActionInvokerTest.TestInterceptor.beforesCounter;

public class ActionInvokerTest {
private Object[] noArgs = new Object[0];

// @org.junit.Before
// public void playBuilderBefore() {
// new PlayBuilder().build();
// }

@Before
public void setUp() throws Exception {
new PlayBuilder().build();
Http.Request.current.set(new Http.Request());
CachedBoundActionMethodArgs.init();
beforesCounter = 0;
aftersCounter = 0;
}

@org.junit.After
public void tearDown() {
CachedBoundActionMethodArgs.clear();
}

@Test
@@ -40,6 +49,22 @@ public void invokeNonStaticJavaMethod() throws Exception {
assertEquals("non-static", ActionInvoker.invokeControllerMethod(TestController.class.getMethod("nonStaticJavaMethod"), noArgs));
}

@Test
public void invokeNonStaticJavaMethodWithNonStaticWith() throws Exception {
Http.Request request = Http.Request.current();
request.controllerClass = TestControllerWithWith.class;
executeMethod("handleBefores", Http.Request.class, request);
executeMethod("handleAfters", Http.Request.class, request);
assertEquals(1, beforesCounter);
assertEquals(1, aftersCounter);
}

private void executeMethod(String methodName, Class parameterClass, Object parameterValue) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = ActionInvoker.class.getDeclaredMethod(methodName, parameterClass);
method.setAccessible(true);
method.invoke(null, parameterValue);
}

@Test
public void invokeScalaObjectMethod() throws Exception {
Http.Request.current().controllerClass = TestScalaObject$.class;
@@ -190,7 +215,7 @@ public static String traitMethod(Object that) {
return "static-with-" + that;
}
}

private static class ActionClass {

private static String privateMethod() {
@@ -228,6 +253,22 @@ public static String finallyMethod() {
}

private static class ActionClassChild extends ActionClass {
}

@With(TestInterceptor.class)
public static class TestControllerWithWith extends Controller {
public String nonStaticJavaMethod() {
return "non-static";
}
}

public static class TestInterceptor extends Controller {
static int beforesCounter, aftersCounter;

@play.mvc.Before
public void beforeMethod() {beforesCounter++;}

@After
public void afterMethod() {aftersCounter++;}
}
}

0 comments on commit 27b1ae3

Please sign in to comment.