Bug 4079: Unable to compile pattern defined in module
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / retest / Bug4079Test.java
1 /*
2  * Copyright (c) 2016 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.stmt.retest;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.regex.Pattern;
16 import java.util.regex.PatternSyntaxException;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
19
20 public class Bug4079Test {
21
22     @Test
23     public void testValidPatternFix() {
24         String fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsArrows})*+");
25         assertEquals("(\\p{InArrows})*+", fixedUnicodeScriptPattern);
26         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
27
28         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsDingbats})++");
29         assertEquals("(\\p{InDingbats})++", fixedUnicodeScriptPattern);
30         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
31
32         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsSpecials})?+");
33         assertEquals("(\\p{InSpecials})?+", fixedUnicodeScriptPattern);
34         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
35
36         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsBatak}){4}+");
37         assertEquals("(\\p{IsBatak}){4}+", fixedUnicodeScriptPattern);
38         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
39
40         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsLatin}){4,6}+");
41         assertEquals("(\\p{IsLatin}){4,6}+", fixedUnicodeScriptPattern);
42         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
43
44         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsTibetan}){4,}+");
45         assertEquals("(\\p{IsTibetan}){4,}+", fixedUnicodeScriptPattern);
46         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
47
48         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsAlphabetic}){4}?");
49         assertEquals("(\\p{IsAlphabetic}){4}?", fixedUnicodeScriptPattern);
50         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
51
52         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsLowercase}){4,6}?");
53         assertEquals("(\\p{IsLowercase}){4,6}?", fixedUnicodeScriptPattern);
54         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
55
56         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsUppercase}){4,}?");
57         assertEquals("(\\p{IsUppercase}){4,}?", fixedUnicodeScriptPattern);
58         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
59
60         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
61         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*", fixedUnicodeScriptPattern);
62         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
63
64         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
65         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+", fixedUnicodeScriptPattern);
66         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
67
68         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
69         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?", fixedUnicodeScriptPattern);
70         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
71
72         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}");
73         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4}", fixedUnicodeScriptPattern);
74         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
75
76         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsLatin}|\\p{IsArmenian}){2,4}");
77         assertEquals("(\\p{IsLatin}|\\p{IsArmenian}){2,4}", fixedUnicodeScriptPattern);
78         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
79
80         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
81         assertEquals("(\\p{IsLatin}|\\p{InBasicLatin}){2,}", fixedUnicodeScriptPattern);
82         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
83
84         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsBasicLatin}|\\p{IsLatin})*?");
85         assertEquals("(\\p{InBasicLatin}|\\p{IsLatin})*?", fixedUnicodeScriptPattern);
86         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
87
88         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern(
89                 "(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsArrows})+?");
90         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{InArrows})+?", fixedUnicodeScriptPattern);
91         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
92
93         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern(
94                 "(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsLatin})??");
95         assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??", fixedUnicodeScriptPattern);
96         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
97
98         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\\\\\p{IsBasicLatin})*+");
99         assertEquals("(\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
100         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
101
102         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\\\\\\\\\p{IsBasicLatin})*+");
103         assertEquals("(\\\\\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
104         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
105
106         fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\\\\\\\\\\\\\p{IsBasicLatin})*+");
107         assertEquals("(\\\\\\\\\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
108         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
109     }
110
111     @Test(expected = PatternSyntaxException.class)
112     public void testInvalidPattern() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
113         String fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\\\p{IsBasicLatin})*+");
114         assertEquals("(\\\\p{IsBasicLatin})*+", fixedUnicodeScriptPattern);
115         // should throw exception
116         Pattern.compile(fixedUnicodeScriptPattern);
117     }
118
119     @Test(expected = PatternSyntaxException.class)
120     public void testInvalidPattern2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
121         String fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+");
122         assertEquals("(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+", fixedUnicodeScriptPattern);
123         // should throw exception
124         Pattern.compile(fixedUnicodeScriptPattern);
125     }
126
127     @Test(expected = PatternSyntaxException.class)
128     public void testInvalidPattern3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
129         String fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+");
130         assertEquals("(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+", fixedUnicodeScriptPattern);
131         // should throw exception
132         Pattern.compile(fixedUnicodeScriptPattern);
133     }
134 }