org.xmlunit.builder.DiffBuilder Java Examples

The following examples show how to use org.xmlunit.builder.DiffBuilder. 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: PlaceholderDifferenceEvaluatorTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void regression_NoPlaceholder_Attributes_Different() throws Exception {
    String control = "<elem1 attr='123'/>";
    String test = "<elem1 attr='abc'/>";
    Diff diff = DiffBuilder.compare(control).withTest(test)
            .withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()).build();

    assertTrue(diff.hasDifferences());
    int count = 0;
    Iterator it = diff.getDifferences().iterator();
    while (it.hasNext()) {
        count++;
        Difference difference = (Difference) it.next();
        assertEquals(ComparisonResult.DIFFERENT, difference.getResult());
    }
    assertEquals(1, count);
}
 
Example #2
Source File: MetadataTransferTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
private void assertMetadataEquals( String expectedMetadataXml, Path actualFile )
    throws Exception
{
    assertNotNull( "Actual File should not be null.", actualFile );

    assertTrue( "Actual file exists.", Files.exists(actualFile) );

    StringWriter actualContents = new StringWriter();
    FilesystemStorage fsStorage = new FilesystemStorage(actualFile.getParent(), new DefaultFileLockManager());
    StorageAsset actualFileAsset = fsStorage.getAsset(actualFile.getFileName().toString());
    ArchivaRepositoryMetadata metadata = metadataTools.getMetadataReader( null ).read( actualFileAsset );
    RepositoryMetadataWriter.write( metadata, actualContents );

    Diff detailedDiff = DiffBuilder.compare( expectedMetadataXml).withTest( actualContents.toString() ).checkForSimilar().build();
    if ( detailedDiff.hasDifferences() )
    {
        for ( Difference diff : detailedDiff.getDifferences() )
        {
            System.out.println( diff );
        }
        assertEquals( expectedMetadataXml, actualContents );
    }

    // assertEquals( "Check file contents.", expectedMetadataXml, actualContents );
}
 
Example #3
Source File: MetadataToolsTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
private void assertMetadata( String expectedMetadata, ManagedRepositoryContent repository,
                             ItemSelector reference )
    throws LayoutException, IOException, SAXException, ParserConfigurationException
{
    Path metadataFile = repository.getRepository().getRoot().getFilePath().resolve( tools.toPath( reference ) );
    String actualMetadata = org.apache.archiva.common.utils.FileUtils.readFileToString( metadataFile, Charset.defaultCharset() );

    Diff detailedDiff = DiffBuilder.compare( expectedMetadata ).withTest( actualMetadata ).checkForSimilar().build();
    if ( detailedDiff.hasDifferences() )
    {
        for ( Difference diff : detailedDiff.getDifferences() ) {
            System.out.println( diff );
        }
        // If it isn't similar, dump the difference.
        assertEquals( expectedMetadata, actualMetadata );
    }
}
 
Example #4
Source File: DmnModelTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
protected void assertModelEqualsFile(String expectedPath) throws Exception {
  File actualFile = tmpFolder.newFile();
  Dmn.writeModelToFile(actualFile, modelInstance);

  File expectedFile = ReflectUtil.getResourceAsFile(expectedPath);

  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  Document actualDocument = docBuilder.parse(actualFile);
  Document expectedDocument = docBuilder.parse(expectedFile);

  Diff diff = DiffBuilder.compare(expectedDocument).withTest(actualDocument)
    .withNodeFilter(new Java9CDataWhitespaceFilter())
    .checkForSimilar()
    .build();

  if (diff.hasDifferences()) {

    String failMsg = "XML differs:\n" + diff.getDifferences() +
        "\n\nActual XML:\n" + Dmn.convertToString(modelInstance);
    fail(failMsg);
  }
}
 
