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.opendaylight.controller.netconf.util.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 class XmlUnitUtil {
27 private XmlUnitUtil() {}
29 public static void assertContainsElementWithText(final Document doc, final String textToFind) throws NodeTestException {
30 NodeTest nt = new NodeTest(doc);
31 NodeTester tester = new AbstractNodeTester() {
33 boolean textFound = false;
36 public void testText(Text text) throws NodeTestException {
38 if (text.getData().equalsIgnoreCase(textToFind)) {
45 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
46 assertTrue(textFound);
49 assertNodeTestPasses(nt, tester, new short[]{Node.TEXT_NODE}, true);
52 public static void assertContainsElement(final Document doc, final Element testElement) throws NodeTestException {
53 NodeTest nt = new NodeTest(doc);
54 NodeTester tester = new AbstractNodeTester() {
56 private boolean elementFound = false;
59 public void testElement(Element element) throws NodeTestException {
61 if(element.isEqualNode(testElement)) {
68 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
69 assertTrue(elementFound);
72 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
75 public static void assertContainsElementWithName(final Document doc, final String elementName) throws NodeTestException {
76 NodeTest nt = new NodeTest(doc);
77 NodeTester tester = new AbstractNodeTester() {
79 private boolean elementFound = false;
82 public void testElement(Element element) throws NodeTestException {
84 if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
91 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
92 assertTrue(XmlUtil.toString(doc), elementFound);
95 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);
98 public static void assertElementsCount(final Document doc, final String elementName, final int expectedCount) {
99 NodeTest nt = new NodeTest(doc);
100 NodeTester tester = new AbstractNodeTester() {
102 private int elementFound = 0;
105 public void testElement(Element element) throws NodeTestException {
106 if (element.getNodeName() != null && element.getNodeName().equals(elementName)) {
112 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
113 assertEquals(expectedCount, elementFound);
116 assertNodeTestPasses(nt, tester, new short[]{Node.ELEMENT_NODE}, true);