2 * Copyright (c) 2014 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.test.util;
10 import static org.custommonkey.xmlunit.XMLAssert.assertNodeTestPasses;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import org.custommonkey.xmlunit.AbstractNodeTester;
15 import org.custommonkey.xmlunit.NodeTest;
16 import org.custommonkey.xmlunit.NodeTestException;
17 import org.custommonkey.xmlunit.NodeTester;
18 import org.opendaylight.netconf.api.xml.XmlUtil;
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21 import org.w3c.dom.Node;
22 import org.w3c.dom.Text;
24 public final class XmlUnitUtil {
25 private XmlUnitUtil() {
29 public static void assertContainsElementWithText(final Document doc, final String textToFind)
30 throws NodeTestException {
31 NodeTest nt = new NodeTest(doc);
32 NodeTester tester = new AbstractNodeTester() {
34 boolean textFound = false;
37 public void testText(final Text text) throws NodeTestException {
39 if (text.getData().equalsIgnoreCase(textToFind)) {
46 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
47 assertTrue(textFound);
50 assertNodeTestPasses(nt, tester, new short[]{Node.TEXT_NODE}, true);
53 public static void assertContainsElement(final Document doc, final Element testElement) throws NodeTestException {
54 NodeTest nt = new NodeTest(doc);
55 NodeTester tester = new AbstractNodeTester() {
57 private boolean elementFound = false;
60 public void testElement(final Element element) throws NodeTestException {
62 if (element.isEqualNode(testElement)) {
69 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
70 assertTrue(elementFound);
73 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
76 public static void assertContainsElementWithName(final Document doc,
77 final String elementName) throws NodeTestException {
78 NodeTest nt = new NodeTest(doc);
79 NodeTester tester = new AbstractNodeTester() {
81 private boolean elementFound = false;
84 public void testElement(final Element element) throws NodeTestException {
86 if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
93 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
94 assertTrue(XmlUtil.toString(doc), elementFound);
97 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
100 public static void assertElementsCount(final Document doc, final String elementName, final int expectedCount) {
101 NodeTest nt = new NodeTest(doc);
102 NodeTester tester = new AbstractNodeTester() {
104 private int elementFound = 0;
107 public void testElement(final Element element) throws NodeTestException {
108 if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
114 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
115 assertEquals(expectedCount, elementFound);
118 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);