Example #5
Source File: XmlSchemaTestHelper.java    From syndesis with Apache License 2.0 6 votes vote down vote up
private static void compareMatchingElement(List<Element> sourceChildNodes, Element element) {
    final String type = element.getLocalName();
    final String name = element.getAttribute(NAME_ATTRIBUTE);
    boolean matchFound = false;
    for (Element sourceElement : sourceChildNodes) {
        if (type.equals(sourceElement.getLocalName()) &&
            name.equals(sourceElement.getAttribute(NAME_ATTRIBUTE))) {
            // compare matching element
            Diff diff = DiffBuilder.compare(Input.fromNode(sourceElement))
                .withTest(Input.fromNode(sourceElement))
                .ignoreComments()
                .ignoreWhitespace()
                .checkForIdentical()
                .build();
            assertThat(diff.hasDifferences())
                .overridingErrorMessage("Schema differences " + diff.toString())
                .isFalse();
            matchFound = true;
        }
    }

    if (!matchFound) {
        fail(String.format("Missing source element %s[name=%s]", type, name));
    }
}
 
Example #6
Source File: MavenMetadataGeneratorTests.java    From artifactory-resource with Apache License 2.0 6 votes vote down vote up
private Condition<File> xmlContent(URL expected) {
	return new Condition<File>("XML Content") {

		@Override
		public boolean matches(File actual) {
			Diff diff = DiffBuilder.compare(Input.from(expected)).withTest(Input.from(actual)).checkForSimilar()
					.ignoreWhitespace().build();
			if (diff.hasDifferences()) {
				try {
					String content = new String(FileCopyUtils.copyToByteArray(actual));
					throw new AssertionError(diff.toString() + "\n" + content);
				}
				catch (IOException ex) {
					throw new IllegalStateException(ex);
				}
			}
			return true;
		}

	};
}
 
Example #7
Source File: MetadataToolsTest.java    From archiva with Apache License 2.0 6 votes vote down vote up
private void assertProjectMetadata( String expectedMetadata, ManagedRepositoryContent repository,
                             ItemSelector reference )
    throws LayoutException, IOException, SAXException, ParserConfigurationException
{
    Path metadataFile = repository.getRepository().getRoot().getFilePath().resolve(tools.toPath( reference ) );
    String actualMetadata = org.apache.archiva.common.utils.FileUtils.readFileToString( metadataFile, Charset.defaultCharset() );

    Diff detailedDiff = DiffBuilder.compare( expectedMetadata ).withTest( actualMetadata ).checkForSimilar().build();
    if ( detailedDiff.hasDifferences() )
    {
        for ( Difference diff : detailedDiff.getDifferences() ) {
            System.out.println( diff );
        }
        // If it isn't similar, dump the difference.
        assertEquals( expectedMetadata, actualMetadata );
    }
}
 
Example #8
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_XML_VERSION() {
    // prepare data
    Diff diff = DiffBuilder.compare("<?xml version=\"1.0\"?><a/>").withTest("<?xml version=\"1.1\"?><a/>").build();
    assertPreRequirements(diff, ComparisonType.XML_VERSION);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected xml version '1.0' but was '1.1' - "
            + "comparing <a...> at / to <?xml version=\"1.1\"?><a...> at /", description);

    assertEquals("<a/>", controlDetails);
    assertEquals("<?xml version=\"1.1\"?>\n<a/>", testDetails);
}
 
Example #9
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_XML_STANDALONE() {
    // prepare data
    Diff diff = DiffBuilder.compare(
        "<?xml version=\"1.0\" standalone=\"yes\"?><a b=\"x\"><b/></a>")
        .withTest("<?xml version=\"1.0\" standalone=\"no\"?><a b=\"x\"><b/></a>").build();
    assertPreRequirements(diff, ComparisonType.XML_STANDALONE);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected xml standalone 'true' but was 'false' - "
            + "comparing <?xml version=\"1.0\" standalone=\"yes\"?><a...> at / to <a...> at /", description);

    assertEquals("<?xml version=\"1.0\" standalone=\"yes\"?>\n<a b=\"x\">\n  ...", controlDetails);
    assertEquals("<a b=\"x\">\n  ...", testDetails);
}
 
Example #10
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_HAS_DOCTYPE_DECLARATION() throws Exception {
    // prepare data
    DocumentBuilderFactory dbf = getDocumentBuilderFactoryWithoutValidation();
    Document controlDoc = Convert.toDocument(Input.fromString("<!DOCTYPE Book><a/>").build(), dbf);

    Diff diff = DiffBuilder.compare(controlDoc).withTest("<a/>")
        .withDifferenceEvaluator(DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.CHILD_NODELIST_LENGTH))
        .withNodeFilter(NodeFilters.AcceptAll).build();
    assertPreRequirements(diff, ComparisonType.HAS_DOCTYPE_DECLARATION);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected has doctype declaration 'true' but was 'false' - "
            + "comparing <!DOCTYPE Book><a...> at / to <a...> at /", description);

    assertEquals("<!DOCTYPE Book>\n<a/>", controlDetails);
    assertEquals("<a/>", testDetails);
}
 
