From 7c4c5ae693daf9f0e12a93400774a9091097ae3e Mon Sep 17 00:00:00 2001 From: dan-s1 Date: Tue, 23 Jul 2024 09:10:42 -0400 Subject: [PATCH] NIFI-13569 Converted TestRegexDateTimeMatcher to Parameterized Test (#9099) Signed-off-by: David Handermann --- .../util/text/TestRegexDateTimeMatcher.java | 83 +++++++++---------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/text/TestRegexDateTimeMatcher.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/text/TestRegexDateTimeMatcher.java index cfb006d0db2f..25377d97b6e2 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/text/TestRegexDateTimeMatcher.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/text/TestRegexDateTimeMatcher.java @@ -16,54 +16,53 @@ */ package org.apache.nifi.util.text; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertTrue; public class TestRegexDateTimeMatcher { - @Test - public void testCommonFormatsExpectedToPass() { - final Map exampleToPattern = new LinkedHashMap<>(); - - // Following examples are intended to test specific functions in the regex generation. - exampleToPattern.put("2018-12-12", "yyyy-MM-dd"); - exampleToPattern.put("2018/12/12", "yyyy/MM/dd"); - exampleToPattern.put("12/12/2018", "MM/dd/yyyy"); - exampleToPattern.put("12/12/18", "MM/dd/yy"); - exampleToPattern.put("1/1/18", "M/d/yy"); - exampleToPattern.put("1/10/18", "M/d/yy"); - exampleToPattern.put("1:40:55", "HH:mm:ss"); - exampleToPattern.put("01:0:5", "HH:mm:ss"); - exampleToPattern.put("12/12/2018 13:04:08 GMT-05:00", "MM/dd/yyyy HH:mm:ss z"); - exampleToPattern.put("12/12/2018 13:04:08 -0500", "MM/dd/yyyy HH:mm:ss Z"); - exampleToPattern.put("12/12/2018 13:04:08 EST", "MM/dd/yyyy HH:mm:ss zzzz"); - exampleToPattern.put("12/12/2018 13:04:08 -05", "MM/dd/yyyy HH:mm:ss X"); - exampleToPattern.put("0:08 PM", "K:mm a"); - exampleToPattern.put("Dec 12, 2018", "MMM dd, yyyy"); - exampleToPattern.put("12 Dec 2018", "dd MMM yyyy"); - exampleToPattern.put("12 December 2018", "dd MMM yyyy"); - - exampleToPattern.put("2001.07.04 AD at 12:08:56 PDT", "yyyy.MM.dd G 'at' HH:mm:ss z"); - exampleToPattern.put("Wed, Jul 4, '01", "EEE, MMM d, ''yy"); - exampleToPattern.put("12:08 PM", "h:mm a"); - exampleToPattern.put("12 o'clock PM, Pacific Daylight Time", "hh 'o''clock' a, zzzz"); - exampleToPattern.put("0:08 PM, PDT", "K:mm a, z"); - exampleToPattern.put("02001.July.04 AD 12:08 PM", "yyyyy.MMMMM.dd GGG hh:mm aaa"); - exampleToPattern.put("Wed, 4 Jul 2001 12:08:56 -0700", "EEE, d MMM yyyy HH:mm:ss Z"); - exampleToPattern.put("010704120856-0700", "yyMMddHHmmssZ"); - exampleToPattern.put("2001-07-04T12:08:56.235-0700", "yyyy-MM-dd'T'HH:mm:ss.SSSZ"); - exampleToPattern.put("2001-07-04T12:08:56.235-07:00", "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); - exampleToPattern.put("2001-W27-3", "YYYY-'W'ww-u"); - - for (final Map.Entry entry : exampleToPattern.entrySet()) { - final RegexDateTimeMatcher matcher = new RegexDateTimeMatcher.Compiler().compile(entry.getValue()); - final boolean matches = matcher.matches(entry.getKey()); + @ParameterizedTest + @MethodSource("exampleToPattern") + public void testCommonFormatsExpectedToPass(String example, String pattern) { + final RegexDateTimeMatcher matcher = new RegexDateTimeMatcher.Compiler().compile(pattern); + assertTrue(matcher.matches(example), String.format("Pattern <%s> did not match <%s>", pattern, example)); + } - assertTrue(matches, "Pattern <" + entry.getValue() + "> did not match <" + entry.getKey() + ">"); - } + private static Stream exampleToPattern() { + return Stream.of( + // Following examples are intended to test specific functions in the regex generation. + Arguments.of("2018-12-12", "yyyy-MM-dd"), + Arguments.of("2018/12/12", "yyyy/MM/dd"), + Arguments.of("12/12/2018", "MM/dd/yyyy"), + Arguments.of("12/12/18", "MM/dd/yy"), + Arguments.of("1/1/18", "M/d/yy"), + Arguments.of("1/10/18", "M/d/yy"), + Arguments.of("1:40:55", "HH:mm:ss"), + Arguments.of("01:0:5", "HH:mm:ss"), + Arguments.of("12/12/2018 13:04:08 GMT-05:00", "MM/dd/yyyy HH:mm:ss z"), + Arguments.of("12/12/2018 13:04:08 -0500", "MM/dd/yyyy HH:mm:ss Z"), + Arguments.of("12/12/2018 13:04:08 EST", "MM/dd/yyyy HH:mm:ss zzzz"), + Arguments.of("12/12/2018 13:04:08 -05", "MM/dd/yyyy HH:mm:ss X"), + Arguments.of("0:08 PM", "K:mm a"), + Arguments.of("Dec 12, 2018", "MMM dd, yyyy"), + Arguments.of("12 Dec 2018", "dd MMM yyyy"), + Arguments.of("12 December 2018", "dd MMM yyyy"), + Arguments.of("2001.07.04 AD at 12:08:56 PDT", "yyyy.MM.dd G 'at' HH:mm:ss z"), + Arguments.of("Wed, Jul 4, '01", "EEE, MMM d, ''yy"), + Arguments.of("12:08 PM", "h:mm a"), + Arguments.of("12 o'clock PM, Pacific Daylight Time", "hh 'o''clock' a, zzzz"), + Arguments.of("0:08 PM, PDT", "K:mm a, z"), + Arguments.of("02001.July.04 AD 12:08 PM", "yyyyy.MMMMM.dd GGG hh:mm aaa"), + Arguments.of("Wed, 4 Jul 2001 12:08:56 -0700", "EEE, d MMM yyyy HH:mm:ss Z"), + Arguments.of("010704120856-0700", "yyMMddHHmmssZ"), + Arguments.of("2001-07-04T12:08:56.235-0700", "yyyy-MM-dd'T'HH:mm:ss.SSSZ"), + Arguments.of("2001-07-04T12:08:56.235-07:00", "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"), + Arguments.of("2001-W27-3", "YYYY-'W'ww-u") + ); } }