Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [#1934] feat(controller): Call all non-static controller methods on…
… the same instance in the scope of a request instead of creating a new one each time. * [#1934] feat(controller):Support for non-static controller methods - they are easier to mock/unit-test
- Loading branch information
Showing
3 changed files
with
176 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package play.mvc; | ||
|
||
import org.junit.*; | ||
import org.junit.Before; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertSame; | ||
|
||
public class ActionInvokerTest { | ||
private Object[] noArgs = new Object[0]; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
Http.Request.current.set(new Http.Request()); | ||
} | ||
|
||
@Test | ||
public void invokeStaticJavaMethod() throws Exception { | ||
Http.Request.current().controllerClass = TestController.class; | ||
assertEquals("static", ActionInvoker.invokeControllerMethod(TestController.class.getMethod("staticJavaMethod"), noArgs)); | ||
} | ||
|
||
@Test | ||
public void invokeNonStaticJavaMethod() throws Exception { | ||
Http.Request.current().controllerClass = TestController.class; | ||
assertEquals("non-static", ActionInvoker.invokeControllerMethod(TestController.class.getMethod("nonStaticJavaMethod"), noArgs)); | ||
} | ||
|
||
@Test | ||
public void invokeScalaObjectMethod() throws Exception { | ||
Http.Request.current().controllerClass = TestScalaObject$.class; | ||
assertEquals("non-static", ActionInvoker.invokeControllerMethod(TestScalaObject$.class.getMethod("objectMethod"), noArgs)); | ||
} | ||
|
||
@Test | ||
public void invokeScalaTraitMethod() throws Exception { | ||
Http.Request.current().controllerClass = TestScalaObject$.class; | ||
assertEquals("static-with-object", ActionInvoker.invokeControllerMethod(TestScalaTrait$class.class.getMethod("traitMethod", Object.class), new Object[] {null})); | ||
} | ||
|
||
@Test | ||
public void controllerInstanceIsPreservedForAllControllerMethodInvocations() throws Exception { | ||
Http.Request.current().controllerClass = FullCycleTestController.class; | ||
|
||
Controller controllerInstance = (Controller) ActionInvoker.invokeControllerMethod(FullCycleTestController.class.getMethod("before"), noArgs); | ||
assertSame(controllerInstance, Http.Request.current().controllerInstance); | ||
|
||
controllerInstance = (Controller) ActionInvoker.invokeControllerMethod(FullCycleTestController.class.getMethod("action"), noArgs); | ||
assertSame(controllerInstance, Http.Request.current().controllerInstance); | ||
|
||
controllerInstance = (Controller) ActionInvoker.invokeControllerMethod(FullCycleTestController.class.getMethod("after"), noArgs); | ||
assertSame(controllerInstance, Http.Request.current().controllerInstance); | ||
} | ||
|
||
public static class TestController extends Controller { | ||
public static String staticJavaMethod() { | ||
return "static"; | ||
} | ||
|
||
public String nonStaticJavaMethod() { | ||
return "non-static"; | ||
} | ||
} | ||
|
||
public static class FullCycleTestController extends Controller { | ||
@play.mvc.Before public Controller before() { | ||
return this; | ||
} | ||
|
||
public Controller action() { | ||
return this; | ||
} | ||
|
||
@play.mvc.After public Controller after() { | ||
return this; | ||
} | ||
} | ||
|
||
public static class TestScalaObject$ extends Controller { | ||
public static final TestScalaObject$ MODULE$ = new TestScalaObject$(); | ||
|
||
public String objectMethod() { | ||
return "non-static"; | ||
} | ||
|
||
@Override public String toString() { | ||
return "object"; | ||
} | ||
} | ||
|
||
public abstract static class TestScalaTrait$class { | ||
public static String traitMethod(Object that) { | ||
return "static-with-" + that; | ||
} | ||
} | ||
} |