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;
13 import static org.junit.Assert.assertTrue;
16 import java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.util.List;
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;
35 public class Bug6180Test {
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());
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);
59 public void doubleQuotesSinbleInsideTest() throws ReactorException, SourceException, FileNotFoundException,
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);
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);
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());
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());
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());
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());