Java Code Examples for org.custommonkey.xmlunit.Diff#similar()

The following examples show how to use org.custommonkey.xmlunit.Diff#similar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlAsserts.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private static void doAssertXmlEquals(String expectedXml, String actualXml) throws Exception {
    Diff diff = new Diff(expectedXml, actualXml);
    diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
    if (!diff.similar()) {
        fail("\nExpected the following XML\n" + formatXml(expectedXml) +
             "\nbut actual XML was\n\n" +
             formatXml(actualXml));
    }
}
 
Example 2
Source File: XmlExpectationsHelper.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the expected and actual content strings as XML and assert that the
 * two are "similar" -- i.e. they contain the same elements and attributes
 * regardless of order.
 * <p>Use of this method assumes the
 * <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available.
 * @param expected the expected XML content
 * @param actual the actual XML content
 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Object...)
 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Map, Object...)
 */
public void assertXmlEqual(String expected, String actual) throws Exception {
	XMLUnit.setIgnoreWhitespace(true);
	XMLUnit.setIgnoreComments(true);
	XMLUnit.setIgnoreAttributeOrder(true);

	Document control = XMLUnit.buildControlDocument(expected);
	Document test = XMLUnit.buildTestDocument(actual);
	Diff diff = new Diff(control, test);
	if (!diff.similar()) {
		AssertionErrors.fail("Body content " + diff.toString());
	}
}
 
Example 3
Source File: ParallelTest.java    From exificient with MIT License 5 votes vote down vote up
private void testParallelDecode(ParallelExiFactory parallelExiFactory,
		int nbThreads, int nbTask) throws Exception {
	EXIFactory exiFactory = getExiFactory();
	ArrayList<ExiSch> nc1NameExiMap = getExiSch(parallelExiFactory, true,
			exiFactory);
	Collection<Callable<DecodeResult>> tasks = new ArrayList<Callable<DecodeResult>>();
	for (int i = 0; i < nbTask; i++) {
		tasks.add(new TaskDecode(
				nc1NameExiMap.get(i % nc1NameExiMap.size())));
	}

	ExecutorService executor = Executors.newFixedThreadPool(nbThreads);
	List<Future<DecodeResult>> results = executor.invokeAll(tasks);
	int differentExiCount = 0;

	XMLUnit.setIgnoreWhitespace(true);
	XMLUnit.setIgnoreAttributeOrder(true);
	XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
	XMLUnit.setIgnoreComments(true);

	for (Future<DecodeResult> result : results) {
		DecodeResult exiResult = result.get();

		Diff diff = compareXML(exiResult.roundTripDoc, exiResult.doc);
		if (!diff.similar()) {
			differentExiCount++;
		}
	}
	executor.shutdown();
	assertEquals(0, differentExiCount);
}
 
Example 4
Source File: XMLUtils.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean different(Element e1, Element e2) throws IOException, SAXException
{
	// logger.debug("e1="+Utils.element2String(e1, false));
	// logger.debug("e2="+Utils.element2String(e2, false));
	Diff diff = new Diff(Utils.element2String(e1, false), Utils.element2String(e2, false));
	boolean similiar = diff.similar();
	// logger.debug("identical="+diff.identical()+", similar="+similiar);
	return !similiar;
}
 
Example 5
Source File: XSD.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(XSD x) {
	if (x == null) return 1;
	if (namespace != null && x.namespace != null) {
		int c = namespace.compareTo(x.namespace);
		if (c != 0) return c;
	}
	if (wsdlSchema != null || url == null || (url.toString().compareTo(x.url.toString()) != 0)) {
		// Compare XSD content to prevent copies of the same XSD showing up
		// more than once in the WSDL. For example the
		// CommonMessageHeader.xsd used by the EsbSoapValidator will
		// normally also be imported by the XSD for the business response
		// message (for the Result part).
		try {
			InputSource control = new InputSource(getInputStream());
			InputSource test = new InputSource(x.getInputStream());
			Diff diff = new Diff(control, test);
			if (diff.similar()) {
				return 0;
			} else if (wsdlSchema != null || url == null) {
				return Misc.streamToString(getInputStream(), "\n", false).compareTo(Misc.streamToString(x.getInputStream(), "\n", false));
			}
		} catch (Exception e) {
			LOG.warn("Exception during XSD compare", e);
		}
	}
	return url.toString().compareTo(x.url.toString());
}
 
Example 6
Source File: TestCreoleAnnotationHandler.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Take a skeleton creole.xml file, process the annotations on the classes it
 * mentions and compare the resulting XML to the expected result.
 */
public void testCreoleAnnotationHandler() throws Exception {
  URL originalUrl = new URL(TestDocument.getTestServerName()+"tests/creole-annotation-handler/initial-creole.xml");
  org.jdom.Document creoleXml =
    jdomBuilder.build(originalUrl.openStream());
  CreoleAnnotationHandler processor = new CreoleAnnotationHandler(new Plugin.Directory(new URL(TestDocument.getTestServerName()+"tests/creole-annotation-handler/")));
  processor.processAnnotations(creoleXml);

  URL expectedURL = new URL(TestDocument.getTestServerName()+"tests/creole-annotation-handler/expected-creole.xml");

  // XMLUnit requires the expected and actual results as W3C DOM rather than
  // JDOM
  DocumentBuilder docBuilder = jaxpFactory.newDocumentBuilder();
  org.w3c.dom.Document targetXmlDOM =
    docBuilder.parse(expectedURL.openStream());

  org.w3c.dom.Document actualXmlDOM = jdom2dom.output(creoleXml);

  Diff diff = XMLUnit.compareXML(targetXmlDOM, actualXmlDOM);

  // compare parameter elements with the same NAME, resources with the same
  // CLASS, and all other elements that have the same element name
  diff.overrideElementQualifier(new ElementNameQualifier() {
    @Override
    public boolean qualifyForComparison(Element control, Element test) {
      if("PARAMETER".equals(control.getTagName()) && "PARAMETER".equals(test.getTagName())) {
        return control.getAttribute("NAME").equals(test.getAttribute("NAME"));
      }
      else if("RESOURCE".equals(control.getTagName()) && "RESOURCE".equals(test.getTagName())) {
        String controlClass = findClass(control);
        String testClass = findClass(test);
        return (controlClass == null) ? (testClass == null)
                  : controlClass.equals(testClass);
      }
      else {
        return super.qualifyForComparison(control, test);
      }
    }

    private String findClass(Element resource) {
      Node node = resource.getFirstChild();
      while(node != null && !"CLASS".equals(node.getNodeName())) {
        node = node.getNextSibling();
      }

      if(node != null) {
        return node.getTextContent();
      }
      else {
        return null;
      }
    }
  });

  // do the comparison!
  boolean match = diff.similar();
  if(!match) {
    // if comparison failed, output the "actual" result document for
    // debugging purposes
    System.err.println("---------actual-----------");   	
    new XMLOutputter(Format.getPrettyFormat()).output(creoleXml, System.err);
  }

  assertTrue("XML produced by annotation handler does not match expected: "
      + diff.toString() , match);
}