Example #11
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_DOCTYPE_NAME() throws Exception {
    // prepare data
    DocumentBuilderFactory dbf = getDocumentBuilderFactoryWithoutValidation();
    Document controlDoc = Convert.toDocument(Input.fromString("<!DOCTYPE Book ><a/>").build(), dbf);
    Document testDoc = Convert.toDocument(Input.fromString("<!DOCTYPE XY ><a/>").build(), dbf);

    Diff diff = DiffBuilder.compare(controlDoc).withTest(testDoc)
        .withDifferenceEvaluator(DifferenceEvaluators.downgradeDifferencesToEqual(ComparisonType.CHILD_NODELIST_LENGTH))
        .withNodeFilter(NodeFilters.AcceptAll).build();
    assertPreRequirements(diff, ComparisonType.DOCTYPE_NAME);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected doctype name 'Book' but was 'XY' - "
            + "comparing <!DOCTYPE Book><a...> at / to <!DOCTYPE XY><a...> at /", description);

    assertEquals("<!DOCTYPE Book>\n<a/>", controlDetails);
    assertEquals("<!DOCTYPE XY>\n<a/>", testDetails);
}
 
Example #12
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_SCHEMA_LOCATION() {
    // prepare data
    Diff diff = DiffBuilder
            .compare("<a xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                    + "xsi:schemaLocation=\"http://www.publishing.org Book.xsd\"/>")
                    .withTest("<a />").build();
    assertPreRequirements(diff, ComparisonType.SCHEMA_LOCATION);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected schema location 'http://www.publishing.org Book.xsd' but was 'null' - "
            + "comparing <a...> at /a[1] to <a...> at /a[1]", description);

    assertEquals("<a xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                    + "xsi:schemaLocation=\"http://www.publishing.org Book.xsd\"/>", controlDetails);
    assertEquals("<a/>", testDetails);
}
 
Example #13
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_NODE_TYPE_similar() {
    // prepare data
    Diff diff = DiffBuilder.compare("<a>Text</a>").withTest("<a><![CDATA[Text]]></a>").build();
    assertPreRequirements(diff, ComparisonType.NODE_TYPE);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected node type 'Text' but was 'CDATA Section' - "
            + "comparing <a ...>Text</a> at /a[1]/text()[1] "
            + "to <a ...><![CDATA[Text]]></a> at /a[1]/text()[1]",
            description);

    assertEquals("<a>Text</a>", controlDetails);
    if (JAVA_9_PLUS && !JAVA_14_PLUS) {
        assertEquals("<a>\n  <![CDATA[Text]]>\n</a>", testDetails);
    } else {
        assertEquals("<a><![CDATA[Text]]></a>", testDetails);
    }
}
 
Example #14
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_NAMESPACE_PREFIX() {
    // prepare data
    Diff diff = DiffBuilder.compare(
        "<ns1:a xmlns:ns1=\"test\">Text</ns1:a>").withTest("<test:a xmlns:test=\"test\">Text</test:a>").build();
    assertPreRequirements(diff, ComparisonType.NAMESPACE_PREFIX);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected namespace prefix 'ns1' but was 'test' - "
            + "comparing <ns1:a...> at /a[1] to <test:a...> at /a[1]", description);

    assertEquals("<ns1:a xmlns:ns1=\"test\">Text</ns1:a>", controlDetails);
    assertEquals("<test:a xmlns:test=\"test\">Text</test:a>", testDetails);
}
 
