2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.xpath.impl;
10 import static org.junit.Assert.assertEquals;
12 import com.google.common.collect.ImmutableMap;
15 import javax.xml.xpath.XPathExpressionException;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.xpath.api.YangBooleanConstantExpr;
21 import org.opendaylight.yangtools.yang.xpath.api.YangExpr;
23 @SuppressWarnings("null")
24 public class XPathParserTest {
25 private static final QNameModule DEF_NS = QNameModule.create(URI.create("defaultns"));
26 private static final Map<String, QNameModule> NAMESPACES = ImmutableMap.of(
27 "foo", QNameModule.create(URI.create("foo")),
28 "bar", QNameModule.create(URI.create("bar")));
30 private @Nullable XPathParser<?> parser;
33 public void before() {
34 parser = new BigDecimalXPathParser(DEF_NS, NAMESPACES::get);
38 public void testSmoke() throws XPathExpressionException {
46 parseExpr("@foo:foo");
47 parseExpr("current()");
48 parseExpr("foo:self()");
49 parseExpr("foo:comment()");
50 parseExpr("/a[foo = 2 and bar = 3]");
51 parseExpr("/a[foo = \"2\" and bar = '3']");
52 parseExpr("/foo:a[foo = 2 and bar = 3]");
53 parseExpr("//a[foo = 2 and bar = 3]");
54 parseExpr("//foo:a[foo=2 and bar:bar=3]");
55 parseExpr("a//b[foo = 2]");
56 parseExpr("foo:a//b[foo = 2]");
57 parseExpr("$foo:comment");
58 parseExpr("$comment");
63 public void testUnionSquashing() throws XPathExpressionException {
64 final YangExpr a = parseExpr("a");
65 assertEquals(a, parseExpr("a|a"));
66 assertEquals(a, parseExpr("a|a|a"));
67 assertEquals(a, parseExpr("a|a|a|a"));
69 final YangExpr ab = parseExpr("a|b");
70 assertEquals(ab, parseExpr("a|b|a"));
71 assertEquals(ab, parseExpr("a|b|a|b"));
72 assertEquals(ab, parseExpr("a|a|b|a|b"));
73 assertEquals(ab, parseExpr("a|b|b|a|b"));
74 assertEquals(ab, parseExpr("a|b|a|a|b"));
78 public void testNumberSquashing() throws XPathExpressionException {
79 final YangExpr two = parseExpr("2");
80 assertEquals(two, parseExpr("1 + 1"));
81 assertEquals(two, parseExpr("3 - 1"));
82 assertEquals(two, parseExpr("2 * 1"));
83 assertEquals(two, parseExpr("4 div 2"));
84 assertEquals(two, parseExpr("6 mod 4"));
88 public void testSameExprSquashing() throws XPathExpressionException {
90 assertEquals(YangBooleanConstantExpr.FALSE, parseExpr("/a != /a"));
91 assertEquals(YangBooleanConstantExpr.TRUE, parseExpr("/a = /a"));
94 assertEquals(YangBooleanConstantExpr.FALSE, parseExpr("2 != 2"));
95 assertEquals(YangBooleanConstantExpr.FALSE, parseExpr("2 != (1 + 1)"));
96 assertEquals(YangBooleanConstantExpr.TRUE, parseExpr("2 = 2"));
97 assertEquals(YangBooleanConstantExpr.TRUE, parseExpr("2 = (1 + 1)"));
100 private YangExpr parseExpr(final String xpath) throws XPathExpressionException {
101 return parser.parseExpression(xpath).getRootExpr();