From 92502510df5ed98267208396f0bbaa8cb67dd802 Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Sat, 4 Nov 2017 09:37:12 +0330 Subject: [PATCH 1/2] WW-4846 Adds unit tests --- .../apache/struts2/json/JSONResultTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java index c5f6493337..5aaa8eb9d7 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java +++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java @@ -35,8 +35,11 @@ import javax.servlet.http.HttpServletResponse; +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; import org.apache.struts2.StrutsStatics; import org.apache.struts2.StrutsTestCase; +import org.springframework.aop.framework.ProxyFactory; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockServletContext; @@ -153,6 +156,43 @@ public void testExcludeNullPropeties() throws Exception { assertEquals(normalizedExpected, normalizedActual); } + public void testNotTraverseOrIncludeProxyInfo() throws Exception { + JSONResult result = new JSONResult(); + JSONUtil jsonUtil = new JSONUtil(); + JSONWriter writer = new DefaultJSONWriter(); + jsonUtil.setWriter(writer); + result.setJsonUtil(jsonUtil); + ProxyFactory factory = new ProxyFactory(new TestAction2()); + factory.addAdvice(new MethodInterceptor() { + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + // fail on any traverse except TestAction2.getName() + if (!TestAction2.class.getMethod("getName").equals(invocation.getMethod())) { + throw new Throwable(invocation.getMethod() + " should not traversed!"); + } + return invocation.proceed(); + } + }); + Object proxiedAction = factory.getProxy(); + stack.push(proxiedAction); + + this.invocation.setAction(proxiedAction); + try { + result.execute(this.invocation); + fail("An exception expected via proxy info traverse because writer.excludeProxyProperties is false!"); + } catch (Exception ignored) { + } + + writer.setExcludeProxyProperties(true); + result.execute(this.invocation); + + String out = response.getContentAsString(); + + String normalizedActual = TestUtils.normalize(out, true); + String normalizedExpected = "{\"name\":\"name\"}"; + assertEquals(normalizedExpected, normalizedActual); + } + public void testWrapPrefix() throws Exception { JSONResult result = new JSONResult(); result.setWrapPrefix("_prefix_"); From fb0a6120db58ba33d331816444be2908295d82a2 Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Sat, 4 Nov 2017 10:22:25 +0330 Subject: [PATCH 2/2] WW-4846 Fixes unit tests for JDK8 --- .../apache/struts2/json/JSONResultTest.java | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java index 5aaa8eb9d7..5062103248 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java +++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java @@ -162,34 +162,29 @@ public void testNotTraverseOrIncludeProxyInfo() throws Exception { JSONWriter writer = new DefaultJSONWriter(); jsonUtil.setWriter(writer); result.setJsonUtil(jsonUtil); - ProxyFactory factory = new ProxyFactory(new TestAction2()); - factory.addAdvice(new MethodInterceptor() { - @Override - public Object invoke(MethodInvocation invocation) throws Throwable { - // fail on any traverse except TestAction2.getName() - if (!TestAction2.class.getMethod("getName").equals(invocation.getMethod())) { - throw new Throwable(invocation.getMethod() + " should not traversed!"); - } - return invocation.proceed(); - } - }); - Object proxiedAction = factory.getProxy(); + Object proxiedAction = new ProxyFactory(new TestAction2()).getProxy(); stack.push(proxiedAction); this.invocation.setAction(proxiedAction); try { result.execute(this.invocation); - fail("An exception expected via proxy info traverse because writer.excludeProxyProperties is false!"); } catch (Exception ignored) { } - writer.setExcludeProxyProperties(true); - result.execute(this.invocation); - String out = response.getContentAsString(); String normalizedActual = TestUtils.normalize(out, true); String normalizedExpected = "{\"name\":\"name\"}"; + assertNotSame(normalizedExpected, normalizedActual); + response.setCommitted(false); + response.reset(); + + writer.setExcludeProxyProperties(true); + result.execute(this.invocation); + + out = response.getContentAsString(); + + normalizedActual = TestUtils.normalize(out, true); assertEquals(normalizedExpected, normalizedActual); }