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.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.opendaylight.netconf.api.xml.XmlUtil;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.Text;
25 public final class XmlUnitUtil {
26 private XmlUnitUtil() {
30 public static void assertContainsElementWithText(final Document doc,
31 final String textToFind) throws NodeTestException {
32 NodeTest nt = new NodeTest(doc);
33 NodeTester tester = new AbstractNodeTester() {
35 boolean textFound = false;
38 public void testText(final Text text) throws NodeTestException {
40 if (text.getData().equalsIgnoreCase(textToFind)) {
47 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
48 assertTrue(textFound);
51 assertNodeTestPasses(nt, tester, new short[]{Node.TEXT_NODE}, true);
54 public static void assertContainsElement(final Document doc, final Element testElement) throws NodeTestException {
55 NodeTest nt = new NodeTest(doc);
56 NodeTester tester = new AbstractNodeTester() {
58 private boolean elementFound = false;
61 public void testElement(final Element element) throws NodeTestException {
63 if (element.isEqualNode(testElement)) {
70 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
71 assertTrue(elementFound);
74 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
77 public static void assertContainsElementWithName(final Document doc,
78 final String elementName) throws NodeTestException {
79 NodeTest nt = new NodeTest(doc);
80 NodeTester tester = new AbstractNodeTester() {
82 private boolean elementFound = false;
85 public void testElement(final Element element) throws NodeTestException {
87 if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
94 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
95 assertTrue(XmlUtil.toString(doc), elementFound);
98 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
101 public static void assertElementsCount(final Document doc, final String elementName, final int expectedCount) {
102 NodeTest nt = new NodeTest(doc);
103 NodeTester tester = new AbstractNodeTester() {
105 private int elementFound = 0;
108 public void testElement(final Element element) throws NodeTestException {
109 if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
115 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
116 assertEquals(expectedCount, elementFound);
119 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);