WW-5449 Increase coverage

This commit is contained in:
Kusal Kithul-Godage
2024-07-26 19:57:20 +10:00
parent ac860d3bba
commit ca358c57d2
4 changed files with 94 additions and 9 deletions
@@ -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 {
* </ul>
*/
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;
@@ -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);
@@ -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
@@ -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