Java Code Examples for org.custommonkey.xmlunit.XMLUnit#setNormalize()

The following examples show how to use org.custommonkey.xmlunit.XMLUnit#setNormalize() . 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: AbstractSourceCodeAwareNodeProcessingTest.java    From jaxb2-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Compares XML documents provided by the two Readers.
 *
 * @param expected The expected document data.
 * @param actual   The actual document data.
 * @return A DetailedDiff object, describing all differences in documents supplied.
 * @throws org.xml.sax.SAXException If a SAXException was raised during parsing of the two Documents.
 * @throws IOException              If an I/O-related exception was raised while acquiring the data from the Readers.
 */
protected static Diff compareXmlIgnoringWhitespace(final String expected, final String actual) throws SAXException,
        IOException {

    // Check sanity
    Validate.notNull(expected, "Cannot handle null expected argument.");
    Validate.notNull(actual, "Cannot handle null actual argument.");

    // Ignore whitespace - and also normalize the Documents.
    XMLUnit.setNormalize(true);
    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalize(true);

    // Compare and return
    return XMLUnit.compareXML(expected, actual);
}
 
Example 2
Source File: StructuredQueryBuilderTest.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void beforeClass() {
  XMLUnit.setIgnoreAttributeOrder(true);
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalize(true);
  XMLUnit.setNormalizeWhitespace(true);
  XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

  Map<String,String> namespaces = new HashMap<>();
  namespaces.put("rapi", "http://marklogic.com/rest-api");
  namespaces.put("prop", "http://marklogic.com/xdmp/property");
  namespaces.put("xs",   "http://www.w3.org/2001/XMLSchema");
  namespaces.put("xsi",  "http://www.w3.org/2001/XMLSchema-instance");
  namespaces.put("search",  "http://marklogic.com/appservices/search");


  SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces);

  xpather = XMLUnit.newXpathEngine();
  xpather.setNamespaceContext(namespaceContext);
}
 
Example 3
Source File: BufferableHandleTest.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void beforeClass() {
  Common.connect();

  XMLUnit.setIgnoreAttributeOrder(true);
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalize(true);
  XMLUnit.setNormalizeWhitespace(true);
  XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

  Map<String,String> namespaces = new HashMap<>();
  namespaces.put("rapi", "http://marklogic.com/rest-api");
  namespaces.put("prop", "http://marklogic.com/xdmp/property");
  namespaces.put("xs",   "http://www.w3.org/2001/XMLSchema");
  namespaces.put("xsi",  "http://www.w3.org/2001/XMLSchema-instance");

  SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces);

  xpather = XMLUnit.newXpathEngine();
  xpather.setNamespaceContext(namespaceContext);
}
 
Example 4
Source File: TransformExtensionsTest.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void beforeClass() throws IOException {
  Common.connect();
  Common.connectAdmin();

  XMLUnit.setIgnoreAttributeOrder(true);
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalize(true);
  XMLUnit.setNormalizeWhitespace(true);
  XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

  Map<String,String> namespaces = new HashMap<>();
  namespaces.put("xsl",  "http://www.w3.org/1999/XSL/Transform");
  namespaces.put("rapi", "http://marklogic.com/rest-api");

  SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces);

  xpather = XMLUnit.newXpathEngine();
  xpather.setNamespaceContext(namespaceContext);

  xqueryTransform = Common.testFileToString(XQUERY_FILE, "UTF-8");
  xslTransform    = Common.testFileToString(XSLT_FILE, "UTF-8");
}
 
Example 5
Source File: ResourceExtensionsTest.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void beforeClass() throws IOException {
  Common.connectAdmin();
  resourceServices = Common.testFileToString(XQUERY_FILE);

  XMLUnit.setIgnoreAttributeOrder(true);
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalize(true);
  XMLUnit.setNormalizeWhitespace(true);
  XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

  Map<String,String> namespaces = new HashMap<>();
  namespaces.put("rapi", "http://marklogic.com/rest-api");

  SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces);

  xpather = XMLUnit.newXpathEngine();
  xpather.setNamespaceContext(namespaceContext);
}
 
