Adds test cases
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/e798a9f6 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/e798a9f6 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/e798a9f6 Branch: refs/heads/feature/http-interceptor Commit: e798a9f62214b727b8de5034f17104a326aa3336 Parents: b855351 Author: Lukasz Lenart <[email protected]> Authored: Sat Apr 19 21:51:10 2014 +0200 Committer: Lukasz Lenart <[email protected]> Committed: Sat Apr 19 21:51:10 2014 +0200 ---------------------------------------------------------------------- .../apache/struts2/HttpMethodsTestAction.java | 47 +++++ .../httpmethod/HttpMethodInterceptorTest.java | 186 +++++++++++++++++++ .../interceptor/httpmethod/HttpMethodTest.java | 35 ++++ 3 files changed, 268 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/e798a9f6/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java b/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java new file mode 100644 index 0000000..79dd16e --- /dev/null +++ b/core/src/test/java/org/apache/struts2/HttpMethodsTestAction.java @@ -0,0 +1,47 @@ +package org.apache.struts2; + +import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.httpmethod.AllowedMethod; +import org.apache.struts2.interceptor.httpmethod.GetOnly; +import org.apache.struts2.interceptor.httpmethod.GetPostOnly; +import org.apache.struts2.interceptor.httpmethod.HttpMethod; +import org.apache.struts2.interceptor.httpmethod.HttpMethodAware; +import org.apache.struts2.interceptor.httpmethod.PostOnly; + +import static org.apache.struts2.interceptor.httpmethod.HttpMethod.POST; + +@AllowedMethod(POST) +public class HttpMethodsTestAction extends ActionSupport implements HttpMethodAware { + + private String resultName = null; + + public HttpMethodsTestAction() { + } + + public HttpMethodsTestAction(String resultName) { + this.resultName = resultName; + } + + @GetOnly + public String onGetOnly() { + return "onGetOnly"; + } + + @PostOnly + public String onPostOnly() { + return "onPostOnly"; + } + + @GetPostOnly + public String onGetPostOnly() { + return "onGetPostOnly"; + } + + public void setMethod(HttpMethod httpMethod) { + + } + + public String getBadRequestResultName() { + return resultName; + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/e798a9f6/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java new file mode 100644 index 0000000..34303ea --- /dev/null +++ b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodInterceptorTest.java @@ -0,0 +1,186 @@ +package org.apache.struts2.interceptor.httpmethod; + +import com.opensymphony.xwork2.mock.MockActionInvocation; +import com.opensymphony.xwork2.mock.MockActionProxy; +import org.apache.struts2.HttpMethodsTestAction; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.StrutsInternalTestCase; +import org.apache.struts2.TestAction; +import org.springframework.mock.web.MockHttpServletRequest; + +public class HttpMethodInterceptorTest extends StrutsInternalTestCase { + + public void testNotAnnotatedAction() throws Exception { + // given + TestAction action = new TestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + invocation.setProxy(new MockActionProxy()); + + invocation.setResultCode("success"); + + MockHttpServletRequest request = new MockHttpServletRequest("post", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("success", resultName); + } + + public void testActionWithPostAllowed() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + invocation.setProxy(new MockActionProxy()); + + invocation.setResultCode("success"); + + MockHttpServletRequest request = new MockHttpServletRequest("post", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("success", resultName); + } + + public void testGetIsNotAllowed() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + invocation.setProxy(new MockActionProxy()); + + invocation.setResultCode("success"); + + MockHttpServletRequest request = new MockHttpServletRequest("get", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("bad-request", resultName); + } + + public void testGetIsNotAllowedWithCustomResultName() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + interceptor.setBadRequestResultName("custom-bad-request"); + + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + invocation.setProxy(new MockActionProxy()); + + invocation.setResultCode("success"); + + MockHttpServletRequest request = new MockHttpServletRequest("get", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("custom-bad-request", resultName); + } + + public void testGetIsNotAllowedWithActionDefinedResultName() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction("action-bad-request"); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + interceptor.setBadRequestResultName("custom-bad-request"); + + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + invocation.setProxy(new MockActionProxy()); + + invocation.setResultCode("success"); + + MockHttpServletRequest request = new MockHttpServletRequest("get", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("action-bad-request", resultName); + } + + public void testGetOnlyOnMethod() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + MockActionProxy proxy = new MockActionProxy(); + proxy.setMethod("onGetOnly"); + proxy.setMethodSpecified(true); + invocation.setProxy(proxy); + + invocation.setResultCode("onGetOnly"); + + MockHttpServletRequest request = new MockHttpServletRequest("get", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("onGetOnly", resultName); + } + + public void testPostOnlyOnMethod() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + MockActionProxy proxy = new MockActionProxy(); + proxy.setMethod("onPostOnly"); + proxy.setMethodSpecified(true); + invocation.setProxy(proxy); + + invocation.setResultCode("onPostOnly"); + + MockHttpServletRequest request = new MockHttpServletRequest("post", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("onPostOnly", resultName); + } + + public void testGetPostOnlyOnMethod() throws Exception { + // given + HttpMethodsTestAction action = new HttpMethodsTestAction(); + HttpMethodInterceptor interceptor = new HttpMethodInterceptor(); + MockActionInvocation invocation = new MockActionInvocation(); + invocation.setAction(action); + MockActionProxy proxy = new MockActionProxy(); + proxy.setMethod("onGetPostOnly"); + proxy.setMethodSpecified(true); + invocation.setProxy(proxy); + + invocation.setResultCode("onGetPostOnly"); + + MockHttpServletRequest request = new MockHttpServletRequest("post", "/action"); + ServletActionContext.setRequest(request); + + // when + String resultName = interceptor.intercept(invocation); + + // then + assertEquals("onGetPostOnly", resultName); + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/e798a9f6/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java new file mode 100644 index 0000000..4de4a40 --- /dev/null +++ b/core/src/test/java/org/apache/struts2/interceptor/httpmethod/HttpMethodTest.java @@ -0,0 +1,35 @@ +package org.apache.struts2.interceptor.httpmethod; + +import junit.framework.TestCase; + +public class HttpMethodTest extends TestCase { + + public void testConvertHttpRequestMethod() throws Exception { + // given + String httpRequestMethod = "post"; + + // when + HttpMethod httpMethod = HttpMethod.parse(httpRequestMethod); + + // then + assertEquals(HttpMethod.POST, httpMethod); + } + + public void testValueOfThrowsException() throws Exception { + // given + String httpRequestMethod = "post"; + + // when + IllegalArgumentException expected = null; + try { + HttpMethod.valueOf(httpRequestMethod); + } catch (IllegalArgumentException e) { + expected = e; + } + + // then + assertNotNull(expected); + assertEquals(expected.getClass(), IllegalArgumentException.class); + } + +}
