Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
XMLUnit-Java.pdf
Скачиваний:
12
Добавлен:
24.03.2015
Размер:
266.81 Кб
Скачать

XMLUnit Java User’s Guide

30 / 35

*is itself an attribute - so a NodeTester that needs to test attributes

*should obtain those attributes from <code>Node.ELEMENT_NODE</code>

*nodes instead

*@exception NodeTestException if test fails

*/

public void performTest(NodeTester tester, short[] nodeTypes);

NodeTester is the class testing each node and is described in the next section.

The second argument limits the tests on DOM Nodes of (a) specific type(s). Node types are specified via the static fields of the Node class. Any Node of a type not specified as the second argument to performTest will be ignored.

Unfortunately XML attributes are not exposed as Nodes during traversal. If you need access to attributes you must add Node. ELEMENT_NODE to the second argument of performTest and access the attributes from their parent Element.

Example 6.1 Accessing Attributes in a NodeTest

...

NodeTest nt = new NodeTest(myXML);

NodeTester tester = new MyNodeTester(); nt.performTest(tester, Node.ELEMENT_NODE);

...

class MyNodeTester implements NodeTester {

public void testNode(Node aNode, NodeTest test) { Element anElement = (Element) aNode;

Attr attributeToTest = anElement.getAttributeNode(ATTRIBUTE_NAME);

...

}

...

}

Any entities that appear as part of the Document are expanded before the traversal starts.

6.3NodeTester

Implementations of the NodeTester interface are responsible for the actual test:

/**

*Validate a single Node

*@param aNode

*@param forTest

*@exception NodeTestException if the node fails the test

*/

void testNode(Node aNode, NodeTest forTest) throws NodeTestException ;

/**

*Validate that the Nodes passed one-by-one to the <code>testNode</code>

*method were all the Nodes expected.

*@param forTest

*@exception NodeTestException if this instance was expecting more nodes

*/

void noMoreNodes(NodeTest forTest) throws NodeTestException ;

NodeTest invokes testNode for each Node as soon as it is reached on the traversal. This means NodeTester "sees" the Nodes in the same order they appear within the tree.

noMoreNodes is invoked when the traversal is finished. It will also be invoked if the tree didn’t contain any matched Nodes at all.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]