org.xmlunit.builder.Input Java Examples

The following examples show how to use org.xmlunit.builder.Input. 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: StaEDIXMLStreamReaderTest.java    From staedi with Apache License 2.0 8 votes vote down vote up
@Test
void testReadXml() throws Exception {
    XMLStreamReader xmlReader = getXmlReader("/x12/extraDelimiter997.edi");
    xmlReader.next(); // Per StAXSource JavaDoc, put in START_DOCUMENT state
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter result = new StringWriter();
    transformer.transform(new StAXSource(xmlReader), new StreamResult(result));
    String resultString = result.toString();
    Diff d = DiffBuilder.compare(Input.fromFile("src/test/resources/x12/extraDelimiter997.xml"))
                        .withTest(resultString).build();
    assertTrue(!d.hasDifferences(), () -> "XML unexpectedly different:\n" + d.toString(new DefaultComparisonFormatter()));
}
 
Example #2
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void doesNotAddDuplicateRelationToSamePredicate() throws Exception {
  testRdfDescriptionSaver.addRelation(BASE_URI,
    "http://purl.org/dc/terms/rightsHolder", "http://example.com/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.addRelation(BASE_URI,
    "http://purl.org/dc/terms/rightsHolder", "http://example.com/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <rightsHolder xmlns=\"http://purl.org/dc/terms/\" " +
        "       rdf:resource=\"http://example.com/rightsHolder\" /> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();


  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );
}
 
Example #3
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void doesNotDeleteValueWhenWhenSubjectUriDoesNotMatchBaseUri() throws Exception {
  testRdfDescriptionSaver.addValue(BASE_URI, "http://purl.org/dc/terms/title", "DWC Data", null, BASE_URI);
  testRdfDescriptionSaver.delValue("http://example.org/datasets/DUMMY/clusius10",
    "http://purl.org/dc/terms/title", "DWC Data", null, BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
        ">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <title xmlns=\"http://purl.org/dc/terms/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" +
        "DWC Data</title> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();

  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );
}
 
Example #4
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 #5
Source File: StaEDIXMLStreamReaderTest.java    From staedi with Apache License 2.0 6 votes vote down vote up
@Test
void testTransactionElementWithXmlns() throws Exception {
    EDIInputFactory ediFactory = EDIInputFactory.newFactory();
    ediFactory.setProperty(EDIInputFactory.XML_DECLARE_TRANSACTION_XMLNS, Boolean.TRUE);
    InputStream stream = getClass().getResourceAsStream("/x12/extraDelimiter997.edi");
    ediReader = ediFactory.createEDIStreamReader(stream);
    XMLStreamReader xmlReader = ediFactory.createXMLStreamReader(ediReader);

    xmlReader.next(); // Per StAXSource JavaDoc, put in START_DOCUMENT state
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter result = new StringWriter();
    transformer.transform(new StAXSource(xmlReader), new StreamResult(result));
    String resultString = result.toString();
    Diff d = DiffBuilder.compare(Input.fromFile("src/test/resources/x12/extraDelimiter997-transaction-xmlns.xml"))
                        .withTest(resultString).build();
    assertTrue(!d.hasDifferences(), () -> "XML unexpectedly different:\n" + d.toString(new DefaultComparisonFormatter()));
}
 
Example #6
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 #7
Source File: XmlMultiConfigurationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleConfigurationsFromXml() throws URISyntaxException {
  URL resource = XmlConfigurationTest.class.getResource("/configs/multi/multiple-configs.xml");

  XmlMultiConfiguration xmlMultiConfiguration = XmlMultiConfiguration.from(resource).build();

  assertThat(xmlMultiConfiguration.configuration("foo").getCacheConfigurations(), hasKey("foo"));
  assertThat(xmlMultiConfiguration.configuration("foo", "prod").getCacheConfigurations(), hasKey("foo"));
  assertThat(xmlMultiConfiguration.configuration("bar").getCacheConfigurations(), hasKey("bar"));
  assertThat(xmlMultiConfiguration.configuration("bar", "prod").getCacheConfigurations(), hasKey("bar"));

  assertThat(xmlMultiConfiguration.identities(), containsInAnyOrder("foo", "bar"));
  assertThat(xmlMultiConfiguration.variants("foo"), empty());
  assertThat(xmlMultiConfiguration.variants("bar"), empty());

  assertThat(xmlMultiConfiguration.toString(), isSimilarTo(Input.fromURI(resource.toURI())).ignoreWhitespace().ignoreComments());
}
 
