vgritsenko 2003/07/31 18:04:50
Modified: xmlutil/src/test/org/apache/excalibur/xml/xpath/test
XPathTestCase.java XPathTestCase.xtest
Log:
* Add more xpath tests
* Package access to test documents
* zap tabs
Revision Changes Path
1.2 +52 -10
avalon-excalibur/xmlutil/src/test/org/apache/excalibur/xml/xpath/test/XPathTestCase.java
Index: XPathTestCase.java
===================================================================
RCS file:
/home/cvs/avalon-excalibur/xmlutil/src/test/org/apache/excalibur/xml/xpath/test/XPathTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XPathTestCase.java 20 May 2003 10:43:00 -0000 1.1
+++ XPathTestCase.java 1 Aug 2003 01:04:50 -0000 1.2
@@ -61,21 +61,23 @@
import org.apache.excalibur.xml.xpath.PrefixResolver;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
import java.io.StringReader;
public class XPathTestCase extends ExcaliburTestCase
{
/** A small test document. */
- private static final String CONTENT1 =
+ static final String CONTENT1 =
"<?xml version=\"1.0\"?>" +
"<test:root xmlns:test=\"http://localhost/test\">" +
"<test:element1/>" +
"<test:element2/>" +
"</test:root>";
- /** Second testdocument, has a different namespace than [EMAIL PROTECTED]
#CONTENT1}. */
- private static final String CONTENT2 =
+ /** Second test document, has a different namespace than [EMAIL PROTECTED]
#CONTENT1}. */
+ static final String CONTENT2 =
"<?xml version=\"1.0\"?>" +
"<test:root xmlns:test=\"http://localhost/test2\">" +
"<test:element1/>" +
@@ -96,14 +98,34 @@
Document document1 = parser.parseDocument(new InputSource(new
StringReader(CONTENT1)));
Document document2 = parser.parseDocument(new InputSource(new
StringReader(CONTENT2)));
- // test with a namespace prefix configured in the component
configuration
- String testExpr = "count(/test:root/*)";
- Number number = processor.evaluateAsNumber(document1, testExpr);
+ // 1. Test single node expression
+ String expr = "/test:root/test:element1";
+ Node node = processor.selectSingleNode(document1, expr);
+ assertNotNull("Must select <test:element1/> node, but got null", node);
+ assertEquals("Must select <test:element1/> node", Node.ELEMENT_NODE,
node.getNodeType());
+ assertEquals("Must select <test:element1/> node", "element1",
node.getLocalName());
+
+ // 2. Test single node expression with no expected result
+ expr = "/test:root/test:element3";
+ node = processor.selectSingleNode(document1, expr);
+ assertNull("Must be null", node);
+
+ // 3. Test multiple node expression
+ expr = "/test:root/test:*";
+ NodeList list = processor.selectNodeList(document1, expr);
+ assertNotNull("Must select two nodes, but got null", list);
+ assertEquals("Must select two nodes", 2, list.getLength());
+ assertEquals("Must select <test:element1/> node", "element1",
list.item(0).getLocalName());
+ assertEquals("Must select <test:element2/> node", "element2",
list.item(1).getLocalName());
+
+ // 4. Test with a namespace prefix configured in the component
configuration
+ expr = "count(/test:root/*)";
+ Number number = processor.evaluateAsNumber(document1, expr);
assertEquals(2, number.intValue());
- // test with a custom prefix resolver using a different document in a
different namespace,
+ // 5. Test with a custom prefix resolver using a different document in
a different namespace,
// to be sure the custom prefix resolver is used
- number = processor.evaluateAsNumber(document2, testExpr, new
PrefixResolver() {
+ number = processor.evaluateAsNumber(document2, expr, new
PrefixResolver() {
public String prefixToNamespace(String prefix)
{
if (prefix.equals("test"))
@@ -113,11 +135,31 @@
});
assertEquals(2, number.intValue());
+ // 6. Test boolean
+ expr = "count(/test:root/*) = 2";
+ boolean bool = processor.evaluateAsBoolean(document1, expr);
+ assertEquals(true, bool);
+
+ // 7. Test expression in the root element context
+ expr = "/test:root/test:element1";
+ node = processor.selectSingleNode(document1.getDocumentElement(), expr);
+ assertNotNull("Must select <test:element1/> node, but got null", node);
+ assertEquals("Must select <test:element1/> node", Node.ELEMENT_NODE,
node.getNodeType());
+ assertEquals("Must select <test:element1/> node", "element1",
node.getLocalName());
+
+ // 8. Test expression in the child node context
+ node =
processor.selectSingleNode(document1.getDocumentElement().getFirstChild(), expr);
+ assertNotNull("Must select <test:element1/> node, but got null", node);
+ assertEquals("Must select <test:element1/> node", Node.ELEMENT_NODE,
node.getNodeType());
+ assertEquals("Must select <test:element1/> node", "element1",
node.getLocalName());
+
} finally {
- if (parser != null)
+ if (parser != null) {
manager.release((Component)parser);
- if (processor != null)
+ }
+ if (processor != null) {
manager.release((Component)processor);
+ }
}
}
}
1.2 +9 -8
avalon-excalibur/xmlutil/src/test/org/apache/excalibur/xml/xpath/test/XPathTestCase.xtest
Index: XPathTestCase.xtest
===================================================================
RCS file:
/home/cvs/avalon-excalibur/xmlutil/src/test/org/apache/excalibur/xml/xpath/test/XPathTestCase.xtest,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XPathTestCase.xtest 20 May 2003 10:43:00 -0000 1.1
+++ XPathTestCase.xtest 1 Aug 2003 01:04:50 -0000 1.2
@@ -1,15 +1,16 @@
<?xml version="1.0"?>
+
<testcase>
- <annotation>XPathProcessor test case</annotation>
- <roles>
- <role name="org.apache.excalibur.xml.dom.DOMParser"
shorthand="domparser" default-class="org.apache.excalibur.xml.impl.JaxpParser"/>
- <role name="org.apache.excalibur.xml.xpath.XPathProcessor"
shorthand="xpathprocessor"
default-class="org.apache.excalibur.xml.xpath.XPathProcessorImpl"/>
- </roles>
- <components>
+ <annotation>XPathProcessor test case</annotation>
+ <roles>
+ <role name="org.apache.excalibur.xml.dom.DOMParser" shorthand="domparser"
default-class="org.apache.excalibur.xml.impl.JaxpParser"/>
+ <role name="org.apache.excalibur.xml.xpath.XPathProcessor"
shorthand="xpathprocessor"
default-class="org.apache.excalibur.xml.xpath.XPathProcessorImpl"/>
+ </roles>
+ <components>
<xpathprocessor>
<namespace-mappings>
<namespace prefix="test" uri="http://localhost/test"/>
</namespace-mappings>
</xpathprocessor>
- </components>
-</testcase>
\ No newline at end of file
+ </components>
+</testcase>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]