Example 6
Source File: DLNAServiceTest.java    From Connect-SDK-Android-Core with Apache License 2.0 5 votes vote down vote up
private void assertXMLEquals(String expectedXML, String actualXML) throws SAXException, IOException {
    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setIgnoreAttributeOrder(true);
    XMLUnit.setNormalize(true);
    DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML));
    List<?> allDifferences = diff.getAllDifferences();
    Assert.assertEquals("XML differences found: " + diff.toString(), 0, allDifferences.size());
}
 
Example 7
Source File: UtilsDiff.java    From apogen with Apache License 2.0 5 votes vote down vote up
/**
 * BETA version of APOGEN-DOM-differencing mechanism
 * 
 * @param doc1
 * @param doc2
 * @return list of Differences
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 */
@SuppressWarnings("unchecked")
public static List<Difference> customisedDomDiff(String string, String string2)
		throws ParserConfigurationException, SAXException, IOException {

	org.w3c.dom.Document doc1 = asDocument(string, true);
	org.w3c.dom.Document doc2 = asDocument(string2, true);

	XMLUnit.setControlParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
	XMLUnit.setTestParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
	XMLUnit.setSAXParserFactory("org.apache.xerces.jaxp.SAXParserFactoryImpl");

	XMLUnit.setNormalizeWhitespace(true);
	XMLUnit.setIgnoreAttributeOrder(true);
	XMLUnit.setIgnoreWhitespace(true);
	XMLUnit.setIgnoreComments(true);
	XMLUnit.setNormalize(true);
	XMLUnit.setIgnoreDiffBetweenTextAndCDATA(false);

	Diff d = new Diff(doc1, doc2);
	DetailedDiff dd = new DetailedDiff(d);

	dd.overrideDifferenceListener(new DomDifferenceListener());
	dd.overrideElementQualifier(null);

	return dd.getAllDifferences();
}
 
Example 8
Source File: Bug5446Test.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug5446/yang/foo.yang");
    final Document doc = loadDocument("/bug5446/xml/foo.xml");

    final ContainerNode docNode = createDocNode();

    Optional<DataContainerChild<? extends PathArgument, ?>> root = docNode.getChild(new NodeIdentifier(ROOT_QNAME));
    assertTrue(root.orElse(null) instanceof ContainerNode);

    Optional<DataContainerChild<? extends PathArgument, ?>> child = ((ContainerNode) root.orElse(null))
            .getChild(new NodeIdentifier(IP_ADDRESS_QNAME));
    assertTrue(child.orElse(null) instanceof LeafNode);
    LeafNode<?> ipAdress = (LeafNode<?>) child.get();

    Object value = ipAdress.getValue();
    assertTrue(value instanceof byte[]);
    assertEquals("fwAAAQ==", Base64.getEncoder().encodeToString((byte[]) value));

    DOMResult serializationResult = writeNormalizedNode(docNode, schemaContext);
    assertNotNull(serializationResult);

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

    String expectedXMLString = toString(doc.getDocumentElement());
    String serializationResultXMLString = toString(serializationResult.getNode());

    assertXMLEqual(expectedXMLString, serializationResultXMLString);
}
 
Example 9
Source File: YT1108Test.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testLeafOfUnionWithIdentityRefNNToXmlSerialization()
        throws XMLStreamException, IOException, SAXException {
    final Document doc = loadDocument("/yt1108/xml/foo-leaf-of-union-with-identity-ref-type.xml");
    final DOMResult domResult = convertNormalizedNodeToXml(buildLeafContainerNodeWithUnionIdentityRefLeaf());

    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalize(true);

    final String expectedXml = toString(doc.getDocumentElement());
    final String serializedXml = toString(domResult.getNode());
    final Diff diff = new Diff(expectedXml, serializedXml);

    new XMLTestCase() {}.assertXMLEqual(diff, true);
}
 
