From 44b98c6a8b966c674d54bf8846cb6c9d4de972a4 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 27 Oct 2023 15:32:59 +0200 Subject: [PATCH] Polish PatternMatchUtils[Tests] --- .../util/PatternMatchUtils.java | 17 ++- .../util/PatternMatchUtilsTests.java | 121 +++++++++++------- 2 files changed, 87 insertions(+), 51 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java b/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java index f533c30159..9f050351f0 100644 --- a/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java +++ b/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java @@ -28,9 +28,10 @@ import org.springframework.lang.Nullable; public abstract class PatternMatchUtils { /** - * Match a String against the given pattern, supporting the following simple - * pattern styles: {@code xxx*}, {@code *xxx}, {@code *xxx*}, and {@code xxx*yyy} - * matches (with an arbitrary number of pattern parts), as well as direct equality. + * Match a String against the given pattern, supporting direct equality as + * well as the following simple pattern styles: {@code xxx*}, {@code *xxx}, + * {@code *xxx*}, and {@code xxx*yyy} (with an arbitrary number of pattern parts). + *

Returns {@code false} if the supplied String or pattern is {@code null}. * @param pattern the pattern to match against * @param str the String to match * @return whether the String matches the given pattern @@ -73,14 +74,16 @@ public abstract class PatternMatchUtils { } /** - * Match a String against the given patterns, supporting the following simple - * pattern styles: {@code xxx*}, {@code *xxx}, {@code *xxx*}, and {@code xxx*yyy} - * matches (with an arbitrary number of pattern parts), as well as direct equality. + * Match a String against the given patterns, supporting direct equality as + * well as the following simple pattern styles: {@code xxx*}, {@code *xxx}, + * {@code *xxx*}, and {@code xxx*yyy} (with an arbitrary number of pattern parts). + *

Returns {@code false} if the supplied String is {@code null} or if the + * supplied patterns array is {@code null} or empty. * @param patterns the patterns to match against * @param str the String to match * @return whether the String matches any of the given patterns */ - public static boolean simpleMatch(@Nullable String[] patterns, String str) { + public static boolean simpleMatch(@Nullable String[] patterns, @Nullable String str) { if (patterns != null) { for (String pattern : patterns) { if (simpleMatch(pattern, str)) { diff --git a/spring-core/src/test/java/org/springframework/util/PatternMatchUtilsTests.java b/spring-core/src/test/java/org/springframework/util/PatternMatchUtilsTests.java index a96f21b0d3..ab9b3c5b49 100644 --- a/spring-core/src/test/java/org/springframework/util/PatternMatchUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/PatternMatchUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,86 +21,119 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; /** + * Unit tests for {@link PatternMatchUtils}. + * * @author Juergen Hoeller * @author Johan Gorter + * @author Sam Brannen */ class PatternMatchUtilsTests { + @Test + void nullAndEmptyValues() { + assertDoesNotMatch((String) null, null); + assertDoesNotMatch((String) null, ""); + assertDoesNotMatch("123", null); + + assertDoesNotMatch((String[]) null, null); + assertDoesNotMatch((String[]) null, ""); + assertDoesNotMatch(new String[] {}, null); + } + @Test void trivial() { - assertThat(PatternMatchUtils.simpleMatch((String) null, "")).isFalse(); - assertThat(PatternMatchUtils.simpleMatch("1", null)).isFalse(); - doTest("*", "123", true); - doTest("123", "123", true); + assertMatches("", ""); + assertMatches("123", "123"); + assertMatches("*", "123"); + + assertMatches(new String[] { "" }, ""); + assertMatches(new String[] { "123" }, "123"); + assertMatches(new String[] { "*" }, "123"); + + assertMatches(new String[] { null, "" }, ""); + assertMatches(new String[] { null, "123" }, "123"); + assertMatches(new String[] { null, "*" }, "123"); } @Test void startsWith() { - doTest("get*", "getMe", true); - doTest("get*", "setMe", false); + assertMatches("get*", "getMe"); + assertDoesNotMatch("get*", "setMe"); } @Test void endsWith() { - doTest("*Test", "getMeTest", true); - doTest("*Test", "setMe", false); + assertMatches("*Test", "getMeTest"); + assertDoesNotMatch("*Test", "setMe"); } @Test void between() { - doTest("*stuff*", "getMeTest", false); - doTest("*stuff*", "getstuffTest", true); - doTest("*stuff*", "stuffTest", true); - doTest("*stuff*", "getstuff", true); - doTest("*stuff*", "stuff", true); + assertDoesNotMatch("*stuff*", "getMeTest"); + assertMatches("*stuff*", "getstuffTest"); + assertMatches("*stuff*", "stuffTest"); + assertMatches("*stuff*", "getstuff"); + assertMatches("*stuff*", "stuff"); } @Test void startsEnds() { - doTest("on*Event", "onMyEvent", true); - doTest("on*Event", "onEvent", true); - doTest("3*3", "3", false); - doTest("3*3", "33", true); + assertMatches("on*Event", "onMyEvent"); + assertMatches("on*Event", "onEvent"); + assertDoesNotMatch("3*3", "3"); + assertMatches("3*3", "33"); } @Test void startsEndsBetween() { - doTest("12*45*78", "12345678", true); - doTest("12*45*78", "123456789", false); - doTest("12*45*78", "012345678", false); - doTest("12*45*78", "124578", true); - doTest("12*45*78", "1245457878", true); - doTest("3*3*3", "33", false); - doTest("3*3*3", "333", true); + assertMatches("12*45*78", "12345678"); + assertDoesNotMatch("12*45*78", "123456789"); + assertDoesNotMatch("12*45*78", "012345678"); + assertMatches("12*45*78", "124578"); + assertMatches("12*45*78", "1245457878"); + assertDoesNotMatch("3*3*3", "33"); + assertMatches("3*3*3", "333"); } @Test void ridiculous() { - doTest("*1*2*3*", "0011002001010030020201030", true); - doTest("1*2*3*4", "10300204", false); - doTest("1*2*3*3", "10300203", false); - doTest("*1*2*3*", "123", true); - doTest("*1*2*3*", "132", false); + assertMatches("*1*2*3*", "0011002001010030020201030"); + assertDoesNotMatch("1*2*3*4", "10300204"); + assertDoesNotMatch("1*2*3*3", "10300203"); + assertMatches("*1*2*3*", "123"); + assertDoesNotMatch("*1*2*3*", "132"); } @Test void patternVariants() { - doTest("*a", "*", false); - doTest("*a", "a", true); - doTest("*a", "b", false); - doTest("*a", "aa", true); - doTest("*a", "ba", true); - doTest("*a", "ab", false); - doTest("**a", "*", false); - doTest("**a", "a", true); - doTest("**a", "b", false); - doTest("**a", "aa", true); - doTest("**a", "ba", true); - doTest("**a", "ab", false); + assertDoesNotMatch("*a", "*"); + assertMatches("*a", "a"); + assertDoesNotMatch("*a", "b"); + assertMatches("*a", "aa"); + assertMatches("*a", "ba"); + assertDoesNotMatch("*a", "ab"); + assertDoesNotMatch("**a", "*"); + assertMatches("**a", "a"); + assertDoesNotMatch("**a", "b"); + assertMatches("**a", "aa"); + assertMatches("**a", "ba"); + assertDoesNotMatch("**a", "ab"); } - private void doTest(String pattern, String str, boolean shouldMatch) { - assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isEqualTo(shouldMatch); + private void assertMatches(String pattern, String str) { + assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isTrue(); + } + + private void assertDoesNotMatch(String pattern, String str) { + assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isFalse(); + } + + private void assertMatches(String[] patterns, String str) { + assertThat(PatternMatchUtils.simpleMatch(patterns, str)).isTrue(); + } + + private void assertDoesNotMatch(String[] patterns, String str) { + assertThat(PatternMatchUtils.simpleMatch(patterns, str)).isFalse(); } }