Bug 4079: Unable to compile pattern defined in module
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / impl / Bug4079Test.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.yangtools.yang.parser.impl;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.io.IOException;
15 import java.lang.reflect.InvocationTargetException;
16 import java.lang.reflect.Method;
17 import java.net.URISyntaxException;
18 import java.util.Set;
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;
23
24 public class Bug4079Test {
25
26     private Set<Module> modules;
27
28     @Test
29     public void testModuleCompilation() throws URISyntaxException, IOException {
30         modules = TestUtils.loadModules(getClass().getResource("/bugs/bug4079").toURI());
31         assertNotNull(modules);
32     }
33
34     @Test
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());
39
40         method.setAccessible(true);
41
42         String fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsArrows})*+");
43         assertEquals("(\\p{InArrows})*+", fixedUnicodeScriptPattern);
44         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
45
46         fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsDingbats})++");
47         assertEquals("(\\p{InDingbats})++", fixedUnicodeScriptPattern);
48         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
49
50         fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsSpecials})?+");
51         assertEquals("(\\p{InSpecials})?+", fixedUnicodeScriptPattern);
52         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
53
54         fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsBatak}){4}+");
55         assertEquals("(\\p{IsBatak}){4}+", fixedUnicodeScriptPattern);
56         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
57
58         fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsLatin}){4,6}+");
59         assertEquals("(\\p{IsLatin}){4,6}+", fixedUnicodeScriptPattern);
60         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
61
62         fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsTibetan}){4,}+");
63         assertEquals("(\\p{IsTibetan}){4,}+", fixedUnicodeScriptPattern);
64         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
65
66         fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsAlphabetic}){4}?");
67         assertEquals("(\\p{IsAlphabetic}){4}?", fixedUnicodeScriptPattern);
68         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
69
70         fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsLowercase}){4,6}?");
71         assertEquals("(\\p{IsLowercase}){4,6}?", fixedUnicodeScriptPattern);
72         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
73
74         fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsUppercase}){4,}?");
75         assertEquals("(\\p{IsUppercase}){4,}?", fixedUnicodeScriptPattern);
76         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
77
78         fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
79         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*", fixedUnicodeScriptPattern);
80         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
81
82         fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
83         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+", fixedUnicodeScriptPattern);
84         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
85
86         fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
87         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?", fixedUnicodeScriptPattern);
88         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
89
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));
93
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));
97
98         fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
99         assertEquals("(\\p{IsLatin}|\\p{InBasicLatin}){2,}", fixedUnicodeScriptPattern);
100         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
101
102         fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{IsBasicLatin}|\\p{IsLatin})*?");
103         assertEquals("(\\p{InBasicLatin}|\\p{IsLatin})*?", fixedUnicodeScriptPattern);
104         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
105
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));
110
111         fixedUnicodeScriptPattern = (String) method.invoke(null, "(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}|" +
112                 "\\p{IsLatin})??");
113         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??", fixedUnicodeScriptPattern);
114         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
115
116         fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\\\\\p{IsBasicLatin})*+");
117         assertEquals("(\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
118         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
119
120         fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\\\\\\\\\p{IsBasicLatin})*+");
121         assertEquals("(\\\\\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
122         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
123
124         fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\\\\\\\\\\\\\p{IsBasicLatin})*+");
125         assertEquals("(\\\\\\\\\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
126         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
127     }
128
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());
134
135         method.setAccessible(true);
136
137         String fixedUnicodeScriptPattern = (String) method.invoke(null,"(\\\\p{IsBasicLatin})*+");
138         assertEquals("(\\\\p{IsBasicLatin})*+", fixedUnicodeScriptPattern);
139         // should throw exception
140         Pattern.compile(fixedUnicodeScriptPattern);
141     }
142
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());
148
149         method.setAccessible(true);
150
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);
155     }
156
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());
162
163         method.setAccessible(true);
164
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);
169     }
170 }