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
9 package org.opendaylight.controller.netconf.util.test;
11 import static org.custommonkey.xmlunit.XMLAssert.assertNodeTestPasses;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
15 import org.custommonkey.xmlunit.AbstractNodeTester;
16 import org.custommonkey.xmlunit.NodeTest;
17 import org.custommonkey.xmlunit.NodeTestException;
18 import org.custommonkey.xmlunit.NodeTester;
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 class XmlUnitUtil {
26 private XmlUnitUtil() {}
28 public static void assertContainsElementWithText(final Document doc, final String textToFind) throws NodeTestException {
29 NodeTest nt = new NodeTest(doc);
30 NodeTester tester = new AbstractNodeTester() {
32 boolean textFound = false;
35 public void testText(Text text) throws NodeTestException {
37 if (text.getData().equalsIgnoreCase(textToFind)) {
44 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
45 assertTrue(textFound);
48 assertNodeTestPasses(nt, tester, new short[]{Node.TEXT_NODE}, true);
51 public static void assertContainsElement(final Document doc, final Element testElement) throws NodeTestException {
52 NodeTest nt = new NodeTest(doc);
53 NodeTester tester = new AbstractNodeTester() {
55 private boolean elementFound = false;
58 public void testElement(Element element) throws NodeTestException {
60 if(element.isEqualNode(testElement)) {
67 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
68 assertTrue(elementFound);
71 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
74 public static void assertContainsElementWithName(final Document doc, final String elementName) throws NodeTestException {
75 NodeTest nt = new NodeTest(doc);
76 NodeTester tester = new AbstractNodeTester() {
78 private boolean elementFound = false;
81 public void testElement(Element element) throws NodeTestException {
83 if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
90 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
91 assertTrue(elementFound);
94 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
97 public static void assertElementsCount(final Document doc, final String elementName, final int expectedCount) {
98 NodeTest nt = new NodeTest(doc);
99 NodeTester tester = new AbstractNodeTester() {
101 private int elementFound = 0;
104 public void testElement(Element element) throws NodeTestException {
105 if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
111 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
112 assertEquals(expectedCount, elementFound);
115 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);