incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stefane...@apache.org
Subject svn commit: r1564784 - in /sling/trunk/bundles/extensions/discovery/impl/src: main/java/org/apache/sling/discovery/impl/common/WildcardHelper.java test/java/org/apache/sling/discovery/impl/common/WildcardHelperTest.java
Date Wed, 05 Feb 2014 14:19:12 GMT
Author: stefanegli
Date: Wed Feb  5 14:19:11 2014
New Revision: 1564784

URL: http://svn.apache.org/r1564784
Log:
SLING-3001 : WildcardHelper added to the project

Added:
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/WildcardHelper.java
    sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/WildcardHelperTest.java

Added: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/WildcardHelper.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/WildcardHelper.java?rev=1564784&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/WildcardHelper.java
(added)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/WildcardHelper.java
Wed Feb  5 14:19:11 2014
@@ -0,0 +1,52 @@
+/*
+ * 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.sling.discovery.impl.common;
+
+import java.util.regex.Pattern;
+
+/** Helper class for wildcards **/
+public class WildcardHelper {
+
+    /** converts a string containing wildcards (* and ?) into a valid regex **/
+    public static String wildcardAsRegex(String patternWithWildcards) {
+        if (patternWithWildcards==null) {
+            throw new IllegalArgumentException("patternWithWildcards must not be null");
+        }
+        return "\\Q"+patternWithWildcards.replace("?", "\\E.\\Q").replace("*", "\\E.*\\Q")+"\\E";
+    }
+
+    /**
+     * Compare a given string (comparee) against a pattern that contains wildcards
+     * and return true if it matches.
+     * @param comparee the string which should be tested against a pattern containing wildcards
+     * @param patternWithWildcards the pattern containing wildcards (* and ?)
+     * @return true if the comparee string matches against the pattern containing wildcards
+     */
+    public static boolean matchesWildcard(String comparee, String patternWithWildcards) {
+        if (comparee==null) {
+            throw new IllegalArgumentException("comparee must not be null");
+        }
+        if (patternWithWildcards==null) {
+            throw new IllegalArgumentException("patternWithEWildcards must not be null");
+        }
+        final String regex = wildcardAsRegex(patternWithWildcards);
+        return Pattern.matches(regex, comparee);
+    }
+
+}

Added: sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/WildcardHelperTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/WildcardHelperTest.java?rev=1564784&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/WildcardHelperTest.java
(added)
+++ sling/trunk/bundles/extensions/discovery/impl/src/test/java/org/apache/sling/discovery/impl/common/WildcardHelperTest.java
Wed Feb  5 14:19:11 2014
@@ -0,0 +1,117 @@
+/*
+ * 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.sling.discovery.impl.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+
+import java.util.UUID;
+
+import org.apache.commons.net.util.SubnetUtils;
+import org.junit.Test;
+
+public class WildcardHelperTest {
+
+    @Test
+    public void testNullValues() {
+        
+        SubnetUtils s = new SubnetUtils("1.2.3.4/10");
+        s = new SubnetUtils("1.2.3.4", "255.255.0.0");
+        
+        try{
+            WildcardHelper.wildcardAsRegex(null);
+            fail("should complain");
+        } catch(IllegalArgumentException iae) {
+            // ok
+        }
+        try{
+            WildcardHelper.matchesWildcard(null, "foo");
+            fail("should complain");
+        } catch(IllegalArgumentException iae) {
+            // ok
+        }
+        try{
+            WildcardHelper.matchesWildcard("foo", null);
+            fail("should complain");
+        } catch(IllegalArgumentException iae) {
+            // ok
+        }
+    }
+    
+    @Test
+    public void testEmptyValue() {
+        final String expected = "\\Q\\E";
+        assertEquals(expected, WildcardHelper.wildcardAsRegex(""));
+    }
+    
+    @Test
+    public void testWithoutWildcards() {
+        for(int i=0; i<1000; i++) {
+            String randomString = UUID.randomUUID().toString();
+            assertTrue(WildcardHelper.matchesWildcard(randomString, randomString));
+        }
+    }
+    
+    @Test
+    public void testWildcards() {
+        assertTrue(WildcardHelper.matchesWildcard("", "*"));
+        assertTrue(WildcardHelper.matchesWildcard("fooo", "*"));
+        assertTrue(WildcardHelper.matchesWildcard("fooo", "**"));
+        assertTrue(WildcardHelper.matchesWildcard("fooo", "**?"));
+        assertTrue(WildcardHelper.matchesWildcard("fooo", "????"));
+        assertFalse(WildcardHelper.matchesWildcard("fooo", "???"));
+        assertFalse(WildcardHelper.matchesWildcard("fooo", "?????"));
+
+        for(int i=0; i<100; i++) {
+            String randomString = UUID.randomUUID().toString();
+            assertTrue(WildcardHelper.matchesWildcard(randomString, "*"));
+            assertTrue(WildcardHelper.matchesWildcard(randomString, randomString.substring(0,
randomString.length()-1)+"?"));
+            assertTrue(WildcardHelper.matchesWildcard(randomString, randomString.substring(0,
randomString.length()-1)+"*"));
+            assertTrue(WildcardHelper.matchesWildcard(randomString, randomString.substring(0,
randomString.length()-2)+"??"));
+            assertTrue(WildcardHelper.matchesWildcard(randomString, randomString.substring(0,
randomString.length()-2)+"*"));
+            assertTrue(WildcardHelper.matchesWildcard(randomString, "?" + randomString.substring(1,
randomString.length())));
+            assertTrue(WildcardHelper.matchesWildcard(randomString, "*" + randomString.substring(1,
randomString.length())));
+            assertTrue(WildcardHelper.matchesWildcard(randomString, "??" + randomString.substring(2,
randomString.length())));
+            assertTrue(WildcardHelper.matchesWildcard(randomString, "*" + randomString.substring(2,
randomString.length())));
+        }
+        
+        assertTrue(WildcardHelper.matchesWildcard("fooo", "f*"));
+        assertTrue(WildcardHelper.matchesWildcard("fooo", "fo*"));
+        assertTrue(WildcardHelper.matchesWildcard("fooo", "foo*"));
+        assertTrue(WildcardHelper.matchesWildcard("fooo", "fooo*"));
+        assertFalse(WildcardHelper.matchesWildcard("fooo", "fooo?"));
+        assertFalse(WildcardHelper.matchesWildcard("fooo", "foooo*"));
+        
+        assertTrue(WildcardHelper.matchesWildcard("fooo", "*"));
+        assertFalse(WildcardHelper.matchesWildcard("fooo", "?"));
+        assertFalse(WildcardHelper.matchesWildcard("fooo", "f?"));
+
+        assertTrue(WildcardHelper.matchesWildcard("fo", "f?"));
+
+        assertTrue(WildcardHelper.matchesWildcard("foooba", "f*b?"));
+        assertFalse(WildcardHelper.matchesWildcard("fooobar", "f*b?"));
+        assertFalse(WildcardHelper.matchesWildcard("foooba", "f*b?r"));
+        assertTrue(WildcardHelper.matchesWildcard("fooobar", "f*b?r"));
+        assertTrue(WildcardHelper.matchesWildcard("foooba", "f???b?"));
+        assertFalse(WildcardHelper.matchesWildcard("foooba", "f??b?"));
+        assertFalse(WildcardHelper.matchesWildcard("foooba", "f??b?"));
+    }
+}



Mime
View raw message