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

The following examples show how to use org.custommonkey.xmlunit.XMLUnit#setNormalizeWhitespace() . 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: TransformersTest.java    From proarc with GNU General Public License v3.0 6 votes vote down vote up
@Test
    public void testMarcAsMods() throws Exception {
        XMLUnit.setNormalizeWhitespace(true);
        InputStream goldenIS = TransformersTest.class.getResourceAsStream("alephXServerDetailResponseAsMods.xml");
        assertNotNull(goldenIS);
        InputStream xmlIS = TransformersTest.class.getResourceAsStream("alephXServerDetailResponseAsMarcXml.xml");// from test
        assertNotNull(xmlIS);
        StreamSource streamSource = new StreamSource(xmlIS);
        Transformers mt = new Transformers();

        try {
            byte[] contents = mt.transformAsBytes(streamSource, Transformers.Format.MarcxmlAsMods3);
            assertNotNull(contents);
//            System.out.println(new String(contents, "UTF-8"));
            XMLAssert.assertXMLEqual(new InputSource(goldenIS), new InputSource(new ByteArrayInputStream(contents)));
        } finally {
            close(xmlIS);
            close(goldenIS);
        }
    }
 
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: 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 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: AbstractTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() {
  XMLUnit.setIgnoreComments(true);
  XMLUnit.setIgnoreAttributeOrder(true);
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalizeWhitespace(true);
  XMLUnit.setCompareUnmatched(false);
}
 
Example 9
Source File: ODataXmlSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() {
  XMLUnit.setIgnoreComments(true);
  XMLUnit.setIgnoreAttributeOrder(true);
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalizeWhitespace(true);
  XMLUnit.setCompareUnmatched(false);
}
 
Example 10
Source File: ODataXmlDeserializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() {
  XMLUnit.setIgnoreComments(true);
  XMLUnit.setIgnoreAttributeOrder(true);
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalizeWhitespace(true);
  XMLUnit.setCompareUnmatched(false);
}
 
Example 11
Source File: TestReaderHandle.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
@Test
public void testXmlCRUD() throws KeyManagementException, NoSuchAlgorithmException, FileNotFoundException, IOException, SAXException, ParserConfigurationException
{
  System.out.println("Running testXmlCRUD");

  String filename = "xml-original-test.xml";
  String uri = "/write-xml-readerhandle/";

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

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

  // write the doc
  writeDocumentReaderHandle(client, filename, uri, "XML");

  // read the document
  ReaderHandle readHandle = readDocumentReaderHandle(client, uri + filename, "XML");

  // access the document content
  Reader fileRead = readHandle.get();

  String readContent = convertReaderToString(fileRead);

  // get xml document for expected result
  Document expectedDoc = expectedXMLDocument(filename);

  // convert actual string to xml doc
  Document readDoc = convertStringToXMLDocument(readContent);

  assertXMLEqual("Write XML difference", expectedDoc, readDoc);

  // update the doc
  // acquire the content for update
  String updateFilename = "xml-updated-test.xml";
  updateDocumentReaderHandle(client, updateFilename, uri + filename, "XML");

  // read the document
  ReaderHandle updateHandle = readDocumentReaderHandle(client, uri + filename, "XML");

  // access the document content
  Reader fileReadUpdate = updateHandle.get();

  String readContentUpdate = convertReaderToString(fileReadUpdate);

  // get xml document for expected result
  Document expectedDocUpdate = expectedXMLDocument(updateFilename);

  // convert actual string to xml doc
  Document readDocUpdate = convertStringToXMLDocument(readContentUpdate);

  assertXMLEqual("Write XML difference", expectedDocUpdate, readDocUpdate);

  // delete the document
  deleteDocument(client, uri + filename, "XML");

  // read the deleted document
  String exception = "";
  try
  {
    readDocumentReaderHandle(client, uri + filename, "XML");
  } catch (Exception e) {
    exception = e.toString();
  }

  String expectedException = "Could not read non-existent document";
  boolean documentIsDeleted = exception.contains(expectedException);
  assertTrue("Document is not deleted", documentIsDeleted);

  // release the client
  client.release();
}
 
