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 0b7c80a47e1..f130352db7e 100644 --- a/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java +++ b/spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2015 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. @@ -50,6 +50,9 @@ public abstract class PatternMatchUtils { return str.endsWith(pattern.substring(1)); } String part = pattern.substring(1, nextIndex); + if ("".equals(part)) { + return simpleMatch(pattern.substring(nextIndex), str); + } int partIndex = str.indexOf(part); while (partIndex != -1) { if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) { @@ -74,8 +77,8 @@ public abstract class PatternMatchUtils { */ public static boolean simpleMatch(String[] patterns, String str) { if (patterns != null) { - for (int i = 0; i < patterns.length; i++) { - if (simpleMatch(patterns[i], str)) { + for (String pattern : patterns) { + if (simpleMatch(pattern, str)) { return true; } } 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 8128cee620a..df116ee53ba 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-2007 the original author or authors. + * Copyright 2002-2015 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. @@ -16,14 +16,17 @@ package org.springframework.util; -import junit.framework.TestCase; +import org.junit.Test; + +import static org.junit.Assert.*; /** * @author Juergen Hoeller * @author Johan Gorter */ -public class PatternMatchUtilsTests extends TestCase { +public class PatternMatchUtilsTests { + @Test public void testTrivial() { assertEquals(false, PatternMatchUtils.simpleMatch((String) null, "")); assertEquals(false, PatternMatchUtils.simpleMatch("1", null)); @@ -31,16 +34,19 @@ public class PatternMatchUtilsTests extends TestCase { doTest("123", "123", true); } + @Test public void testStartsWith() { doTest("get*", "getMe", true); doTest("get*", "setMe", false); } + @Test public void testEndsWith() { doTest("*Test", "getMeTest", true); doTest("*Test", "setMe", false); } + @Test public void testBetween() { doTest("*stuff*", "getMeTest", false); doTest("*stuff*", "getstuffTest", true); @@ -49,6 +55,7 @@ public class PatternMatchUtilsTests extends TestCase { doTest("*stuff*", "stuff", true); } + @Test public void testStartsEnds() { doTest("on*Event", "onMyEvent", true); doTest("on*Event", "onEvent", true); @@ -56,6 +63,7 @@ public class PatternMatchUtilsTests extends TestCase { doTest("3*3", "33", true); } + @Test public void testStartsEndsBetween() { doTest("12*45*78", "12345678", true); doTest("12*45*78", "123456789", false); @@ -66,6 +74,7 @@ public class PatternMatchUtilsTests extends TestCase { doTest("3*3*3", "333", true); } + @Test public void testRidiculous() { doTest("*1*2*3*", "0011002001010030020201030", true); doTest("1*2*3*4", "10300204", false); @@ -74,6 +83,22 @@ public class PatternMatchUtilsTests extends TestCase { doTest("*1*2*3*", "132", false); } + @Test + public void testPatternVariants() { + 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); + } + private void doTest(String pattern, String str, boolean shouldMatch) { assertEquals(shouldMatch, PatternMatchUtils.simpleMatch(pattern, str)); }