From 53161d9235deb77da61a34273e1233f5b6ffba10 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 20 Feb 2018 19:12:19 +0100 Subject: [PATCH] Encapsulate regexes in a non-capturing group Regular expressions are pesky, nwo we are being bitten by branches. Let's just encapsulate the internal pattern into a non-capturing group and be done with it. JIRA: YANGTOOLS-798 Change-Id: If3b3b2fb35ecd336bc23fd7acaf29efa48eb9b74 Signed-off-by: Robert Varga (cherry picked from commit 42b62f098cbabbc286c3a506494c5e280f6c365b) --- .../StringPatternCheckingCodecTest.java | 4 +- .../bug2444/yin/modifier-yang1@1970-01-01.yin | 4 +- .../bugs/bug2444/yin/modifier@1970-01-01.yin | 4 +- .../yangtools/yang/model/util/RegexUtils.java | 3 +- .../yang/model/util/Bug4079Test.java | 64 ++++++++++++------- .../yang/model/util/Bug5410Test.java | 27 ++++---- .../yang/parser/stmt/rfc6020/Bug5410Test.java | 4 +- .../yangtools/yang/stmt/Bug4623Test.java | 8 +-- .../yangtools/yang/stmt/Bug5396Test.java | 8 +-- .../yangtools/yang/stmt/Bug6180Test.java | 6 +- .../yang/stmt/EffectiveStatementTypeTest.java | 4 +- .../yang/stmt/TypesResolutionTest.java | 20 +++--- .../yangtools/yang/stmt/YangParserTest.java | 14 ++-- 13 files changed, 96 insertions(+), 74 deletions(-) diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codecs/StringPatternCheckingCodecTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codecs/StringPatternCheckingCodecTest.java index 19e53794c8..ce836b9e6f 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codecs/StringPatternCheckingCodecTest.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codecs/StringPatternCheckingCodecTest.java @@ -65,8 +65,8 @@ public class StringPatternCheckingCodecTest { codec.deserialize("abcd"); fail("Exception should have been thrown."); } catch (final IllegalArgumentException ex) { - LOG.debug("IllegalArgumentException was thrown as expected: {}", ex); - assertTrue(ex.getMessage().contains("Supplied value does not match the regular expression ^[A-Z]+$. [abcd]")); + LOG.debug("IllegalArgumentException was thrown as expected", ex); + assertEquals("Supplied value does not match the regular expression ^(?:[A-Z]+)$. [abcd]", ex.getMessage()); } } } diff --git a/yang/yang-model-export/src/test/resources/bugs/bug2444/yin/modifier-yang1@1970-01-01.yin b/yang/yang-model-export/src/test/resources/bugs/bug2444/yin/modifier-yang1@1970-01-01.yin index 55b167b7be..26b058ce6d 100644 --- a/yang/yang-model-export/src/test/resources/bugs/bug2444/yin/modifier-yang1@1970-01-01.yin +++ b/yang/yang-model-export/src/test/resources/bugs/bug2444/yin/modifier-yang1@1970-01-01.yin @@ -16,7 +16,7 @@ Supplied value does not match the regular - expression ^.*$. + expression ^(?:.*)$. @@ -25,4 +25,4 @@ - \ No newline at end of file + diff --git a/yang/yang-model-export/src/test/resources/bugs/bug2444/yin/modifier@1970-01-01.yin b/yang/yang-model-export/src/test/resources/bugs/bug2444/yin/modifier@1970-01-01.yin index d31a2e837b..bd4cff2be4 100644 --- a/yang/yang-model-export/src/test/resources/bugs/bug2444/yin/modifier@1970-01-01.yin +++ b/yang/yang-model-export/src/test/resources/bugs/bug2444/yin/modifier@1970-01-01.yin @@ -16,7 +16,7 @@ Supplied value does not match the regular - expression ^.*$. + expression ^(?:.*)$. @@ -26,4 +26,4 @@ - \ No newline at end of file + diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/RegexUtils.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/RegexUtils.java index 0991750e6c..7aab1eaf94 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/RegexUtils.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/RegexUtils.java @@ -246,7 +246,8 @@ public final class RegexUtils { * @return Java-compatible regex */ public static String getJavaRegexFromXSD(final String xsdRegex) { - return "^" + fixUnicodeScriptPattern(escapeChars(xsdRegex)) + '$'; + // Note: we are using a non-capturing group to deal with internal structure issues, like branches and similar. + return "^(?:" + fixUnicodeScriptPattern(escapeChars(xsdRegex)) + ")$"; } /* diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4079Test.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4079Test.java index 5333c9882b..28db4a52b6 100644 --- a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4079Test.java +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4079Test.java @@ -8,8 +8,11 @@ package org.opendaylight.yangtools.yang.model.util; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import org.junit.Test; @@ -19,96 +22,96 @@ public class Bug4079Test { @Test public void testValidPatternFix() { String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsArrows})*+"); - assertEquals("^(\\p{InArrows})*+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InArrows})*+)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsDingbats})++"); - assertEquals("^(\\p{InDingbats})++$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InDingbats})++)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsSpecials})?+"); - assertEquals("^(\\p{InSpecials})?+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InSpecials})?+)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBatak}){4}+"); - assertEquals("^(\\p{IsBatak}){4}+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{IsBatak}){4}+)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}){4,6}+"); - assertEquals("^(\\p{IsLatin}){4,6}+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{IsLatin}){4,6}+)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsTibetan}){4,}+"); - assertEquals("^(\\p{IsTibetan}){4,}+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{IsTibetan}){4,}+)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsAlphabetic}){4}?"); - assertEquals("^(\\p{IsAlphabetic}){4}?$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{IsAlphabetic}){4}?)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLowercase}){4,6}?"); - assertEquals("^(\\p{IsLowercase}){4,6}?$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{IsLowercase}){4,6}?)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsUppercase}){4,}?"); - assertEquals("^(\\p{IsUppercase}){4,}?$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{IsUppercase}){4,}?)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*"); - assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+"); - assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?"); - assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}"); - assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4}$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4})$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsArmenian}){2,4}"); - assertEquals("^(\\p{IsLatin}|\\p{IsArmenian}){2,4}$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{IsLatin}|\\p{IsArmenian}){2,4})$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsBasicLatin}){2,}"); - assertEquals("^(\\p{IsLatin}|\\p{InBasicLatin}){2,}$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{IsLatin}|\\p{InBasicLatin}){2,})$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin})*?"); - assertEquals("^(\\p{InBasicLatin}|\\p{IsLatin})*?$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InBasicLatin}|\\p{IsLatin})*?)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD( "(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsArrows})+?"); - assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{InArrows})+?$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{InArrows})+?)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD( "(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsLatin})??"); - assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\p{IsBasicLatin})*+"); - assertEquals("^(\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\p{IsBasicLatin})*+"); - assertEquals("^(\\\\\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\\\\\p{IsBasicLatin})*+"); - assertEquals("^(\\\\\\\\\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\\\\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern); assertNotNull(Pattern.compile(fixedUnicodeScriptPattern)); } @Test(expected = PatternSyntaxException.class) public void testInvalidPattern() { String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\p{IsBasicLatin})*+"); - assertEquals("^(\\\\p{IsBasicLatin})*+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\\\p{IsBasicLatin})*+)$", fixedUnicodeScriptPattern); // should throw exception Pattern.compile(fixedUnicodeScriptPattern); } @@ -117,7 +120,7 @@ public class Bug4079Test { public void testInvalidPattern2() { String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD( "(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+"); - assertEquals("^(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+)$", fixedUnicodeScriptPattern); // should throw exception Pattern.compile(fixedUnicodeScriptPattern); } @@ -126,8 +129,21 @@ public class Bug4079Test { public void testInvalidPattern3() { String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD( "(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+"); - assertEquals("^(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+$", fixedUnicodeScriptPattern); + assertEquals("^(?:(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+)$", fixedUnicodeScriptPattern); // should throw exception Pattern.compile(fixedUnicodeScriptPattern); } + + @Test + public void testCorrectBranches() { + String str = RegexUtils.getJavaRegexFromXSD("a|bb"); + assertEquals("^(?:a|bb)$", str); + Predicate pred = Pattern.compile(str).asPredicate(); + + assertTrue(pred.test("a")); + assertTrue(pred.test("bb")); + assertFalse(pred.test("ab")); + assertFalse(pred.test("abb")); + assertFalse(pred.test("ac")); + } } diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug5410Test.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug5410Test.java index 41daafa856..0e99709379 100644 --- a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug5410Test.java +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug5410Test.java @@ -118,26 +118,29 @@ public class Bug5410Test { @Test public void testJavaRegexFromXSD() { - testPattern("^[^:]+$", "^\\^[^:]+\\$$", ImmutableList.of("^a$", "^abc$"), + testPattern("^[^:]+$", "^(?:\\^[^:]+\\$)$", ImmutableList.of("^a$", "^abc$"), ImmutableList.of("abc$", "^abc", "^a:bc$")); - testPattern("^[$^]$", "^\\^[$^]\\$$", ImmutableList.of("^^$", "^$$"), ImmutableList.of("^^", "^$", "$^", "$$")); - testPattern("[$-%]+", "^[$-%]+$", ImmutableList.of("$", "%", "%$"), ImmutableList.of("$-", "$-%", "-", "^")); - testPattern("[$-&]+", "^[$-&]+$", ImmutableList.of("$", "%&", "%$", "$%&"), ImmutableList.of("#", "$-&", "'")); - - testPattern("[a-z&&[^m-p]]+", "^[a-z&&[^m-p]]+$", ImmutableList.of("a", "z", "az"), + testPattern("^[$^]$", "^(?:\\^[$^]\\$)$", ImmutableList.of("^^$", "^$$"), + ImmutableList.of("^^", "^$", "$^", "$$")); + testPattern("[$-%]+", "^(?:[$-%]+)$", ImmutableList.of("$", "%", "%$"), + ImmutableList.of("$-", "$-%", "-", "^")); + testPattern("[$-&]+", "^(?:[$-&]+)$", ImmutableList.of("$", "%&", "%$", "$%&"), + ImmutableList.of("#", "$-&", "'")); + + testPattern("[a-z&&[^m-p]]+", "^(?:[a-z&&[^m-p]]+)$", ImmutableList.of("a", "z", "az"), ImmutableList.of("m", "anz", "o")); - testPattern("^[\\[-b&&[^^-a]]+$", "^\\^[\\[-b&&[^^-a]]+\\$$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"), + testPattern("^[\\[-b&&[^^-a]]+$", "^(?:\\^[\\[-b&&[^^-a]]+\\$)$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"), ImmutableList.of("^a$", "^^$", "^_$")); - testPattern("[^^-~&&[^$-^]]", "^[^^-~&&[^$-^]]$", ImmutableList.of("!", "\"", "#"), + testPattern("[^^-~&&[^$-^]]", "^(?:[^^-~&&[^$-^]])$", ImmutableList.of("!", "\"", "#"), ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}")); - testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^\\\\\\[\\^[^^-~&&[^$-^]]$", + testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^(?:\\\\\\[\\^[^^-~&&[^$-^]])$", ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"), ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}")); - testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^$", + testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^(?:\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^)$", ImmutableList.of("^[^\\c]^", "^[^\\Z]^"), ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^")); - testPattern("[\\^]$", "^[\\^]\\$$", ImmutableList.of("^$"), + testPattern("[\\^]$", "^(?:[\\^]\\$)$", ImmutableList.of("^$"), ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$")); } @@ -160,7 +163,7 @@ public class Bug5410Test { } private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) { - testPattern(xsdRegex, '^' + unanchoredJavaRegex + '$', ImmutableList.of(), ImmutableList.of()); + testPattern(xsdRegex, "^(?:" + unanchoredJavaRegex + ")$", ImmutableList.of(), ImmutableList.of()); } private static void testPattern(final String javaRegex, final List positiveMatches, diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Bug5410Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Bug5410Test.java index 9f02e00efa..2195142d6f 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Bug5410Test.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Bug5410Test.java @@ -40,7 +40,9 @@ public class Bug5410Test { assertEquals(expectedYangRegex, rawRegex); final String javaRegexFromYang = pattern.getRegularExpression(); - final String expectedJavaRegex = "^\\$0\\$.*|\\$1\\$[a-zA-Z0-9./]{1,8}\\$[a-zA-Z0-9./]{22}|\\$5\\$(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{43}|\\$6\\$(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{86}$"; + final String expectedJavaRegex = "^(?:\\$0\\$.*|\\$1\\$[a-zA-Z0-9./]{1,8}\\$[a-zA-Z0-9./]{22}|\\$5\\$" + + "(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{43}|\\$6\\$(rounds=\\d+\\$)?" + + "[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{86})$"; assertEquals(expectedJavaRegex, javaRegexFromYang); final String value = "$6$AnrKGc0V$B/0/A.pWg4HrrA6YiEJOtFGibQ9Fmm5.4rI/00gEz3QeB7joSxBU3YtbHDm6NSkS1dKTQy3BWhwKKDS8nB5S//"; diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java index 04fef165b5..7ccda315e8 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java @@ -61,7 +61,7 @@ public class Bug4623Test { Assert.assertEquals(lengthConstraint.getMax(), Integer.valueOf(10)); final PatternConstraint patternConstraint = patternConstraints.get(0); - Assert.assertEquals(patternConstraint.getRegularExpression(), "^[0-9a-fA-F]$"); + Assert.assertEquals(patternConstraint.getRegularExpression(), "^(?:[0-9a-fA-F])$"); } @Test @@ -102,7 +102,7 @@ public class Bug4623Test { Assert.assertEquals(lengthConstraint.getMax(), Integer.valueOf(10)); final PatternConstraint patternConstraint = patternConstraints.get(0); - Assert.assertEquals(patternConstraint.getRegularExpression(), "^[0-9a-fA-F]$"); + Assert.assertEquals(patternConstraint.getRegularExpression(), "^(?:[0-9a-fA-F])$"); } @Test @@ -143,6 +143,6 @@ public class Bug4623Test { Assert.assertEquals(lengthConstraint.getMax(), Integer.valueOf(10)); final PatternConstraint patternConstraint = patternConstraints.get(0); - Assert.assertEquals(patternConstraint.getRegularExpression(), "^[0-9a-fA-F]$"); + Assert.assertEquals(patternConstraint.getRegularExpression(), "^(?:[0-9a-fA-F])$"); } -} \ No newline at end of file +} diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5396Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5396Test.java index 816aa58bb8..08da5c8947 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5396Test.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5396Test.java @@ -82,10 +82,10 @@ public class Bug5396Test { PatternConstraint patternConstraint2 = patternConstraints2.get(0); PatternConstraint patternConstraint3 = patternConstraints3.get(0); - assertEquals("^dp[0-9]+o[0-9]+(d[0-9]+)?$", patternConstraint0.getRegularExpression()); - assertEquals("^dp[0-9]+s[0-9]+(f[0-9]+)?(d[0-9]+)?$", patternConstraint1.getRegularExpression()); - assertEquals("^dp[0-9]+(P[0-9]+)?p[0-9]{1,3}s[0-9]{1,3}(f[0-9]+)?(d[0-9]+)?$", + assertEquals("^(?:dp[0-9]+o[0-9]+(d[0-9]+)?)$", patternConstraint0.getRegularExpression()); + assertEquals("^(?:dp[0-9]+s[0-9]+(f[0-9]+)?(d[0-9]+)?)$", patternConstraint1.getRegularExpression()); + assertEquals("^(?:dp[0-9]+(P[0-9]+)?p[0-9]{1,3}s[0-9]{1,3}(f[0-9]+)?(d[0-9]+)?)$", patternConstraint2.getRegularExpression()); - assertEquals("^dp[0-9]+p[0-9]+p[0-9]+$", patternConstraint3.getRegularExpression()); + assertEquals("^(?:dp[0-9]+p[0-9]+p[0-9]+)$", patternConstraint3.getRegularExpression()); } } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java index 05f2288f12..ac46c854b3 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java @@ -80,7 +80,7 @@ public class Bug6180Test { final List patternConstraints = ((StringTypeDefinition) type).getPatternConstraints(); assertEquals(1, patternConstraints.size()); final PatternConstraint pattern = patternConstraints.iterator().next(); - assertEquals("^\".*\"$", pattern.getRegularExpression()); + assertEquals("^(?:\".*\")$", pattern.getRegularExpression()); assertTrue(Pattern.compile(pattern.getRegularExpression()).matcher("\"enclosed string in quotes\"").matches()); } @@ -98,7 +98,7 @@ public class Bug6180Test { final List patternConstraints = ((StringTypeDefinition) type).getPatternConstraints(); assertEquals(1, patternConstraints.size()); final PatternConstraint pattern = patternConstraints.iterator().next(); - assertEquals("^'.*'$", pattern.getRegularExpression()); + assertEquals("^(?:'.*')$", pattern.getRegularExpression()); assertTrue(Pattern.compile(pattern.getRegularExpression()).matcher("'enclosed string in quotes'").matches()); } -} \ No newline at end of file +} diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java index b60f652e0f..721d269e6d 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java @@ -423,10 +423,10 @@ public class EffectiveStatementTypeTest { final PatternConstraintEffectiveImpl patternConstraintSecond = (PatternConstraintEffectiveImpl) ((StringTypeDefinition) currentLeaf .getType()).getPatternConstraints().get(0); - assertEquals("^[0-9a-fA-F]*$", patternConstraint.getRegularExpression()); + assertEquals("^(?:[0-9a-fA-F]*)$", patternConstraint.getRegularExpression()); assertNull(patternConstraint.getReference()); assertNull(patternConstraint.getDescription()); - assertEquals("Supplied value does not match the regular expression ^[0-9a-fA-F]*$.", patternConstraint.getErrorMessage()); + assertEquals("Supplied value does not match the regular expression ^(?:[0-9a-fA-F]*)$.", patternConstraint.getErrorMessage()); assertEquals("invalid-regular-expression", patternConstraint.getErrorAppTag()); assertNotNull(patternConstraint.toString()); assertNotNull(patternConstraint.hashCode()); diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/TypesResolutionTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/TypesResolutionTest.java index 49dbba72b6..60a8b72cc3 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/TypesResolutionTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/TypesResolutionTest.java @@ -130,20 +130,20 @@ public class TypesResolutionTest { StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0); assertNotNull(ipv4.getBaseType()); - String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}" - + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$"; + String expectedPattern = "^(?:(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}" + + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?)$"; assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getRegularExpression()); StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1); assertNotNull(ipv6.getBaseType()); List ipv6Patterns = ipv6.getPatternConstraints(); - expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}" + expectedPattern = "^(?:((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}" + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|" + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}" - + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$"; + + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?)$"; assertEquals(expectedPattern, ipv6Patterns.get(0).getRegularExpression()); - expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" - + "(%.+)?$"; + expectedPattern = "^(?:(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + + "(%.+)?)$"; assertEquals(expectedPattern, ipv6Patterns.get(1).getRegularExpression()); } @@ -155,8 +155,8 @@ public class TypesResolutionTest { assertNotNull(type.getBaseType()); List patterns = type.getPatternConstraints(); assertEquals(1, patterns.size()); - String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*" - + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$"; + String expectedPattern = "^(?:((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*" + + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.)$"; assertEquals(expectedPattern, patterns.get(0).getRegularExpression()); List lengths = type.getLengthConstraints(); @@ -310,7 +310,7 @@ public class TypesResolutionTest { List patterns = testedType.getPatternConstraints(); assertEquals(1, patterns.size()); PatternConstraint pattern = patterns.get(0); - assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getRegularExpression()); + assertEquals("^(?:\\d*(\\.\\d*){1,127})$", pattern.getRegularExpression()); QName testedTypeQName = testedType.getQName(); assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace()); @@ -322,7 +322,7 @@ public class TypesResolutionTest { assertEquals(1, patterns.size()); pattern = patterns.get(0); - assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$", + assertEquals("^(?:(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*)$", pattern.getRegularExpression()); QName testedTypeBaseQName = testedTypeBase.getQName(); diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserTest.java index 6f0aa2189d..4db6ea855e 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserTest.java @@ -244,7 +244,7 @@ public class YangParserTest { List patterns = type.getPatternConstraints(); assertEquals(1, patterns.size()); PatternConstraint pattern = patterns.iterator().next(); - assertEquals("^[e-z]*$", pattern.getRegularExpression()); + assertEquals("^(?:[e-z]*)$", pattern.getRegularExpression()); assertEquals(1, type.getLengthConstraints().size()); final StringTypeDefinition baseType1 = type.getBaseType(); @@ -257,7 +257,7 @@ public class YangParserTest { patterns = baseType1.getPatternConstraints(); assertEquals(1, patterns.size()); pattern = patterns.iterator().next(); - assertEquals("^[b-u]*$", pattern.getRegularExpression()); + assertEquals("^(?:[b-u]*)$", pattern.getRegularExpression()); assertEquals(1, baseType1.getLengthConstraints().size()); final StringTypeDefinition baseType2 = baseType1.getBaseType(); @@ -284,7 +284,7 @@ public class YangParserTest { patterns = baseType3.getPatternConstraints(); assertEquals(1, patterns.size()); pattern = patterns.iterator().next(); - assertEquals("^[a-k]*$", pattern.getRegularExpression()); + assertEquals("^(?:[a-k]*)$", pattern.getRegularExpression()); final List baseType3Lengths = baseType3.getLengthConstraints(); assertEquals(1, baseType3Lengths.size()); length = baseType3Lengths.get(0); @@ -334,7 +334,7 @@ public class YangParserTest { assertTrue(!patterns.isEmpty()); assertEquals(1, patterns.size()); final PatternConstraint pattern = patterns.iterator().next(); - assertEquals("^[e-z]*$", pattern.getRegularExpression()); + assertEquals("^(?:[e-z]*)$", pattern.getRegularExpression()); assertEquals(1, type.getLengthConstraints().size()); final LeafSchemaNode multiplePatternDirectStringDefLeaf = (LeafSchemaNode) foo @@ -353,9 +353,9 @@ public class YangParserTest { boolean isEZPattern = false; boolean isADPattern = false; for (final PatternConstraint patternConstraint : patterns) { - if (patternConstraint.getRegularExpression().equals("^[e-z]*$")) { + if (patternConstraint.getRegularExpression().equals("^(?:[e-z]*)$")) { isEZPattern = true; - } else if (patternConstraint.getRegularExpression().equals("^[a-d]*$")) { + } else if (patternConstraint.getRegularExpression().equals("^(?:[a-d]*)$")) { isADPattern = true; } } @@ -407,7 +407,7 @@ public class YangParserTest { final List patterns = baseType2.getPatternConstraints(); assertEquals(1, patterns.size()); final PatternConstraint pattern = patterns.iterator().next(); - assertEquals("^[a-k]*$", pattern.getRegularExpression()); + assertEquals("^(?:[a-k]*)$", pattern.getRegularExpression()); final List baseType3Lengths = baseType2.getLengthConstraints(); assertEquals(1, baseType3Lengths.size()); length = baseType3Lengths.get(0); -- 2.36.6