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 22a6ae9d63527fe11fd53ff24f38700854699157
Author: Kusal Kithul-Godage <g...@kusal.io>
AuthorDate: Sun Aug 20 20:47:40 2023 +1000

    WW-5334 Add further unit tests to VelocityManagerTest
---
 .../views/velocity/VelocityManagerTest.java        | 55 +++++++++++++++++-----
 plugins/velocity/src/test/resources/tools.xml      | 20 ++++++++
 2 files changed, 64 insertions(+), 11 deletions(-)

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 b959dc345..d550e9efb 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,9 +20,12 @@ package org.apache.struts2.views.velocity;
 
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.util.ValueStack;
+import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.junit.StrutsJUnit4TestCase;
 import org.apache.struts2.views.jsp.ui.OgnlTool;
 import org.apache.velocity.context.Context;
+import org.apache.velocity.tools.ToolContext;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -30,18 +33,25 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.util.Properties;
 
+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.junit.Assert.assertTrue;
 
 public class VelocityManagerTest extends StrutsJUnit4TestCase {
 
     VelocityManager velocityManager = new VelocityManager();
 
     @Before
-    public void inject() throws Exception {
+    public void inject() {
         container.inject(velocityManager);
+        ServletActionContext.setServletContext(servletContext);
+    }
+
+    @After
+    public void reset() {
+        ServletActionContext.setServletContext(null);
     }
 
     @Test
@@ -59,6 +69,24 @@ public class VelocityManagerTest extends 
StrutsJUnit4TestCase {
         assertNotNull(velocityManager.getVelocityEngine());
     }
 
+    @Test
+    public void testInitWithToolbox() {
+        velocityManager.setToolBoxLocation("tools.xml");
+
+        velocityManager.init(servletContext);
+
+        assertNotNull(velocityManager.getVelocityEngine());
+        assertNotNull(velocityManager.getToolboxManager());
+    }
+
+    @Test
+    public void testInitFailsWithInvalidToolBoxLocation() {
+        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() {
         velocityManager.init(servletContext);
@@ -66,18 +94,23 @@ public class VelocityManagerTest extends 
StrutsJUnit4TestCase {
         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);
+        
assertThat(context.get("struts")).isInstanceOf(VelocityStrutsUtil.class);
+        assertThat(context.get("ognl")).isInstanceOf(OgnlTool.class);
+        assertThat(context.get("stack")).isInstanceOf(ValueStack.class);
+        
assertThat(context.get("request")).isInstanceOf(HttpServletRequest.class);
+        
assertThat(context.get("response")).isInstanceOf(HttpServletResponse.class);
+        assertEquals(context, 
request.getAttribute(KEY_VELOCITY_STRUTS_CONTEXT));
     }
 
     @Test
-    public void testInitFailsWithInvalidToolBoxLocation() {
-        velocityManager.setToolBoxLocation("nonexistent");
+    public void testCreateToolboxContext() {
+        velocityManager.setToolBoxLocation("tools.xml");
+        velocityManager.init(servletContext);
 
-        Exception e = assertThrows(Exception.class, () -> 
velocityManager.init(servletContext));
-        assertTrue(e.getMessage().contains("Could not find any configuration 
at nonexistent"));
+        Context context = 
velocityManager.createContext(ActionContext.getContext().getValueStack(), 
request, response);
+
+        assertNotNull(context);
+        assertThat(context).isInstanceOf(ToolContext.class);
+        assertEquals(context, 
request.getAttribute(KEY_VELOCITY_STRUTS_CONTEXT));
     }
 }
diff --git a/plugins/velocity/src/test/resources/tools.xml 
b/plugins/velocity/src/test/resources/tools.xml
new file mode 100644
index 000000000..41c7db1ac
--- /dev/null
+++ b/plugins/velocity/src/test/resources/tools.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+-->
+<tools/>

Reply via email to