Example 10
Source File: YT1108Test.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testLeafOfIdentityRefTypeNNToXmlSerialization()
        throws XMLStreamException, IOException, SAXException {
    final Document doc = loadDocument("/yt1108/xml/foo-leaf-of-identity-ref-type.xml");
    final DOMResult domResult = convertNormalizedNodeToXml(buildLeafContainerNodeWithIdentityRefLeaf());

    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalize(true);

    final String expectedXml = toString(doc.getDocumentElement());
    final String serializedXml = toString(domResult.getNode());
    final Diff diff = new Diff(expectedXml, serializedXml);

    new XMLTestCase() {}.assertXMLEqual(diff, true);
}
 
Example 11
Source File: AnydataSerializeTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testAnydataLoadFromXML() throws IOException, SAXException, XMLStreamException, URISyntaxException {
    // Load XML file
    Document doc = loadXmlDocument("/test-anydata.xml");
    final DOMSource domSource = new DOMSource(doc.getDocumentElement());

    //Load XML from file and write it with xmlParseStream
    final DOMResult domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());
    final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(domResult);
    final AnydataSchemaNode anyDataSchemaNode = (AnydataSchemaNode) SchemaContextUtil.findDataSchemaNode(
            SCHEMA_CONTEXT, SchemaPath.create(true, FOO_QNAME));
    final NormalizedNodeStreamWriter streamWriter = XMLStreamNormalizedNodeStreamWriter.create(
            xmlStreamWriter, SCHEMA_CONTEXT);
    final XMLStreamReader reader = new DOMSourceXMLStreamReader(domSource);
    final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, SCHEMA_CONTEXT, anyDataSchemaNode);

    xmlParser.parse(reader);
    xmlParser.flush();

    //Set XML comparing
    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalize(true);

    // Check diff
    final String expectedXml = toString(doc.getDocumentElement());
    final String serializedXml = toString(domResult.getNode());
    final Diff diff = new Diff(expectedXml, serializedXml);
    final DifferenceListener differenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
    diff.overrideDifferenceListener(differenceListener);

    XMLAssert.assertXMLEqual(diff, true);
}
 
Example 12
Source File: Bug8745Test.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testParsingAttributes() throws Exception {
    final QName contWithAttributes = QName.create("foo", "cont-with-attributes");
    final ContainerSchemaNode contWithAttr = (ContainerSchemaNode) SchemaContextUtil.findDataSchemaNode(
        SCHEMA_CONTEXT, SchemaPath.create(true, contWithAttributes));

    final Document doc = loadDocument("/bug8745/foo.xml");
    final DOMSource domSource = new DOMSource(doc.getDocumentElement());

    final DOMResult domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());

    final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(domResult);

    final NormalizedNodeStreamWriter streamWriter = XMLStreamNormalizedNodeStreamWriter.create(
            xmlStreamWriter, SCHEMA_CONTEXT);

    final XMLStreamReader reader = new DOMSourceXMLStreamReader(domSource);
    final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, SCHEMA_CONTEXT, contWithAttr);
    xmlParser.parse(reader);

    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalize(true);

    final String expectedXml = toString(doc.getDocumentElement());
    final String serializedXml = toString(domResult.getNode());
    final Diff diff = new Diff(expectedXml, serializedXml);

    XMLAssert.assertXMLEqual(diff, true);
}
 
Example 13
Source File: AbstractYinExportTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private static void assertXMLEquals(final String fileName, final Document expectedXMLDoc, final String output)
        throws SAXException, IOException {
    final String expected = YinExportTestUtils.toString(expectedXMLDoc.getDocumentElement());

    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalize(true);
    XMLUnit.setNormalizeWhitespace(true);

    final Diff diff = new Diff(expected, output);
    diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
    XMLAssert.assertXMLEqual(fileName, diff, true);
}
 
