Bug 6180 - Parser: Backslash double-quote in double-quoted string not recognized
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / Bug6180Test.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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.util.List;
19 import java.util.Set;
20 import java.util.regex.Pattern;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
30 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.stmt.test.StmtTestUtils;
34
35 public class Bug6180Test {
36
37     @Test
38     public void stringTest() throws ReactorException, SourceException, FileNotFoundException, URISyntaxException {
39         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(new File(getClass().getResource(
40                 "/bugs/bug6180/string-test.yang").toURI()));
41         assertNotNull(schemaContext);
42         assertEquals(1, schemaContext.getModules().size());
43         final Module module = schemaContext.getModules().iterator().next();
44         assertEquals("    1. this text contains \"string enclosed in double quotes\" and"+
45                 " special characters: \\,\n,\t          2. this text contains \"string enclosed in double quotes\" and"+
46                 " special characters: \\,\n,\n,                     3. this text contains \"string enclosed in double quotes\" and"+
47                 " special characters: \\,\n,\t      ", module.getDescription());
48     }
49
50     @Test
51     public void doubleQuotesTest() throws ReactorException, SourceException, FileNotFoundException, URISyntaxException {
52         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(new File(getClass().getResource(
53                 "/bugs/bug6180/double-quotes.yang").toURI()));
54         assertNotNull(schemaContext);
55         verifyDoubleQuotesExpression(schemaContext);
56     }
57
58     @Test
59     public void doubleQuotesSinbleInsideTest() throws ReactorException, SourceException, FileNotFoundException,
60             URISyntaxException {
61         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(new File(getClass().getResource(
62                 "/bugs/bug6180/double-quotes-single-inside.yang").toURI()));
63         assertNotNull(schemaContext);
64         verifySingleQuotesExpression(schemaContext);
65     }
66
67     @Test
68     public void singleQuotesTest() throws ReactorException, SourceException, FileNotFoundException, URISyntaxException {
69         final SchemaContext schemaContext = StmtTestUtils.parseYangSources(new File(getClass().getResource(
70                 "/bugs/bug6180/single-quotes.yang").toURI()));
71         assertNotNull(schemaContext);
72         verifyDoubleQuotesExpression(schemaContext);
73     }
74
75     private static void verifyDoubleQuotesExpression(final SchemaContext schemaContext) {
76         final DataSchemaNode dataNodeBar = schemaContext.getDataChildByName(QName.create("foo", "2016-07-11", "bar"));
77         assertTrue(dataNodeBar instanceof ContainerSchemaNode);
78         final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
79         final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition();
80         assertEquals("/foo != \"bar\"", whenCondition.toString());
81
82         final Set<TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
83         assertEquals(1, typeDefinitions.size());
84         final TypeDefinition<?> type = typeDefinitions.iterator().next();
85         assertTrue(type instanceof StringTypeDefinition);
86         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
87         assertEquals(1, patternConstraints.size());
88         final PatternConstraint pattern = patternConstraints.iterator().next();
89         assertEquals("^\".*\"$", pattern.getRegularExpression());
90         assertTrue(Pattern.compile(pattern.getRegularExpression()).matcher("\"enclosed string in quotes\"").matches());
91     }
92
93     private static void verifySingleQuotesExpression(final SchemaContext schemaContext) {
94         final DataSchemaNode dataNodeBar = schemaContext.getDataChildByName(QName.create("foo", "2016-07-11", "bar"));
95         assertTrue(dataNodeBar instanceof ContainerSchemaNode);
96         final ContainerSchemaNode bar = (ContainerSchemaNode) dataNodeBar;
97         final RevisionAwareXPath whenCondition = bar.getConstraints().getWhenCondition();
98         assertEquals("/foo != 'bar'", whenCondition.toString());
99
100         final Set<TypeDefinition<?>> typeDefinitions = schemaContext.getTypeDefinitions();
101         assertEquals(1, typeDefinitions.size());
102         final TypeDefinition<?> type = typeDefinitions.iterator().next();
103         assertTrue(type instanceof StringTypeDefinition);
104         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
105         assertEquals(1, patternConstraints.size());
106         final PatternConstraint pattern = patternConstraints.iterator().next();
107         assertEquals("^'.*'$", pattern.getRegularExpression());
108         assertTrue(Pattern.compile(pattern.getRegularExpression()).matcher("'enclosed string in quotes'").matches());
109     }
110 }