Example #8
Source File: XmlMultiConfigurationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleVariantsFromXml() throws URISyntaxException {
  URL resource = XmlConfigurationTest.class.getResource("/configs/multi/multiple-variants.xml");

  XmlMultiConfiguration xmlMultiConfiguration = XmlMultiConfiguration.from(resource).build();

  assertThrows(() -> xmlMultiConfiguration.configuration("foo"), IllegalStateException.class);

  assertThat(xmlMultiConfiguration.configuration("foo", "development").getCacheConfigurations(), hasKey("foo-dev"));
  assertThat(xmlMultiConfiguration.configuration("foo", "production").getCacheConfigurations(), hasKey("foo-prod"));
  assertThat(xmlMultiConfiguration.configuration("bar", "development").getCacheConfigurations(), hasKey("bar-dev"));
  assertThat(xmlMultiConfiguration.configuration("bar", "production").getCacheConfigurations(), hasKey("bar-prod"));

  assertThat(xmlMultiConfiguration.identities(), containsInAnyOrder("foo", "bar"));
  assertThat(xmlMultiConfiguration.variants("foo"), containsInAnyOrder("development", "production"));
  assertThat(xmlMultiConfiguration.variants("bar"), containsInAnyOrder("development", "production"));

  assertThat(xmlMultiConfiguration.toString(), isSimilarTo(Input.fromURI(resource.toURI())).ignoreWhitespace().ignoreComments());
}
 
Example #9
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void replacesValue() throws Exception {
  testRdfDescriptionSaver.addValue(BASE_URI, "http://purl.org/dc/terms/title", "DWC Data", null, BASE_URI);
  testRdfDescriptionSaver.delValue(BASE_URI, "http://purl.org/dc/terms/title", "DWC Data", null, BASE_URI);
  testRdfDescriptionSaver.addValue(BASE_URI, "http://purl.org/dc/terms/title", "DWC Data New", null, BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
        ">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <title xmlns=\"http://purl.org/dc/terms/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" +
        "DWC Data New</title> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();

  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );
}
 
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: DOMDifferenceEngineTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void nodeFilterAppliesToDocTypes() {
    DOMDifferenceEngine d = new DOMDifferenceEngine();
    DiffExpecter ex = new DiffExpecter(ComparisonType.HAS_DOCTYPE_DECLARATION);
    d.addDifferenceListener(ex);
    d.setComparisonController(ComparisonControllers.StopWhenDifferent);
    Document d1 = Convert.toDocument(Input.fromString("<Book/>").build());
    Document d2 =
        Convert.toDocument(Input.fromString("<!DOCTYPE Book PUBLIC "
                + "\"XMLUNIT/TEST/PUB\" "
                + "\"" + TestResources.BOOK_DTD
                + "\">"
                + "<Book/>")
                           .build());
    assertEquals(wrap(ComparisonResult.EQUAL),
                 d.compareNodes(d1, new XPathContext(),
                                d2, new XPathContext()));
    assertEquals(0, ex.invoked);
}
 
Example #13
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void addsRelation() throws Exception {
  testRdfDescriptionSaver.addRelation(BASE_URI,
    "http://purl.org/dc/terms/rightsHolder", "http://example.com/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <rightsHolder xmlns=\"http://purl.org/dc/terms/\" " +
        "       rdf:resource=\"http://example.com/rightsHolder\" /> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();


  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );
}
 
