You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by gk...@apache.org on 2021/12/06 15:42:34 UTC
[turbine-core] 06/06: request context for date time formatter and update path converter test
This is an automated email from the ASF dual-hosted git repository.
gk pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/turbine-core.git
commit 8f3ed431e2f308b0a10468cfcef60abb74bc3fbe
Author: Georg Kallidis <gk...@cedis.fu-berlin.de>
AuthorDate: Mon Dec 6 16:24:08 2021 +0100
request context for date time formatter and update path converter test
---
conf/test/CompleteTurbineResources.properties | 7 +-
.../localization/DateTimeFormatterServiceTest.java | 59 ++++++++++----
.../services/velocity/PathConverterTest.java | 95 ++++++++++++----------
3 files changed, 101 insertions(+), 60 deletions(-)
diff --git a/conf/test/CompleteTurbineResources.properties b/conf/test/CompleteTurbineResources.properties
index 50db826..b5dd8d0 100644
--- a/conf/test/CompleteTurbineResources.properties
+++ b/conf/test/CompleteTurbineResources.properties
@@ -476,12 +476,14 @@ tool.request.l10n=org.apache.turbine.services.localization.LocalizationTool
# This pull tool is to allow for easy formatting of Date object into Strings
tool.request.dateFormatter=org.apache.turbine.services.pull.util.DateFormatter
+# This pull tool is to allow for easy formatting of Datetime / TemporalAccessor object into Strings
+#
+tool.request.dateTimeFormatter=org.apache.turbine.services.pull.util.DateTimeFormatterTool
# Use this tool if you need a place to store data that will persist between
# requests. Any data stored using this tool will be stored in the session.
tool.session.sessionData=org.apache.turbine.services.pull.util.SessionData
-
# These are intake tools.
# tool.request.intake=org.apache.turbine.services.intake.IntakeTool
@@ -491,9 +493,6 @@ tool.session.sessionData=org.apache.turbine.services.pull.util.SessionData
# This pull tool can be used to provide skins to an application
tool.global.ui = org.apache.turbine.services.pull.tools.UITool
-# This pull tool is to allow for easy formatting of Datetime / TemporalAccessor object into Strings
-tool.global.dateTimeFormatter=org.apache.turbine.services.pull.util.DateTimeFormatterTool
-
# # These properties apply to both the old UIManager and the newer UIService
tool.ui.dir.skin = /turbine-skins/
tool.ui.dir.image = /turbine-images/
diff --git a/src/test/org/apache/turbine/services/localization/DateTimeFormatterServiceTest.java b/src/test/org/apache/turbine/services/localization/DateTimeFormatterServiceTest.java
index 87970c4..8b915e4 100644
--- a/src/test/org/apache/turbine/services/localization/DateTimeFormatterServiceTest.java
+++ b/src/test/org/apache/turbine/services/localization/DateTimeFormatterServiceTest.java
@@ -1,10 +1,15 @@
package org.apache.turbine.services.localization;
+import org.apache.fulcrum.parser.DefaultParameterParser;
import org.apache.turbine.annotation.AnnotationProcessor;
import org.apache.turbine.annotation.TurbineService;
import org.apache.turbine.services.pull.PullService;
import org.apache.turbine.services.pull.util.DateTimeFormatterTool;
+import org.apache.turbine.services.rundata.RunDataService;
+import org.apache.turbine.services.velocity.VelocityService;
+import org.apache.turbine.test.BaseTestCase;
+import org.apache.turbine.util.RunData;
import org.apache.turbine.util.TurbineConfig;
import org.apache.turbine.util.TurbineException;
import org.apache.velocity.context.Context;
@@ -12,10 +17,16 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DynamicNode;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.TestMethodOrder;
+import javax.servlet.ServletConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
@@ -30,13 +41,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.DynamicContainer.dynamicContainer;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
+import static org.mockito.Mockito.mock;
/**
* Test class for DateTimeFormatter.
*
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
-public class DateTimeFormatterServiceTest {
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+public class DateTimeFormatterServiceTest extends BaseTestCase {
@TurbineService
private DateTimeFormatterService df;
@@ -47,9 +60,15 @@ public class DateTimeFormatterServiceTest {
private PullService pullService;
DateTimeFormatterTool dateTimeFormatterTool;
+
+ @TurbineService
+ private VelocityService vs = null;
+
+ @TurbineService
+ RunDataService runDataService = null;
@BeforeAll
- public void setup() throws TurbineException
+ public void setup() throws Exception
{
// required to initialize defaults
tc = new TurbineConfig(
@@ -60,33 +79,45 @@ public class DateTimeFormatterServiceTest {
AnnotationProcessor.process(this);
assertNotNull(pullService);
- Context globalContext = pullService.getGlobalContext();
- assertNotNull(globalContext);
+ assertNotNull(vs);
+ }
- dateTimeFormatterTool = (DateTimeFormatterTool) globalContext.get("dateTimeFormatter");
- assertNotNull(dateTimeFormatterTool);
+ private RunData getRunData() throws Exception
+ {
+ ServletConfig config = mock(ServletConfig.class);
+ HttpServletRequest request = getMockRequest();
+ HttpServletResponse response = mock(HttpServletResponse.class);
+ RunData runData = runDataService.getRunData(request, response, config);
+ assertEquals(DefaultParameterParser.class, runData.getParameters()
+ .getClass(), "Verify we are using Fulcrum parameter parser");
+ return runData;
}
@AfterAll
public void tearDown()
{
+ vs.shutdown();
tc.dispose();
}
/*
* Class under test for String format(Date, String)
*/
+ @Order(1)
@Test
- void testTool() throws TurbineException
+ void testTool() throws Exception
{
- assertNotNull(pullService);
- Context globalContext = pullService.getGlobalContext();
- assertNotNull(globalContext);
+ RunData rundata = getRunData();
+ Context requestContext = vs.getContext(rundata);
+ assertNotNull(requestContext);
+ pullService.populateContext(requestContext, rundata);
- DateTimeFormatterTool dateTimeFormatterTool = (DateTimeFormatterTool) globalContext.get("dateTimeFormatter");
+ // taking from request context
+ dateTimeFormatterTool = (DateTimeFormatterTool) requestContext.get("dateTimeFormatter");
assertNotNull(dateTimeFormatterTool);
}
+ @Order(2)
@TestFactory
Stream<DynamicNode> testDateTimeFormatterInstances() {
// Stream of DateTimeFormatterInterface to check
@@ -106,10 +137,10 @@ public class DateTimeFormatterServiceTest {
dynamicTest("test formatDateStringEmptyString",() -> formatDateStringEmptyString(dtf))
))
);
- // Or returns a stream of dynamic tests instead of Dynamic nodes,
- // but requires Function<DateTimeFormatterInterface, String> displayNameGenerator and
+ // Or return a stream of dynamic tests instead of Dynamic nodes,
+ // but this requires Function<DateTimeFormatterInterface, String> displayNameGenerator and
// e.g. ThrowingConsumer<DateTimeFormatterInterface> testExecutor = dtf
- //return DynamicTest.stream(inputStream, displayNameGenerator, testExecutor);
+ // return DynamicTest.stream(inputStream, displayNameGenerator, testExecutor);
}
void formatDateString(DateTimeFormatterInterface dateTime)
diff --git a/src/test/org/apache/turbine/services/velocity/PathConverterTest.java b/src/test/org/apache/turbine/services/velocity/PathConverterTest.java
index f608131..0320174 100644
--- a/src/test/org/apache/turbine/services/velocity/PathConverterTest.java
+++ b/src/test/org/apache/turbine/services/velocity/PathConverterTest.java
@@ -21,59 +21,66 @@ package org.apache.turbine.services.velocity;
*/
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
-import java.io.File;
-
import org.apache.commons.configuration2.Configuration;
import org.apache.turbine.Turbine;
-import org.apache.turbine.services.TurbineServices;
+import org.apache.turbine.annotation.AnnotationProcessor;
+import org.apache.turbine.annotation.TurbineService;
import org.apache.turbine.test.BaseTestCase;
import org.apache.turbine.util.TurbineConfig;
import org.apache.velocity.app.VelocityEngine;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+
+import java.io.File;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* Tests startup of the Velocity Service and translation of various
* path patterns.
*
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
- * @version $Id$
+ *
*/
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class PathConverterTest
extends BaseTestCase
{
private static TurbineConfig tc = null;
- private static VelocityService vs = null;
- @BeforeClass
- public static void setUp() throws Exception {
+ @TurbineService
+ private VelocityService vs = null;
+
+ @BeforeAll
+ public void setUp() throws Exception {
tc = new TurbineConfig(".", "/conf/test/TemplateService.properties");
tc.initialize();
- vs = (VelocityService) TurbineServices.getInstance().getService(VelocityService.SERVICE_NAME);
+ AnnotationProcessor.process(this);
+ //vs = (VelocityService) TurbineServices.getInstance().getService(VelocityService.SERVICE_NAME);
+ assertNotNull(vs);
}
- @AfterClass
- public static void destroy() throws Exception {
+ @AfterAll
+ public void destroy() throws Exception {
vs.shutdown();
tc.dispose();
}
- @Test public void testService()
+ @Test
+ void testService()
throws Exception
{
-
// Can we start the service?
- assertNotNull("Could not load Service!", vs);
+ assertNotNull(vs, "Could not load Service!");
}
@Test
- public void testPathTranslation()
+ void testPathTranslation()
throws Exception
{
Configuration conf = vs.getConfiguration();
@@ -83,41 +90,45 @@ public class PathConverterTest
String rootPath = Turbine.getRealPath("");
String test1 = (String) ve.getProperty("test1.resource.loader.path");
- assertNotNull("No Test1 Property found", test1);
- assertEquals("Test1 Path translation failed",
- String.join(File.separator, rootPath, "relative", "path"), test1);
+ assertNotNull( test1, "No Test1 Property found");
+ assertEquals( String.join(File.separator, rootPath, "relative", "path"), test1,
+ "Test1 Path translation failed");
String test2 = (String) ve.getProperty("test2.resource.loader.path");
- assertNotNull("No Test2 Property found", test2);
- assertEquals("Test2 Path translation failed",
- String.join(File.separator, rootPath, "absolute", "path"), test2);
+ assertNotNull( test2, "No Test2 Property found");
+ assertEquals(String.join(File.separator, rootPath, "absolute", "path"), test2,
+ "Test2 Path translation failed");
String test3 = (String) ve.getProperty("test3.resource.loader.path");
- assertNotNull("No Test3 Property found", test3);
- assertEquals("Test3 Path translation failed",
- rootPath +File.separator+"jar-file.jar!/", test3);
+ assertNotNull( test3, "No Test3 Property found");
+ assertEquals(
+ rootPath +File.separator+"jar-file.jar!/", test3,
+ "Test3 Path translation failed");
String test4 = (String) ve.getProperty("test4.resource.loader.path");
- assertNotNull("No Test4 Property found", test4);
- assertEquals("Test4 Path translation failed", rootPath
- +File.separator+"jar-file.jar!/with/some/extensions" , test4);
+ assertNotNull( test4, "No Test4 Property found");
+ assertEquals(rootPath +File.separator+"jar-file.jar!/with/some/extensions" , test4,
+ "Test4 Path translation failed");
String test5 = (String) ve.getProperty("test5.resource.loader.path");
- assertNotNull("No Test5 Property found", test5);
- assertEquals("Test5 Path translation failed", rootPath
- +File.separator+"jar-file.jar" , test5);
+ assertNotNull( test5,"No Test5 Property found");
+ assertEquals(rootPath
+ +File.separator+"jar-file.jar" , test5,
+ "Test5 Path translation failed");
String test6 = (String) ve.getProperty("test6.resource.loader.path");
- assertNotNull("No Test6 Property found", test6);
- assertEquals("Test6 Path translation failed", "jar:http://jar.on.website/" , test6);
+ assertNotNull(test6, "No Test6 Property found");
+ assertEquals("jar:http://jar.on.website/" , test6,
+ "Test6 Path translation failed");
String test7 = (String) ve.getProperty("test7.resource.loader.path");
- assertNotNull("No Test7 Property found", test7);
- assertEquals("Test7 Path translation failed",
- String.join(File.separator, rootPath, "file", "system", "reference"), test7);
+ assertNotNull(test7, "No Test7 Property found");
+ assertEquals(String.join(File.separator, rootPath, "file", "system", "reference"), test7,
+ "Test7 Path translation failed");
String test8 = (String) ve.getProperty("test8.resource.loader.path");
- assertNotNull("No Test8 Property found", test8);
- assertEquals("Test8 Path translation failed", "http://reference.on.website/" , test8);
+ assertNotNull(test8, "No Test8 Property found");
+ assertEquals("http://reference.on.website/", test8,
+ "Test8 Path translation failed");
}
}