Example 12
Source File: Main.java    From libreveris with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void compare ()
{
    // Comparing
    try {
        output.println("Comparing " + controlFile + " to " + testFile);

        final Document controlDoc;
        final Document testDoc;

        try (InputStream cis = new FileInputStream(controlFile)) {
            controlDoc = new PositionalXMLReader().readXML(cis);
        }
        try (InputStream tis = new FileInputStream(testFile)) {
            testDoc = new PositionalXMLReader().readXML(tis);
        }

        // Tuning
        XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
        XMLUnit.setNormalizeWhitespace(true);
        XMLUnit.setIgnoreWhitespace(true);

        ///XMLUnit.setIgnoreComments(true); NO!!!!!!!!
        // The setIgnoreComments triggers the use of XSLT transform
        // which 1/ ruins userdata and 2/ fails on xml:space and xml:lang.
        // Moreover, comments are actually ignored by Diff

        ///XMLUnit.setCompareUnmatched(false); NO need

        // Customization
        Filter filter = (filterFile != null)
                ? new BasicFilter(new FileInputStream(filterFile))
                : null;

        Diff diff = new Diff(controlDoc, testDoc, null);
        diff.overrideElementQualifier(
                new ElementNameAndAttributeQualifier("number"));
        diff.overrideDifferenceListener(
                new MusicDifferenceListener(filter, output));

        output.println("Similar:     " + diff.similar());
        output.println("Identical:   " + diff.identical());

        DetailedDiff detDiff = new DetailedDiff(diff);

        List differences = detDiff.getAllDifferences();
        output.println();
        output.println("Physical differences: " + differences.size());

        int diffId = 0;

        for (Object object : differences) {
            Difference difference = (Difference) object;

            if (!difference.isRecoverable()
                && ((filter == null) || !filter.canIgnore(difference))) {
                diffId++;
                output.dump(diffId, difference);
            }
        }
        output.println("Logical  differences: " + diffId);
        logger.info("Logical  differences: {}", diffId);

    } catch (ConfigurationException | SAXException | IOException e) {
        e.printStackTrace();
    }
}
 
Example 13
Source File: TestBytesHandle.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
@Test
public void testXmlCRUD() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException {

  String filename = "xml-original-test.xml";
  String uri = "/write-xml-domhandle/";
  System.out.println("Running testXmlCRUD");
  XMLUnit.setIgnoreWhitespace(true);
  XMLUnit.setNormalizeWhitespace(true);

  // connect the client
  SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("eval-user", "x");
  DatabaseClient client = DatabaseClientFactory.newClient(appServerHostname, uberPort, dbName, secContext, getConnType());

  // write docs
  writeDocumentUsingBytesHandle(client, filename, uri, null, "XML");

  // read docs
  BytesHandle contentHandle = readDocumentUsingBytesHandle(client, uri + filename, "XML");

  // get the contents

  byte[] readDoc1 = (byte[]) contentHandle.get();
  String readDoc2 = new String(readDoc1);
  Document readDoc = convertStringToXMLDocument(readDoc2);

  // get xml document for expected result
  Document expectedDoc = expectedXMLDocument(filename);
  assertXMLEqual("Write XML difference", expectedDoc, readDoc);

  // Update the Doc
  // acquire the content for update
  String updateFilename = "xml-updated-test.xml";
  updateDocumentUsingByteHandle(client, updateFilename, uri + filename, "XML");

  // read the document
  BytesHandle updateHandle = readDocumentUsingBytesHandle(client, uri + filename, "XML");
  byte[] readDocUpdateInBytes = updateHandle.get();
  String readDocUpdateInString = new String(readDocUpdateInBytes);

  // convert actual string to xml doc
  Document readDocUpdate = convertStringToXMLDocument(readDocUpdateInString);

  // get xml document for expected result
  Document expectedDocUpdate = expectedXMLDocument(updateFilename);
  assertXMLEqual("Write XML Difference", expectedDocUpdate, readDocUpdate);

  // delete the document
  deleteDocument(client, uri + filename, "XML");

  // read the deleted document
  String exception = "";
  try
  {
    readDocumentUsingBytesHandle(client, uri + filename, "XML");
  } catch (Exception e) {
    exception = e.toString();
  }

  String expectedException = "com.marklogic.client.ResourceNotFoundException: Local message: Could not read non-existent document. Server Message: RESTAPI-NODOCUMENT: (err:FOER0000) Resource or document does not exist:  category: content message: /write-xml-domhandle/xml-original-test.xml";
  assertEquals("Document is not deleted", expectedException, exception);

  // assertFalse("Document is not deleted", isDocumentExist(client, uri +
  // filename, "XML"));

  // release client
  client.release();

}
 
