From a0d34210f194b200d52e462743f764cf511e3538 Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Fri, 26 Jul 2024 19:57:20 +1000 Subject: [PATCH] WW-5449 Increase coverage --- .../views/velocity/VelocityManager.java | 5 +- .../views/velocity/VelocityManagerTest.java | 48 ++++++++++++++++--- .../src/test/resources/velocity.properties | 1 + 3 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 plugins/velocity/src/test/resources/velocity.properties diff --git a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java index e1cfa8cd05..25ded15ad7 100644 --- a/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java +++ b/plugins/velocity/src/main/java/org/apache/struts2/views/velocity/VelocityManager.java @@ -65,6 +65,7 @@ public class VelocityManager implements VelocityManagerInterface { private ObjectFactory objectFactory; + public static final String DEFAULT_CONFIG_FILE = "velocity.properties"; public static final String KEY_VELOCITY_STRUTS_CONTEXT = ".KEY_velocity.struts2.context"; private VelocityEngine velocityEngine; @@ -173,7 +174,7 @@ public synchronized void init(ServletContext context) { } } - public Properties loadConfiguration(ServletContext context) { + protected Properties loadConfiguration(ServletContext context) { if (context == null) { throw new IllegalArgumentException("Error attempting to create a loadConfiguration from a null ServletContext!"); } @@ -207,7 +208,7 @@ public Properties loadConfiguration(ServletContext context) { * */ private void applyUserConfiguration(ServletContext context, Properties properties) { - String configFile = requireNonNullElse(customConfigFile, "velocity.properties").trim(); + String configFile = requireNonNullElse(customConfigFile, DEFAULT_CONFIG_FILE).trim(); try { if (loadFile(properties, context.getRealPath(configFile))) { return; 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 index ec2ebe5ce6..5f357017f6 100644 --- 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 @@ -20,6 +20,7 @@ import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.util.ValueStack; +import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; @@ -32,11 +33,14 @@ import java.util.Properties; +import static org.apache.struts2.views.velocity.VelocityManager.DEFAULT_CONFIG_FILE; import static org.apache.struts2.views.velocity.VelocityManager.KEY_VELOCITY_STRUTS_CONTEXT; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class VelocityManagerTest extends StrutsJUnit4TestCase { @@ -54,22 +58,52 @@ public void reset() { } @Test - public void testProperties() { - Properties props = new Properties(); + public void overridingPropertiesLoaded() { + var props = new Properties(); + props.setProperty("test", "value"); velocityManager.setVelocityProperties(props); + velocityManager.init(servletContext); + + assertEquals("value", velocityManager.getVelocityEngine().getProperty("test")); assertEquals(props, velocityManager.getVelocityProperties()); } @Test - public void testInitSuccess() { + public void initSuccessful() { velocityManager.init(servletContext); assertNotNull(velocityManager.getVelocityEngine()); } @Test - public void testInitWithToolbox() { + public void exceptionThrownOnNoServletContext() { + assertThrows(IllegalArgumentException.class, () -> velocityManager.init(null)); + } + + @Test + public void initMethodIdempotent() { + velocityManager.init(servletContext); + + var engine = velocityManager.getVelocityEngine(); + + velocityManager.init(servletContext); + + assertEquals(engine, velocityManager.getVelocityEngine()); + } + + @Test + public void loadsConfigFromServletContextPath() { + var servletContext = mock(ServletContext.class); + when(servletContext.getRealPath(DEFAULT_CONFIG_FILE)).thenReturn("src/test/resources/" + DEFAULT_CONFIG_FILE); + + velocityManager.init(servletContext); + + assertEquals("value", velocityManager.getVelocityEngine().getProperty("test")); + } + + @Test + public void initWithToolboxLocation() { velocityManager.setToolBoxLocation("tools.xml"); velocityManager.init(servletContext); @@ -79,7 +113,7 @@ public void testInitWithToolbox() { } @Test - public void testInitFailsWithInvalidToolBoxLocation() { + public void initFailsWithInvalidToolBoxLocation() { velocityManager.setToolBoxLocation("invalid.xml"); Exception e = assertThrows(Exception.class, () -> velocityManager.init(servletContext)); @@ -87,7 +121,7 @@ public void testInitFailsWithInvalidToolBoxLocation() { } @Test - public void testCreateContext() { + public void createContext() { velocityManager.init(servletContext); Context context = velocityManager.createContext(ActionContext.getContext().getValueStack(), request, response); @@ -101,7 +135,7 @@ public void testCreateContext() { } @Test - public void testCreateToolboxContext() { + public void createToolboxContext() { velocityManager.setToolBoxLocation("tools.xml"); velocityManager.init(servletContext); diff --git a/plugins/velocity/src/test/resources/velocity.properties b/plugins/velocity/src/test/resources/velocity.properties new file mode 100644 index 0000000000..d83ec8a523 --- /dev/null +++ b/plugins/velocity/src/test/resources/velocity.properties @@ -0,0 +1 @@ +test=value