Example #15
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_NAMESPACE_URI() {
    // prepare data
    Diff diff = DiffBuilder.compare(
        "<test:a xmlns:test=\"test.org\">Text</test:a>")
        .withTest("<test:a xmlns:test=\"test.net\">Text</test:a>").build();
    assertPreRequirements(diff, ComparisonType.NAMESPACE_URI);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected namespace uri 'test.org' but was 'test.net' - "
            + "comparing <test:a...> at /a[1] to <test:a...> at /a[1]", description);

    assertEquals("<test:a xmlns:test=\"test.org\">Text</test:a>", controlDetails);
    assertEquals("<test:a xmlns:test=\"test.net\">Text</test:a>", testDetails);
}
 
Example #16
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_TEXT_VALUE() {
    // prepare data
    Diff diff = DiffBuilder.compare("<a>Text one</a>").withTest("<a>Text two</a>").build();
    assertPreRequirements(diff, ComparisonType.TEXT_VALUE);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected text value 'Text one' but was 'Text two' - "
        + "comparing <a ...>Text one</a> at /a[1]/text()[1] "
        + "to <a ...>Text two</a> at /a[1]/text()[1]", description);

    assertEquals("<a>Text one</a>", controlDetails);
    assertEquals("<a>Text two</a>", testDetails);
}
 
Example #17
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_PROCESSING_INSTRUCTION_TARGET() {
    // prepare data
    Diff diff = DiffBuilder.compare(
        "<?xml-stylesheet type=\"text/xsl\" href=\"animal.xsl\" ?><a>Text one</a>")
        .withTest("<?xml-xy type=\"text/xsl\" href=\"animal.xsl\" ?><a>Text one</a>").build();
    assertPreRequirements(diff, ComparisonType.PROCESSING_INSTRUCTION_TARGET);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected processing instruction target 'xml-stylesheet' but was 'xml-xy' - "
        + "comparing <?xml-stylesheet type=\"text/xsl\" href=\"animal.xsl\" ?> at /processing-instruction()[1] "
        + "to <?xml-xy type=\"text/xsl\" href=\"animal.xsl\" ?> at /processing-instruction()[1]", description);

    assertEquals("<?xml-stylesheet type=\"text/xsl\" href=\"animal.xsl\" ?>", controlDetails);
    assertEquals("<?xml-xy type=\"text/xsl\" href=\"animal.xsl\" ?>", testDetails);
}
 
Example #18
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_PROCESSING_INSTRUCTION_DATA() {
    // prepare data
    Diff diff = DiffBuilder.compare("<?xml-stylesheet type=\"text/xsl\" href=\"animal.xsl\" ?><a>Text one</a>")
            .withTest("<?xml-stylesheet type=\"text/xsl\" href=\"animal.css\" ?><a>Text one</a>")
            .build();
    assertPreRequirements(diff, ComparisonType.PROCESSING_INSTRUCTION_DATA);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected processing instruction data 'type=\"text/xsl\" href=\"animal.xsl\" ' "
        + "but was 'type=\"text/xsl\" href=\"animal.css\" ' - "
        + "comparing <?xml-stylesheet type=\"text/xsl\" href=\"animal.xsl\" ?> at /processing-instruction()[1] "
        + "to <?xml-stylesheet type=\"text/xsl\" href=\"animal.css\" ?> at /processing-instruction()[1]", description);

    assertEquals("<?xml-stylesheet type=\"text/xsl\" href=\"animal.xsl\" ?>", controlDetails);
    assertEquals("<?xml-stylesheet type=\"text/xsl\" href=\"animal.css\" ?>", testDetails);
}
 
Example #19
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_ATTR_VALUE_EXPLICITLY_SPECIFIED() {
    // prepare data
    Diff diff = DiffBuilder.compare(
            "<?xml version=\"1.0\" ?>" +
            "<!DOCTYPE root [<!ELEMENT root ANY><!ATTLIST root c CDATA #FIXED \"xxx\">]>" +
            "<root/>")
            .withTest("<?xml version=\"1.0\" ?>" +
            "<!DOCTYPE root [<!ELEMENT root ANY><!ATTLIST root c CDATA #FIXED \"xxx\">]>" +
            "<root c=\"xxx\"/>").build();
    assertPreRequirements(diff, ComparisonType.ATTR_VALUE_EXPLICITLY_SPECIFIED);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected attribute value explicitly specified 'false' but was 'true' - "
        + "comparing <root c=\"xxx\"...> at /root[1]/@c to <root c=\"xxx\"...> at /root[1]/@c", description);

    assertEquals("<root c=\"xxx\"/>", controlDetails);
    assertEquals("<root c=\"xxx\"/>", testDetails);
}
 
