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 e1cfa8cd0..25ded15ad 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 class VelocityManager implements VelocityManagerInterface { } } - 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 class VelocityManager implements VelocityManagerInterface { * */ 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 ec2ebe5ce..d611cb569 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 @@ package org.apache.struts2.views.velocity; 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; @@ -37,6 +38,9 @@ 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.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; public class VelocityManagerTest extends StrutsJUnit4TestCase { @@ -54,22 +58,62 @@ public class VelocityManagerTest extends StrutsJUnit4TestCase { } @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 loadsConfigFromWebInfPath() { + velocityManager.setCustomConfigFile("webinf-velocity.properties"); + + velocityManager.init(servletContext); + + assertEquals("webinf", velocityManager.getVelocityEngine().getProperty("test")); + } + + @Test + public void loadsConfigFromClassPath() { + var servletContext = mock(ServletContext.class); + doReturn(null).when(servletContext).getRealPath(anyString()); + velocityManager.setCustomConfigFile("test-velocity.properties"); + + velocityManager.init(servletContext); + + assertEquals("value", velocityManager.getVelocityEngine().getProperty("test")); + } + + @Test + public void initWithToolboxLocation() { velocityManager.setToolBoxLocation("tools.xml"); velocityManager.init(servletContext); @@ -79,7 +123,7 @@ public class VelocityManagerTest extends StrutsJUnit4TestCase { } @Test - public void testInitFailsWithInvalidToolBoxLocation() { + public void initFailsWithInvalidToolBoxLocation() { velocityManager.setToolBoxLocation("invalid.xml"); Exception e = assertThrows(Exception.class, () -> velocityManager.init(servletContext)); @@ -87,7 +131,7 @@ public class VelocityManagerTest extends StrutsJUnit4TestCase { } @Test - public void testCreateContext() { + public void createContext() { velocityManager.init(servletContext); Context context = velocityManager.createContext(ActionContext.getContext().getValueStack(), request, response); @@ -101,7 +145,7 @@ public class VelocityManagerTest extends StrutsJUnit4TestCase { } @Test - public void testCreateToolboxContext() { + public void createToolboxContext() { velocityManager.setToolBoxLocation("tools.xml"); velocityManager.init(servletContext); diff --git a/plugins/velocity/src/test/resources/WEB-INF/webinf-velocity.properties b/plugins/velocity/src/test/resources/WEB-INF/webinf-velocity.properties new file mode 100644 index 000000000..bb9a740f5 --- /dev/null +++ b/plugins/velocity/src/test/resources/WEB-INF/webinf-velocity.properties @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +test=webinf diff --git a/plugins/velocity/src/test/resources/test-velocity.properties b/plugins/velocity/src/test/resources/test-velocity.properties new file mode 100644 index 000000000..0e9675880 --- /dev/null +++ b/plugins/velocity/src/test/resources/test-velocity.properties @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +test=value