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.parser.stmt.rfc6020.Utils;
20 public class Bug4079Test {
23 public void testValidPatternFix() {
24 String fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsArrows})*+");
25 assertEquals("(\\p{InArrows})*+", fixedUnicodeScriptPattern);
26 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
28 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsDingbats})++");
29 assertEquals("(\\p{InDingbats})++", fixedUnicodeScriptPattern);
30 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
32 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsSpecials})?+");
33 assertEquals("(\\p{InSpecials})?+", fixedUnicodeScriptPattern);
34 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
36 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsBatak}){4}+");
37 assertEquals("(\\p{IsBatak}){4}+", fixedUnicodeScriptPattern);
38 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
40 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsLatin}){4,6}+");
41 assertEquals("(\\p{IsLatin}){4,6}+", fixedUnicodeScriptPattern);
42 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
44 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsTibetan}){4,}+");
45 assertEquals("(\\p{IsTibetan}){4,}+", fixedUnicodeScriptPattern);
46 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
48 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsAlphabetic}){4}?");
49 assertEquals("(\\p{IsAlphabetic}){4}?", fixedUnicodeScriptPattern);
50 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
52 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsLowercase}){4,6}?");
53 assertEquals("(\\p{IsLowercase}){4,6}?", fixedUnicodeScriptPattern);
54 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
56 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsUppercase}){4,}?");
57 assertEquals("(\\p{IsUppercase}){4,}?", fixedUnicodeScriptPattern);
58 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
60 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
61 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*", fixedUnicodeScriptPattern);
62 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
64 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
65 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+", fixedUnicodeScriptPattern);
66 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
68 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
69 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?", fixedUnicodeScriptPattern);
70 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
72 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}");
73 assertEquals("(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4}", fixedUnicodeScriptPattern);
74 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
76 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsLatin}|\\p{IsArmenian}){2,4}");
77 assertEquals("(\\p{IsLatin}|\\p{IsArmenian}){2,4}", fixedUnicodeScriptPattern);
78 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
80 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
81 assertEquals("(\\p{IsLatin}|\\p{InBasicLatin}){2,}", fixedUnicodeScriptPattern);
82 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
84 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\p{IsBasicLatin}|\\p{IsLatin})*?");
85 assertEquals("(\\p{InBasicLatin}|\\p{IsLatin})*?", fixedUnicodeScriptPattern);
86 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
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));
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));
98 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\\\\\p{IsBasicLatin})*+");
99 assertEquals("(\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
100 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
102 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\\\\\\\\\p{IsBasicLatin})*+");
103 assertEquals("(\\\\\\\\\\p{InBasicLatin})*+", fixedUnicodeScriptPattern);
104 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
106 fixedUnicodeScriptPattern = Utils.fixUnicodeScriptPattern("(\\\\\\\\\\\\\\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 = Utils.fixUnicodeScriptPattern("(\\\\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 = Utils.fixUnicodeScriptPattern("(\\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 = Utils.fixUnicodeScriptPattern("(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+");
130 assertEquals("(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+", fixedUnicodeScriptPattern);
131 // should throw exception
132 Pattern.compile(fixedUnicodeScriptPattern);