| 1 |
package com.fasterxml.jackson.module.jaxb.misc; |
| 2 |
|
| 3 |
import java.util.*; |
| 4 |
|
| 5 |
import javax.xml.bind.annotation.XmlElement; |
| 6 |
import javax.xml.bind.annotation.XmlElementWrapper; |
| 7 |
|
| 8 |
import com.fasterxml.jackson.databind.MapperFeature; |
| 9 |
import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 |
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest; |
| 11 |
|
| 12 |
/** |
| 13 |
* Unit tests to verify handling of @XmlElementWrapper annotation. |
| 14 |
*/ |
| 15 |
public class TestElementWrapper extends BaseJaxbTest |
| 16 |
{ |
| 17 |
/* |
| 18 |
/********************************************************** |
| 19 |
/* Helper beans |
| 20 |
/********************************************************** |
| 21 |
*/ |
| 22 |
|
| 23 |
// Beans for [JACKSON-436] |
| 24 |
static class Person { |
| 25 |
@XmlElementWrapper(name="phones") |
| 26 |
@XmlElement(type=Phone.class) |
| 27 |
public Collection<IPhone> phone; |
| 28 |
} |
| 29 |
|
| 30 |
interface IPhone { |
| 31 |
public String getNumber(); |
| 32 |
} |
| 33 |
|
| 34 |
static class Phone implements IPhone |
| 35 |
{ |
| 36 |
private String number; |
| 37 |
|
| 38 |
public Phone() { } |
| 39 |
|
| 40 |
public Phone(String number) { this.number = number; } |
| 41 |
@Override |
| 42 |
public String getNumber() { return number; } |
| 43 |
public void setNumber(String number) { this.number = number; } |
| 44 |
} |
| 45 |
|
| 46 |
// [Issue#13] |
| 47 |
static class Bean13 |
| 48 |
{ |
| 49 |
@XmlElementWrapper(name="wrap") |
| 50 |
public int id; |
| 51 |
} |
| 52 |
|
| 53 |
// [Issue#25]: should also work with 'default' name |
| 54 |
static class Bean25 |
| 55 |
{ |
| 56 |
@XmlElement(name="element") |
| 57 |
// This would work |
| 58 |
// @XmlElementWrapper(name="values") |
| 59 |
@XmlElementWrapper |
| 60 |
public List<Integer> values; |
| 61 |
|
| 62 |
public Bean25() { } |
| 63 |
public Bean25(int... v0) { |
| 64 |
values = new ArrayList<Integer>(); |
| 65 |
for (int v : v0) { |
| 66 |
values.add(v); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
/********************************************************** |
| 73 |
/* Unit tests |
| 74 |
/********************************************************** |
| 75 |
*/ |
| 76 |
|
| 77 |
// [JACKSON-436] |
| 78 |
public void testWrapperWithCollection() throws Exception |
| 79 |
{ |
| 80 |
ObjectMapper mapper = getJaxbMapper(); |
| 81 |
// for fun, force renaming with wrapper annotation, even for JSON |
| 82 |
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME); |
| 83 |
Collection<IPhone> phones = new HashSet<IPhone>(); |
| 84 |
phones.add(new Phone("555-6666")); |
| 85 |
Person p = new Person(); |
| 86 |
p.phone = phones; |
| 87 |
|
| 88 |
String json = mapper.writeValueAsString(p); |
| 89 |
|
| 90 |
// as per |
| 91 |
assertEquals("{\"phones\":[{\"number\":\"555-6666\"}]}", json); |
| 92 |
|
| 93 |
// System.out.println("JSON == "+json); |
| 94 |
|
| 95 |
Person result = mapper.readValue(json, Person.class); |
| 96 |
assertNotNull(result.phone); |
| 97 |
assertEquals(1, result.phone.size()); |
| 98 |
assertEquals("555-6666", result.phone.iterator().next().getNumber()); |
| 99 |
} |
| 100 |
|
| 101 |
// [Issue#13] |
| 102 |
public void testWrapperRenaming() throws Exception |
| 103 |
{ |
| 104 |
ObjectMapper mapper = getJaxbMapper(); |
| 105 |
// verify that by default feature is off: |
| 106 |
assertFalse(mapper.isEnabled(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME)); |
| 107 |
Bean13 input = new Bean13(); |
| 108 |
input.id = 3; |
| 109 |
assertEquals("{\"id\":3}", mapper.writeValueAsString(input)); |
| 110 |
// but if we create new instance, configure |
| 111 |
mapper = getJaxbMapper(); |
| 112 |
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME); |
| 113 |
assertEquals("{\"wrap\":3}", mapper.writeValueAsString(input)); |
| 114 |
} |
| 115 |
|
| 116 |
// [Issue#25] |
| 117 |
public void testWrapperDefaultName() throws Exception |
| 118 |
{ |
| 119 |
ObjectMapper mapper = getJaxbMapper(); |
| 120 |
mapper = getJaxbMapper(); |
| 121 |
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME); |
| 122 |
Bean25 input = new Bean25(1, 2, 3); |
| 123 |
final String JSON = "{\"values\":[1,2,3]}"; |
| 124 |
assertEquals(JSON, mapper.writeValueAsString(input)); |
| 125 |
|
| 126 |
// plus needs to come back ok as well |
| 127 |
Bean25 result = mapper.readValue(JSON, Bean25.class); |
| 128 |
assertNotNull(result); |
| 129 |
assertNotNull(result.values); |
| 130 |
assertEquals(3, result.values.size()); |
| 131 |
|
| 132 |
// and finally verify roundtrip as well |
| 133 |
assertEquals(JSON, mapper.writeValueAsString(result)); |
| 134 |
} |
| 135 |
} |