You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2023/03/30 17:46:04 UTC

[struts] 01/01: WW-5295 Adds support for java.time.LocalTime to tag

This is an automated email from the ASF dual-hosted git repository.

lukaszlenart pushed a commit to branch WW-5295-local-time
in repository https://gitbox.apache.org/repos/asf/struts.git

commit 34208e69609567143901678936f34bad3cb11ab2
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Thu Mar 30 19:45:56 2023 +0200

    WW-5295 Adds support for java.time.LocalTime to <s:date/> tag
---
 .../java/org/apache/struts2/components/Date.java   |  2 ++
 .../org/apache/struts2/components/DateTest.java    | 25 ++++++++++++++++++++++
 .../apache/struts2/views/jsp/ui/DateTagTest.java   | 22 +++++++++++++++++++
 3 files changed, 49 insertions(+)

diff --git a/core/src/main/java/org/apache/struts2/components/Date.java b/core/src/main/java/org/apache/struts2/components/Date.java
index de640cbda..2cded8f38 100644
--- a/core/src/main/java/org/apache/struts2/components/Date.java
+++ b/core/src/main/java/org/apache/struts2/components/Date.java
@@ -308,6 +308,8 @@ public class Date extends ContextBean {
             date = ((LocalDateTime) dateObject).atZone(tz);
         } else if (dateObject instanceof LocalDate) {
             date = ((LocalDate) dateObject).atStartOfDay(tz);
+        } else if (dateObject instanceof LocalTime) {
+            date = ((LocalTime) dateObject).atDate(ZonedDateTime.now(tz).toLocalDate()).atZone(tz);
         } else if (dateObject instanceof Instant) {
             date = ((Instant) dateObject).atZone(tz);
         } else {
diff --git a/core/src/test/java/org/apache/struts2/components/DateTest.java b/core/src/test/java/org/apache/struts2/components/DateTest.java
index 477b128a6..69247ef17 100644
--- a/core/src/test/java/org/apache/struts2/components/DateTest.java
+++ b/core/src/test/java/org/apache/struts2/components/DateTest.java
@@ -28,6 +28,7 @@ import java.io.StringWriter;
 import java.io.Writer;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.time.format.DateTimeFormatter;
 import java.util.Map;
 
 public class DateTest extends StrutsInternalTestCase {
@@ -127,6 +128,30 @@ public class DateTest extends StrutsInternalTestCase {
         assertEquals(expected, writer.toString());
     }
 
+    public void testJavaLocalTime() {
+        // given
+        Date date = new Date(stack);
+        date.setDateFormatter(new SimpleDateFormatAdapter());
+
+        java.time.LocalTime now = java.time.LocalTime.now();
+
+        String timeFormat = "hh:mm:ss";
+        String expected = DateTimeFormatter.ofPattern(timeFormat).format(now);
+        context.put("myTime", now);
+
+        Writer writer = new StringWriter();
+
+        // when
+        date.setName("myTime");
+        date.setNice(false);
+        date.setFormat(timeFormat);
+        date.start(writer);
+        date.end(writer, "");
+
+        // then
+        assertEquals(expected, writer.toString());
+    }
+
     private DateFormat prepareFormat() {
         return SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, ActionContext.getContext().getLocale());
     }
diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java
index 05e267e05..f8897aea4 100644
--- a/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java
+++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java
@@ -30,6 +30,7 @@ import java.text.DateFormat;
 import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.util.Calendar;
@@ -993,6 +994,27 @@ public class DateTagTest extends AbstractTagTest {
             strutsBodyTagsAreReflectionEqual(tag, freshTag));
     }
 
+    public void testJavaLocalTime() throws Exception {
+        String format = "hh:mm";
+        LocalTime now = LocalTime.now();
+        String formatted = DateTimeFormatter.ofPattern(format, ActionContext.getContext().getLocale()).format(now);
+        context.put("myTime", now);
+
+        tag.setName("myTime");
+        tag.setNice(false);
+        tag.setFormat(format);
+        tag.doStartTag();
+        tag.doEndTag();
+        assertEquals(formatted, writer.toString());
+
+        // Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
+        DateTag freshTag = new DateTag();
+        freshTag.setPageContext(pageContext);
+        assertFalse("Tag state after doEndTag() under default tag clear state is equal to new Tag with pageContext/parent set.  " +
+                "May indicate that clearTagStateForTagPoolingServers() calls are not working properly.",
+            strutsBodyTagsAreReflectionEqual(tag, freshTag));
+    }
+
     /**
      * Utility method to create a new {@link DateTextFieldTag} instance for code coverage tests.
      * <p>