Bug 4969: Invalid regex pattern for xpath conditions stripping. 80/33780/3
authorPeter Kajsa <pkajsa@cisco.com>
Fri, 29 Jan 2016 13:56:10 +0000 (14:56 +0100)
committerRobert Varga <nite@hq.sk>
Sat, 30 Jan 2016 11:47:33 +0000 (11:47 +0000)
Original regex pattern performed greedy match.
The fix excludes square brackets inside a match.

Change-Id: I3a95a041ab87f6d9d7d4c1e1bcf70095ece24fff
Signed-off-by: Peter Kajsa <pkajsa@cisco.com>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4969Test.java [new file with mode: 0644]

index 7ad5070f195a118faf8590d75e3671208029c578..25e4cab4ffbc069ac5e87c5a97e36cd7f3f331cc 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.model.util;
 
 import com.google.common.annotations.Beta;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
@@ -766,7 +767,7 @@ public final class SchemaContextUtil {
         }
     }
 
-    private static final Pattern STRIP_PATTERN = Pattern.compile("\\[.*\\]");
+    private static final Pattern STRIP_PATTERN = Pattern.compile("\\[[^\\[\\]]*\\]");
 
     /**
      * Removes conditions from xPath pointed to target node.
@@ -776,7 +777,8 @@ public final class SchemaContextUtil {
      * @return string representation of xPath without conditions
      *
      */
-    private static String stripConditionsFromXPathString(final RevisionAwareXPath pathStatement) {
+    @VisibleForTesting
+    static String stripConditionsFromXPathString(final RevisionAwareXPath pathStatement) {
         return STRIP_PATTERN.matcher(pathStatement.toString()).replaceAll("");
     }
 
diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4969Test.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4969Test.java
new file mode 100644 (file)
index 0000000..271dd70
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.util;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
+
+public class Bug4969Test {
+    @Test
+    public void testRegex() {
+        RevisionAwareXPath xPath = new RevisionAwareXPathImpl(
+                "nd:network[nd:network-id=current()/../network-ref]/nd:node[nd:node-id=current()/../node-ref]/termination-point/tp-id",
+                true);
+        assertEquals("nd:network/nd:node/termination-point/tp-id",
+                SchemaContextUtil.stripConditionsFromXPathString(xPath));
+    }
+}