2 * Copyright (c) 2015 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.netconf.util.xml;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThrows;
15 import javax.xml.xpath.XPathConstants;
16 import javax.xml.xpath.XPathExpression;
17 import org.junit.Test;
18 import org.opendaylight.netconf.api.xml.XmlUtil;
19 import org.w3c.dom.Element;
21 public class XMLNetconfUtilTest {
23 public void testXPath() throws Exception {
24 final XPathExpression correctXPath = XMLNetconfUtil.compileXPath("/top/innerText");
25 final IllegalStateException ex = assertThrows(IllegalStateException.class,
26 () -> XMLNetconfUtil.compileXPath("!@(*&$!"));
27 assertThat(ex.getMessage(), startsWith("Error while compiling xpath expression "));
28 final Object value = XmlUtil.evaluateXPath(correctXPath,
29 XmlUtil.readXmlToDocument("<top><innerText>value</innerText></top>"), XPathConstants.NODE);
30 assertEquals("value", ((Element) value).getTextContent());