Example #14
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void doesNotAddDuplicateValuesToSamePredicate() throws Exception {
  testRdfDescriptionSaver.addValue(BASE_URI, "http://purl.org/dc/terms/title", "DWC Data", null, BASE_URI);
  testRdfDescriptionSaver.addValue(BASE_URI,
    "http://purl.org/dc/terms/title", "DWC Data",
    null, BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
        ">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <title xmlns=\"http://purl.org/dc/terms/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" +
        "DWC Data</title> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();

  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );
}
 
Example #15
Source File: MultipleNodeAssert.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
static MultipleNodeAssert create(Object xmlSource, Map<String, String> prefix2Uri, DocumentBuilderFactory dbf,
                                 XPathFactory xpf, String xPath) {

    AssertionsAdapter.assertThat(xPath).isNotBlank();

    final JAXPXPathEngine engine = xpf == null ? new JAXPXPathEngine() : new JAXPXPathEngine(xpf);
    if (prefix2Uri != null) {
        engine.setNamespaceContext(prefix2Uri);
    }

    Source s = Input.from(xmlSource).build();
    Node root = dbf != null ? Convert.toNode(s, dbf) : Convert.toNode(s);
    Iterable<Node> nodes = engine.selectNodes(xPath, root);

    return new MultipleNodeAssert(nodes, engine)
            .describedAs("XPath \"%s\" evaluated to node set", xPath);
}
 
Example #16
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void doesNotDeleteRelationWhenSubjectUriDoesNotMatchBaseUri() throws Exception {
  testRdfDescriptionSaver.addRelation(BASE_URI,
    "http://purl.org/dc/terms/rightsHolder", "http://example.com/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.delRelation("http://example.org/datasets/DUMMY/test",
    "http://purl.org/dc/terms/title", "http://purl.org/dc/terms/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <rightsHolder xmlns=\"http://purl.org/dc/terms/\" " +
        "       rdf:resource=\"http://example.com/rightsHolder\" /> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();

  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );

}
 
Example #17
Source File: ValueAssert.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
static ValueAssert create(Object xmlSource, Map<String, String> prefix2Uri, DocumentBuilderFactory dbf,
                          XPathFactory xpf, String xPath) {
    AssertionsAdapter.assertThat(xPath).isNotBlank();

    final JAXPXPathEngine engine = xpf == null ? new JAXPXPathEngine() : new JAXPXPathEngine(xpf);
    if (prefix2Uri != null) {
        engine.setNamespaceContext(prefix2Uri);
    }

    Source s = Input.from(xmlSource).build();
    Node root = dbf != null ? Convert.toNode(s, dbf) : Convert.toNode(s);
    String value = engine.evaluate(xPath, root);

    return new ValueAssert(value)
            .describedAs("XPath \"%s\" evaluated to value", xPath);
}
 
Example #18
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void addsMultipleValues() throws Exception {
  testRdfDescriptionSaver.addValue(BASE_URI, "http://purl.org/dc/terms/title", "DWC Data", null, BASE_URI);
  testRdfDescriptionSaver.addValue(BASE_URI,
    "http://purl.org/dc/terms/description", "Biographical data of the Digital Web Centre for " +
      "the History of Science (DWC)", null, BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <description xmlns=\"http://purl.org/dc/terms/\">" +
        "Biographical data of the Digital Web Centre for the History of Science (DWC)</description> \n" +
        "    <title xmlns=\"http://purl.org/dc/terms/\">DWC Data</title> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();

  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );
}
 
Example #19
Source File: DOMDifferenceEngineTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDetectMissingXsiType() {
    DOMDifferenceEngine d = new DOMDifferenceEngine();
    Document d1 = Convert.toDocument(Input.fromString("<doc xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
                                                      + "<effectiveTime xsi:type=\"IVL_TS\"></effectiveTime></doc>")
                                     .build());
    Document d2 = Convert.toDocument(Input.fromString("<doc xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
                                                      + "<effectiveTime></effectiveTime></doc>")
                                     .build());

    DiffExpecter ex = new DiffExpecter(ComparisonType.ATTR_NAME_LOOKUP,
                                       "/doc[1]/effectiveTime[1]/@type",
                                       "/doc[1]/effectiveTime[1]");
    d.addDifferenceListener(ex);
    d.setComparisonController(ComparisonControllers.StopWhenDifferent);
    assertEquals(wrapAndStop(ComparisonResult.DIFFERENT),
                 d.compareNodes(d1, new XPathContext(),
                                d2, new XPathContext()));
}
 