Example #20
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_ELEMENT_NUM_ATTRIBUTES() {
    // prepare data
    Diff diff = DiffBuilder.compare("<a b=\"xxx\"></a>")
            .withTest("<a b=\"xxx\" c=\"xxx\"></a>").build();
    assertPreRequirements(diff, ComparisonType.ELEMENT_NUM_ATTRIBUTES);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected number of attributes '1' but was '2' - "
        + "comparing <a...> at /a[1] to <a...> at /a[1]", description);

    assertEquals("<a b=\"xxx\"/>", controlDetails);
    assertEquals("<a b=\"xxx\" c=\"xxx\"/>", testDetails);
}
 
Example #21
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_ATTR_VALUE() {
    // prepare data
    Diff diff = DiffBuilder.compare("<a b=\"xxx\"></a>").withTest("<a b=\"yyy\"></a>").build();
    assertPreRequirements(diff, ComparisonType.ATTR_VALUE);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected attribute value 'xxx' but was 'yyy' - "
        + "comparing <a b=\"xxx\"...> at /a[1]/@b to <a b=\"yyy\"...> at /a[1]/@b", description);

    assertEquals("<a b=\"xxx\"/>", controlDetails);
    assertEquals("<a b=\"yyy\"/>", testDetails);
}
 
Example #22
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_CHILD_NODELIST_LENGTH() {
    // prepare data
    Diff diff = DiffBuilder.compare("<a><b/></a>").withTest("<a><b/><c/></a>").build();
    assertPreRequirements(diff, ComparisonType.CHILD_NODELIST_LENGTH);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected child nodelist length '1' but was '2' - "
        + "comparing <a...> at /a[1] to <a...> at /a[1]", description);

    assertEquals("<a>\n  <b/>\n</a>", controlDetails);
    assertEquals("<a>\n  <b/>\n  <c/>\n</a>", testDetails);
}
 
Example #23
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_CHILD_NODELIST_SEQUENCE() {
    // prepare data
    Diff diff = DiffBuilder.compare("<a><b>XXX</b><b>YYY</b></a>").withTest("<a><b>YYY</b><b>XXX</b></a>")
        .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
        .build();
    assertPreRequirements(diff, ComparisonType.CHILD_NODELIST_SEQUENCE);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected child nodelist sequence '0' but was '1' - "
        + "comparing <b...> at /a[1]/b[1] to <b...> at /a[1]/b[2]", description);

    assertEquals("<a>\n  <b>XXX</b>\n  <b>YYY</b>\n</a>", controlDetails);
    assertEquals("<a>\n  <b>YYY</b>\n  <b>XXX</b>\n</a>", testDetails);
}
 
Example #24
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_CHILD_LOOKUP() {
    // prepare data
    Diff diff = DiffBuilder.compare("<a>Text</a>").withTest("<a><Element/></a>").build();
    assertPreRequirements(diff, ComparisonType.CHILD_LOOKUP);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected child '#text' but was 'null' - "
        + "comparing <a ...>Text</a> at /a[1]/text()[1] to <NULL>",
            description);

    assertEquals("<a>Text</a>", controlDetails);
    assertEquals("<NULL>", testDetails);
}
 
Example #25
Source File: DefaultComparisonFormatterTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void testComparisonType_ATTR_NAME_LOOKUP() {
    // prepare data
    Diff diff = DiffBuilder.compare("<a b=\"xxx\"></a>").withTest("<a c=\"yyy\"></a>").build();
    assertPreRequirements(diff, ComparisonType.ATTR_NAME_LOOKUP);
    Comparison firstDiff = diff.getDifferences().iterator().next().getComparison();

    // run test
    String description = compFormatter.getDescription(firstDiff);
    String controlDetails =  getDetails(firstDiff.getControlDetails(), firstDiff.getType());
    String testDetails =  getDetails(firstDiff.getTestDetails(), firstDiff.getType());

    // validate result
    Assert.assertEquals("Expected attribute name '/a[1]/@b' - "
        + "comparing <a...> at /a[1]/@b to <a...> at /a[1]", description);

    assertEquals("<a b=\"xxx\"/>", controlDetails);
    assertEquals("<a c=\"yyy\"/>", testDetails);
}
 
