From 0c14a1efce54ef0292b39fee6aaf56d9d0ada0af Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 21 Feb 2018 10:43:03 +0100 Subject: [PATCH] Move Bug4079/Bug5410 tests from yang-parser to yang-model-util This co-locates the unit test with the class tested. Change-Id: Ib7d044208946be3347e1b1aedbd87a0cfe1e5a23 Signed-off-by: Robert Varga (cherry picked from commit cc0855ee7a788a4ef96592f055e8a22698f024b3) --- .../yang/model/util}/Bug4079Test.java | 17 +- .../yang/model/util/Bug5410Test.java | 191 ++++++++++++++++++ .../yang/parser/stmt/rfc6020/Bug5410Test.java | 185 +---------------- 3 files changed, 209 insertions(+), 184 deletions(-) rename yang/{yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt => yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util}/Bug4079Test.java (91%) create mode 100644 yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug5410Test.java diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4079Test.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4079Test.java similarity index 91% rename from yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4079Test.java rename to yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4079Test.java index 969f8d633e..5333c9882b 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4079Test.java +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4079Test.java @@ -5,17 +5,14 @@ * 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.stmt; +package org.opendaylight.yangtools.yang.model.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import java.lang.reflect.InvocationTargetException; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import org.junit.Test; -import org.opendaylight.yangtools.yang.model.util.RegexUtils; public class Bug4079Test { @@ -109,7 +106,7 @@ public class Bug4079Test { } @Test(expected = PatternSyntaxException.class) - public void testInvalidPattern() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + public void testInvalidPattern() { String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\p{IsBasicLatin})*+"); assertEquals("^(\\\\p{IsBasicLatin})*+$", fixedUnicodeScriptPattern); // should throw exception @@ -117,16 +114,18 @@ public class Bug4079Test { } @Test(expected = PatternSyntaxException.class) - public void testInvalidPattern2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+"); + public void testInvalidPattern2() { + String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD( + "(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+"); assertEquals("^(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+$", fixedUnicodeScriptPattern); // should throw exception Pattern.compile(fixedUnicodeScriptPattern); } @Test(expected = PatternSyntaxException.class) - public void testInvalidPattern3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+"); + public void testInvalidPattern3() { + String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD( + "(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+"); assertEquals("^(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+$", fixedUnicodeScriptPattern); // should throw exception Pattern.compile(fixedUnicodeScriptPattern); 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 new file mode 100644 index 0000000000..41daafa856 --- /dev/null +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug5410Test.java @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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 static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.ImmutableList; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.util.List; +import org.junit.Test; + +public class Bug5410Test { + @Test + public void testCaret() { + testPattern("^", "\\^"); + } + + @Test + public void testTextCaret() { + testPattern("abc^", "abc\\^"); + } + + @Test + public void testTextDollar() { + testPattern("abc$", "abc\\$"); + } + + @Test + public void testCaretCaret() { + testPattern("^^", "\\^\\^"); + } + + @Test + public void testCaretDollar() { + testPattern("^$", "\\^\\$"); + } + + @Test + public void testDot() { + testPattern(".", "."); + } + + @Test + public void testNotColon() { + testPattern("[^:]+", "[^:]+"); + } + + @Test + public void testDollar() { + testPattern("$", "\\$"); + } + + @Test + public void testDollarOneDollar() { + testPattern("$1$", "\\$1\\$"); + } + + @Test + public void testDollarPercentRange() { + testPattern("[$-%]+", "[$-%]+"); + } + + @Test + public void testDollarRange() { + testPattern("[$$]+", "[$$]+"); + } + + @Test + public void testDollarCaretRange() { + testPattern("[$^]+", "[$^]+"); + } + + @Test + public void testSimple() { + testPattern("abc", "abc"); + } + + @Test + public void testDotPlus() { + testPattern(".+", ".+"); + } + + @Test + public void testDotStar() { + testPattern(".*", ".*"); + } + + @Test + public void testSimpleOptional() { + testPattern("a?", "a?"); + } + + @Test + public void testRangeOptional() { + testPattern("[a-z]?", "[a-z]?"); + } + + @Test + public void testInvalidXSDRegexes() throws UnsupportedEncodingException { + testInvalidPattern("$^a^[$^\\]", "Unclosed character class"); + testInvalidPattern("$(\\)", "Unclosed group"); + } + + @Test + public void testJavaPattern() { + testPattern("^[$^]+$", ImmutableList.of("$^", "^", "$"), ImmutableList.of("\\", "a")); + testPattern("^[^$-^]$", ImmutableList.of("a", "_", "#"), ImmutableList.of("%", "^", "$", "]", "\\")); + } + + @Test + public void testJavaRegexFromXSD() { + 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"), + ImmutableList.of("m", "anz", "o")); + testPattern("^[\\[-b&&[^^-a]]+$", "^\\^[\\[-b&&[^^-a]]+\\$$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"), + ImmutableList.of("^a$", "^^$", "^_$")); + + testPattern("[^^-~&&[^$-^]]", "^[^^-~&&[^$-^]]$", ImmutableList.of("!", "\"", "#"), + ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}")); + testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^\\\\\\[\\^[^^-~&&[^$-^]]$", + ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"), + ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}")); + testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^$", + ImmutableList.of("^[^\\c]^", "^[^\\Z]^"), + ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^")); + testPattern("[\\^]$", "^[\\^]\\$$", ImmutableList.of("^$"), + ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$")); + } + + @SuppressWarnings("checkstyle:regexpSinglelineJava") + private static void testInvalidPattern(final String xsdRegex, final String expectedMessage) + throws UnsupportedEncodingException { + final PrintStream stdout = System.out; + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + System.setOut(new PrintStream(output, true, "UTF-8")); + + RegexUtils.getJavaRegexFromXSD(xsdRegex); + + final String testLog = output.toString(); + assertTrue(testLog.contains(expectedMessage)); + System.setOut(stdout); + } + + private static boolean testMatch(final String javaRegex, final String value) { + return value.matches(javaRegex); + } + + private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) { + testPattern(xsdRegex, '^' + unanchoredJavaRegex + '$', ImmutableList.of(), ImmutableList.of()); + } + + private static void testPattern(final String javaRegex, final List positiveMatches, + final List negativeMatches) { + for (final String value : positiveMatches) { + assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'", + testMatch(javaRegex, value)); + } + for (final String value : negativeMatches) { + assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value)); + } + } + + private static void testPattern(final String xsdRegex, final String expectedJavaRegex, + final List positiveMatches, final List negativeMatches) { + final String javaRegexFromXSD = RegexUtils.getJavaRegexFromXSD(xsdRegex); + assertEquals(expectedJavaRegex, javaRegexFromXSD); + + for (final String value : positiveMatches) { + assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'", + testMatch(javaRegexFromXSD, value)); + } + for (final String value : negativeMatches) { + assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'", + testMatch(javaRegexFromXSD, value)); + } + } +} 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 62654c814e..9f02e00efa 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 @@ -13,9 +13,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableList; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.io.UnsupportedEncodingException; import java.util.List; import org.junit.Test; import org.opendaylight.yangtools.yang.common.QName; @@ -25,50 +22,12 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.RegexUtils; import org.opendaylight.yangtools.yang.stmt.StmtTestUtils; public class Bug5410Test { private static final String FOO_NS = "foo"; private static final String FOO_REV = "1970-01-01"; - @Test - public void testJavaRegexFromXSD() { - 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"), - ImmutableList.of("m", "anz", "o")); - testPattern("^[\\[-b&&[^^-a]]+$", "^\\^[\\[-b&&[^^-a]]+\\$$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"), - ImmutableList.of("^a$", "^^$", "^_$")); - - testPattern("[^^-~&&[^$-^]]", "^[^^-~&&[^$-^]]$", ImmutableList.of("!", "\"", "#"), - ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}")); - testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^\\\\\\[\\^[^^-~&&[^$-^]]$", - ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"), - ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}")); - testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^$", - ImmutableList.of("^[^\\c]^", "^[^\\Z]^"), - ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^")); - testPattern("[\\^]$", "^[\\^]\\$$", ImmutableList.of("^$"), - ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$")); - } - - @Test - public void testInvalidXSDRegexes() throws UnsupportedEncodingException { - testInvalidPattern("$^a^[$^\\]", "Unclosed character class"); - testInvalidPattern("$(\\)", "Unclosed group"); - } - - @Test - public void testJavaPattern() { - testPattern("^[$^]+$", ImmutableList.of("$^", "^", "$"), ImmutableList.of("\\", "a")); - testPattern("^[^$-^]$", ImmutableList.of("a", "_", "#"), ImmutableList.of("%", "^", "$", "]", "\\")); - } - @Test public void testYangPattern() throws Exception { final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5410"); @@ -88,152 +47,28 @@ public class Bug5410Test { testPattern(javaRegexFromYang, ImmutableList.of(value), ImmutableList.of()); } - @Test - public void testCaret() { - testPattern("^", "\\^"); - } - - @Test - public void testTextCaret() { - testPattern("abc^", "abc\\^"); - } - - @Test - public void testTextDollar() { - testPattern("abc$", "abc\\$"); - } - - @Test - public void testCaretCaret() { - testPattern("^^", "\\^\\^"); - } - - @Test - public void testCaretDollar() { - testPattern("^$", "\\^\\$"); - } - - @Test - public void testDot() { - testPattern(".", "."); - } - - @Test - public void testNotColon() { - testPattern("[^:]+", "[^:]+"); - } - - @Test - public void testDollar() { - testPattern("$", "\\$"); - } - - @Test - public void testDollarOneDollar() { - testPattern("$1$", "\\$1\\$"); - } - - @Test - public void testDollarPercentRange() { - testPattern("[$-%]+", "[$-%]+"); - } - - @Test - public void testDollarRange() { - testPattern("[$$]+", "[$$]+"); - } - - @Test - public void testDollarCaretRange() { - testPattern("[$^]+", "[$^]+"); - } - - @Test - public void testSimple() { - testPattern("abc", "abc"); - } - - @Test - public void testDotPlus() { - testPattern(".+", ".+"); - } - - @Test - public void testDotStar() { - testPattern(".*", ".*"); - } - - @Test - public void testSimpleOptional() { - testPattern("a?", "a?"); - } - - @Test - public void testRangeOptional() { - testPattern("[a-z]?", "[a-z]?"); + private static PatternConstraint getPatternConstraintOf(final SchemaContext context, final String leafName) { + final DataSchemaNode dataChildByName = context.getDataChildByName(foo(leafName)); + assertTrue(dataChildByName instanceof LeafSchemaNode); + final LeafSchemaNode leaf = (LeafSchemaNode) dataChildByName; + final TypeDefinition> type = leaf.getType(); + assertTrue(type instanceof StringTypeDefinition); + final StringTypeDefinition strType = (StringTypeDefinition) type; + return strType.getPatternConstraints().iterator().next(); } - private static void testPattern(final String xsdRegex, final String expectedJavaRegex, - final List positiveMatches, final List negativeMatches) { - final String javaRegexFromXSD = javaRegexFromXSD(xsdRegex); - assertEquals(expectedJavaRegex, javaRegexFromXSD); - - for (final String value : positiveMatches) { - assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'", - testMatch(javaRegexFromXSD, value)); - } - for (final String value : negativeMatches) { - assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'", - testMatch(javaRegexFromXSD, value)); - } - } private static void testPattern(final String javaRegex, final List positiveMatches, final List negativeMatches) { for (final String value : positiveMatches) { - assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'", - testMatch(javaRegex, value)); + assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'", value.matches(javaRegex)); } for (final String value : negativeMatches) { - assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value)); + assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", value.matches(javaRegex)); } } - private static String javaRegexFromXSD(final String xsdRegex) { - return RegexUtils.getJavaRegexFromXSD(xsdRegex); - } - - private static boolean testMatch(final String javaRegex, final String value) { - return value.matches(javaRegex); - } - - private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) { - testPattern(xsdRegex, '^' + unanchoredJavaRegex + '$', ImmutableList.of(), ImmutableList.of()); - } - - private static PatternConstraint getPatternConstraintOf(final SchemaContext context, final String leafName) { - final DataSchemaNode dataChildByName = context.getDataChildByName(foo(leafName)); - assertTrue(dataChildByName instanceof LeafSchemaNode); - final LeafSchemaNode leaf = (LeafSchemaNode) dataChildByName; - final TypeDefinition> type = leaf.getType(); - assertTrue(type instanceof StringTypeDefinition); - final StringTypeDefinition strType = (StringTypeDefinition) type; - return strType.getPatternConstraints().iterator().next(); - } - private static QName foo(final String localName) { return QName.create(FOO_NS, FOO_REV, localName); } - - private static void testInvalidPattern(final String xsdRegex, final String expectedMessage) throws UnsupportedEncodingException { - final PrintStream stdout = System.out; - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - System.setOut(new PrintStream(output, true, "UTF-8")); - - javaRegexFromXSD(xsdRegex); - - final String testLog = output.toString(); - assertTrue(testLog.contains(expectedMessage)); - System.setOut(stdout); - } } -- 2.36.6