Example #20
Source File: DOMDifferenceEngineTest.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDetectCommentInPrelude() {
    DOMDifferenceEngine d = new DOMDifferenceEngine();
    Document d1 = Convert.toDocument(Input.fromFile(TestResources.TEST_RESOURCE_DIR
                                                    + "BookXsdGenerated.xml")
                                     .build());
    Document d2 = Convert.toDocument(Input.fromFile(TestResources.TEST_RESOURCE_DIR
                                                    + "BookXsdGeneratedWithComment.xml")
                                     .build());
    DiffExpecter ex = new DiffExpecter(ComparisonType.CHILD_NODELIST_LENGTH,
                                       "/", "/");
    d.addDifferenceListener(ex);
    d.setComparisonController(ComparisonControllers.StopWhenDifferent);
    assertEquals(wrapAndStop(ComparisonResult.DIFFERENT),
                 d.compareNodes(d1, new XPathContext(),
                                d2, new XPathContext()));
}
 
Example #21
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void doesNotDeleteRelationWhenSubjectNull() throws Exception {
  testRdfDescriptionSaver.addRelation(BASE_URI,
    "http://purl.org/dc/terms/rightsHolder", "http://example.com/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.delRelation(null,
    "http://purl.org/dc/terms/title", "http://purl.org/dc/terms/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <rightsHolder xmlns=\"http://purl.org/dc/terms/\" " +
        "       rdf:resource=\"http://example.com/rightsHolder\" /> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();

  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );

}
 
