Skip to content

Commit

Permalink
WW-5449 Increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Jul 26, 2024
1 parent ac860d3 commit a0d3421
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
}
Expand Down Expand Up @@ -207,7 +208,7 @@ public Properties loadConfiguration(ServletContext context) {
* </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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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);
Expand All @@ -79,15 +113,15 @@ public void testInitWithToolbox() {
}

@Test
public void testInitFailsWithInvalidToolBoxLocation() {
public void initFailsWithInvalidToolBoxLocation() {
velocityManager.setToolBoxLocation("invalid.xml");

Exception e = assertThrows(Exception.class, () -> velocityManager.init(servletContext));
assertThat(e).hasMessageContaining("Could not find any configuration at invalid.xml");
}

@Test
public void testCreateContext() {
public void createContext() {
velocityManager.init(servletContext);

Context context = velocityManager.createContext(ActionContext.getContext().getValueStack(), request, response);
Expand All @@ -101,7 +135,7 @@ public void testCreateContext() {
}

@Test
public void testCreateToolboxContext() {
public void createToolboxContext() {
velocityManager.setToolBoxLocation("tools.xml");
velocityManager.init(servletContext);

Expand Down
1 change: 1 addition & 0 deletions plugins/velocity/src/test/resources/velocity.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test=value

0 comments on commit a0d3421

Please sign in to comment.