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