[Practicalxml-commits] SF.net SVN: practicalxml:[39] trunk/src
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2008-12-03 14:08:48
|
Revision: 39 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=39&view=rev Author: kdgregory Date: 2008-12-03 14:08:45 +0000 (Wed, 03 Dec 2008) Log Message: ----------- add DomAsserts Added Paths: ----------- trunk/src/main/java/net/sf/practicalxml/junit/ trunk/src/main/java/net/sf/practicalxml/junit/DomAsserts.java trunk/src/test/java/net/sf/practicalxml/junit/ trunk/src/test/java/net/sf/practicalxml/junit/TestDomAsserts.java Added: trunk/src/main/java/net/sf/practicalxml/junit/DomAsserts.java =================================================================== --- trunk/src/main/java/net/sf/practicalxml/junit/DomAsserts.java (rev 0) +++ trunk/src/main/java/net/sf/practicalxml/junit/DomAsserts.java 2008-12-03 14:08:45 UTC (rev 39) @@ -0,0 +1,273 @@ +package net.sf.practicalxml.junit; + +import static junit.framework.Assert.*; + +import java.util.List; + +import junit.framework.Assert; + +import net.sf.practicalxml.DomUtil; +import net.sf.practicalxml.XPathWrapper; + +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** + * JUnit assertions for DOM documents. These are defined as static methods, + * so may be statically imported (although in some cases this will clash + * with the standard assertions in <code>junit.framework.Assert</code>). + * <p> + * As with the standard JUnit assertions, there are two forms for each method: + * one that takes an explanatory message, and one that doesn't. + */ +public class DomAsserts +{ + /** + * Asserts that an element has the given name, ignoring namespace. + * + * @param expected The expected name. + * @param elem The element to assert. + */ + public static void assertName(String expected, Element elem) + { + Assert.assertEquals(expected, DomUtil.getLocalName(elem)); + } + + /** + * Asserts that an element has the given name, ignoring namespace. + * + * @param message Message to display if assertion fails. + * @param expected The expected name. + * @param elem The element to assert. + */ + public static void assertName(String message, String expected, Element elem) + { + Assert.assertEquals(message, expected, DomUtil.getLocalName(elem)); + } + + + /** + * Asserts that an element has the given name and namespace URI. + * <p> + * If assertion fails, will display message indicating whether name or + * namespace was invalid. + * + * @param expectedNSUri The expected namespace URI. May be <code>null + * </code> to assert that the element does not + * have a namespace. + * @param expectedName The expected name. + * @param elem The element to assert. + */ + public static void assertNamespaceAndName( + String expectedNSUri, String expectedName, Element elem) + { + Assert.assertEquals("invalid namespace", expectedNSUri, elem.getNamespaceURI()); + Assert.assertEquals("invalid localname", expectedName, DomUtil.getLocalName(elem)); + } + + + /** + * Asserts that an element has the given name and namespace URI. + * + * @param message Message to display if assertion fails. + * @param expectedNSUri The expected namespace URI. May be <code>null + * </code> to assert that the element does not + * have a namespace. + * @param expectedName The expected name. + * @param elem The element to assert. + */ + public static void assertNamespaceAndName( + String message, String expectedNSUri, String expectedName, Element elem) + { + Assert.assertEquals(message, expectedNSUri, elem.getNamespaceURI()); + Assert.assertEquals(message, expectedName, DomUtil.getLocalName(elem)); + } + + + /** + * Asserts that the specified XPath selects at least one node. + * <p> + * Will display the XPath if assertion fails. + * + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertExists(Element elem, String xpath) + { + assertExists(xpath, elem, xpath); + } + + + /** + * Asserts that the specified XPath selects at least one node. + * + * @param message Message to display if assertion fails. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertExists(String message, Element elem, String xpath) + { + assertExists(message, elem, new XPathWrapper(xpath)); + } + + + /** + * Asserts that the specified XPath selects at least one node. Uses the + * <code>XPathWrapper</code> class to allow more complex paths, including + * namespace bindings. + * <p> + * Will display the XPath if assertion fails. + * + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertExists(Element elem, XPathWrapper xpath) + { + assertExists(xpath.toString(), elem, xpath); + } + + + /** + * Asserts that the specified XPath selects at least one node. Uses the + * <code>XPathWrapper</code> class to allow more complex paths, including + * namespace bindings. + * + * @param message Message to display if assertion fails. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertExists(String message, Element elem, XPathWrapper xpath) + { + List<Node> result = xpath.evaluate(elem); + assertTrue(message, result.size() > 0); + } + + + /** + * Asserts that the specified XPath selects a specified number of nodes. + * <p> + * Will display the XPath if assertion fails. + * + * @param expected The expected number of nodes selected. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertCount(int expected, Element elem, String xpath) + { + assertCount(xpath, expected, elem, xpath); + } + + + /** + * Asserts that the specified XPath selects a specified number of nodes. + * + * @param message Message to display if assertion fails. + * @param expected The expected number of nodes selected. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertCount( + String message, int expected, Element elem, String xpath) + { + assertCount(message, expected, elem, new XPathWrapper(xpath)); + } + + + /** + * Asserts that the specified XPath selects a specified number of nodes. + * Uses the <code>XPathWrapper</code> class to allow more complex paths, + * including namespace bindings. + * <p> + * Will display the XPath if assertion fails. + * + * @param expected The expected number of nodes selected. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertCount(int expected, Element elem, XPathWrapper xpath) + { + assertCount(xpath.toString(), expected, elem, xpath); + } + + + /** + * Asserts that the specified XPath selects a specified number of nodes. + * Uses the <code>XPathWrapper</code> class to allow more complex paths, + * including namespace bindings. + * + * @param message Message to display if assertion fails. + * @param expected The expected number of nodes selected. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertCount( + String message, int expected, Element elem, XPathWrapper xpath) + { + List<Node> result = xpath.evaluate(elem); + Assert.assertEquals(message, expected, result.size()); + } + + + /** + * Asserts that the specified XPath selects a particular String value. + * <p> + * Will display the XPath if assertion fails. + * + * @param expected The expected value. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertEquals(String expected, Element elem, String xpath) + { + assertEquals(xpath, expected, elem, new XPathWrapper(xpath)); + } + + + /** + * Asserts that the specified XPath selects a particular String value. + * + * @param message Message to display if assertion fails. + * @param expected The expected value. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertEquals( + String message, String expected, Element elem, String xpath) + { + assertEquals(message, expected, elem, new XPathWrapper(xpath)); + } + + + /** + * Asserts that the specified XPath selects a particular String value. + * This variant uses the <code>XPathWrapper</code> class to allow + * more complex paths, including namespace bindings. + * <p> + * Will display the XPath if assertion fails. + * + * @param expected The expected value. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertEquals(String expected, Element elem, XPathWrapper xpath) + { + assertEquals(xpath.toString(), expected, elem, xpath); + } + + + /** + * Asserts that the specified XPath selects a particular String value. + * This variant uses the <code>XPathWrapper</code> class to allow + * more complex paths, including namespace bindings. + * + * @param message Message to display if assertion fails. + * @param expected The expected value. + * @param elem The element to serve as initial context. + * @param xpath The path expression. + */ + public static void assertEquals( + String message, String expected, Element elem, XPathWrapper xpath) + { + Assert.assertEquals(message, expected, xpath.evaluateAsString(elem)); + } +} Added: trunk/src/test/java/net/sf/practicalxml/junit/TestDomAsserts.java =================================================================== --- trunk/src/test/java/net/sf/practicalxml/junit/TestDomAsserts.java (rev 0) +++ trunk/src/test/java/net/sf/practicalxml/junit/TestDomAsserts.java 2008-12-03 14:08:45 UTC (rev 39) @@ -0,0 +1,241 @@ +package net.sf.practicalxml.junit; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import junit.framework.AssertionFailedError; + +import net.sf.practicalxml.AbstractTestCase; +import net.sf.practicalxml.DomUtil; +import net.sf.practicalxml.XPathWrapper; + + +public class TestDomAsserts +extends AbstractTestCase +{ + public TestDomAsserts(String name) + { + super(name); + } + + +//---------------------------------------------------------------------------- +// Test data +//---------------------------------------------------------------------------- + + public final static String MESSAGE = "qwery this is a test asdf"; + + public final static String INVALID_NAME = "slkdfio"; + + public final static String EL_ROOT = "root"; + public final static String EL_CHILD = "child"; + public final static String NS = "ns"; + public final static String NS1 = "ns1"; + public final static String NS2 = "ns2"; + public final static String ATTR1 = "foo"; + public final static String ATTVAL1a = "bar"; + public final static String ATTVAL1b = "10"; + + public final static String XPATH1 = "//" + EL_CHILD; + public final static String XPATH1a = "//" + EL_CHILD + "[@" + ATTR1 + "=\"" + ATTVAL1a + "\"]"; + public final static String XPATH2 = "//" + NS + ":" + EL_CHILD; + public final static String XPATH2a = "//" + NS + ":" + EL_CHILD + "[@" + ATTR1 + "=\"" + ATTVAL1a + "\"]"; + public final static String XPATH3 = "@" + ATTR1; + public final static String XPATH4 = "//" + INVALID_NAME; + + + Document _dom; + Element _root; + Element _child1; + Element _child2; + Element _child3; + Element _child4; + Element _child5; + + + @Override + protected void setUp() + { + _root = DomUtil.newDocument(EL_ROOT); + _child1 = DomUtil.appendChild(_root, EL_CHILD); + _child2 = DomUtil.appendChild(_root, EL_CHILD); + _child3 = DomUtil.appendChild(_root, EL_CHILD); + _child4 = DomUtil.appendChild(_root, NS1, EL_CHILD); + _child5 = DomUtil.appendChild(_root, NS2, EL_CHILD); + + _child2.setAttribute(ATTR1, ATTVAL1a); + _child3.setAttribute(ATTR1, ATTVAL1b); + _child4.setAttribute(ATTR1, ATTVAL1a); + _child5.setAttribute(ATTR1, ATTVAL1b); + + _dom = _root.getOwnerDocument(); + } + + +//---------------------------------------------------------------------------- +// Support Code +//---------------------------------------------------------------------------- + + +//---------------------------------------------------------------------------- +// Test Cases +//---------------------------------------------------------------------------- + + public void testAssertName() throws Exception + { + DomAsserts.assertName(EL_CHILD, _child1); + DomAsserts.assertName(EL_CHILD, _child4); + + AssertionFailedError fail1 = null; + try + { + DomAsserts.assertName(INVALID_NAME, _child1); + } + catch (AssertionFailedError ee) + { + fail1 = ee; + } + assertNotNull("asserted invalid name", fail1); + + + AssertionFailedError fail2 = null; + try + { + DomAsserts.assertName(MESSAGE, INVALID_NAME, _child1); + } + catch (AssertionFailedError ee) + { + fail2 = ee; + } + assertTrue("missing message", fail2.getMessage().contains(MESSAGE)); + } + + + public void testAssertNameAndNamespace() throws Exception + { + DomAsserts.assertNamespaceAndName(null, EL_CHILD, _child1); + DomAsserts.assertNamespaceAndName(NS1, EL_CHILD, _child4); + + AssertionFailedError fail1 = null; + try + { + DomAsserts.assertNamespaceAndName(INVALID_NAME, EL_CHILD, _child1); + } + catch (AssertionFailedError ee) + { + fail1 = ee; + } + assertNotNull("asserted invalid namespace", fail1); + + AssertionFailedError fail2 = null; + try + { + DomAsserts.assertNamespaceAndName(NS1, INVALID_NAME, _child4); + } + catch (AssertionFailedError ee) + { + fail2 = ee; + } + assertNotNull("asserted invalid name", fail2); + + AssertionFailedError fail3 = null; + try + { + DomAsserts.assertNamespaceAndName(MESSAGE, NS1, INVALID_NAME, _child4); + } + catch (AssertionFailedError ee) + { + fail3 = ee; + } + assertTrue("missing message", fail3.getMessage().contains(MESSAGE)); + } + + + public void testAssertExists() throws Exception + { + DomAsserts.assertExists(_root, XPATH1); + DomAsserts.assertExists(_root, XPATH1a); + DomAsserts.assertExists(_root, new XPathWrapper(XPATH2).bindNamespace(NS, NS1)); + + AssertionFailedError fail1 = null; + try + { + DomAsserts.assertExists(_root, XPATH4); + } + catch (AssertionFailedError ee) + { + fail1 = ee; + } + assertNotNull("asserted invalid xpath", fail1); + + AssertionFailedError fail2 = null; + try + { + DomAsserts.assertExists(MESSAGE, _root, XPATH4); + } + catch (AssertionFailedError ee) + { + fail2 = ee; + } + assertTrue("missing message", fail2.getMessage().contains(MESSAGE)); + } + + + public void testAssertCount() throws Exception + { + DomAsserts.assertCount(3, _root, XPATH1); + DomAsserts.assertCount(1, _root, XPATH1a); + DomAsserts.assertCount(1, _root, new XPathWrapper(XPATH2).bindNamespace(NS, NS1)); + DomAsserts.assertCount(0, _root, XPATH4); + + AssertionFailedError fail1 = null; + try + { + DomAsserts.assertCount(2, _root, XPATH1); + } + catch (AssertionFailedError ee) + { + fail1 = ee; + } + assertNotNull("asserted incorrect count", fail1); + + AssertionFailedError fail2 = null; + try + { + DomAsserts.assertCount(MESSAGE, 2, _root, XPATH1); + } + catch (AssertionFailedError ee) + { + fail2 = ee; + } + assertTrue("missing message", fail2.getMessage().contains(MESSAGE)); + } + + + public void testAssertEqualsString() throws Exception + { + DomAsserts.assertEquals(ATTVAL1a, _child2, XPATH3); + + AssertionFailedError fail1 = null; + try + { + DomAsserts.assertEquals(ATTVAL1a, _child1, XPATH3); + } + catch (AssertionFailedError ee) + { + fail1 = ee; + } + assertNotNull("asserted when xpath should have returned nothing", fail1); + + AssertionFailedError fail2 = null; + try + { + DomAsserts.assertEquals(MESSAGE, ATTVAL1a, _child1, XPATH3); + } + catch (AssertionFailedError ee) + { + fail2 = ee; + } + assertTrue("missing message", fail2.getMessage().contains(MESSAGE)); + } +} Property changes on: trunk/src/test/java/net/sf/practicalxml/junit/TestDomAsserts.java ___________________________________________________________________ Added: svn:executable + * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |