METAMODEL-1103: Fixed
authorkaspersorensen <i.am.kasper.sorensen@gmail.com>
Fri, 15 Jul 2016 23:04:50 +0000 (16:04 -0700)
committerkaspersorensen <i.am.kasper.sorensen@gmail.com>
Fri, 15 Jul 2016 23:04:50 +0000 (16:04 -0700)
Closes #115

CHANGES.md
core/src/main/java/org/apache/metamodel/util/WildcardPattern.java
core/src/test/java/org/apache/metamodel/util/WildcardPatternTest.java

index 65223ac..0b2b49d 100644 (file)
@@ -1,6 +1,7 @@
 ### Apache MetaModel 4.5.4 (work in progress)
 
  * [METAMODEL-1099] - Created a new DataContextFactory SPI and a extensible registry of implementations based on ServiceLoader.
+ * [METAMODEL-1103] - Fixed a bug pertaining to anchoring of wildcards in LIKE operands.
  * [METAMODEL-1088] - Add support for aliases in MongoDB.
  * [METAMODEL-1086] - Fixed encoding issue when CsvDataContext is instantiated with InputStream.
  * [METAMODEL-1094] - Added support for Apache Cassandra version 3.x.
index 648c2ca..715e0d2 100644 (file)
@@ -32,14 +32,20 @@ import org.apache.metamodel.query.FilterItem;
 public final class WildcardPattern implements Serializable {
 
        private static final long serialVersionUID = 857462137797209624L;
+       private final boolean _startsWithDelim;
+       private final boolean _endsWithDelim;
        private String _pattern;
        private char _wildcard;
-       private boolean _endsWithDelim;
 
        public WildcardPattern(String pattern, char wildcard) {
                _pattern = pattern;
                _wildcard = wildcard;
-               _endsWithDelim = (_pattern.charAt(pattern.length() - 1) == _wildcard);
+               if(_pattern.isEmpty()){
+                       _startsWithDelim = _endsWithDelim = false;
+               } else {
+                       _startsWithDelim = _pattern.charAt(0) == _wildcard;
+                       _endsWithDelim = _pattern.charAt(pattern.length() - 1) == _wildcard;
+               }
        }
 
        public boolean matches(String value) {
@@ -50,9 +56,10 @@ public final class WildcardPattern implements Serializable {
                                Character.toString(_wildcard));
                int charIndex = 0;
                while (st.hasMoreTokens()) {
+                       int oldIndex = charIndex;
                        String token = st.nextToken();
                        charIndex = value.indexOf(token, charIndex);
-                       if (charIndex == -1) {
+                       if (charIndex == -1 || !_startsWithDelim && oldIndex == 0 && charIndex != 0) {
                                return false;
                        }
                        charIndex = charIndex + token.length();
index 1e075dd..4bc45f7 100644 (file)
@@ -41,5 +41,16 @@ public class WildcardPatternTest extends TestCase {
                assertTrue(pattern.matches("foobarbar"));
                assertFalse(pattern.matches("w00p"));
 
+               pattern = new WildcardPattern("oba%", '%');
+               assertTrue(pattern.matches("obar"));
+               assertFalse(pattern.matches("foobar"));
+
+               pattern = new WildcardPattern("bar", '%');
+               assertTrue(pattern.matches("bar"));
+               assertFalse(pattern.matches("foobar"));
+
+               pattern = new WildcardPattern("", '%');
+               assertTrue(pattern.matches(""));
+               assertFalse(pattern.matches("foo"));
        }
 }
\ No newline at end of file