2 * Copyright (c) 2016 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.stmt;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
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.model.util.RegexUtils;
20 public class Bug4079Test {
23 public void testValidPatternFix() {
24 String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsArrows})*+");
25 assertEquals("^(\\p{InArrows})*+$", fixedUnicodeScriptPattern);
26 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
28 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsDingbats})++");
29 assertEquals("^(\\p{InDingbats})++$", fixedUnicodeScriptPattern);
30 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
32 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsSpecials})?+");
33 assertEquals("^(\\p{InSpecials})?+$", fixedUnicodeScriptPattern);
34 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
36 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBatak}){4}+");
37 assertEquals("^(\\p{IsBatak}){4}+$", fixedUnicodeScriptPattern);
38 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
40 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}){4,6}+");
41 assertEquals("^(\\p{IsLatin}){4,6}+$", fixedUnicodeScriptPattern);
42 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
44 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsTibetan}){4,}+");
45 assertEquals("^(\\p{IsTibetan}){4,}+$", fixedUnicodeScriptPattern);
46 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
48 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsAlphabetic}){4}?");
49 assertEquals("^(\\p{IsAlphabetic}){4}?$", fixedUnicodeScriptPattern);
50 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
52 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLowercase}){4,6}?");
53 assertEquals("^(\\p{IsLowercase}){4,6}?$", fixedUnicodeScriptPattern);
54 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
56 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsUppercase}){4,}?");
57 assertEquals("^(\\p{IsUppercase}){4,}?$", fixedUnicodeScriptPattern);
58 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
60 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
61 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*$", fixedUnicodeScriptPattern);
62 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
64 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
65 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+$", fixedUnicodeScriptPattern);
66 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
68 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
69 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?$", fixedUnicodeScriptPattern);
70 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
72 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}");
73 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4}$", fixedUnicodeScriptPattern);
74 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
76 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsArmenian}){2,4}");
77 assertEquals("^(\\p{IsLatin}|\\p{IsArmenian}){2,4}$", fixedUnicodeScriptPattern);
78 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
80 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
81 assertEquals("^(\\p{IsLatin}|\\p{InBasicLatin}){2,}$", fixedUnicodeScriptPattern);
82 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
84 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin})*?");
85 assertEquals("^(\\p{InBasicLatin}|\\p{IsLatin})*?$", fixedUnicodeScriptPattern);
86 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
88 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
89 "(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsArrows})+?");
90 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{InArrows})+?$", fixedUnicodeScriptPattern);
91 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
93 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
94 "(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsLatin})??");
95 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??$", fixedUnicodeScriptPattern);
96 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
98 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\p{IsBasicLatin})*+");
99 assertEquals("^(\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern);
100 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
102 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\p{IsBasicLatin})*+");
103 assertEquals("^(\\\\\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern);
104 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
106 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\\\\\p{IsBasicLatin})*+");
107 assertEquals("^(\\\\\\\\\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern);
108 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
111 @Test(expected = PatternSyntaxException.class)
112 public void testInvalidPattern() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
113 String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\p{IsBasicLatin})*+");
114 assertEquals("^(\\\\p{IsBasicLatin})*+$", fixedUnicodeScriptPattern);
115 // should throw exception
116 Pattern.compile(fixedUnicodeScriptPattern);
119 @Test(expected = PatternSyntaxException.class)
120 public void testInvalidPattern2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
121 String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+");
122 assertEquals("^(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+$", fixedUnicodeScriptPattern);
123 // should throw exception
124 Pattern.compile(fixedUnicodeScriptPattern);
127 @Test(expected = PatternSyntaxException.class)
128 public void testInvalidPattern3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
129 String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+");
130 assertEquals("^(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+$", fixedUnicodeScriptPattern);
131 // should throw exception
132 Pattern.compile(fixedUnicodeScriptPattern);