This is an automated email from the ASF dual-hosted git repository. kusal pushed a commit to branch WW-5334-velocity-manager in repository https://gitbox.apache.org/repos/asf/struts.git
commit 22506396053e3172f29402d5df0e2dba67134921 Author: Kusal Kithul-Godage <g...@kusal.io> AuthorDate: Fri Aug 18 10:47:48 2023 +1000 WW-5334 Modernise VelocityResultTest --- .../views/velocity/result/VelocityResultTest.java | 102 +++++++++++---------- 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/plugins/velocity/src/test/java/org/apache/struts2/views/velocity/result/VelocityResultTest.java b/plugins/velocity/src/test/java/org/apache/struts2/views/velocity/result/VelocityResultTest.java index b027e928f..fe26dc025 100644 --- a/plugins/velocity/src/test/java/org/apache/struts2/views/velocity/result/VelocityResultTest.java +++ b/plugins/velocity/src/test/java/org/apache/struts2/views/velocity/result/VelocityResultTest.java @@ -18,93 +18,100 @@ */ package org.apache.struts2.views.velocity.result; -import com.opensymphony.xwork2.XWorkTestCase; -import junit.framework.TestCase; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.util.ValueStack; +import org.apache.struts2.junit.XWorkJUnit4TestCase; import org.apache.struts2.result.StrutsResultSupport; import org.apache.velocity.Template; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; -import com.mockobjects.dynamic.Mock; -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.ActionInvocation; -import com.opensymphony.xwork2.ActionProxy; -import com.opensymphony.xwork2.util.ValueStack; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.when; +public class VelocityResultTest extends XWorkJUnit4TestCase { -/** - * - */ -public class VelocityResultTest extends XWorkTestCase { + @Rule + public MockitoRule mockitoRule = MockitoJUnit.rule(); + @Mock ActionInvocation actionInvocation; - Mock mockActionProxy; + @Mock + ActionProxy actionProxy; ValueStack stack; String namespace; TestVelocityEngine velocity; - VelocityResult result; + VelocityResult velocityResult; + @Before + public void init() throws Exception { + namespace = "/html"; + velocityResult = new VelocityResult(); + stack = ActionContext.getContext().getValueStack(); + velocity = new TestVelocityEngine(); + when(actionProxy.getNamespace()).thenReturn(namespace); + when(actionInvocation.getProxy()).thenReturn(actionProxy); + when(actionInvocation.getStack()).thenReturn(stack); + } + @Test public void testCanResolveLocationUsingOgnl() throws Exception { - TestResult result = new TestResult(); + TestResult testResult = new TestResult(); String location = "/myaction.action"; Bean bean = new Bean(); bean.setLocation(location); - ValueStack stack = ActionContext.getContext().getValueStack(); stack.push(bean); - TestCase.assertEquals(location, stack.findValue("location")); + assertEquals(location, stack.findValue("location")); - result.setLocation("${location}"); - result.execute(actionInvocation); - TestCase.assertEquals(location, result.finalLocation); + testResult.setLocation("${location}"); + testResult.execute(actionInvocation); + assertEquals(location, testResult.finalLocation); } + @Test public void testCanResolveLocationUsingStaticExpression() throws Exception { TestResult result = new TestResult(); String location = "/any.action"; result.setLocation("${'" + location + "'}"); result.execute(actionInvocation); - TestCase.assertEquals(location, result.finalLocation); + assertEquals(location, result.finalLocation); } + @Test public void testResourcesFoundUsingAbsolutePath() throws Exception { String location = "/WEB-INF/views/registration.vm"; - Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8"); - TestCase.assertNotNull(template); - TestCase.assertEquals("expect absolute locations to be handled as is", location, velocity.templateName); + Template template = velocityResult.getTemplate(stack, velocity, actionInvocation, location, "UTF-8"); + assertNotNull(template); + assertEquals("expect absolute locations to be handled as is", location, velocity.templateName); } + @Test public void testResourcesFoundUsingNames() throws Exception { String location = "Registration.vm"; String expectedTemplateName = namespace + "/" + location; - Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8"); - TestCase.assertNotNull(template); - TestCase.assertEquals("expect the prefix to be appended to the path when the location is not absolute", expectedTemplateName, velocity.templateName); - } - - protected void setUp() throws Exception { - super.setUp(); - namespace = "/html"; - result = new VelocityResult(); - stack = ActionContext.getContext().getValueStack(); - velocity = new TestVelocityEngine(); - mockActionProxy = new Mock(ActionProxy.class); - mockActionProxy.expectAndReturn("getNamespace", "/html"); - - Mock mockActionInvocation = new Mock(ActionInvocation.class); - mockActionInvocation.expectAndReturn("getProxy", mockActionProxy.proxy()); - mockActionInvocation.expectAndReturn("getStack", stack); - actionInvocation = (ActionInvocation) mockActionInvocation.proxy(); + Template template = velocityResult.getTemplate(stack, velocity, actionInvocation, location, "UTF-8"); + assertNotNull(template); + assertEquals("expect the prefix to be appended to the path when the location is not absolute", + expectedTemplateName, + velocity.templateName); } - - class Bean { + static class Bean { private String location; public void setLocation(String location) { @@ -116,7 +123,7 @@ public class VelocityResultTest extends XWorkTestCase { } } - class TestResult extends StrutsResultSupport { + static class TestResult extends StrutsResultSupport { private static final long serialVersionUID = -1512206785088317315L; @@ -127,18 +134,17 @@ public class VelocityResultTest extends XWorkTestCase { } } - class TestVelocityEngine extends VelocityEngine { + static class TestVelocityEngine extends VelocityEngine { public String templateName; public Template getTemplate(String templateName) throws ResourceNotFoundException, ParseErrorException { this.templateName = templateName; - return new Template(); } - public Template getTemplate(String templateName, String charSet) throws ResourceNotFoundException, ParseErrorException { + public Template getTemplate(String templateName, + String charSet) throws ResourceNotFoundException, ParseErrorException { this.templateName = templateName; - return new Template(); } }