Skip to content

Commit

Permalink
Adding tests for GlobMatch
Browse files Browse the repository at this point in the history
Signed-off-by: Niyati Aggarwal <[email protected]>
  • Loading branch information
niyatiagg committed Apr 5, 2024
1 parent fb3f588 commit ab6c653
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libs/common/src/main/java/org/opensearch/common/Glob.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class Glob {
* @return whether the String matches the given pattern
*/
public static boolean globMatch(String pattern, String str) {
if (pattern == null || str == null) {
return false;
}
int sIdx = 0, pIdx = 0, match = 0, wildcardIdx = -1;
while (sIdx < str.length()) {
// both chars matching, incrementing both pointers
Expand Down
67 changes: 67 additions & 0 deletions server/src/test/java/org/opensearch/common/GlobTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common;

import org.opensearch.test.OpenSearchTestCase;

public class GlobTests extends OpenSearchTestCase {

public void testGlobMatchForNull() {
assertFalse(Glob.globMatch(null, "test"));
assertFalse(Glob.globMatch("test", null));
assertFalse(Glob.globMatch(null, null));
}

public void testGlobMatchNoWildcard() {
assertTrue(Glob.globMatch("abcd", "abcd"));
assertFalse(Glob.globMatch("abcd", "foobar"));
}

public void testGlobMatchSingleWildcard() {
assertTrue(Glob.globMatch("*foo", "barfoo"));
assertFalse(Glob.globMatch("*foo", "foobar"));
assertTrue(Glob.globMatch("foo*", "foobarfoo"));
assertFalse(Glob.globMatch("foo*", "barfoobar"));
assertTrue(Glob.globMatch("foo*bar", "foobarnfoosbar"));
}

public void testGlobMatchMultipleWildcards() {
assertTrue(Glob.globMatch("*foo*", "barfoobar"));
assertFalse(Glob.globMatch("*foo*", "baroofbar"));
assertTrue(Glob.globMatch("*foo*bar", "abcdfooefghbar"));
assertFalse(Glob.globMatch("*foo*bar", "foonotbars"));
}

public void testGlobalMatchDoubleWildcard() {
assertTrue(Glob.globMatch("**foo", "barbarfoo"));
assertFalse(Glob.globMatch("**foo", "barbarfoowoof"));
assertTrue(Glob.globMatch("**bar**", "foobarfoo"));
assertFalse(Glob.globMatch("**bar**", "foobanfoo"));
}

public void testGlobMatchMultipleCharactersWithSingleWildcard() {
assertTrue(Glob.globMatch("a*b", "acb"));
assertTrue(Glob.globMatch("f*oo", "foo"));
assertTrue(Glob.globMatch("a*b", "aab"));
assertTrue(Glob.globMatch("a*b", "aaab"));
}

public void testGlobMatchWildcardWithEmptyString() {
assertTrue(Glob.globMatch("*", ""));
assertTrue(Glob.globMatch("a*", "a"));
assertFalse(Glob.globMatch("a*", ""));
}

public void testGlobMatchMultipleWildcardsWithMultipleCharacters() {
assertTrue(Glob.globMatch("a*b*c", "abc"));
assertTrue(Glob.globMatch("a*b*c", "axxxbxbc"));
assertFalse(Glob.globMatch("a*b*c", "abca"));
assertFalse(Glob.globMatch("a*b*c", "ac"));
}
}

0 comments on commit ab6c653

Please sign in to comment.