/** * Execute a <code>NodeTest</code> for multiple node types and make an * assertion about it whether it is expected to pass * @param test a NodeTest instance containing the XML source to be tested * @param tester The test strategy * @param nodeTypes The node types to be tested: constants defined * in {@link Node org.w3c.dom.Node} e.g. <code>Node.ELEMENT_NODE</code> * @param assertion true if the test is expected to pass, false otherwise * @see AbstractNodeTester * @see CountingNodeTester */ public static void assertNodeTestPasses(NodeTest test, NodeTester tester, short[] nodeTypes, boolean assertion) { try { test.performTest(tester, nodeTypes); if (!assertion) { fail("Expected node test to fail, but it passed!"); } } catch (NodeTestException e) { if (assertion) { fail("Expected node test to pass, but it failed! " + e.getMessage()); } } }
/** * Execute a <code>NodeTest<code> for multiple node types and make an * assertion about it whether it is expected to pass * @param test a NodeTest instance containing the XML source to be tested * @param tester The test strategy * @param nodeTypes The node types to be tested: constants defined * in {@link Node org.w3c.dom.Node} e.g. <code>Node.ELEMENT_NODE</code> * @param assertion true if the test is expected to pass, false otherwise * @see AbstractNodeTester * @see CountingNodeTester */ public static void assertNodeTestPasses(NodeTest test, NodeTester tester, short[] nodeTypes, boolean assertion) { try { test.performTest(tester, nodeTypes); if (!assertion) { fail("Expected node test to fail, but it passed!"); } } catch (NodeTestException e) { if (assertion) { fail("Expected node test to pass, but it failed! " + e.getMessage()); } } }
/** * Assert that the result of an XML comparison is or is not identical * @param msg Message to display if assertion fails * @param diff the result of an XML comparison * @param assertion true if asserting that result is identical */ public static void assertXMLIdentical(String msg, Diff diff, boolean assertion) { if (assertion != diff.identical()) { fail(getFailMessage(msg, diff)); } }
/** * Assert that the result of an XML comparison is or is not similar. * @param msg additional message to display if assertion fails * @param diff the result of an XML comparison * @param assertion true if asserting that result is similar */ public static void assertXMLEqual(String msg, Diff diff, boolean assertion) { if (assertion != diff.similar()) { fail(getFailMessage(msg, diff)); } }
/** * Assert that the result of an XML comparison is or is not similar. * @param msg additional message to display if assertion fails * @param diff the result of an XML comparison * @param assertion true if asserting that result is similar */ public static void assertXMLEqual(String msg, Diff diff, boolean assertion) { if (assertion != diff.similar()) { fail(getFailMessage(msg, diff)); } }
/** * Assert that the result of an XML comparison is or is not identical * @param msg Message to display if assertion fails * @param diff the result of an XML comparison * @param assertion true if asserting that result is identical */ public static void assertXMLIdentical(String msg, Diff diff, boolean assertion) { if (assertion != diff.identical()) { fail(getFailMessage(msg, diff)); } }
/** * Assert that the evaluation of two Xpaths in two documents are * NOT equal * @param xpathOne * @param xpathTwo * @param document */ public static void assertXpathValuesNotEqual(String controlXpath, Document controlDocument, String testXpath, Document testDocument) throws XpathException { XpathEngine xpath = XMLUnit.newXpathEngine(); String control = xpath.evaluate(controlXpath, controlDocument); String test = xpath.evaluate(testXpath, testDocument); if (control!=null) { if (control.equals(test)) { fail("Expected test value NOT to be equal to control but both were " + test); } } else if (test != null) { fail("control xPath evaluated to empty node set, " + "but test xPath evaluated to " + test); } }
/** * Assert that the evaluation of two Xpaths in two documents are * NOT equal * @param controlXpath * @param testXpath * @param controlDocument * @param testDocument */ public static void assertXpathValuesNotEqual(String controlXpath, Document controlDocument, String testXpath, Document testDocument) throws XpathException { XpathEngine xpath = XMLUnit.newXpathEngine(); String control = xpath.evaluate(controlXpath, controlDocument); String test = xpath.evaluate(testXpath, testDocument); if (control!=null) { if (control.equals(test)) { fail("Expected test value NOT to be equal to control but both were " + test); } } else if (test == null) { fail("Expected test value NOT to be equal to control but both were empty"); } }
@Test public void testXMLReaderInvalid() throws Exception { // create a schema invalid request String capRequest = getResourceAsString("GetCapabilities.xml"); capRequest = capRequest.replace("ows:Sections", "ows:foo"); try { CSWXmlReader reader = new CSWXmlReader("GetCapabilities", "2.0.2", new CSWConfiguration()); reader.read(null, new StringReader(capRequest), (Map) null); fail("the parsing should have failed, the document is invalid"); } catch (ServiceException e) { // it is a validation exception right? assertTrue(e.getCause() instanceof SAXParseException); SAXParseException cause = (SAXParseException) e.getCause(); assertTrue(cause.getMessage().contains("ows:foo")); } }
@Test public void testPreconditions() throws TransformerException { try { new DescribeLayerTransformer(null); Assert.fail("expected NPE on null base url"); } catch (NullPointerException e) { Assert.assertTrue(true); } transformer = new DescribeLayerTransformer("http://geoserver.org"); try { transformer.transform(null); Assert.fail("expected IAE on null request"); } catch (TransformerException e) { Assert.assertTrue(e.getCause() instanceof IllegalArgumentException); } try { transformer.transform(new Object()); fail("expected IAE on argument non a DescribeLayerRequest instance"); } catch (TransformerException e) { assertTrue(e.getCause() instanceof IllegalArgumentException); } }