Encapsulate regexes in a non-capturing group
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / Bug5410Test.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
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;
20
21 public class Bug5410Test {
22     @Test
23     public void testCaret() {
24         testPattern("^", "\\^");
25     }
26
27     @Test
28     public void testTextCaret() {
29         testPattern("abc^", "abc\\^");
30     }
31
32     @Test
33     public void testTextDollar() {
34         testPattern("abc$", "abc\\$");
35     }
36
37     @Test
38     public void testCaretCaret() {
39         testPattern("^^", "\\^\\^");
40     }
41
42     @Test
43     public void testCaretDollar() {
44         testPattern("^$", "\\^\\$");
45     }
46
47     @Test
48     public void testDot() {
49         testPattern(".", ".");
50     }
51
52     @Test
53     public void testNotColon() {
54         testPattern("[^:]+", "[^:]+");
55     }
56
57     @Test
58     public void testDollar() {
59         testPattern("$", "\\$");
60     }
61
62     @Test
63     public void testDollarOneDollar() {
64         testPattern("$1$", "\\$1\\$");
65     }
66
67     @Test
68     public void testDollarPercentRange() {
69         testPattern("[$-%]+", "[$-%]+");
70     }
71
72     @Test
73     public void testDollarRange() {
74         testPattern("[$$]+", "[$$]+");
75     }
76
77     @Test
78     public void testDollarCaretRange() {
79         testPattern("[$^]+", "[$^]+");
80     }
81
82     @Test
83     public void testSimple() {
84         testPattern("abc", "abc");
85     }
86
87     @Test
88     public void testDotPlus() {
89         testPattern(".+", ".+");
90     }
91
92     @Test
93     public void testDotStar() {
94         testPattern(".*", ".*");
95     }
96
97     @Test
98     public void testSimpleOptional() {
99         testPattern("a?", "a?");
100     }
101
102     @Test
103     public void testRangeOptional() {
104         testPattern("[a-z]?", "[a-z]?");
105     }
106
107     @Test
108     public void testInvalidXSDRegexes() throws UnsupportedEncodingException {
109         testInvalidPattern("$^a^[$^\\]", "Unclosed character class");
110         testInvalidPattern("$(\\)", "Unclosed group");
111     }
112
113     @Test
114     public void testJavaPattern() {
115         testPattern("^[$^]+$", ImmutableList.of("$^", "^", "$"), ImmutableList.of("\\", "a"));
116         testPattern("^[^$-^]$", ImmutableList.of("a", "_", "#"), ImmutableList.of("%", "^", "$", "]", "\\"));
117     }
118
119     @Test
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("#", "$-&", "'"));
129
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$", "^^$", "^_$"));
134
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("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$"));
145     }
146
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"));
153
154         RegexUtils.getJavaRegexFromXSD(xsdRegex);
155
156         final String testLog = output.toString();
157         assertTrue(testLog.contains(expectedMessage));
158         System.setOut(stdout);
159     }
160
161     private static boolean testMatch(final String javaRegex, final String value) {
162         return value.matches(javaRegex);
163     }
164
165     private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) {
166         testPattern(xsdRegex, "^(?:" + unanchoredJavaRegex + ")$", ImmutableList.of(), ImmutableList.of());
167     }
168
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));
174         }
175         for (final String value : negativeMatches) {
176             assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value));
177         }
178     }
179
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);
184
185         for (final String value : positiveMatches) {
186             assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'",
187                     testMatch(javaRegexFromXSD, value));
188         }
189         for (final String value : negativeMatches) {
190             assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'",
191                     testMatch(javaRegexFromXSD, value));
192         }
193     }
194 }