Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML -> JSON deserializer + tests #1

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1e12c62
An XML -> JSON deserializer draft; An XmlNamespaceManager (should be …
Mar 10, 2014
1c6a195
An XML -> JSON deserializer draft; An XmlNamespaceManager (should be …
Mar 10, 2014
69cc90a
Separated files for XMl and JSON testing
Mar 14, 2014
b8d93ed
A thorough XML subtrees comparator.
Mar 14, 2014
8a086e1
Helper functions for tests
Mar 14, 2014
ff2bd93
Roundtrip tests for Json->XML conversions and vice versa
Mar 14, 2014
9c77bfd
Added method for consuming whitespaces.
Mar 14, 2014
94c431a
XML -> Json deserializer, does not support whitespace
Mar 14, 2014
01fd836
tweaks
Mar 14, 2014
648c844
Support for whitespaces between JSON tokens
Mar 14, 2014
260a0ca
Updates to tests
Mar 14, 2014
2d3424c
Not so brute force no more
Mar 14, 2014
2a8ae92
Updates to the error msgs
Mar 14, 2014
4c2ea8f
Moved to a more appropriate location
Mar 14, 2014
decf9fe
special type nodes' names added
Mar 14, 2014
b0b10b0
Modified test cases and updated tests
Mar 14, 2014
7362d5b
A bit of refactoring
Mar 17, 2014
e8efb4b
Removed local scripts
Mar 17, 2014
658c519
Updated readme
Mar 17, 2014
5e40de2
Serializing of CDATA, Text and Comment arrays
Mar 18, 2014
c35978c
Updated version of tests
Mar 18, 2014
abecbb4
New Json and Xml test files
Mar 21, 2014
b35b1e8
Tests separated by files
Mar 21, 2014
d78f05e
Removed XmlNamespaceManager
Mar 21, 2014
cc5d96d
Deprecated
Mar 21, 2014
b359641
Few tweaks
Mar 21, 2014
c239d33
Strict Json testing
Mar 21, 2014
4f89bce
Tests cleanup
Mar 25, 2014
ef8a0d3
deleted old test files
Mar 25, 2014
e5fec1d
Unsupported use cases tests moved to separate dirs
Mar 25, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions json/Json2XmlRoundTripTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.dslplatform.client;

import static com.dslplatform.client.Helpers.getFileForResource;
import static com.dslplatform.client.Helpers.parseXmlFile;
import static com.dslplatform.client.Helpers.stringFromFile;
import static com.dslplatform.client.Helpers.jsonStringFromXml;
import static com.dslplatform.client.Helpers.xmlDocumentFromJson;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Json2XmlRoundTripTest {

@Test
public void assertRoundTripEquivalenceWithReferenceConversion() throws URISyntaxException, JSONException,
SAXException, IOException, ParserConfigurationException, TransformerConfigurationException,
TransformerException, TransformerFactoryConfigurationError {

final File jsonSources_dir = getFileForResource("/roundtripTests/json/source/");

for (final File jsonSourceFile : jsonSources_dir.listFiles()) {
if (jsonSourceFile.isFile()) {
System.out.println("Testiramo za datoteku: " + jsonSourceFile.getName());

/* Filename initialisation */
final String sourceFilename_json = jsonSourceFile.getName();
final String convertedFilename_xml = sourceFilename_json + ".xml";
final String roundtripFilename_json = sourceFilename_json + ".xml.json";

final File referenceFile_xml = getFileForResource("/roundtripTests/json/reference/"
+ convertedFilename_xml);
assertTrue("The reference JSON file does not exist for: " + sourceFilename_json,
(referenceFile_xml != null && referenceFile_xml.exists()));

final File referenceRoundtripFile_json = getFileForResource("/roundtripTests/json/reference/"
+ roundtripFilename_json);
assertTrue("The reference XML->JSON roundtrip file does not exist for: " + sourceFilename_json,
(referenceRoundtripFile_json != null && referenceRoundtripFile_json.exists()));

/* Parse input files */
final String source_json = stringFromFile(jsonSourceFile);
final Document referenceXml = parseXmlFile(referenceFile_xml);
final String referenceRoundTrip_json = stringFromFile(referenceRoundtripFile_json);

/* Convert to XML and compare with reference conversion */
final Document convertedXml = xmlDocumentFromJson(source_json);
// System.out.println("Converted XML:");
// printXmlDocument(convertedXml);
// System.out.println("Reference XML:");
// printXmlDocument(referenceXml);
assertXmlEquivalence("The converted XML does not match the reference XML", convertedXml, referenceXml);

/* Convert back to Json, and compare with reference documents */
final String roundtripJsonString = jsonStringFromXml(convertedXml);

assertJsonEquivalence(roundtripJsonString, referenceRoundTrip_json);
assertJsonEquivalence(roundtripJsonString, source_json);
assertJsonEquivalence(referenceRoundTrip_json, source_json);
}
}
}

private static void assertJsonEquivalence(String lhs, String rhs) throws JSONException {
System.out.println();
System.out.println(lhs);
System.out.println(rhs);
System.out.println();
JSONAssert.assertEquals(lhs, rhs, false);
}

private static void assertXmlEquivalence(String message, Document lhs, Document rhs) {
XmlBruteForceComparator comparator = new XmlBruteForceComparator();

assertTrue(message, comparator.compare(lhs.getDocumentElement(), rhs.getDocumentElement()) == 0);
}

}
108 changes: 108 additions & 0 deletions json/Xml2JsonRoundTripTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.dslplatform.client;

import static com.dslplatform.client.Helpers.getFileForResource;
import static com.dslplatform.client.Helpers.jsonStringFromXml;
import static com.dslplatform.client.Helpers.parseXmlFile;
import static com.dslplatform.client.Helpers.printXmlDocument;
import static com.dslplatform.client.Helpers.stringFromFile;
import static com.dslplatform.client.Helpers.xmlDocumentFromJson;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.json.JSONException;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Xml2JsonRoundTripTest {

/**
* For each of the XML files in 'resources/roundtripTests/source/xml'
* generates the entire roundtrip conversion (xml -> json -> xml) using
* io.jvm.json.serializers.XmlJsonSerializer, and asserts the generated XML
* equivalence with the reference conversions (obtained by using Json.NET)
*/
@Test
public void assertRoundTripEquivalenceWithReferenceConversion() throws URISyntaxException, JSONException,
SAXException, IOException, ParserConfigurationException, TransformerConfigurationException,
TransformerException, TransformerFactoryConfigurationError {

final File xmlSources_dir = getFileForResource("/roundtripTests/xml/source/");

for (final File xmlSourceFile : xmlSources_dir.listFiles()) {
if (xmlSourceFile.isFile()) {
System.out.println();
System.out.println("Testiramo za datoteku: " + xmlSourceFile.getName());
/* Filename initialisation */
final String sourceFilename_xml = xmlSourceFile.getName();
final String convertedFilename_json = sourceFilename_xml + ".json";
final String roundtripFilename_xml = sourceFilename_xml + ".json.xml";

final File referenceFile_json = getFileForResource("/roundtripTests/xml/reference/"
+ convertedFilename_json);
assertTrue("The reference JSON file does not exist for: " + sourceFilename_xml,
(referenceFile_json != null && referenceFile_json.exists()));

final File referenceRoundtripFile_xml = getFileForResource("/roundtripTests/xml/reference/"
+ roundtripFilename_xml);
assertTrue("The reference XML->JSON roundtrip file does not exist for: " + sourceFilename_xml,
(referenceRoundtripFile_xml != null && referenceRoundtripFile_xml.exists()));

/* Parse input files */
final Document source_xml = parseXmlFile(xmlSourceFile);
final String referenceJson = stringFromFile(referenceFile_json);
final Document referenceRoundTrip_xml = parseXmlFile(referenceRoundtripFile_xml);

/* Convert to json and compare with reference conversion */
final String convertedJson = jsonStringFromXml(source_xml);

// System.out.println("Converted JSON:");
// System.out.println(convertedJson);
// System.out.println("Reference JSON:");
// System.out.println(referenceJson);

assertJsonEquivalence(convertedJson, referenceJson);

/* Convert back to XML, and compare with reference documents */
final Document roundtripXmlDocument = xmlDocumentFromJson(convertedJson);

System.out.println("Our roundtrip XML:");
printXmlDocument(roundtripXmlDocument);
System.out.println("Reference roundtrip xml:");
printXmlDocument(referenceRoundTrip_xml);

assertXmlEquivalence("The reference roundtrip XML does not match the converted roundtrip XML",
roundtripXmlDocument, referenceRoundTrip_xml);
assertXmlEquivalence("The roundtrip XML does not match the source XML", roundtripXmlDocument,
source_xml);
assertXmlEquivalence("The reference roundtrip XML does not match the source XML",
referenceRoundTrip_xml, source_xml);
}
}
}

private static void assertJsonEquivalence(String lhs, String rhs) throws JSONException {
System.out.println();
System.out.println("Checking JSon equivalence: ");
System.out.println(lhs);
System.out.println(rhs);

JSONAssert.assertEquals(lhs, rhs, false);
}

private static void assertXmlEquivalence(String message, Document lhs, Document rhs) {
XmlBruteForceComparator comparator = new XmlBruteForceComparator();

assertTrue(message, comparator.compare(lhs.getDocumentElement(), rhs.getDocumentElement()) == 0);
}

}
16 changes: 15 additions & 1 deletion json/src/main/java/io/jvm/json/EntryPoint.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
package io.jvm.json;

import io.jvm.json.serializers.XmlJsonSerializer;

import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;

public class EntryPoint {
public static String toString(org.w3c.dom.Element doc) {
try
Expand Down Expand Up @@ -45,7 +59,7 @@ public static void main(final String[] args) throws Exception {
final Element elem = toXml(input).getDocumentElement();
System.out.println("INPUT: " + toString(elem));

final JsonSerializer<Element> xmlSerialization = new XMLJsonSerializer();
final JsonSerializer<Element> xmlSerialization = new XmlJsonSerializer();

final StringWriter sw = new StringWriter();
xmlSerialization.toJson(new JsonWriter(sw), elem);
Expand Down
Loading