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

Extend test coverage #406

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Extend test coverage
* Add more tests for JSON and XML
  • Loading branch information
jodastephen committed Nov 5, 2024
commit ca59997fd0224f7590820c8405432aac652e0882
7 changes: 6 additions & 1 deletion src/test/java/org/joda/beans/ser/SerTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ public static ImmAddress testImmAddress() {
.listInListInMap(map3)
.objectListInListInMap(map5)
.mapInMap(map4)
.simpleTable(ImmutableTable.of(1, 1, "Hello"))
.simpleTable(ImmutableTable.<Integer, Integer, String>builder()
.put(1, 1, "Hello")
.put(1, 2, "There")
.build())
.compoundTable(table)
.sparseGrid(sparseGrid)
.denseGrid(denseGrid)
Expand All @@ -205,6 +208,7 @@ public static ImmGuava<String> testCollections() {
ImmutableMap<String, String> map = ImmutableMap.of("A", "AA", "B", "BB");
ImmutableSortedMap<String, String> sortedMap = ImmutableSortedMap.of("A", "AA", "B", "BB");
ImmutableBiMap<String, String> bimap = ImmutableBiMap.of("A", "AA", "B", "BB");
ImmutableMultiset<String> multiset = ImmutableMultiset.of("A", "B", "C", "B", "C", "C");
return ImmGuava.<String> builder()
.list(list)
.listInterface(list)
Expand All @@ -218,6 +222,7 @@ public static ImmGuava<String> testCollections() {
.sortedMapInterface(sortedMap)
.biMap(bimap)
.biMapInterface(bimap)
.multiset(multiset)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.joda.beans.Bean;
import org.joda.beans.impl.flexi.FlexiBean;
import org.joda.beans.sample.Address;
import org.joda.beans.sample.ImmAddress;
import org.joda.beans.sample.ImmArrays;
import org.joda.beans.sample.ImmDoubleFloat;
import org.joda.beans.sample.ImmGuava;
Expand Down Expand Up @@ -90,6 +91,27 @@ public void test_writeImmArrays() throws IOException {
BeanAssert.assertBeanEquals(bean, parsed);
}

@Test
public void test_writeAddress() throws IOException {
Address address = SerTestHelper.testAddress();
String json = JodaBeanSer.PRETTY.simpleJsonWriter().write(address);
// System.out.println(json);
assertEqualsSerialization(json, "/org/joda/beans/ser/Address.simplejson");
// no round trip with simple JSON
}

@Test
public void test_writeImmAddress() throws IOException {
ImmAddress address = SerTestHelper.testImmAddress().toBuilder()
.mapInMap(new HashMap<>())
.beanBeanMap(new HashMap<>())
.build();
String json = JodaBeanSer.PRETTY.simpleJsonWriter().write(address);
// System.out.println(json);
assertEqualsSerialization(json, "/org/joda/beans/ser/ImmAddress.simplejson");
// no round trip with simple JSON
}

@Test
public void test_writeCollections() throws IOException {
ImmGuava<String> optional = SerTestHelper.testCollections();
Expand All @@ -98,7 +120,7 @@ public void test_writeCollections() throws IOException {
assertEqualsSerialization(json, "/org/joda/beans/ser/Collections.simplejson");

@SuppressWarnings("unchecked")
ImmGuava<String> bean = (ImmGuava<String>) JodaBeanSer.PRETTY.simpleJsonReader().read(json, ImmGuava.class);
ImmGuava<String> bean = JodaBeanSer.PRETTY.simpleJsonReader().read(json, ImmGuava.class);
// System.out.println(bean);
BeanAssert.assertBeanEquals(bean, optional);
}
Expand Down
24 changes: 19 additions & 5 deletions src/test/java/org/joda/beans/ser/xml/TestSerializeXml.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.CharArrayWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.joda.beans.Bean;
import org.joda.beans.impl.flexi.FlexiBean;
Expand All @@ -43,21 +44,24 @@
import org.joda.beans.ser.JodaBeanSer;
import org.joda.beans.ser.SerDeserializers;
import org.joda.beans.ser.SerTestHelper;
import org.joda.beans.ser.json.TestSerializeJson;
import org.joda.beans.test.BeanAssert;
import org.junit.jupiter.api.Test;

import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;

/**
* Test property roundtrip using XML.
*/
public class TestSerializeXml {

@Test
public void test_writeAddress() {
public void test_writeAddress() throws IOException {
Address address = SerTestHelper.testAddress();
String xml = JodaBeanSer.PRETTY.xmlWriter().write(address);
// System.out.println(xml);
assertEqualsSerialization(xml, "/org/joda/beans/ser/Address.xml");

Address bean = (Address) JodaBeanSer.PRETTY.xmlReader().read(xml);
// System.out.println(bean);
Expand All @@ -76,34 +80,37 @@ public void test_writeToAppendable() throws IOException {
}

@Test
public void test_writeImmAddress() {
public void test_writeImmAddress() throws IOException {
ImmAddress address = SerTestHelper.testImmAddress();
String xml = JodaBeanSer.PRETTY.xmlWriter().write(address);
// System.out.println(xml);
assertEqualsSerialization(xml, "/org/joda/beans/ser/ImmAddress.xml");

xml = xml.replace("185", "18<!-- comment -->5");
// System.out.println(xml);

ImmAddress bean = (ImmAddress) JodaBeanSer.PRETTY.xmlReader().read(xml);
// System.out.println(bean);
BeanAssert.assertBeanEquals(bean, address);
}

@Test
public void test_writeImmOptional() {
public void test_writeImmOptional() throws IOException {
ImmOptional optional = SerTestHelper.testImmOptional();
String xml = JodaBeanSer.PRETTY.withIncludeDerived(true).xmlWriter().write(optional);
// System.out.println(xml);
assertEqualsSerialization(xml, "/org/joda/beans/ser/ImmOptional.xml");

ImmOptional bean = (ImmOptional) JodaBeanSer.PRETTY.xmlReader().read(xml);
// System.out.println(bean);
BeanAssert.assertBeanEquals(bean, optional);
}

@Test
public void test_writeCollections() {
public void test_writeCollections() throws IOException {
ImmGuava<String> optional = SerTestHelper.testCollections();
String xml = JodaBeanSer.PRETTY.xmlWriter().write(optional);
// System.out.println(xml);
assertEqualsSerialization(xml, "/org/joda/beans/ser/Collections.xml");

@SuppressWarnings("unchecked")
ImmGuava<String> bean = (ImmGuava<String>) JodaBeanSer.PRETTY.xmlReader().read(xml);
Expand All @@ -125,6 +132,13 @@ public void test_writeJodaConvertInterface() {
BeanAssert.assertBeanEquals(bean, array);
}

private void assertEqualsSerialization(String xml, String expectedResource) throws IOException {
var url = TestSerializeJson.class.getResource(expectedResource);
var expected = Resources.asCharSource(url, StandardCharsets.UTF_8).read();
assertThat(xml.trim().replace(System.lineSeparator(), "\n"))
.isEqualTo(expected.trim().replace(System.lineSeparator(), "\n"));
}

//-----------------------------------------------------------------------
@Test
public void test_readWriteBeanEmptyChild_pretty() {
Expand Down
58 changes: 58 additions & 0 deletions src/test/resources/org/joda/beans/ser/Address.simplejson
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"number": 251,
"street": "Big Road",
"city": "London & Capital of the World <!>",
"owner": {
"forename": "Etienne",
"surname": "Colebourne",
"numberOfCars": 0,
"addressList": [{
"number": 65432,
"street": "Big Road",
"city": "Bigton"
}, null, {
"number": 185,
"street": "Park Street",
"city": "London",
"companyName": "OpenGamma"
}],
"otherAddressMap": {
"other": null,
"work": {
"number": 185,
"street": "Park Street",
"city": "London",
"companyName": "OpenGamma"
},
"home": {
"number": 65432,
"street": "Big Road",
"city": "Bigton"
}
},
"addressesList": [[{
"number": 65432,
"street": "Big Road",
"city": "Bigton"
}, {
"number": 185,
"street": "Park Street",
"city": "London",
"companyName": "OpenGamma"
}]],
"mainAddress": {
"number": 185,
"street": "Park Street",
"city": "London",
"companyName": "OpenGamma"
},
"extensions": {
"interests": "joda",
"conferenceCount": 21,
"quality": "B",
"company": {
"companyName": "OpenGamma"
}
}
}
}
68 changes: 68 additions & 0 deletions src/test/resources/org/joda/beans/ser/Address.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<bean type="org.joda.beans.sample.Address">
<number>251</number>
<street>Big Road</street>
<city>London &amp; Capital of the World &lt;!&gt;</city>
<owner>
<forename>Etienne</forename>
<surname>Colebourne</surname>
<numberOfCars>0</numberOfCars>
<addressList>
<item>
<number>65432</number>
<street>Big Road</street>
<city>Bigton</city>
</item>
<item null="true"/>
<item type="CompanyAddress">
<number>185</number>
<street>Park Street</street>
<city>London</city>
<companyName>OpenGamma</companyName>
</item>
</addressList>
<otherAddressMap>
<entry key="other" null="true"/>
<entry key="work" type="CompanyAddress">
<number>185</number>
<street>Park Street</street>
<city>London</city>
<companyName>OpenGamma</companyName>
</entry>
<entry key="home">
<number>65432</number>
<street>Big Road</street>
<city>Bigton</city>
</entry>
</otherAddressMap>
<addressesList>
<item>
<item>
<number>65432</number>
<street>Big Road</street>
<city>Bigton</city>
</item>
<item type="CompanyAddress">
<number>185</number>
<street>Park Street</street>
<city>London</city>
<companyName>OpenGamma</companyName>
</item>
</item>
</addressesList>
<mainAddress type="CompanyAddress">
<number>185</number>
<street>Park Street</street>
<city>London</city>
<companyName>OpenGamma</companyName>
</mainAddress>
<extensions>
<interests>joda</interests>
<conferenceCount type="Integer">21</conferenceCount>
<quality type="Character">B</quality>
<company type="Company">
<companyName>OpenGamma</companyName>
</company>
</extensions>
</owner>
</bean>
11 changes: 10 additions & 1 deletion src/test/resources/org/joda/beans/ser/Collections.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@
},
"listMultimap": [],
"setMultimap": [],
"multiset": [],
"multiset": [[{
"@type": "String",
"value": "A"
}, 1], [{
"@type": "String",
"value": "B"
}, 2], [{
"@type": "String",
"value": "C"
}, 3]],
"sortedMultiset": [],
"collectionInterface": {
"@meta": "List",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"multimap": {},
"listMultimap": {},
"setMultimap": {},
"multiset": [],
"multiset": [["A", 1], ["B", 2], ["C", 3]],
"sortedMultiset": [],
"collectionInterface": [],
"listInterface": ["A", "B"],
Expand Down
Loading