From 42b62f098cbabbc286c3a506494c5e280f6c365b 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 --- .../StringPatternCheckingCodecTest.java | 2 +- .../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 | 6 +- .../yangtools/yang/stmt/Bug5396Test.java | 8 +-- .../yangtools/yang/stmt/Bug6180Test.java | 4 +- .../yang/stmt/EffectiveStatementTypeTest.java | 4 +- .../yang/stmt/TypesResolutionTest.java | 20 +++--- .../yangtools/yang/stmt/YangParserTest.java | 14 ++-- 11 files changed, 88 insertions(+), 68 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 643a7a5012..4cf56becb5 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 @@ -57,7 +57,7 @@ public class StringPatternCheckingCodecTest { fail("Exception should have been thrown."); } catch (final IllegalArgumentException ex) { LOG.debug("IllegalArgumentException was thrown as expected", ex); - assertEquals("Supplied value does not match the regular expression ^[A-Z]+$.", ex.getMessage()); + assertEquals("Supplied value does not match the regular expression ^(?:[A-Z]+)$.", ex.getMessage()); } } } 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-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Bug5410Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Bug5410Test.java index e9a65eea4c..8e4903b041 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Bug5410Test.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Bug5410Test.java @@ -40,9 +40,9 @@ public class Bug5410Test { assertEquals(expectedYangRegex, rawRegex); final String javaRegexFromYang = pattern.getJavaPatternString(); - final String expectedJavaRegex = "^\\$0\\$.*|\\$1\\$[a-zA-Z0-9./]{1,8}\\$[a-zA-Z0-9./]{22}|\\$5\\$" + 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}$"; + + "[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{86})$"; assertEquals(expectedJavaRegex, javaRegexFromYang); final String value = "$6$AnrKGc0V$B/0/A.pWg4HrrA6YiEJOtFGibQ9Fmm5.4rI/" diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java index a5fb57d3dc..8ec6becc39 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java @@ -65,7 +65,7 @@ public class Bug4623Test { Assert.assertEquals(Integer.valueOf(10), span.upperEndpoint()); final PatternConstraint patternConstraint = patternConstraints.get(0); - Assert.assertEquals(patternConstraint.getJavaPatternString(), "^[0-9a-fA-F]$"); + Assert.assertEquals(patternConstraint.getRegularExpressionString(), "[0-9a-fA-F]"); } @Test @@ -107,7 +107,7 @@ public class Bug4623Test { Assert.assertEquals(Integer.valueOf(10), lengthConstraint.upperEndpoint()); final PatternConstraint patternConstraint = patternConstraints.get(0); - Assert.assertEquals(patternConstraint.getJavaPatternString(), "^[0-9a-fA-F]$"); + Assert.assertEquals(patternConstraint.getRegularExpressionString(), "[0-9a-fA-F]"); } @Test @@ -150,7 +150,7 @@ public class Bug4623Test { Assert.assertEquals(Integer.valueOf(10), lengthConstraint.upperEndpoint()); final PatternConstraint patternConstraint = patternConstraints.get(0); - Assert.assertEquals(patternConstraint.getJavaPatternString(), "^[0-9a-fA-F]$"); + Assert.assertEquals(patternConstraint.getRegularExpressionString(), "[0-9a-fA-F]"); } private static Module typesModule(final SchemaContext context) { diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5396Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5396Test.java index 0e287e19fa..caa5a82eb5 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5396Test.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5396Test.java @@ -82,10 +82,10 @@ public class Bug5396Test { final PatternConstraint patternConstraint2 = patternConstraints2.get(0); final PatternConstraint patternConstraint3 = patternConstraints3.get(0); - assertEquals("^dp[0-9]+o[0-9]+(d[0-9]+)?$", patternConstraint0.getJavaPatternString()); - assertEquals("^dp[0-9]+s[0-9]+(f[0-9]+)?(d[0-9]+)?$", patternConstraint1.getJavaPatternString()); - 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.getJavaPatternString()); + assertEquals("^(?:dp[0-9]+s[0-9]+(f[0-9]+)?(d[0-9]+)?)$", patternConstraint1.getJavaPatternString()); + assertEquals("^(?:dp[0-9]+(P[0-9]+)?p[0-9]{1,3}s[0-9]{1,3}(f[0-9]+)?(d[0-9]+)?)$", patternConstraint2.getJavaPatternString()); - assertEquals("^dp[0-9]+p[0-9]+p[0-9]+$", patternConstraint3.getJavaPatternString()); + assertEquals("^(?:dp[0-9]+p[0-9]+p[0-9]+)$", patternConstraint3.getJavaPatternString()); } } diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java index 2984100caf..60f5f3b899 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java @@ -81,7 +81,7 @@ public class Bug6180Test { final List patternConstraints = ((StringTypeDefinition) type).getPatternConstraints(); assertEquals(1, patternConstraints.size()); final PatternConstraint pattern = patternConstraints.iterator().next(); - assertEquals("^\".*\"$", pattern.getJavaPatternString()); + assertEquals("^(?:\".*\")$", pattern.getJavaPatternString()); assertTrue(Pattern.compile(pattern.getJavaPatternString()).matcher("\"enclosed string in quotes\"").matches()); } @@ -99,7 +99,7 @@ public class Bug6180Test { final List patternConstraints = ((StringTypeDefinition) type).getPatternConstraints(); assertEquals(1, patternConstraints.size()); final PatternConstraint pattern = patternConstraints.iterator().next(); - assertEquals("^'.*'$", pattern.getJavaPatternString()); + assertEquals("^(?:'.*')$", pattern.getJavaPatternString()); assertTrue(Pattern.compile(pattern.getJavaPatternString()).matcher("'enclosed string in quotes'").matches()); } } \ No newline at end of file diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java index 0cdbc4b0e2..33211f566d 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java @@ -434,10 +434,10 @@ public class EffectiveStatementTypeTest { final PatternConstraint patternConstraintSecond = ((StringTypeDefinition) currentLeaf.getType()) .getPatternConstraints().get(0); - assertEquals("^[0-9a-fA-F]*$", patternConstraint.getJavaPatternString()); + assertEquals("^(?:[0-9a-fA-F]*)$", patternConstraint.getJavaPatternString()); assertFalse(patternConstraint.getReference().isPresent()); assertFalse(patternConstraint.getDescription().isPresent()); - assertEquals(Optional.of("Supplied value does not match the regular expression ^[0-9a-fA-F]*$."), + assertEquals(Optional.of("Supplied value does not match the regular expression ^(?:[0-9a-fA-F]*)$."), patternConstraint.getErrorMessage()); assertEquals(Optional.of("invalid-regular-expression"), patternConstraint.getErrorAppTag()); assertNotNull(patternConstraint.toString()); diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TypesResolutionTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TypesResolutionTest.java index 5319b9dae0..ccf51f1557 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TypesResolutionTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TypesResolutionTest.java @@ -133,20 +133,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).getJavaPatternString()); 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).getJavaPatternString()); - expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" - + "(%.+)?$"; + expectedPattern = "^(?:(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)" + + "(%.+)?)$"; assertEquals(expectedPattern, ipv6Patterns.get(1).getJavaPatternString()); } @@ -158,8 +158,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).getJavaPatternString()); LengthConstraint lengths = type.getLengthConstraint().get(); @@ -314,7 +314,7 @@ public class TypesResolutionTest { List patterns = testedType.getPatternConstraints(); assertEquals(1, patterns.size()); PatternConstraint pattern = patterns.get(0); - assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getJavaPatternString()); + assertEquals("^(?:\\d*(\\.\\d*){1,127})$", pattern.getJavaPatternString()); QName testedTypeQName = testedType.getQName(); assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace()); @@ -326,7 +326,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.getJavaPatternString()); QName testedTypeBaseQName = testedTypeBase.getQName(); diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserTest.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserTest.java index 0621280a83..6c16627b1e 100644 --- a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserTest.java +++ b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserTest.java @@ -233,7 +233,7 @@ public class YangParserTest { List patterns = type.getPatternConstraints(); assertEquals(1, patterns.size()); PatternConstraint pattern = patterns.iterator().next(); - assertEquals("^[e-z]*$", pattern.getJavaPatternString()); + assertEquals("^(?:[e-z]*)$", pattern.getJavaPatternString()); assertEquals(1, type.getLengthConstraint().get().getAllowedRanges().asRanges().size()); final StringTypeDefinition baseType1 = type.getBaseType(); @@ -245,7 +245,7 @@ public class YangParserTest { patterns = baseType1.getPatternConstraints(); assertEquals(1, patterns.size()); pattern = patterns.iterator().next(); - assertEquals("^[b-u]*$", pattern.getJavaPatternString()); + assertEquals("^(?:[b-u]*)$", pattern.getJavaPatternString()); assertEquals(1, baseType1.getLengthConstraint().get().getAllowedRanges().asRanges().size()); final StringTypeDefinition baseType2 = baseType1.getBaseType(); @@ -268,7 +268,7 @@ public class YangParserTest { patterns = baseType3.getPatternConstraints(); assertEquals(1, patterns.size()); pattern = patterns.iterator().next(); - assertEquals("^[a-k]*$", pattern.getJavaPatternString()); + assertEquals("^(?:[a-k]*)$", pattern.getJavaPatternString()); final RangeSet baseType3Lengths = baseType3.getLengthConstraint().get().getAllowedRanges(); assertEquals(1, baseType3Lengths.asRanges().size()); length = baseType3Lengths.span(); @@ -309,7 +309,7 @@ public class YangParserTest { assertTrue(!patterns.isEmpty()); assertEquals(1, patterns.size()); final PatternConstraint pattern = patterns.iterator().next(); - assertEquals("^[e-z]*$", pattern.getJavaPatternString()); + assertEquals("^(?:[e-z]*)$", pattern.getJavaPatternString()); assertEquals(1, type.getLengthConstraint().get().getAllowedRanges().asRanges().size()); final LeafSchemaNode multiplePatternDirectStringDefLeaf = (LeafSchemaNode) foo @@ -325,9 +325,9 @@ public class YangParserTest { boolean isEZPattern = false; boolean isADPattern = false; for (final PatternConstraint patternConstraint : patterns) { - if (patternConstraint.getJavaPatternString().equals("^[e-z]*$")) { + if (patternConstraint.getJavaPatternString().equals("^(?:[e-z]*)$")) { isEZPattern = true; - } else if (patternConstraint.getJavaPatternString().equals("^[a-d]*$")) { + } else if (patternConstraint.getJavaPatternString().equals("^(?:[a-d]*)$")) { isADPattern = true; } } @@ -369,7 +369,7 @@ public class YangParserTest { final List patterns = baseType2.getPatternConstraints(); assertEquals(1, patterns.size()); final PatternConstraint pattern = patterns.iterator().next(); - assertEquals("^[a-k]*$", pattern.getJavaPatternString()); + assertEquals("^(?:[a-k]*)$", pattern.getJavaPatternString()); final RangeSet baseType3Lengths = baseType2.getLengthConstraint().get().getAllowedRanges(); assertEquals(1, baseType3Lengths.asRanges().size()); length = baseType3Lengths.span(); -- 2.36.6