Example 14
Source File: AnydataSerializeTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testXmlParseAnydata()
        throws IOException, SAXException, XMLStreamException, URISyntaxException, TransformerException {

    //Create Data Scheme from yang file
    SchemaPath anydataPath = SchemaPath.create(true, FOO_QNAME);
    final SchemaNode dataSchemaNode = SchemaContextUtil.findDataSchemaNode(SCHEMA_CONTEXT, anydataPath);
    assertTrue(dataSchemaNode instanceof AnydataSchemaNode);
    final AnydataSchemaNode anyDataSchemaNode = (AnydataSchemaNode) dataSchemaNode;

    // deserialization
    final XMLStreamReader reader
            = UntrustedXML.createXMLStreamReader(loadResourcesAsInputStream("/test-anydata.xml"));

    final NormalizedNodeResult result = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
    final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, SCHEMA_CONTEXT, anyDataSchemaNode);
    xmlParser.parse(reader);

    final NormalizedNode<?, ?> transformedInput = result.getResult();
    assertNotNull(transformedInput);
    assertTrue(transformedInput instanceof AnydataNode);
    AnydataNode<?> anydataNode = (AnydataNode<?>) transformedInput;

    // serialization
    final StringWriter writer = new StringWriter();
    final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(writer);
    final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(
            xmlStreamWriter, SCHEMA_CONTEXT);
    final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
            xmlNormalizedNodeStreamWriter);
    normalizedNodeWriter.write(transformedInput);
    normalizedNodeWriter.flush();

    final String serializedXml = writer.toString();
    final String deserializeXml = getXmlFromDOMSource(((DOMSourceAnydata) anydataNode.getValue()).getSource());
    assertFalse(serializedXml.isEmpty());

    // Check if is Serialize Node same as Deserialize Node
    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalize(true);
    final Diff diff = new Diff(deserializeXml, serializedXml);
    final DifferenceListener differenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
    diff.overrideDifferenceListener(differenceListener);

    XMLAssert.assertXMLEqual(diff, true);
}
 
Example 15
Source File: NormalizedNodeXmlTranslationTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
private void testTranslation(final XMLOutputFactory factory) throws Exception {
    final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream(xmlPath);

    final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);

    final NormalizedNodeResult result = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);

    final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, schema, containerNode);
    xmlParser.parse(reader);

    final NormalizedNode<?, ?> built = result.getResult();
    assertNotNull(built);

    if (expectedNode != null) {
        org.junit.Assert.assertEquals(expectedNode, built);
    }

    final Document document = UntrustedXML.newDocumentBuilder().newDocument();
    final DOMResult domResult = new DOMResult(document);

    final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(domResult);

    final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter
            .create(xmlStreamWriter, schema);

    final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
            xmlNormalizedNodeStreamWriter);

    normalizedNodeWriter.write(built);

    final Document doc = loadDocument(xmlPath);

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

    final String expectedXml = toString(doc.getDocumentElement());
    final String serializedXml = toString(domResult.getNode());

    final Diff diff = new Diff(expectedXml, serializedXml);
    diff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
    diff.overrideElementQualifier(new ElementNameAndTextQualifier());

    // FIXME the comparison cannot be performed, since the qualifiers supplied by XMlUnit do not work correctly in
    // this case
    // We need to implement custom qualifier so that the element ordering does not mess the DIFF
    // dd.overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(100, true));
    // assertTrue(dd.toString(), dd.similar());

    //new XMLTestCase() {}.assertXMLEqual(diff, true);
}
 
