Author: maartenc Date: Mon Mar 17 15:12:58 2008 New Revision: 638096 URL: http://svn.apache.org/viewvc?rev=638096&view=rev Log: IMPROVEMENT: Smarter determination if an expression is exact or not for RegexpPatternMatcher and GlobPatternMatcher
Modified: ant/ivy/core/trunk/CHANGES.txt ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcher.java ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/GlobPatternMatcher.java ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcher.java ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/AbstractPatternMatcherTest.java ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcherTest.java ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactPatternMatcherTest.java ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/GlobPatternMatcherTest.java ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcherTest.java Modified: ant/ivy/core/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/CHANGES.txt?rev=638096&r1=638095&r2=638096&view=diff ============================================================================== --- ant/ivy/core/trunk/CHANGES.txt (original) +++ ant/ivy/core/trunk/CHANGES.txt Mon Mar 17 15:12:58 2008 @@ -71,6 +71,7 @@ - IMPROVEMENT: add branch attribute in ivy:install task (IVY-727) - IMPROVEMENT: Parse description information in ivy files (IVY-766) - IMPROVEMENT: Parse description and home page from poms (IVY-767) +- IMPROVEMENT: Smarter determination if an expression is exact or not for RegexpPatternMatcher and GlobPatternMatcher - FIX: Ivy reports a conflict when the same file is supposed to be retrieved at the same location twice (or more) (IVY-743) - FIX: StackOverflowError when configuration extends itself (IVY-696) Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcher.java URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcher.java?rev=638096&r1=638095&r2=638096&view=diff ============================================================================== --- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcher.java (original) +++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcher.java Mon Mar 17 15:12:58 2008 @@ -56,7 +56,7 @@ } public boolean isExact() { - return false; + return regexp.isExact(); // && exact.isExact(); } } } Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/GlobPatternMatcher.java URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/GlobPatternMatcher.java?rev=638096&r1=638095&r2=638096&view=diff ============================================================================== --- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/GlobPatternMatcher.java (original) +++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/GlobPatternMatcher.java Mon Mar 17 15:12:58 2008 @@ -56,8 +56,12 @@ private static class GlobMatcher implements Matcher { private Pattern pattern; + private String expression; + private Boolean exact; + public GlobMatcher(String expression) throws PatternSyntaxException { + this.expression = expression; try { pattern = new GlobCompiler().compile(expression); } catch (MalformedPatternException e) { @@ -73,7 +77,25 @@ } public boolean isExact() { - return false; + if (exact == null) { + exact = calculateExact(); + } + return exact.booleanValue(); + } + + private Boolean calculateExact() { + Boolean result = Boolean.TRUE; + + char[] expressionChars = expression.toCharArray(); + for (int i = 0; i < expressionChars.length; i++) { + char ch = expressionChars[i]; + if (ch == '*' || ch == '?' || ch == '[' || ch == ']') { + result = Boolean.FALSE; + break; + } + } + + return result; } } Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcher.java URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcher.java?rev=638096&r1=638095&r2=638096&view=diff ============================================================================== --- ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcher.java (original) +++ ant/ivy/core/trunk/src/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcher.java Mon Mar 17 15:12:58 2008 @@ -27,7 +27,7 @@ */ public final/* @Immutable */class RegexpPatternMatcher extends AbstractPatternMatcher { public static final RegexpPatternMatcher INSTANCE = new RegexpPatternMatcher(); - + /* * NOTE: Regexp compiler does ~200K compilation/s - If necessary look into using ThreadLocal * Pattern to cut on useless object creation - If expression are reused over and over a LRU @@ -44,11 +44,15 @@ private static/* @Immutable */class RegexpMatcher implements Matcher { private Pattern pattern; + private String expression; + + private Boolean exact; public RegexpMatcher(String expression) throws PatternSyntaxException { if (expression == null) { throw new NullPointerException(); } + this.expression = expression; pattern = Pattern.compile(expression); } @@ -60,7 +64,25 @@ } public boolean isExact() { - return false; + if (exact == null) { + exact = calculateExact(); + } + return exact.booleanValue(); + } + + private Boolean calculateExact() { + Boolean result = Boolean.TRUE; + + char[] expressionChars = expression.toCharArray(); + for (int i = 0; i < expressionChars.length; i++) { + char ch = expressionChars[i]; + if (!Character.isLetterOrDigit(ch) && !Character.isWhitespace(ch) && ('-' != ch) && ('_' != ch)) { + result = Boolean.FALSE; + break; + } + } + + return result; } } } Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/AbstractPatternMatcherTest.java URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/AbstractPatternMatcherTest.java?rev=638096&r1=638095&r2=638096&view=diff ============================================================================== --- ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/AbstractPatternMatcherTest.java (original) +++ ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/AbstractPatternMatcherTest.java Mon Mar 17 15:12:58 2008 @@ -25,13 +25,10 @@ public abstract class AbstractPatternMatcherTest extends TestCase { protected PatternMatcher patternMatcher; - protected boolean exact; - protected abstract void setUp() throws Exception; - protected void setUp(PatternMatcher matcher, boolean exact) { + protected void setUp(PatternMatcher matcher) { this.patternMatcher = matcher; - this.exact = exact; } public void testAnyExpression() { @@ -42,16 +39,34 @@ } public void testIsExact() { + // '*' is a special matcher Matcher matcher = patternMatcher.getMatcher("*"); assertEquals(false, matcher.isExact()); matcher.matches("The words aren't what they were."); assertEquals(false, matcher.isExact()); - matcher = patternMatcher.getMatcher("some expression"); - assertEquals(exact, matcher.isExact()); - matcher.matches("The words aren't what they were."); - assertEquals(exact, matcher.isExact()); + // test some exact patterns for this matcher + String[] expressions = getExactExpressions(); + for (int i = 0; i < expressions.length; i++) { + matcher = patternMatcher.getMatcher(expressions[i]); + assertTrue("Expression '" + expressions[i] + "' should be exact", matcher.isExact()); + matcher.matches("The words aren't what they were."); + assertTrue("Expression '" + expressions[i] + "' should be exact", matcher.isExact()); + } + + // test some inexact patterns for this matcher + expressions = getInexactExpressions(); + for (int i = 0; i < expressions.length; i++) { + matcher = patternMatcher.getMatcher(expressions[i]); + assertFalse("Expression '" + expressions[i] + "' should be inexact", matcher.isExact()); + matcher.matches("The words aren't what they were."); + assertFalse("Expression '" + expressions[i] + "' should be inexact", matcher.isExact()); + } + } + + protected abstract String[] getExactExpressions(); + protected abstract String[] getInexactExpressions(); public void testNullInput() { Matcher matcher = patternMatcher.getMatcher("some expression"); Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcherTest.java URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcherTest.java?rev=638096&r1=638095&r2=638096&view=diff ============================================================================== --- ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcherTest.java (original) +++ ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactOrRegexpPatternMatcherTest.java Mon Mar 17 15:12:58 2008 @@ -25,22 +25,35 @@ public class ExactOrRegexpPatternMatcherTest extends AbstractPatternMatcherTest { protected void setUp() throws Exception { - setUp(new ExactOrRegexpPatternMatcher(), false); + setUp(new ExactOrRegexpPatternMatcher()); + } + + protected String[] getExactExpressions() { + return new String[] {"abc", "123", "abc-123", "abc_123"}; + } + + protected String[] getInexactExpressions() { + return new String[] {"abc+", "12.3", "abc-123*", "abc_123\\d"}; } public void testImplementation() { Matcher matcher = patternMatcher.getMatcher("."); + assertFalse(matcher.isExact()); assertFalse(matcher.matches("")); assertTrue("Exact match failed", matcher.matches(".")); assertTrue("Regexp match failed", matcher.matches("a")); assertFalse(matcher.matches("aa")); matcher = patternMatcher.getMatcher(".*"); + assertFalse(matcher.isExact()); assertTrue("Exact match failed", matcher.matches(".*")); assertTrue("Regexp match failed", matcher.matches("")); assertTrue(matcher.matches("a")); assertTrue(matcher.matches("aa")); + matcher = patternMatcher.getMatcher("abc-123_ABC"); + assertTrue(matcher.isExact()); + try { matcher = patternMatcher.getMatcher("("); fail("Should fail on invalid regexp syntax"); Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactPatternMatcherTest.java URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactPatternMatcherTest.java?rev=638096&r1=638095&r2=638096&view=diff ============================================================================== --- ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactPatternMatcherTest.java (original) +++ ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/ExactPatternMatcherTest.java Mon Mar 17 15:12:58 2008 @@ -23,7 +23,15 @@ public class ExactPatternMatcherTest extends AbstractPatternMatcherTest { protected void setUp() throws Exception { - setUp(new ExactPatternMatcher(), true); + setUp(new ExactPatternMatcher()); + } + + protected String[] getExactExpressions() { + return new String[] {"abc", "123", "abc-123", "abc_123"}; + } + + protected String[] getInexactExpressions() { + return new String[0]; // there are no inexact expressions possible } public void testImplementation() { Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/GlobPatternMatcherTest.java URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/GlobPatternMatcherTest.java?rev=638096&r1=638095&r2=638096&view=diff ============================================================================== --- ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/GlobPatternMatcherTest.java (original) +++ ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/GlobPatternMatcherTest.java Mon Mar 17 15:12:58 2008 @@ -25,11 +25,20 @@ public class GlobPatternMatcherTest extends AbstractPatternMatcherTest { protected void setUp() throws Exception { - setUp(new GlobPatternMatcher(), false); + setUp(new GlobPatternMatcher()); + } + + protected String[] getExactExpressions() { + return new String[] {"abc", "123", "abc-123", "abc_123"}; + } + + protected String[] getInexactExpressions() { + return new String[] {"abc*", "12?3", "abc[123]"}; } public void testValidRegexpSyntaxAsNormalCharacter() { Matcher matcher = patternMatcher.getMatcher("."); + assertTrue(matcher.isExact()); assertFalse(matcher.matches("")); assertTrue(matcher.matches(".")); assertFalse(matcher.matches("a")); @@ -38,6 +47,7 @@ public void testRegexpSyntaxAndGlob() { Matcher matcher = patternMatcher.getMatcher(".*"); + assertFalse(matcher.isExact()); assertTrue(matcher.matches(".*")); assertFalse(matcher.matches("")); assertFalse(matcher.matches("a")); @@ -47,6 +57,8 @@ } public void testImplementation() { + Matcher matcher = patternMatcher.getMatcher("abc-123_ABC"); + assertTrue(matcher.isExact()); } public void testQuoteMeta() { Modified: ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcherTest.java URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcherTest.java?rev=638096&r1=638095&r2=638096&view=diff ============================================================================== --- ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcherTest.java (original) +++ ant/ivy/core/trunk/test/java/org/apache/ivy/plugins/matcher/RegexpPatternMatcherTest.java Mon Mar 17 15:12:58 2008 @@ -25,7 +25,15 @@ public class RegexpPatternMatcherTest extends AbstractPatternMatcherTest { protected void setUp() throws Exception { - setUp(new RegexpPatternMatcher(), false); + setUp(new RegexpPatternMatcher()); + } + + protected String[] getExactExpressions() { + return new String[] {"abc", "123", "abc-123", "abc_123"}; + } + + protected String[] getInexactExpressions() { + return new String[] {"abc+", "12.3", "abc-123*", "abc_123\\d"}; } public void testImplementation() {