2 * Copyright (c) 2017 Cisco Systems, Inc. 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.parser.stmt.rfc6020;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.collect.ImmutableList;
16 import java.io.ByteArrayOutputStream;
17 import java.io.PrintStream;
18 import java.io.UnsupportedEncodingException;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
27 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
28 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
30 public class Bug5410Test {
31 private static final String FOO_NS = "foo";
32 private static final String FOO_REV = "1970-01-01";
35 public void testJavaRegexFromXSD() {
36 testPattern("^[^:]+$", "^\\^[^:]+\\$$", ImmutableList.of("^a$", "^abc$"),
37 ImmutableList.of("abc$", "^abc", "^a:bc$"));
38 testPattern("^[$^]$", "^\\^[$^]\\$$", ImmutableList.of("^^$", "^$$"), ImmutableList.of("^^", "^$", "$^", "$$"));
39 testPattern("[$-%]+", "^[$-%]+$", ImmutableList.of("$", "%", "%$"), ImmutableList.of("$-", "$-%", "-", "^"));
40 testPattern("[$-&]+", "^[$-&]+$", ImmutableList.of("$", "%&", "%$", "$%&"), ImmutableList.of("#", "$-&", "'"));
42 testPattern("[a-z&&[^m-p]]+", "^[a-z&&[^m-p]]+$", ImmutableList.of("a", "z", "az"),
43 ImmutableList.of("m", "anz", "o"));
44 testPattern("^[\\[-b&&[^^-a]]+$", "^\\^[\\[-b&&[^^-a]]+\\$$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"),
45 ImmutableList.of("^a$", "^^$", "^_$"));
47 testPattern("[^^-~&&[^$-^]]", "^[^^-~&&[^$-^]]$", ImmutableList.of("!", "\"", "#"),
48 ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}"));
49 testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^\\\\\\[\\^[^^-~&&[^$-^]]$",
50 ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"),
51 ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}"));
52 testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^$",
53 ImmutableList.of("^[^\\c]^", "^[^\\Z]^"),
54 ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^"));
55 testPattern("[\\^]$", "^[\\^]\\$$", ImmutableList.of("^$"),
56 ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$"));
60 public void testInvalidXSDRegexes() throws UnsupportedEncodingException {
61 testInvalidPattern("$^a^[$^\\]", "Unclosed character class");
62 testInvalidPattern("$(\\)", "Unclosed group");
66 public void testJavaPattern() {
67 testPattern("^[$^]+$", ImmutableList.of("$^", "^", "$"), ImmutableList.of("\\", "a"));
68 testPattern("^[^$-^]$", ImmutableList.of("a", "_", "#"), ImmutableList.of("%", "^", "$", "]", "\\"));
72 public void testYangPattern() throws Exception {
73 final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5410");
74 assertNotNull(context);
76 final PatternConstraint pattern = getPatternConstraintOf(context, "leaf-with-pattern");
78 final String rawRegex = pattern.getRawRegularExpression();
79 final String expectedYangRegex = "$0$.*|$1$[a-zA-Z0-9./]{1,8}$[a-zA-Z0-9./]{22}|$5$(rounds=\\d+$)?[a-zA-Z0-9./]{1,16}$[a-zA-Z0-9./]{43}|$6$(rounds=\\d+$)?[a-zA-Z0-9./]{1,16}$[a-zA-Z0-9./]{86}";
80 assertEquals(expectedYangRegex, rawRegex);
82 final String javaRegexFromYang = pattern.getRegularExpression();
83 final String expectedJavaRegex = "^\\$0\\$.*|\\$1\\$[a-zA-Z0-9./]{1,8}\\$[a-zA-Z0-9./]{22}|\\$5\\$(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{43}|\\$6\\$(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{86}$";
84 assertEquals(expectedJavaRegex, javaRegexFromYang);
86 final String value = "$6$AnrKGc0V$B/0/A.pWg4HrrA6YiEJOtFGibQ9Fmm5.4rI/00gEz3QeB7joSxBU3YtbHDm6NSkS1dKTQy3BWhwKKDS8nB5S//";
87 testPattern(javaRegexFromYang, ImmutableList.of(value), ImmutableList.of());
91 public void testCaret() {
92 testPattern("^", "\\^");
96 public void testTextCaret() {
97 testPattern("abc^", "abc\\^");
101 public void testTextDollar() {
102 testPattern("abc$", "abc\\$");
106 public void testCaretCaret() {
107 testPattern("^^", "\\^\\^");
111 public void testCaretDollar() {
112 testPattern("^$", "\\^\\$");
116 public void testDot() {
117 testPattern(".", ".");
121 public void testNotColon() {
122 testPattern("[^:]+", "[^:]+");
126 public void testDollar() {
127 testPattern("$", "\\$");
131 public void testDollarOneDollar() {
132 testPattern("$1$", "\\$1\\$");
136 public void testDollarPercentRange() {
137 testPattern("[$-%]+", "[$-%]+");
141 public void testDollarRange() {
142 testPattern("[$$]+", "[$$]+");
146 public void testDollarCaretRange() {
147 testPattern("[$^]+", "[$^]+");
151 public void testSimple() {
152 testPattern("abc", "abc");
156 public void testDotPlus() {
157 testPattern(".+", ".+");
161 public void testDotStar() {
162 testPattern(".*", ".*");
166 public void testSimpleOptional() {
167 testPattern("a?", "a?");
171 public void testRangeOptional() {
172 testPattern("[a-z]?", "[a-z]?");
175 private static void testPattern(final String xsdRegex, final String expectedJavaRegex,
176 final List<String> positiveMatches, final List<String> negativeMatches) {
177 final String javaRegexFromXSD = javaRegexFromXSD(xsdRegex);
178 assertEquals(expectedJavaRegex, javaRegexFromXSD);
180 for (final String value : positiveMatches) {
181 assertTrue("Value '" + value + "' does not match java regex '" + javaRegexFromXSD + "'",
182 testMatch(javaRegexFromXSD, value));
184 for (final String value : negativeMatches) {
185 assertFalse("Value '" + value + "' matches java regex '" + javaRegexFromXSD + "'",
186 testMatch(javaRegexFromXSD, value));
190 private static void testPattern(final String javaRegex, final List<String> positiveMatches,
191 final List<String> negativeMatches) {
192 for (final String value : positiveMatches) {
193 assertTrue("Value '" + value + "' does not match java regex '" + javaRegex + "'",
194 testMatch(javaRegex, value));
196 for (final String value : negativeMatches) {
197 assertFalse("Value '" + value + "' matches java regex '" + javaRegex + "'", testMatch(javaRegex, value));
201 private static String javaRegexFromXSD(final String xsdRegex) {
202 return PatternStatementImpl.Definition.getJavaRegexFromXSD(xsdRegex);
205 private static boolean testMatch(final String javaRegex, final String value) {
206 return value.matches(javaRegex);
209 private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) {
210 testPattern(xsdRegex, '^' + unanchoredJavaRegex + '$', ImmutableList.of(), ImmutableList.of());
213 private static PatternConstraint getPatternConstraintOf(final SchemaContext context, final String leafName) {
214 final DataSchemaNode dataChildByName = context.getDataChildByName(foo(leafName));
215 assertTrue(dataChildByName instanceof LeafSchemaNode);
216 final LeafSchemaNode leaf = (LeafSchemaNode) dataChildByName;
217 final TypeDefinition<? extends TypeDefinition<?>> type = leaf.getType();
218 assertTrue(type instanceof StringTypeDefinition);
219 final StringTypeDefinition strType = (StringTypeDefinition) type;
220 return strType.getPatternConstraints().iterator().next();
223 private static QName foo(final String localName) {
224 return QName.create(FOO_NS, FOO_REV, localName);
227 private static void testInvalidPattern(final String xsdRegex, final String expectedMessage) throws UnsupportedEncodingException {
228 final PrintStream stdout = System.out;
229 final ByteArrayOutputStream output = new ByteArrayOutputStream();
230 System.setOut(new PrintStream(output, true, "UTF-8"));
232 javaRegexFromXSD(xsdRegex);
234 final String testLog = output.toString();
235 assertTrue(testLog.contains(expectedMessage));
236 System.setOut(stdout);