Example 16
Source File: YangModeledAnyXMLSerializationTest.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testSerializationOfBaz() throws Exception {
    final InputStream resourceAsStream = XmlToNormalizedNodesTest.class.getResourceAsStream(
            "/anyxml-support/serialization/baz.xml");
    final Module bazModule = SCHEMA_CONTEXT.findModules("baz").iterator().next();
    final ContainerSchemaNode bazCont = (ContainerSchemaNode) bazModule.findDataChildByName(
            QName.create(bazModule.getQNameModule(), "baz")).get();

    final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);

    final NormalizedNodeResult result = new NormalizedNodeResult();

    final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);

    final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, SCHEMA_CONTEXT, bazCont);
    xmlParser.parse(reader);

    final NormalizedNode<?, ?> transformedInput = result.getResult();
    assertNotNull(transformedInput);

    assertTrue(transformedInput instanceof ContainerNode);
    ContainerNode bazContainer = (ContainerNode) transformedInput;
    assertEquals(bazContainer.getNodeType(), bazQName);

    Optional<DataContainerChild<? extends PathArgument, ?>> bazContainerChild = bazContainer.getChild(
            new NodeIdentifier(myAnyXMLDataBaz));
    assertTrue(bazContainerChild.orElse(null) instanceof YangModeledAnyXmlNode);
    YangModeledAnyXmlNode yangModeledAnyXmlNode = (YangModeledAnyXmlNode) bazContainerChild.get();

    DataSchemaNode schemaOfAnyXmlData = yangModeledAnyXmlNode.getSchemaOfAnyXmlData();
    SchemaNode myContainer2SchemaNode = SchemaContextUtil.findDataSchemaNode(SCHEMA_CONTEXT,
            SchemaPath.create(true, bazQName, myContainer2QName));
    assertTrue(myContainer2SchemaNode instanceof ContainerSchemaNode);
    assertEquals(myContainer2SchemaNode, schemaOfAnyXmlData);

    final Document document = UntrustedXML.newDocumentBuilder().newDocument();
    final DOMResult domResult = new DOMResult(document);

    final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(domResult);

    final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter
            .create(xmlStreamWriter, SCHEMA_CONTEXT);

    final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
            xmlNormalizedNodeStreamWriter);

    normalizedNodeWriter.write(transformedInput);

    final Document doc = loadDocument("/anyxml-support/serialization/baz.xml");

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

    String expectedXml = toString(doc.getDocumentElement());
    String serializedXml = toString(domResult.getNode());

    assertXMLEqual(expectedXml, serializedXml);
}
 
Example 17
Source File: TestBug18026.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug18026WithJson() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException
{
  String filename = "json-original.json";
  String uri = "/write-buffer/";
  byte[] before = null;
  byte[] after = null;
  String strBefore = null;
  String strAfter = null;

  System.out.println("Running testBug18026WithJson");

  XMLUnit.setIgnoreAttributeOrder(true);
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalize(true);
  XMLUnit.setNormalizeWhitespace(true);
  XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

  Map<String, String> namespaces = new HashMap<>();
  namespaces.put("rapi", "http://marklogic.com/rest-api");
  namespaces.put("prop", "http://marklogic.com/xdmp/property");
  namespaces.put("xs", "http://www.w3.org/2001/XMLSchema");
  namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");

  SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces);

  xpather = XMLUnit.newXpathEngine();
  xpather.setNamespaceContext(namespaceContext);

  ObjectMapper mapper = new ObjectMapper();

  // connect the client
  DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType());

  InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/" + filename);

  InputStreamHandle isHandle = new InputStreamHandle();
  isHandle.set(inputStream);
  before = isHandle.toBuffer();
  strBefore = new String(before);
  System.out.println("Before: " + strBefore);

  JsonNode contentBefore = mapper.readValue(strBefore, JsonNode.class);

  // write doc
  JSONDocumentManager docMgr = client.newJSONDocumentManager();
  String docId = uri + filename;
  docMgr.write(docId, isHandle);

  // read doc
  docMgr.read(docId, isHandle);
  isHandle.get();

  after = isHandle.toBuffer();
  strAfter = new String(after);
  System.out.println("After: " + strAfter);

  JsonNode contentAfter = mapper.readValue(strAfter, JsonNode.class);

  assertTrue("Buffered JSON document difference", contentBefore.equals(contentAfter));

  // release client
  client.release();
}
 
