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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import java.util.function.Predicate;
16 import java.util.regex.Pattern;
17 import java.util.regex.PatternSyntaxException;
18 import org.junit.Test;
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() {
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() {
121 String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
122 "(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+");
123 assertEquals("^(?:(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+)$", fixedUnicodeScriptPattern);
124 // should throw exception
125 Pattern.compile(fixedUnicodeScriptPattern);
128 @Test(expected = PatternSyntaxException.class)
129 public void testInvalidPattern3() {
130 String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
131 "(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+");
132 assertEquals("^(?:(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+)$", fixedUnicodeScriptPattern);
133 // should throw exception
134 Pattern.compile(fixedUnicodeScriptPattern);
138 public void testCorrectBranches() {
139 String str = RegexUtils.getJavaRegexFromXSD("a|bb");
140 assertEquals("^(?:a|bb)$", str);
141 Predicate<String> pred = Pattern.compile(str).asPredicate();
143 assertTrue(pred.test("a"));
144 assertTrue(pred.test("bb"));
145 assertFalse(pred.test("ab"));
146 assertFalse(pred.test("abb"));
147 assertFalse(pred.test("ac"));