Move Bug4079/Bug5410 tests from yang-parser to yang-model-util
[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("^^$", "^$$"), ImmutableList.of("^^", "^$", "$^", "$$"));
124         testPattern("[$-%]+", "^[$-%]+$", ImmutableList.of("$", "%", "%$"), ImmutableList.of("$-", "$-%", "-", "^"));
125         testPattern("[$-&]+", "^[$-&]+$", ImmutableList.of("$", "%&", "%$", "$%&"), ImmutableList.of("#", "$-&", "'"));
126
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$", "^^$", "^_$"));
131
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("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$"));
142     }
143
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"));
150
151         RegexUtils.getJavaRegexFromXSD(xsdRegex);
152
153         final String testLog = output.toString();
154         assertTrue(testLog.contains(expectedMessage));
155         System.setOut(stdout);
156     }
157
158     private static boolean testMatch(final String javaRegex, final String value) {
159         return value.matches(javaRegex);
160     }
161
162     private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) {
163         testPattern(xsdRegex, '^' + unanchoredJavaRegex + '$', ImmutableList.of(), ImmutableList.of());
164     }
165
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));
171         }
172         for (final String value : negativeMatches) {
173             assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value));
174         }
175     }
176
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);
181
182         for (final String value : positiveMatches) {
183             assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'",
184                     testMatch(javaRegexFromXSD, value));
185         }
186         for (final String value : negativeMatches) {
187             assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'",
188                     testMatch(javaRegexFromXSD, value));
189         }
190     }
191 }