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 2fa9d60a23a546bd70b2828f31f174066146bd6c Author: Kusal Kithul-Godage <g...@kusal.io> AuthorDate: Fri Aug 18 13:17:19 2023 +1000 WW-5334 Add basic unit tests for VelocityManager --- .../views/velocity/VelocityManagerTest.java | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/plugins/velocity/src/test/java/org/apache/struts2/views/velocity/VelocityManagerTest.java b/plugins/velocity/src/test/java/org/apache/struts2/views/velocity/VelocityManagerTest.java new file mode 100644 index 000000000..692d7d57b --- /dev/null +++ b/plugins/velocity/src/test/java/org/apache/struts2/views/velocity/VelocityManagerTest.java @@ -0,0 +1,65 @@ +package org.apache.struts2.views.velocity; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.util.ValueStack; +import org.apache.struts2.junit.StrutsJUnit4TestCase; +import org.apache.struts2.views.jsp.ui.OgnlTool; +import org.apache.velocity.context.Context; +import org.junit.Before; +import org.junit.Test; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +public class VelocityManagerTest extends StrutsJUnit4TestCase { + + VelocityManager velocityManager = new VelocityManager(); + + @Before + public void inject() throws Exception { + container.inject(velocityManager); + } + + @Test + public void testProperties() { + Properties props = new Properties(); + velocityManager.setVelocityProperties(props); + + assertEquals(props, velocityManager.getVelocityProperties()); + } + + @Test + public void testInitSuccess() { + velocityManager.init(servletContext); + + assertNotNull(velocityManager.getVelocityEngine()); + } + + @Test + public void testCreateContext() { + velocityManager.init(servletContext); + + Context context = velocityManager.createContext(ActionContext.getContext().getValueStack(), request, response); + + assertNotNull(context); + assertTrue(context.get("struts") instanceof VelocityStrutsUtil); + assertTrue(context.get("ognl") instanceof OgnlTool); + assertTrue(context.get("stack") instanceof ValueStack); + assertTrue(context.get("request") instanceof HttpServletRequest); + assertTrue(context.get("response") instanceof HttpServletResponse); + } + + @Test + public void testInitFailsWithInvalidToolBoxLocation() { + velocityManager.setToolBoxLocation("nonexistent"); + + Exception e = assertThrows(Exception.class, () -> velocityManager.init(servletContext)); + assertTrue(e.getMessage().contains("Could not find any configuration at nonexistent")); + } +}