Example #22
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void deletesRelation() throws Exception {
  testRdfDescriptionSaver.addRelation(BASE_URI,
    "http://purl.org/dc/terms/rightsHolder", "http://example.com/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.commit();
  testRdfDescriptionSaver.delRelation(BASE_URI,
    "http://purl.org/dc/terms/rightsHolder", "http://example.com/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    ("<rdf:RDF xmlns=\"http://purl.org/dc/terms/\"\n xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
      "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();

  Source actual = Input.fromFile(descriptionFile).build();

  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );

}
 
Example #23
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void doesNotDeleteValueWhenSubjectNull() throws Exception {
  testRdfDescriptionSaver.addValue(BASE_URI, "http://purl.org/dc/terms/title", "DWC Data", null, BASE_URI);
  testRdfDescriptionSaver.delValue(null, "http://purl.org/dc/terms/title", "DWC Data", null, BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
        ">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <title xmlns=\"http://purl.org/dc/terms/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" +
        "DWC Data</title> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();

  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );
}
 
Example #24
Source File: AbstractXPathEngineTest.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
@Test public void selectNodesWithDefaultNSEmptyPrefix() {
    XPathEngine e = getEngine();
    source = Input.fromString("<d xmlns='urn:test:1'><e/></d>")
        .build();
    HashMap<String, String> m = new HashMap<String, String>();
    m.put("", "urn:test:1");
    e.setNamespaceContext(m);
    Iterable<Node> it = e.selectNodes("/:d/:e", source);
    assertTrue(it.iterator().hasNext());
}
 
Example #25
Source File: RdfDescriptionSaverTest.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void addsMultipleRelation() throws Exception {
  testRdfDescriptionSaver.addRelation(BASE_URI,
    "http://purl.org/dc/terms/rightsHolder", "http://example.com/rightsHolder", BASE_URI);
  testRdfDescriptionSaver.addRelation(BASE_URI,
    "http://purl.org/dc/terms/abstract", "http://example.com/summaryProperties", BASE_URI);
  testRdfDescriptionSaver.addRelation(BASE_URI,
    "http://purl.org/dc/terms/provenance", "http://example.com/provenance", BASE_URI);
  testRdfDescriptionSaver.commit();

  Source expected = Input.fromByteArray(
    (
      "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
        "  <rdf:Description rdf:about=\"http://example.org/datasets/DUMMY/clusius2\">\n" +
        "    <abstract xmlns=\"http://purl.org/dc/terms/\" " +
        "       rdf:resource=\"http://example.com/summaryProperties\" /> \n" +
        "    <provenance xmlns=\"http://purl.org/dc/terms/\" rdf:resource=\"http://example.com/provenance\" /> \n" +
        "    <rightsHolder xmlns=\"http://purl.org/dc/terms/\" " +
        "       rdf:resource=\"http://example.com/rightsHolder\" /> \n" +
        "  </rdf:Description>\n" +
        "</rdf:RDF>\n"
    ).getBytes(StandardCharsets.UTF_8)
  ).build();

  Source actual = Input.fromFile(descriptionFile).build();
  assertThat(actual,
    isSimilarTo(expected).ignoreWhitespace().withComparisonFormatter(new DefaultComparisonFormatter())
  );
}
 
Example #26
Source File: NodesTest.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
private Document handleWsSetup() {
    return Convert.toDocument(Input.fromString(
            "<root>\n"
                    + "<!-- trim\tme -->\n"
                    + "<child attr=' trim me ' attr2='not me'>\n"
                    + " trim me \n"
                    + "</child><![CDATA[ trim me ]]>\n"
                    + "<?target  trim me ?>\n"
                    + "<![CDATA[          ]]>\n"
                    + "</root>").build());
}
 
Example #27
Source File: AbstractConverterTest.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
protected static void compareXml(InputStream expected, InputStream actual) {
    Source control = Input.fromStream(expected).build();
    Source test = Input.fromStream(actual).build();
    Diff myDiff = DiffBuilder.compare(control).withTest(test).ignoreWhitespace().ignoreComments().build();
    boolean hasDiff = myDiff.hasDifferences();
    if (hasDiff) {
        System.err.println(myDiff.toString());
    }
    assertFalse(hasDiff);
}
 
Example #28
Source File: RemoteApiITest.java    From warnings-ng-plugin with MIT License 5 votes vote down vote up
private void assertThatRemoteApiEquals(final Run<?, ?> build, final String url, final String expectedXml) {
    assertThat(callXmlRemoteApi(build.getUrl() + url))
            .and(Input.from(readAllBytes(FOLDER_PREFIX + expectedXml)))
            .ignoreChildNodesOrder()
            .normalizeWhitespace()
            .areIdentical();
}
 
Example #29
Source File: HasXPathMatcher.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(Object object) {
    JAXPXPathEngine engine = xpf == null ? new JAXPXPathEngine() : new JAXPXPathEngine(xpf);
    if (prefix2Uri != null) {
        engine.setNamespaceContext(prefix2Uri);
    }

    Source s = Input.from(object).build();
    Node n = dbf != null ? Convert.toNode(s, dbf) : Convert.toNode(s);
    Iterable<Node> nodes = engine.selectNodes(xPath, n);

    return nodes.iterator().hasNext();
}
 
Example #30
Source File: EvaluateXPathMatcher.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates the provided XML input to the configured <code>xPath</code> field XPath expression.
 * @param input an XML input
 * @return the result of the XPath evaluation
 */
private String xPathEvaluate(Object input) {
    JAXPXPathEngine engine = xpf == null ? new JAXPXPathEngine() : new JAXPXPathEngine(xpf);
    if (prefix2Uri != null) {
        engine.setNamespaceContext(prefix2Uri);
    }

    Source s = Input.from(input).build();
    Node n = dbf != null ? Convert.toNode(s, dbf) : Convert.toNode(s);
    return engine.evaluate(xPath, n);
}