2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.model.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.collect.ImmutableList;
15 import java.io.ByteArrayOutputStream;
16 import java.io.PrintStream;
17 import java.io.UnsupportedEncodingException;
18 import java.util.List;
19 import org.junit.Test;
21 public class Bug5410Test {
23 public void testCaret() {
24 testPattern("^", "\\^");
28 public void testTextCaret() {
29 testPattern("abc^", "abc\\^");
33 public void testTextDollar() {
34 testPattern("abc$", "abc\\$");
38 public void testCaretCaret() {
39 testPattern("^^", "\\^\\^");
43 public void testCaretDollar() {
44 testPattern("^$", "\\^\\$");
48 public void testDot() {
49 testPattern(".", ".");
53 public void testNotColon() {
54 testPattern("[^:]+", "[^:]+");
58 public void testDollar() {
59 testPattern("$", "\\$");
63 public void testDollarOneDollar() {
64 testPattern("$1$", "\\$1\\$");
68 public void testDollarPercentRange() {
69 testPattern("[$-%]+", "[$-%]+");
73 public void testDollarRange() {
74 testPattern("[$$]+", "[$$]+");
78 public void testDollarCaretRange() {
79 testPattern("[$^]+", "[$^]+");
83 public void testSimple() {
84 testPattern("abc", "abc");
88 public void testDotPlus() {
89 testPattern(".+", ".+");
93 public void testDotStar() {
94 testPattern(".*", ".*");
98 public void testSimpleOptional() {
99 testPattern("a?", "a?");
103 public void testRangeOptional() {
104 testPattern("[a-z]?", "[a-z]?");
108 public void testInvalidXSDRegexes() throws UnsupportedEncodingException {
109 testInvalidPattern("$^a^[$^\\]", "Unclosed character class");
110 testInvalidPattern("$(\\)", "Unclosed group");
114 public void testJavaPattern() {
115 testPattern("^[$^]+$", ImmutableList.of("$^", "^", "$"), ImmutableList.of("\\", "a"));
116 testPattern("^[^$-^]$", ImmutableList.of("a", "_", "#"), ImmutableList.of("%", "^", "$", "]", "\\"));
120 public void testJavaRegexFromXSD() {
121 testPattern("^[^:]+$", "^(?:\\^[^:]+\\$)$", ImmutableList.of("^a$", "^abc$"),
122 ImmutableList.of("abc$", "^abc", "^a:bc$"));
123 testPattern("^[$^]$", "^(?:\\^[$^]\\$)$", ImmutableList.of("^^$", "^$$"),
124 ImmutableList.of("^^", "^$", "$^", "$$"));
125 testPattern("[$-%]+", "^(?:[$-%]+)$", ImmutableList.of("$", "%", "%$"),
126 ImmutableList.of("$-", "$-%", "-", "^"));
127 testPattern("[$-&]+", "^(?:[$-&]+)$", ImmutableList.of("$", "%&", "%$", "$%&"),
128 ImmutableList.of("#", "$-&", "'"));
130 testPattern("[a-z&&[^m-p]]+", "^(?:[a-z&&[^m-p]]+)$", ImmutableList.of("a", "z", "az"),
131 ImmutableList.of("m", "anz", "o"));
132 testPattern("^[\\[-b&&[^^-a]]+$", "^(?:\\^[\\[-b&&[^^-a]]+\\$)$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"),
133 ImmutableList.of("^a$", "^^$", "^_$"));
135 testPattern("[^^-~&&[^$-^]]", "^(?:[^^-~&&[^$-^]])$", ImmutableList.of("!", "\"", "#"),
136 ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}"));
137 testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^(?:\\\\\\[\\^[^^-~&&[^$-^]])$",
138 ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"),
139 ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}"));
140 testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^(?:\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^)$",
141 ImmutableList.of("^[^\\c]^", "^[^\\Z]^"),
142 ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^"));
143 testPattern("[\\^]$", "^(?:[\\^]\\$)$", ImmutableList.of("^$"),
144 ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$"));
147 @SuppressWarnings("checkstyle:regexpSinglelineJava")
148 private static void testInvalidPattern(final String xsdRegex, final String expectedMessage)
149 throws UnsupportedEncodingException {
150 final PrintStream stdout = System.out;
151 final ByteArrayOutputStream output = new ByteArrayOutputStream();
152 System.setOut(new PrintStream(output, true, "UTF-8"));
154 RegexUtils.getJavaRegexFromXSD(xsdRegex);
156 final String testLog = output.toString();
157 assertTrue(testLog.contains(expectedMessage));
158 System.setOut(stdout);
161 private static boolean testMatch(final String javaRegex, final String value) {
162 return value.matches(javaRegex);
165 private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) {
166 testPattern(xsdRegex, "^(?:" + unanchoredJavaRegex + ")$", ImmutableList.of(), ImmutableList.of());
169 private static void testPattern(final String javaRegex, final List<String> positiveMatches,
170 final List<String> negativeMatches) {
171 for (final String value : positiveMatches) {
172 assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'",
173 testMatch(javaRegex, value));
175 for (final String value : negativeMatches) {
176 assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value));
180 private static void testPattern(final String xsdRegex, final String expectedJavaRegex,
181 final List<String> positiveMatches, final List<String> negativeMatches) {
182 final String javaRegexFromXSD = RegexUtils.getJavaRegexFromXSD(xsdRegex);
183 assertEquals(expectedJavaRegex, javaRegexFromXSD);
185 for (final String value : positiveMatches) {
186 assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'",
187 testMatch(javaRegexFromXSD, value));
189 for (final String value : negativeMatches) {
190 assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'",
191 testMatch(javaRegexFromXSD, value));