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("^^$", "^$$"), ImmutableList.of("^^", "^$", "$^", "$$"));
124 testPattern("[$-%]+", "^[$-%]+$", ImmutableList.of("$", "%", "%$"), ImmutableList.of("$-", "$-%", "-", "^"));
125 testPattern("[$-&]+", "^[$-&]+$", ImmutableList.of("$", "%&", "%$", "$%&"), ImmutableList.of("#", "$-&", "'"));
127 testPattern("[a-z&&[^m-p]]+", "^[a-z&&[^m-p]]+$", ImmutableList.of("a", "z", "az"),
128 ImmutableList.of("m", "anz", "o"));
129 testPattern("^[\\[-b&&[^^-a]]+$", "^\\^[\\[-b&&[^^-a]]+\\$$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"),
130 ImmutableList.of("^a$", "^^$", "^_$"));
132 testPattern("[^^-~&&[^$-^]]", "^[^^-~&&[^$-^]]$", ImmutableList.of("!", "\"", "#"),
133 ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}"));
134 testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^\\\\\\[\\^[^^-~&&[^$-^]]$",
135 ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"),
136 ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}"));
137 testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^$",
138 ImmutableList.of("^[^\\c]^", "^[^\\Z]^"),
139 ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^"));
140 testPattern("[\\^]$", "^[\\^]\\$$", ImmutableList.of("^$"),
141 ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$"));
144 @SuppressWarnings("checkstyle:regexpSinglelineJava")
145 private static void testInvalidPattern(final String xsdRegex, final String expectedMessage)
146 throws UnsupportedEncodingException {
147 final PrintStream stdout = System.out;
148 final ByteArrayOutputStream output = new ByteArrayOutputStream();
149 System.setOut(new PrintStream(output, true, "UTF-8"));
151 RegexUtils.getJavaRegexFromXSD(xsdRegex);
153 final String testLog = output.toString();
154 assertTrue(testLog.contains(expectedMessage));
155 System.setOut(stdout);
158 private static boolean testMatch(final String javaRegex, final String value) {
159 return value.matches(javaRegex);
162 private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) {
163 testPattern(xsdRegex, '^' + unanchoredJavaRegex + '$', ImmutableList.of(), ImmutableList.of());
166 private static void testPattern(final String javaRegex, final List<String> positiveMatches,
167 final List<String> negativeMatches) {
168 for (final String value : positiveMatches) {
169 assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'",
170 testMatch(javaRegex, value));
172 for (final String value : negativeMatches) {
173 assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value));
177 private static void testPattern(final String xsdRegex, final String expectedJavaRegex,
178 final List<String> positiveMatches, final List<String> negativeMatches) {
179 final String javaRegexFromXSD = RegexUtils.getJavaRegexFromXSD(xsdRegex);
180 assertEquals(expectedJavaRegex, javaRegexFromXSD);
182 for (final String value : positiveMatches) {
183 assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'",
184 testMatch(javaRegexFromXSD, value));
186 for (final String value : negativeMatches) {
187 assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'",
188 testMatch(javaRegexFromXSD, value));