2 * Copyright (c) 2015 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
9 package org.opendaylight.yangtools.yang.parser.impl;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
14 import java.io.IOException;
15 import java.lang.reflect.InvocationTargetException;
16 import java.lang.reflect.Method;
17 import java.net.URISyntaxException;
19 import java.util.regex.Pattern;
20 import java.util.regex.PatternSyntaxException;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.model.api.Module;
24 public class Bug4079Test {
26 private Set<Module> modules;
29 public void testModuleCompilation() throws URISyntaxException, IOException {
30 modules = TestUtils.loadModules(getClass().getResource("/bugs/bug4079").toURI());
31 assertNotNull(modules);
35 public void testValidPatternFix() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
36 Method method = ParserListenerUtils.class.getDeclaredMethod("fixUnicodeScriptPattern", String.class);
37 assertNotNull(method);
38 assertEquals("fixUnicodeScriptPattern", method.getName());
40 method.setAccessible(true);
42 String fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsArrows})*+");
43 assertEquals("(\\p{InArrows})*+", fixedUnicodeScriptPattern);
44 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
46 fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsDingbats})++");
47 assertEquals("(\\p{InDingbats})++", fixedUnicodeScriptPattern);
48 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
50 fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsSpecials})?+");
51 assertEquals("(\\p{InSpecials})?+", fixedUnicodeScriptPattern);
52 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
54 fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsBatak}){4}+");
55 assertEquals("(\\p{IsBatak}){4}+", fixedUnicodeScriptPattern);
56 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
58 fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsLatin}){4,6}+");
59 assertEquals("(\\p{IsLatin}){4,6}+", fixedUnicodeScriptPattern);
60 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
62 fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsTibetan}){4,}+");
63 assertEquals("(\\p{IsTibetan}){4,}+", fixedUnicodeScriptPattern);
64 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
66 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsAlphabetic}){4}?");
67 assertEquals("(\\p{IsAlphabetic}){4}?", fixedUnicodeScriptPattern);
68 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
70 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsLowercase}){4,6}?");
71 assertEquals("(\\p{IsLowercase}){4,6}?", fixedUnicodeScriptPattern);
72 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
74 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsUppercase}){4,}?");
75 assertEquals("(\\p{IsUppercase}){4,}?", fixedUnicodeScriptPattern);
76 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
78 fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
79 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*", fixedUnicodeScriptPattern);
80 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
82 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
83 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+", fixedUnicodeScriptPattern);
84 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
86 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
87 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?", fixedUnicodeScriptPattern);
88 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
90 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}");
91 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4}", fixedUnicodeScriptPattern);
92 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
94 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsLatin}|\\p{IsArmenian}){2,4}");
95 assertEquals("(\\p{IsLatin}|\\p{IsArmenian}){2,4}", fixedUnicodeScriptPattern);
96 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
98 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
99 assertEquals("(\\p{IsLatin}|\\p{InBasicLatin}){2,}", fixedUnicodeScriptPattern);
100 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
102 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsBasicLatin}|\\p{IsLatin})*?");
103 assertEquals("(\\p{InBasicLatin}|\\p{IsLatin})*?", fixedUnicodeScriptPattern);
104 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
106 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement}" +
107 "|\\p{IsArrows})+?");
108 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{InArrows})+?", fixedUnicodeScriptPattern);
109 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
111 fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}|" +
113 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??", fixedUnicodeScriptPattern);
114 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
116 fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\\\\\p{IsBasicLatin})*+");
117 assertEquals("(\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
118 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
120 fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\\\\\\\\\p{IsBasicLatin})*+");
121 assertEquals("(\\\\\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
122 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
124 fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\\\\\\\\\\\\\p{IsBasicLatin})*+");
125 assertEquals("(\\\\\\\\\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
126 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
129 @Test(expected = PatternSyntaxException.class)
130 public void testInvalidPattern() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
131 Method method = ParserListenerUtils.class.getDeclaredMethod("fixUnicodeScriptPattern", String.class);
132 assertNotNull(method);
133 assertEquals("fixUnicodeScriptPattern", method.getName());
135 method.setAccessible(true);
137 String fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\\\p{IsBasicLatin})*+");
138 assertEquals("(\\\\p{IsBasicLatin})*+", fixedUnicodeScriptPattern);
139 // should throw exception
140 Pattern.compile(fixedUnicodeScriptPattern);
143 @Test(expected = PatternSyntaxException.class)
144 public void testInvalidPattern2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
145 Method method = ParserListenerUtils.class.getDeclaredMethod("fixUnicodeScriptPattern", String.class);
146 assertNotNull(method);
147 assertEquals("fixUnicodeScriptPattern", method.getName());
149 method.setAccessible(true);
151 String fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+");
152 assertEquals("(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+", fixedUnicodeScriptPattern);
153 // should throw exception
154 Pattern.compile(fixedUnicodeScriptPattern);
157 @Test(expected = PatternSyntaxException.class)
158 public void testInvalidPattern3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
159 Method method = ParserListenerUtils.class.getDeclaredMethod("fixUnicodeScriptPattern", String.class);
160 assertNotNull(method);
161 assertEquals("fixUnicodeScriptPattern", method.getName());
163 method.setAccessible(true);
165 String fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+");
166 assertEquals("(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+", fixedUnicodeScriptPattern);
167 // should throw exception
168 Pattern.compile(fixedUnicodeScriptPattern);