Fix another issue with substring matching add test case. (FELIX-2107)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@912301 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/util/Util.java b/framework/src/main/java/org/apache/felix/framework/util/Util.java
index 3f5f8ae..8f87408 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/Util.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/Util.java
@@ -613,6 +613,7 @@
public static boolean checkSubstring(String[] pieces, String s)
{
+System.out.println("MATCHING " + s);
// Walk the pieces to match the string
// There are implicit stars between each piece,
// and the first and last pieces might be "" to anchor the match.
@@ -627,7 +628,9 @@
loop: for (int i = 0; i < len; i++)
{
String piece = pieces[i];
- // initial non-star; assert index == 0
+
+ // If this is the first piece, then make sure the
+ // string starts with it.
if (i == 0)
{
if (!s.startsWith(piece))
@@ -636,8 +639,10 @@
break loop;
}
}
- // this is the last piece
- else if (i == len - 1)
+
+ // If this is the last piece, then make sure the
+ // string ends with it.
+ if (i == len - 1)
{
if (s.endsWith(piece))
{
@@ -649,10 +654,11 @@
}
break loop;
}
- // assert i > 0 && i < len-1
- else
+
+ // If this is neither the first or last piece, then
+ // make sure the string contains it.
+ if ((i > 0) && (i < (len - 1)))
{
- // Sure wish stringbuffer supported e.g. indexOf
index = s.indexOf(piece, index);
if (index < 0)
{
@@ -660,7 +666,8 @@
break loop;
}
}
- // start beyond the matching piece
+
+ // Move string index beyond the matching piece.
index += piece.length();
}
diff --git a/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java b/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java
new file mode 100644
index 0000000..56fb313
--- /dev/null
+++ b/framework/src/test/java/org/apache/felix/framework/util/UtilTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.felix.framework.util;
+
+import junit.framework.TestCase;
+
+public class UtilTest extends TestCase
+{
+ public void testSubstringMatching()
+ {
+ String[] pieces;
+
+ pieces = Util.parseSubstring("foo");
+ assertTrue("Should match!", Util.checkSubstring(pieces, "foo"));
+ assertFalse("Should not match!", Util.checkSubstring(pieces, "barfoo"));
+ assertFalse("Should not match!", Util.checkSubstring(pieces, "foobar"));
+
+ pieces = Util.parseSubstring("foo*");
+ assertTrue("Should match!", Util.checkSubstring(pieces, "foo"));
+ assertFalse("Should not match!", Util.checkSubstring(pieces, "barfoo"));
+ assertTrue("Should match!", Util.checkSubstring(pieces, "foobar"));
+
+ pieces = Util.parseSubstring("*foo");
+ assertTrue("Should match!", Util.checkSubstring(pieces, "foo"));
+ assertTrue("Should match!", Util.checkSubstring(pieces, "barfoo"));
+ assertFalse("Should match!", Util.checkSubstring(pieces, "foobar"));
+
+ pieces = Util.parseSubstring("foo*bar");
+ assertTrue("Should match!", Util.checkSubstring(pieces, "foobar"));
+ assertFalse("Should not match!", Util.checkSubstring(pieces, "barfoo"));
+ assertTrue("Should match!", Util.checkSubstring(pieces, "foosldfjbar"));
+
+ pieces = Util.parseSubstring("*foo*bar");
+ assertTrue("Should match!", Util.checkSubstring(pieces, "foobar"));
+ assertFalse("Should not match!", Util.checkSubstring(pieces, "foobarfoo"));
+ assertTrue("Should match!", Util.checkSubstring(pieces, "barfoobar"));
+ assertTrue("Should match!", Util.checkSubstring(pieces, "sdffoobsdfbar"));
+
+ pieces = Util.parseSubstring("*foo*bar*");
+ assertTrue("Should match!", Util.checkSubstring(pieces, "foobar"));
+ assertTrue("Should match!", Util.checkSubstring(pieces, "foobarfoo"));
+ assertTrue("Should match!", Util.checkSubstring(pieces, "barfoobar"));
+ assertTrue("Should match!", Util.checkSubstring(pieces, "sdffoobsdfbar"));
+ assertTrue("Should match!", Util.checkSubstring(pieces, "sdffoobsdfbarlj"));
+ assertFalse("Should not match!", Util.checkSubstring(pieces, "sdffobsdfbarlj"));
+ }
+}
\ No newline at end of file