Skip to content

Commit

Permalink
Test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 12, 2020
1 parent 242c869 commit fab0b8b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;

public class CaseInsensitiveDeser273Test extends XmlTestBase
{
Expand All @@ -26,7 +25,7 @@ public void setElement(List<Depot273> l) {
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
// @JsonIgnoreProperties(ignoreUnknown = true)
static class Depot273
{
@JacksonXmlProperty(isAttribute = true)
Expand All @@ -35,6 +34,10 @@ static class Depot273
@JacksonXmlProperty(isAttribute = true)
public String name;

// Should not actually be necessary but...
@JacksonXmlText
public String text;

public void setNumber(String n) {
//System.err.println("SetNumber: '"+n+"'");
number = n;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,44 @@

import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

public class ListDeser393Test extends XmlTestBase
{
@JacksonXmlRootElement(localName = "result")
@JsonInclude(JsonInclude.Include.NON_NULL)
static class Value393 {
private Prices prices = new Prices();
private Prices393 prices = new Prices393();

public void setPrices(Prices prices) {
public void setPrices(Prices393 prices) {
this.prices = prices;
}

@JacksonXmlProperty(localName = "prices")
public Prices getPrices() {
public Prices393 getPrices() {
return this.prices;
}
}

@JacksonXmlRootElement(localName = "prices")
static class Prices {
private List<Price> price = new ArrayList<Price>();
static class Prices393 {
private List<Price393> price = new ArrayList<Price393>();

public void setPrice(List<Price> price) {
public void setPrice(List<Price393> price) {
this.price = price;
}

@JacksonXmlElementWrapper(useWrapping = false)
public List<Price> getPrice() {
public List<Price393> getPrice() {
return this.price;
}
}

static class Price {
static class Price393 {
private String price;
private String num;

protected Price() { }
public Price(String p, String n) {
protected Price393() { }
public Price393(String p, String n) {
price = p;
num = n;
}
Expand Down Expand Up @@ -80,37 +78,6 @@ public String getNum() {
// [dataform#393]
public void testDeser393() throws Exception
{
// for debugging:
/*
Value393 input = new Value393();
Prices prices = new Prices();
prices.setPrice(Arrays.asList(
new Price("100", "7.0"),
new Price("100", "4.0")
));
input.setPrices(prices);
String xml = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(input);
System.out.println("XML:\n"+xml);
*/
/*
String content = //"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "\n" +
"<result>\n"
+ " <prices>\n"
+ " <price>\n"
+ " <num>100</num>\n"
+ " <price>7.0</price>\n"
+ " </price>\n"
+ " <price>\n"
+ " <num>100</num>\n"
+ " <price>4.0</price>\n"
+ " </price>"
+ " </prices>\n"
+ "</result>";
Value393 result = MAPPER.readValue(content, Value393.class);
assertNotNull(result);
*/

String content =
"<prices>\n"
+ " <price>\n"
Expand All @@ -122,7 +89,9 @@ public void testDeser393() throws Exception
+ " <price>4.0</price>\n"
+ " </price>"
+ "</prices>\n";
Prices result = MAPPER.readValue(content, Prices.class);
Prices393 result = MAPPER.readValue(content, Prices393.class);
assertNotNull(result);
assertNotNull(result.getPrice());
assertEquals(2, result.getPrice().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.*;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
Expand Down Expand Up @@ -69,6 +71,32 @@ static class ListBeanUnwrapped
public List<Integer> values;
}

// [dataformat-xml#191]
static class TestList191 {
@JacksonXmlElementWrapper(useWrapping = true, localName = "items")
@JacksonXmlProperty(localName = "item")
public ArrayList<ListItem191> items;
}

static class ListItem191 {
@JacksonXmlProperty(isAttribute = true)
public String name;
}

// [dataformat-xml#294]
@JacksonXmlRootElement(localName = "levels")
static class RootLevel294 {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "sublevel")
public List<Sublevel294> sublevels = new ArrayList<>();
}

@JsonPropertyOrder({ "id", "sublevel" })
static class Sublevel294 {
public Integer id;
public String sublevel;
}

/*
/**********************************************************
/* Unit tests
Expand Down Expand Up @@ -164,4 +192,46 @@ public void testUnwrappedListBeanDeser() throws Exception
assertEquals(Integer.valueOf(2), bean.values.get(1));
assertEquals(Integer.valueOf(3), bean.values.get(2));
}

// [dataformat-xml#191]
public void testListDeser191() throws Exception
{
final String XML =
"<TestList>\n"+
" <items>\n"+
" <item name='Item1'/>\n"+
" <item name='Item2'> </item>\n"+ // important: at least one ws char between start/end
" <item name='Item3'/>\n"+
" </items>\n"+
"</TestList>"
;
TestList191 testList = MAPPER.readValue(XML, TestList191.class);
assertNotNull(testList);
assertNotNull(testList.items);
assertEquals(3, testList.items.size());
}

// [dataformat-xml#294]
public void testNestedLists294() throws Exception
{
RootLevel294 tree = new RootLevel294();
tree.sublevels.add(_newSublevel(1, "Name A"));
tree.sublevels.add(_newSublevel(2, "Name B"));
String xml = MAPPER
.writerWithDefaultPrettyPrinter()
.writeValueAsString(tree);
//System.err.println("XML:\n"+xml);
RootLevel294 resTree = MAPPER.readValue(xml, RootLevel294.class);
assertNotNull(resTree);
assertNotNull(resTree.sublevels);
assertEquals(2, resTree.sublevels.size());
assertEquals("Name B", resTree.sublevels.get(1).sublevel);
}

private Sublevel294 _newSublevel(Integer id, String sublevel) {
Sublevel294 res = new Sublevel294();
res.id = id;
res.sublevel = sublevel;
return res;
}
}

0 comments on commit fab0b8b

Please sign in to comment.