Example 18
Source File: TestBug18026.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
@Test
public void testBug18026() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException
{
  String filename = "xml-original.xml";
  String uri = "/write-buffer/";
  byte[] before = null;
  byte[] after = null;
  String strBefore = null;
  String strAfter = null;

  System.out.println("Running testBug18026");

  XMLUnit.setIgnoreAttributeOrder(true);
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalize(true);
  XMLUnit.setNormalizeWhitespace(true);
  XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

  Map<String, String> namespaces = new HashMap<>();
  namespaces.put("rapi", "http://marklogic.com/rest-api");
  namespaces.put("prop", "http://marklogic.com/xdmp/property");
  namespaces.put("xs", "http://www.w3.org/2001/XMLSchema");
  namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");

  SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(namespaces);

  xpather = XMLUnit.newXpathEngine();
  xpather.setNamespaceContext(namespaceContext);

  // connect the client
  DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType());

  Document readDoc = expectedXMLDocument(filename);

  DOMHandle dHandle = new DOMHandle();
  dHandle.set(readDoc);
  before = dHandle.toBuffer();
  strBefore = new String(before);
  System.out.println("Before: " + strBefore);

  // write doc
  XMLDocumentManager docMgr = client.newXMLDocumentManager();
  String docId = uri + filename;
  docMgr.write(docId, dHandle);

  // read doc
  docMgr.read(docId, dHandle);
  dHandle.get();

  after = dHandle.toBuffer();
  strAfter = new String(after);
  System.out.println("After: " + strAfter);

  assertXMLEqual("Buffer is not the same", strBefore, strAfter);

  // release client
  client.release();
}
 
Example 19
Source File: SchemalessXMLStreamNormalizedNodeStreamWriterTest.java    From yangtools with Eclipse Public License 1.0 3 votes vote down vote up
@Test
public void testWrite() throws XMLStreamException, IOException, SAXException {
    final Document doc = loadDocument("/foobar.xml");

    final DOMResult domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());

    final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(domResult);

    final NormalizedNodeStreamWriter schemalessXmlNormalizedNodeStreamWriter =
            XMLStreamNormalizedNodeStreamWriter.createSchemaless(xmlStreamWriter);

    final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
            schemalessXmlNormalizedNodeStreamWriter);

    normalizedNodeWriter.write(buildOuterContainerNode());

    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalize(true);

    final String expectedXml = toString(doc.getDocumentElement());
    final String serializedXml = toString(domResult.getNode());
    final Diff diff = new Diff(expectedXml, serializedXml);

    final DifferenceListener differenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
    diff.overrideDifferenceListener(differenceListener);

    XMLAssert.assertXMLEqual(diff, true);
}
 
Example 20
Source File: NormalizedNodesToXmlTest.java    From yangtools with Eclipse Public License 1.0 3 votes vote down vote up
@Test
public void testNormalizedNodeToXmlSerialization() throws XMLStreamException, IOException, SAXException {
    final Document doc = loadDocument("/baz.xml");

    final DOMResult domResult = new DOMResult(UntrustedXML.newDocumentBuilder().newDocument());

    final XMLStreamWriter xmlStreamWriter = factory.createXMLStreamWriter(domResult);

    final NormalizedNodeStreamWriter xmlNormalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(
            xmlStreamWriter, SCHEMA_CONTEXT);

    final NormalizedNodeWriter normalizedNodeWriter = NormalizedNodeWriter.forStreamWriter(
            xmlNormalizedNodeStreamWriter);

    normalizedNodeWriter.write(buildOuterContainerNode());

    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setNormalize(true);

    final String expectedXml = toString(doc.getDocumentElement());
    final String serializedXml = toString(domResult.getNode());
    final Diff diff = new Diff(expectedXml, serializedXml);

    final DifferenceListener differenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
    diff.overrideDifferenceListener(differenceListener);

    new XMLTestCase() {}.assertXMLEqual(diff, true);
}