You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2022/01/21 15:52:22 UTC

[logging-log4j2] branch master updated: Expand test suite for DateLookup (#721)

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

vy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 7d330e0  Expand test suite for DateLookup (#721)
7d330e0 is described below

commit 7d330e01ebf736f3b29f8947515a5cdd79e1efd2
Author: Arie van Deursen <av...@users.noreply.github.com>
AuthorDate: Fri Jan 21 16:47:40 2022 +0100

    Expand test suite for DateLookup (#721)
---
 .../logging/log4j/core/lookup/DateLookupTest.java  | 56 ++++++++++++++--------
 .../logging/log4j/core/lookup/DateLookup.java      | 13 ++---
 2 files changed, 44 insertions(+), 25 deletions(-)

diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/DateLookupTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/DateLookupTest.java
index a90a6e7..c89de82 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/DateLookupTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/DateLookupTest.java
@@ -16,38 +16,56 @@
  */
 package org.apache.logging.log4j.core.lookup;
 
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
 
-import org.apache.logging.log4j.core.AbstractLogEvent;
 import org.apache.logging.log4j.core.LogEvent;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.NullSource;
+import org.junit.jupiter.params.provider.ValueSource;
 
 import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class DateLookupTest {
 
-
     @Test
-    public void testLookup() {
-        final StrLookup lookup = new DateLookup();
-        final LogEvent event = new MyLogEvent();
-        final String value = lookup.lookup(event, "MM/dd/yyyy");
-        assertNotNull(value);
-        assertEquals("12/30/2011", value);
+    public void testCorrectEvent() {
+        final LogEvent mockedEvent = mock(LogEvent.class);
+        final Calendar cal = Calendar.getInstance();
+        cal.set(2011, Calendar.DECEMBER, 30, 10, 56, 35);
+        when(mockedEvent.getTimeMillis()).thenReturn(cal.getTimeInMillis());
+
+        final String lookupDate = new DateLookup().lookup(mockedEvent, "MM/dd/yyyy");
+        assertEquals("12/30/2011", lookupDate);
     }
 
-    private static class MyLogEvent extends AbstractLogEvent {
-        /**
-         * Generated serial version ID.
-         */
-        private static final long serialVersionUID = -2663819677970643109L;
+    @Test
+    public void testValidKeyWithoutEvent() {
+        final String dateFormat = "MM/dd/yyyy";
 
-        @Override
-        public long getTimeMillis() {
-            final Calendar cal = Calendar.getInstance();
-            cal.set(2011, Calendar.DECEMBER, 30, 10, 56, 35);
-            return cal.getTimeInMillis();
-        }
+        final Calendar cal = Calendar.getInstance();
+        final DateFormat formatter = new SimpleDateFormat(dateFormat);
+        cal.setTimeInMillis(System.currentTimeMillis());
+        final String today = formatter.format(cal.getTime());
+        cal.add(Calendar.DATE, 1);
+        final String tomorrow = formatter.format(cal.getTime());
+
+        final String lookupTime = new DateLookup().lookup(null, dateFormat);
+        // lookup gives current time, which by now could be tomorrow at midnight sharp
+        assertTrue(lookupTime.equals(today) || lookupTime.equals(tomorrow));
+    }
 
+    @ParameterizedTest
+    @NullSource
+    @ValueSource(strings = { "bananas" })
+    public void testInvalidKey(String key) {
+        // For invalid keys without event, the current time in default format should be returned.
+        // Checking this may depend on locale and exact time, and could become flaky.
+        // Therefore we just check that the result isn't null and that (formatting) exceptions are caught.
+        assertNotNull(new DateLookup().lookup(null, key));
     }
 }
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/DateLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/DateLookup.java
index 2a60cf8..6811321 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/DateLookup.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/DateLookup.java
@@ -28,7 +28,8 @@ import org.apache.logging.log4j.plugins.Plugin;
 import org.apache.logging.log4j.status.StatusLogger;
 
 /**
- * Formats the current date or the date in the LogEvent. The "key" is used as the format String.
+ * Formats the current date or the date in the LogEvent. The "key" is used as the format String,
+ * following the java.text.SimpleDateFormat date and time pattern strings.
  */
 @Plugin(name = "date", category = StrLookup.CATEGORY)
 public class DateLookup implements StrLookup {
@@ -37,9 +38,9 @@ public class DateLookup implements StrLookup {
     private static final Marker LOOKUP = MarkerManager.getMarker("LOOKUP");
 
     /**
-     * Looks up the value of the environment variable.
+     * Looks up the current date.
      * @param key the format to use. If null, the default DateFormat will be used.
-     * @return The value of the environment variable.
+     * @return The formatted current date, never null.
      */
     @Override
     public String lookup(final String key) {
@@ -47,10 +48,10 @@ public class DateLookup implements StrLookup {
     }
 
     /**
-     * Looks up the value of the environment variable.
-     * @param event The current LogEvent (is ignored by this StrLookup).
+     * Looks up the the current date or the date in the LogEvent.
+     * @param event The LogEvent for which the date is returned. If null, current date is returned.
      * @param key the format to use. If null, the default DateFormat will be used.
-     * @return The value of the environment variable.
+     * @return The formatted date, never null.
      */
     @Override
     public String lookup(final LogEvent event, final String key) {