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
8 package org.opendaylight.yangtools.yang.model.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
13 import java.util.regex.Pattern;
14 import java.util.regex.PatternSyntaxException;
15 import org.junit.Test;
17 public class Bug4079Test {
20 public void testValidPatternFix() {
21 String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsArrows})*+");
22 assertEquals("^(\\p{InArrows})*+$", fixedUnicodeScriptPattern);
23 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
25 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsDingbats})++");
26 assertEquals("^(\\p{InDingbats})++$", fixedUnicodeScriptPattern);
27 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
29 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsSpecials})?+");
30 assertEquals("^(\\p{InSpecials})?+$", fixedUnicodeScriptPattern);
31 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
33 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBatak}){4}+");
34 assertEquals("^(\\p{IsBatak}){4}+$", fixedUnicodeScriptPattern);
35 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
37 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}){4,6}+");
38 assertEquals("^(\\p{IsLatin}){4,6}+$", fixedUnicodeScriptPattern);
39 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
41 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsTibetan}){4,}+");
42 assertEquals("^(\\p{IsTibetan}){4,}+$", fixedUnicodeScriptPattern);
43 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
45 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsAlphabetic}){4}?");
46 assertEquals("^(\\p{IsAlphabetic}){4}?$", fixedUnicodeScriptPattern);
47 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
49 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLowercase}){4,6}?");
50 assertEquals("^(\\p{IsLowercase}){4,6}?$", fixedUnicodeScriptPattern);
51 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
53 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsUppercase}){4,}?");
54 assertEquals("^(\\p{IsUppercase}){4,}?$", fixedUnicodeScriptPattern);
55 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
57 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
58 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*$", fixedUnicodeScriptPattern);
59 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
61 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
62 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+$", fixedUnicodeScriptPattern);
63 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
65 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
66 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?$", fixedUnicodeScriptPattern);
67 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
69 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}");
70 assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4}$", fixedUnicodeScriptPattern);
71 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
73 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsArmenian}){2,4}");
74 assertEquals("^(\\p{IsLatin}|\\p{IsArmenian}){2,4}$", fixedUnicodeScriptPattern);
75 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
77 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
78 assertEquals("^(\\p{IsLatin}|\\p{InBasicLatin}){2,}$", fixedUnicodeScriptPattern);
79 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
81 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin})*?");
82 assertEquals("^(\\p{InBasicLatin}|\\p{IsLatin})*?$", fixedUnicodeScriptPattern);
83 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
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));
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));
95 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\p{IsBasicLatin})*+");
96 assertEquals("^(\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern);
97 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
99 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\p{IsBasicLatin})*+");
100 assertEquals("^(\\\\\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern);
101 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
103 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\\\\\p{IsBasicLatin})*+");
104 assertEquals("^(\\\\\\\\\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern);
105 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
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);
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);
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);