Example 14
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 15
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 16
Source File: TestDOMHandle.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
@Test
public void testXmlCRUD() throws KeyManagementException, NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException
{
  String filename = "xml-original-test.xml";
  String uri = "/write-xml-domhandle/";

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

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

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

  // write docs
  writeDocumentUsingDOMHandle(client, filename, uri, "XML");

  // read docs
  DOMHandle contentHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML");

  Document readDoc = contentHandle.get();

  // get xml document for expected result
  Document expectedDoc = expectedXMLDocument(filename);

  assertEquals("First Node incorrect in input doc", readDoc.getFirstChild().getNodeName().trim(), "food");
  assertEquals("First Node attribute incorrect in input doc", readDoc.getFirstChild().getAttributes().item(0).getNodeValue().trim(), "en");
  assertEquals("Child Node value incorrect in input doc", readDoc.getChildNodes().item(0).getTextContent().trim(), "noodle");

  assertEquals("First Node incorrect in output doc", expectedDoc.getFirstChild().getNodeName().trim(), "food");
  assertEquals("First Node attribute incorrect in output doc", expectedDoc.getFirstChild().getAttributes().item(0).getNodeValue().trim(), "en");
  assertEquals("Child Node value incorrect in output doc", expectedDoc.getChildNodes().item(0).getTextContent().trim(), "noodle");

  // update the doc
  // acquire the content for update
  String updateFilename = "xml-updated-test.xml";
  updateDocumentUsingDOMHandle(client, updateFilename, uri + filename, "XML");

  // read the document
  DOMHandle updateHandle = readDocumentUsingDOMHandle(client, uri + filename, "XML");

  Document readDocUpdate = updateHandle.get();

  assertEquals("First Node incorrect in output doc", readDocUpdate.getFirstChild().getNodeName().trim(), "food");
  assertEquals("First Node attribute incorrect in output doc", readDocUpdate.getFirstChild().getAttributes().item(0).getNodeValue().trim(), "en");
  assertEquals("Child Node value incorrect in output doc", readDocUpdate.getChildNodes().item(0).getTextContent().trim(), "fried noodle");

  // delete the document
  deleteDocument(client, uri + filename, "XML");

  // read the deleted document
  String exception = "";
  try
  {
    readDocumentUsingInputStreamHandle(client, uri + filename, "XML");
  } catch (Exception e) {
    exception = e.toString();
  }

  String expectedException = "Could not read non-existent document";
  boolean documentIsDeleted = exception.contains(expectedException);
  assertTrue("Document is not deleted", documentIsDeleted);

  // release client
  client.release();
}
 
Example 17
Source File: TransformersTest.java    From proarc with GNU General Public License v3.0 4 votes vote down vote up
@After
public void tearDown() {
    assertTrue(externalConnections.toString(), externalConnections.isEmpty());
    XMLUnit.setIgnoreWhitespace(false);
    XMLUnit.setNormalizeWhitespace(false);
}