You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2008/08/05 01:57:12 UTC

svn commit: r682544 - in /tapestry/tapestry5/trunk/tapestry-ioc/src: main/java/org/apache/tapestry5/ioc/internal/ test/java/org/apache/tapestry5/ioc/internal/

Author: hlship
Date: Mon Aug  4 16:57:12 2008
New Revision: 682544

URL: http://svn.apache.org/viewvc?rev=682544&view=rev
Log:
TAPESTRY-2410: Service decorator method not invoked for @Match with "*" in middle

Removed:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/MatchType.java
Modified:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcher.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcherTest.java

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcher.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcher.java?rev=682544&r1=682543&r2=682544&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcher.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcher.java Mon Aug  4 16:57:12 2008
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2006, 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -14,82 +14,41 @@
 
 package org.apache.tapestry5.ioc.internal;
 
-import static org.apache.tapestry5.ioc.internal.MatchType.*;
+import java.util.regex.Pattern;
 
+/**
+ * Used when matching identifiers.  In the early days of T5 IoC, matching was based on shell-style glob matches (a '*'
+ * could represent zero or more characters).  But that was limiting so now we check to see if the provided pattern looks
+ * like a glob (just characters and asterisks, for compatibility with older code) and, if not, we assume it is a regular
+ * expression.
+ */
 public class GlobPatternMatcher
 {
-    private String substring;
+    private final Pattern pattern;
 
-    private MatchType type;
+    private final static Pattern oldStyleGlob =
+            Pattern.compile("[a-z\\*]+", Pattern.CASE_INSENSITIVE);
 
     public GlobPatternMatcher(String pattern)
     {
-        analyze(pattern);
+        this.pattern = compilePattern(pattern);
     }
 
-    private void analyze(String pattern)
+    private static Pattern compilePattern(String pattern)
     {
-        if (pattern.equals("*"))
-        {
-            type = ANY;
-            return;
-        }
-
-        boolean globPrefix = pattern.startsWith("*");
-        boolean globSuffix = pattern.endsWith("*");
-
-        if (globPrefix && globSuffix)
-        {
-            substring = pattern.substring(1, pattern.length() - 1);
-            type = INFIX;
-            return;
-        }
-
-        if (globPrefix)
-        {
-            substring = pattern.substring(1);
-            type = SUFFIX;
-            return;
-        }
-
-        if (globSuffix)
-        {
-            substring = pattern.substring(0, pattern.length() - 1);
-            type = PREFIX;
-            return;
-        }
-
-        type = MatchType.EXACT;
-        substring = pattern;
+        return Pattern.compile(createRegexpFromGlob(pattern), Pattern.CASE_INSENSITIVE);
     }
 
-    public boolean matches(String input)
+    private static String createRegexpFromGlob(String pattern)
     {
-        switch (type)
-        {
-            case ANY:
-                return true;
-
-            case EXACT:
-
-                return input.equalsIgnoreCase(substring);
-
-            case INFIX:
-
-                return input.toLowerCase().contains(substring.toLowerCase());
-
-            case PREFIX:
-
-                return input.regionMatches(true, 0, substring, 0, substring.length());
+        return oldStyleGlob.matcher(pattern).matches()
+               ? pattern.replace("*", ".*")
+               : pattern;
+    }
 
-            default:
 
-                return input.regionMatches(
-                        true,
-                        input.length() - substring.length(),
-                        substring,
-                        0,
-                        substring.length());
-        }
+    public boolean matches(String input)
+    {
+        return pattern.matcher(input).matches();
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcherTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcherTest.java?rev=682544&r1=682543&r2=682544&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcherTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/GlobPatternMatcherTest.java Mon Aug  4 16:57:12 2008
@@ -1,80 +1,85 @@
-// Copyright 2006 The Apache Software Foundation
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
+// Copyright 2006, 2008 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
 package org.apache.tapestry5.ioc.internal;
 
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertTrue;
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
-/**
- *
- */
-public class GlobPatternMatcherTest
+public class GlobPatternMatcherTest extends Assert
 {
     private boolean globMatch(String input, String pattern)
     {
         return new GlobPatternMatcher(pattern).matches(input);
     }
 
-    @Test
-    public void glob_match_exact()
+    @DataProvider(name = "matches")
+    public Object[][] matches()
     {
-        assertTrue(globMatch("fred", "fred"));
-        assertTrue(globMatch("fred", "FRED"));
-        assertFalse(globMatch("xfred", "fred"));
-        assertFalse(globMatch("fredx", "fred"));
-        assertFalse(globMatch("fred", "xfred"));
-        assertFalse(globMatch("fred", "fredx"));
+        return new Object[][]
+                {
+                        {"fred", "fred"},
+                        {"fred", "FRED"},
+                        {"fred", "*"},
+                        {"", "*"},
+                        {"fred.Barney", "*Barney"},
+                        {"fred.Barney", "*BARNEY"},
+                        {"fred.Barney", "fred*"},
+                        {"fred.Barney", "FRED*"},
+                        {"fredBarney", "*dB*"},
+                        {"fredBarney", "*DB*"},
+                        {"fred.Barney", "*Barney*"},
+                        {"fred.Barney", "*fred*"},
+                        {"fred.Barney", "*FRED*"},
+                        {"MyEntityDAO", ".*dao"},
+                        {"FredDAO", "(fred|barney)dao"}
+                };
+    }
+
+    @Test(dataProvider = "matches")
+    public void successful_glob_match(String input, String pattern)
+    {
+        assertTrue(globMatch(input, pattern));
     }
 
-    @Test
-    public void glob_match_wild()
-    {
-        assertTrue(globMatch("fred", "*"));
-        assertTrue(globMatch("", "*"));
-    }
 
-    @Test
-    public void glob_match_prefix()
-    {
-        assertTrue(globMatch("fred.Barney", "*Barney"));
-        assertTrue(globMatch("fred.Barney", "*BARNEY"));
-        assertFalse(globMatch("fred.Barneyx", "*Barney"));
-        assertFalse(globMatch("fred.Barney", "*Barneyx"));
-        assertFalse(globMatch("fred.Barney", "*xBarney"));
+    @DataProvider(name = "mismatches")
+    public Object[][] mismatches()
+    {
+        return new Object[][]
+                {
+                        {"xfred", "fred"},
+                        {"fredx", "fred"},
+                        {"fred", "xfred"},
+                        {"fred", "fredx"},
+                        {"fred.Barneyx", "*Barney"},
+                        {"fred.Barney", "*Barneyx"},
+                        {"fred.Barney", "*xBarney"},
+                        {"xfred.Barney", "fred*"},
+                        {"fred.Barney", "fredx*"},
+                        {"fred.Barney", "xfred*"},
+                        {"fred.Barney", "*flint*"},
+                        {"MyEntityDAL", ".*dao"},
+                        {"WilmaDAO", "(fred|barney)dao"}
+                };
     }
 
-    @Test
-    public void glob_match_suffix()
-    {
-        assertTrue(globMatch("fred.Barney", "fred*"));
-        assertTrue(globMatch("fred.Barney", "FRED*"));
-        assertFalse(globMatch("xfred.Barney", "fred*"));
-        assertFalse(globMatch("fred.Barney", "fredx*"));
-        assertFalse(globMatch("fred.Barney", "xfred*"));
-    }
 
-    @Test
-    public void glob_match_infix()
+    @Test(dataProvider = "mismatches")
+    public void unsuccessful_glob_match(String input, String pattern)
     {
-        assertTrue(globMatch("fred.Barney", "*d.B*"));
-        assertTrue(globMatch("fred.Barney", "*D.B*"));
-        assertTrue(globMatch("fred.Barney", "*Barney*"));
-        assertTrue(globMatch("fred.Barney", "*fred*"));
-        assertTrue(globMatch("fred.Barney", "*FRED*"));
-        assertFalse(globMatch("fred.Barney", "*flint*"));
+        assertFalse(globMatch(input, pattern));
     }
-
 }