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 // FIXME: YANGTOOLS-887: these patterns are not translated correctly, "&&" is a different construct in XSD
136 // testPattern("[^^-~&&[^$-^]]", "^(?:[^^-~&&[^$-^]])$", ImmutableList.of("!", "\"", "#"),
137 // ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}"));
138 // testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^(?:\\\\\\[\\^[^^-~&&[^$-^]])$",
139 // ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"),
140 // ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}"));
141 // testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^(?:\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^)$",
142 // ImmutableList.of("^[^\\c]^", "^[^\\Z]^"),
143 // ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^"));
144 // testPattern("[\\^]$", "^(?:[\\^]\\$)$", ImmutableList.of("^$"),
145 // ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$"));
148 @SuppressWarnings("checkstyle:regexpSinglelineJava")
149 private static void testInvalidPattern(final String xsdRegex, final String expectedMessage)
150 throws UnsupportedEncodingException {
151 final PrintStream stdout = System.out;
152 final ByteArrayOutputStream output = new ByteArrayOutputStream();
153 System.setOut(new PrintStream(output, true, "UTF-8"));
155 RegexUtils.getJavaRegexFromXSD(xsdRegex);
157 final String testLog = output.toString();
158 assertTrue(testLog.contains(expectedMessage));
159 System.setOut(stdout);
162 private static boolean testMatch(final String javaRegex, final String value) {
163 return value.matches(javaRegex);
166 private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) {
167 testPattern(xsdRegex, "^(?:" + unanchoredJavaRegex + ")$", ImmutableList.of(), ImmutableList.of());
170 private static void testPattern(final String javaRegex, final List<String> positiveMatches,
171 final List<String> negativeMatches) {
172 for (final String value : positiveMatches) {
173 assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'",
174 testMatch(javaRegex, value));
176 for (final String value : negativeMatches) {
177 assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value));
181 private static void testPattern(final String xsdRegex, final String expectedJavaRegex,
182 final List<String> positiveMatches, final List<String> negativeMatches) {
183 final String javaRegexFromXSD = RegexUtils.getJavaRegexFromXSD(xsdRegex);
184 assertEquals(expectedJavaRegex, javaRegexFromXSD);
186 for (final String value : positiveMatches) {
187 assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'",
188 testMatch(javaRegexFromXSD, value));
190 for (final String value : negativeMatches) {
191 assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'",
192 testMatch(javaRegexFromXSD, value));