Updates tests
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/2f95b9a1 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/2f95b9a1 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/2f95b9a1 Branch: refs/heads/support-2-3 Commit: 2f95b9a130322b62cdd8d61c62b7462b354ca548 Parents: 103e3b1 Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Wed Aug 2 15:00:01 2017 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Thu Aug 24 08:56:07 2017 +0200 ---------------------------------------------------------------------- .../rest/DefaultContentTypeHandlerManager.java | 21 +++++++++++++++---- .../rest/ContentTypeHandlerManagerTest.java | 22 +++++++++++++++----- .../DefaultContentTypeHandlerManagerTest.java | 8 ++++--- .../rest/RestWorkflowInterceptorTest.java | 1 - .../rest/handler/JacksonLibHandlerTest.java | 9 ++++---- .../rest/handler/JsonLibHandlerTest.java | 7 ++++--- 6 files changed, 48 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/2f95b9a1/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java b/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java index dba7d13..02a314a 100644 --- a/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java +++ b/plugins/rest/src/main/java/org/apache/struts2/rest/DefaultContentTypeHandlerManager.java @@ -21,9 +21,12 @@ package org.apache.struts2.rest; +import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.config.entities.ActionConfig; import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.inject.Inject; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.struts2.ServletActionContext; import org.apache.struts2.rest.handler.ContentTypeHandler; @@ -41,6 +44,8 @@ import java.util.Set; */ public class DefaultContentTypeHandlerManager implements ContentTypeHandlerManager { + private static final Logger LOG = LogManager.getLogger(DefaultContentTypeHandlerManager.class); + /** ContentTypeHandlers keyed by the extension */ Map<String, ContentTypeHandler> handlersByExtension = new HashMap<String, ContentTypeHandler>(); /** ContentTypeHandlers keyed by the content-type */ @@ -115,7 +120,7 @@ public class DefaultContentTypeHandlerManager implements ContentTypeHandlerManag /** * Gets the handler for the response by looking at the extension of the request - * @param req The request + * @param request The request * @return The appropriate handler * * WW-4588: modified to get a handler for the response side and auto generate the response type @@ -153,20 +158,28 @@ public class DefaultContentTypeHandlerManager implements ContentTypeHandlerManag return handler; } + @Override + public String handleResult(ActionConfig actionConfig, Object methodResult, Object target) throws IOException { + LOG.warn("This method is deprecated!"); + return readResultCode(methodResult); + } + /** * Handles the result using handlers to generate content type-specific content * - * @param actionConfig The action config for the current request + * @param invocation The action invocation for the current request * @param methodResult The object returned from the action method * @param target The object to return, usually the action object * @return The new result code to process * @throws IOException If unable to write to the response */ - public String handleResult(ActionConfig actionConfig, Object methodResult, Object target) throws IOException { + public String handleResult(ActionInvocation invocation, Object methodResult, Object target) throws IOException { String resultCode = readResultCode(methodResult); Integer statusCode = readStatusCode(methodResult); HttpServletRequest req = ServletActionContext.getRequest(); HttpServletResponse res = ServletActionContext.getResponse(); + ActionConfig actionConfig = invocation.getProxy().getConfig(); + if(statusCode != null) { res.setStatus(statusCode); } @@ -178,7 +191,7 @@ public class DefaultContentTypeHandlerManager implements ContentTypeHandlerManag resultCode = extCode; } else { StringWriter writer = new StringWriter(); - resultCode = handler.fromObject(target, resultCode, writer); + resultCode = handler.fromObject(invocation, target, resultCode, writer); String text = writer.toString(); if (text.length() > 0) { byte[] data = text.getBytes("UTF-8"); http://git-wip-us.apache.org/repos/asf/struts/blob/2f95b9a1/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java b/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java index 6d0e1e8..6bad14a 100644 --- a/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java +++ b/plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeHandlerManagerTest.java @@ -24,10 +24,14 @@ package org.apache.struts2.rest; import com.mockobjects.dynamic.C; import com.mockobjects.dynamic.Mock; import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.config.entities.ActionConfig; import com.opensymphony.xwork2.inject.Container; +import com.opensymphony.xwork2.mock.MockActionInvocation; +import com.opensymphony.xwork2.mock.MockActionProxy; import junit.framework.TestCase; import org.apache.struts2.ServletActionContext; +import org.apache.struts2.rest.handler.AbstractContentTypeHandler; import org.apache.struts2.rest.handler.ContentTypeHandler; import org.apache.struts2.rest.handler.FormUrlEncodedHandler; import org.springframework.mock.web.MockHttpServletRequest; @@ -49,6 +53,7 @@ public class ContentTypeHandlerManagerTest extends TestCase { private DefaultContentTypeHandlerManager mgr; private MockHttpServletResponse mockResponse; private MockHttpServletRequest mockRequest; + private MockActionInvocation invocation; @Override public void setUp() { @@ -59,6 +64,9 @@ public class ContentTypeHandlerManagerTest extends TestCase { ActionContext.setContext(new ActionContext(new HashMap())); ServletActionContext.setRequest(mockRequest); ServletActionContext.setResponse(mockResponse); + + invocation = new MockActionInvocation(); + invocation.setProxy(new MockActionProxy()); } @Override @@ -71,9 +79,9 @@ public class ContentTypeHandlerManagerTest extends TestCase { public void testHandleResultOK() throws IOException { String obj = "mystring"; - ContentTypeHandler handler = new ContentTypeHandler() { - public void toObject(Reader in, Object target) {} - public String fromObject(Object obj, String resultCode, Writer stream) throws IOException { + ContentTypeHandler handler = new AbstractContentTypeHandler() { + public void toObject(ActionInvocation invocation, Reader in, Object target) {} + public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) throws IOException { stream.write(obj.toString()); return resultCode; } @@ -82,7 +90,11 @@ public class ContentTypeHandlerManagerTest extends TestCase { }; mgr.handlersByExtension.put("xml", handler); mgr.setDefaultExtension("xml"); - mgr.handleResult(new ActionConfig.Builder("", "", "").build(), new DefaultHttpHeaders().withStatus(SC_OK), obj); + ActionConfig actionConfig = new ActionConfig.Builder("", "", "").build(); + MockActionProxy proxy = new MockActionProxy(); + proxy.setConfig(actionConfig); + invocation.setProxy(proxy); + mgr.handleResult(invocation, new DefaultHttpHeaders().withStatus(SC_OK), obj); assertEquals(obj.getBytes().length, mockResponse.getContentLength()); } @@ -92,7 +104,7 @@ public class ContentTypeHandlerManagerTest extends TestCase { Mock mockHandlerXml = new Mock(ContentTypeHandler.class); mockHandlerXml.matchAndReturn("getExtension", "xml"); mgr.handlersByExtension.put("xml", (ContentTypeHandler) mockHandlerXml.proxy()); - mgr.handleResult(null, new DefaultHttpHeaders().withStatus(SC_NOT_MODIFIED), new Object()); + mgr.handleResult(invocation, new DefaultHttpHeaders().withStatus(SC_NOT_MODIFIED), new Object()); assertEquals(0, mockResponse.getContentLength()); } http://git-wip-us.apache.org/repos/asf/struts/blob/2f95b9a1/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java b/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java index 7904f7d..d230ce6 100644 --- a/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java +++ b/plugins/rest/src/test/java/org/apache/struts2/rest/DefaultContentTypeHandlerManagerTest.java @@ -1,8 +1,10 @@ package org.apache.struts2.rest; +import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.XWorkTestCase; import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.inject.Scope; +import org.apache.struts2.rest.handler.AbstractContentTypeHandler; import org.apache.struts2.rest.handler.ContentTypeHandler; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -113,13 +115,13 @@ class DummyContainer implements Container { private ContentTypeHandler handler; DummyContainer(final String contentType, final String extension) { - handler = new ContentTypeHandler() { + handler = new AbstractContentTypeHandler() { - public void toObject(Reader in, Object target) throws IOException { + public void toObject(ActionInvocation invocation, Reader in, Object target) throws IOException { } - public String fromObject(Object obj, String resultCode, Writer stream) throws IOException { + public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) throws IOException { return null; } http://git-wip-us.apache.org/repos/asf/struts/blob/2f95b9a1/plugins/rest/src/test/java/org/apache/struts2/rest/RestWorkflowInterceptorTest.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/RestWorkflowInterceptorTest.java b/plugins/rest/src/test/java/org/apache/struts2/rest/RestWorkflowInterceptorTest.java index a7fe9b2..f061938 100644 --- a/plugins/rest/src/test/java/org/apache/struts2/rest/RestWorkflowInterceptorTest.java +++ b/plugins/rest/src/test/java/org/apache/struts2/rest/RestWorkflowInterceptorTest.java @@ -44,7 +44,6 @@ public class RestWorkflowInterceptorTest extends TestCase { Mock mockActionInvocation = new Mock(ActionInvocation.class); Mock mockActionProxy = new Mock(ActionProxy.class); mockActionProxy.expectAndReturn("getConfig", null); - mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy()); mockActionInvocation.expectAndReturn("getAction", action); Mock mockContentTypeHandlerManager = new Mock(ContentTypeHandlerManager.class); mockContentTypeHandlerManager.expectAndReturn("handleResult", new AnyConstraintMatcher() { http://git-wip-us.apache.org/repos/asf/struts/blob/2f95b9a1/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JacksonLibHandlerTest.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JacksonLibHandlerTest.java b/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JacksonLibHandlerTest.java index ebdda71..a45208b 100644 --- a/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JacksonLibHandlerTest.java +++ b/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JacksonLibHandlerTest.java @@ -21,6 +21,7 @@ package org.apache.struts2.rest.handler; +import com.opensymphony.xwork2.mock.MockActionInvocation; import junit.framework.TestCase; import java.io.IOException; @@ -37,7 +38,7 @@ public class JacksonLibHandlerTest extends TestCase { StringWriter writer = new StringWriter(); JacksonLibHandler handler = new JacksonLibHandler(); - handler.fromObject(contact, "success", writer); + handler.fromObject(new MockActionInvocation(), contact, "success", writer); String data = writer.toString(); assertTrue(data.startsWith("{")); assertTrue(data.contains("\"age\":44")); @@ -50,7 +51,7 @@ public class JacksonLibHandlerTest extends TestCase { StringWriter writer = new StringWriter(); JacksonLibHandler handler = new JacksonLibHandler(); - handler.fromObject(Arrays.asList(contact), "success", writer); + handler.fromObject(new MockActionInvocation(), Arrays.asList(contact), "success", writer); String data = writer.toString(); assertTrue(data.startsWith("[{")); @@ -65,7 +66,7 @@ public class JacksonLibHandlerTest extends TestCase { Contact target = new Contact(); StringReader reader = new StringReader("{\"age\":44,\"important\":true,\"name\":\"bob\"}"); JacksonLibHandler handler = new JacksonLibHandler(); - handler.toObject(reader, target); + handler.toObject(new MockActionInvocation(), reader, target); assertEquals(contact, target); } @@ -78,7 +79,7 @@ public class JacksonLibHandlerTest extends TestCase { List<Contact> target = new ArrayList<Contact>(); StringReader reader = new StringReader("[{\"age\":44,\"important\":true,\"name\":\"bob\"},{\"age\":33,\"important\":false,\"name\":\"john\"}]"); JacksonLibHandler handler = new JacksonLibHandler(); - handler.toObject(reader, target); + handler.toObject(new MockActionInvocation(), reader, target); assertEquals(source.size(), target.size()); } http://git-wip-us.apache.org/repos/asf/struts/blob/2f95b9a1/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java b/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java index 1bf74d2..dd7470c 100644 --- a/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java +++ b/plugins/rest/src/test/java/org/apache/struts2/rest/handler/JsonLibHandlerTest.java @@ -26,6 +26,7 @@ import java.io.StringReader; import java.io.StringWriter; import java.util.Arrays; +import com.opensymphony.xwork2.mock.MockActionInvocation; import junit.framework.TestCase; public class JsonLibHandlerTest extends TestCase { @@ -35,7 +36,7 @@ public class JsonLibHandlerTest extends TestCase { StringWriter writer = new StringWriter(); JsonLibHandler handler = new JsonLibHandler(); - handler.fromObject(contact, "success", writer); + handler.fromObject(new MockActionInvocation(), contact, "success", writer); String data = writer.toString(); assertTrue(data.startsWith("{")); assertTrue(data.contains("\"age\":44")); @@ -48,7 +49,7 @@ public class JsonLibHandlerTest extends TestCase { StringWriter writer = new StringWriter(); JsonLibHandler handler = new JsonLibHandler(); - handler.fromObject(Arrays.asList(contact), "success", writer); + handler.fromObject(new MockActionInvocation(), Arrays.asList(contact), "success", writer); String data = writer.toString(); assertTrue(data.startsWith("[{")); @@ -63,7 +64,7 @@ public class JsonLibHandlerTest extends TestCase { Contact target = new Contact(); StringReader reader = new StringReader("{\"age\":44,\"important\":true,\"name\":\"bob\"}"); JsonLibHandler handler = new JsonLibHandler(); - handler.toObject(reader, target); + handler.toObject(new MockActionInvocation(), reader, target); assertEquals(contact, target); }