Example #26
Source File: DOMDifferenceEngineTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void xPathKnowsAboutNodeFiltersForUnmatchedTestNodes() {
    final Diff diff = DiffBuilder.compare("<Document><Section><Binding /><Binding /><Finding /><Finding /><Finding /><Finding /><Finding /><Finding /></Section></Document>")
        .withTest("<Document><Section><Binding /><Binding /><Finding /><Finding /><Finding /><Finding /><Finding /><Finding /><Finding /></Section></Document>")
        .ignoreWhitespace()
        .withNodeFilter(new Predicate<Node>() {
            @Override
            public boolean test(final Node node) {
                return Arrays.asList("Document", "Section", "Finding").contains(node.getNodeName());
            }
        })
        .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
        .build();
    final List<Difference> differences = Linqy.asList(diff.getDifferences());
    assertEquals(2, differences.size());
    assertEquals(ComparisonType.CHILD_NODELIST_LENGTH, differences.get(0).getComparison().getType());
    assertEquals("/Document[1]/Section[1]", differences.get(0).getComparison().getControlDetails().getXPath());
    assertEquals("/Document[1]/Section[1]", differences.get(0).getComparison().getTestDetails().getXPath());
    assertEquals(ComparisonType.CHILD_LOOKUP, differences.get(1).getComparison().getType());
    assertEquals("/Document[1]/Section[1]/Finding[7]", differences.get(1).getComparison().getTestDetails().getXPath());
    assertNull(differences.get(1).getComparison().getControlDetails().getXPath());
    assertEquals("/Document[1]/Section[1]", differences.get(1).getComparison().getControlDetails().getParentXPath());
}
 
Example #27
Source File: PlaceholderDifferenceEvaluatorTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void regression_NoPlaceholder_Different() throws Exception {
    String control = "<elem1><elem11>123</elem11></elem1>";
    String test = "<elem1><elem11>abc</elem11></elem1>";
    Diff diff = DiffBuilder.compare(control).withTest(test)
            .withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()).build();

    assertTrue(diff.hasDifferences());
    int count = 0;
    Iterator it = diff.getDifferences().iterator();
    while (it.hasNext()) {
        count++;
        Difference difference = (Difference) it.next();
        assertEquals(ComparisonResult.DIFFERENT, difference.getResult());
    }
    assertEquals(1, count);
}
 
Example #28
Source File: PlaceholderDifferenceEvaluatorTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void hasMatchesRegexPlaceholder_Element_Exception_MalformedRegex() {
    String control = "<elem1>${xmlunit.matchesRegex[^(\\d+$]}</elem1>";
    String test = "<elem1>23abc</elem1>";
    DiffBuilder diffBuilder = DiffBuilder.compare(control).withTest(test)
            .withDifferenceEvaluator(new PlaceholderDifferenceEvaluator(null, null, Pattern.quote("["), Pattern.quote("]"), null));

    try {
        diffBuilder.build();
        fail();
    } catch (XMLUnitException e) {
        assertThat(e.getCause().getMessage(), containsString("Unclosed group near index"));
    }
}
 
Example #29
Source File: PlaceholderDifferenceEvaluatorTest.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
@Test
public void hasIgnorePlaceholder_Equal_StartAndEndWhitespacesInPlaceholder() throws Exception {
    String control = "<elem1><elem11>${  xmlunit.ignore  }</elem11></elem1>";
    String test = "<elem1><elem11>abc</elem11></elem1>";
    Diff diff = DiffBuilder.compare(control).withTest(test)
            .withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()).build();

    assertFalse(diff.hasDifferences());
}
 
Example #30
Source File: PlaceholderDifferenceEvaluatorTest.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
@Test
public void isDateTimePlaceholder_Attribute_IsDateTime() {
    String control = "<elem1 attr='${xmlunit.isDateTime}'/>";
    String test = "<elem1 attr='2020-01-01 15:00:00Z'/>";
    Diff diff = PlaceholderSupport.withPlaceholderSupport(DiffBuilder.compare(control).withTest(test)).build();

    assertFalse(diff.hasDifferences());
}