| 1 |
package com.fasterxml.jackson.module.jaxb.adapters; |
| 2 |
|
| 3 |
import java.util.*; |
| 4 |
import java.util.Map.Entry; |
| 5 |
|
| 6 |
import javax.xml.bind.annotation.*; |
| 7 |
import javax.xml.bind.annotation.adapters.*; |
| 8 |
|
| 9 |
import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 |
import com.fasterxml.jackson.databind.SerializationFeature; |
| 11 |
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest; |
| 12 |
import com.fasterxml.jackson.module.jaxb.introspect.TestJaxbAnnotationIntrospector.KeyValuePair; |
| 13 |
|
| 14 |
/** |
| 15 |
* Unit tests to check that {@link XmlAdapter}s also work with |
| 16 |
* container types (Lists, Maps) |
| 17 |
*/ |
| 18 |
public class TestAdaptersForContainers extends BaseJaxbTest |
| 19 |
{ |
| 20 |
// Support for Maps |
| 21 |
|
| 22 |
@XmlRootElement |
| 23 |
@XmlAccessorType(XmlAccessType.FIELD) |
| 24 |
public static class ParentJAXBBean |
| 25 |
{ |
| 26 |
@XmlJavaTypeAdapter(JAXBMapAdapter.class) |
| 27 |
private Map<String, String> params = new HashMap<String, String>(); |
| 28 |
|
| 29 |
public Map<String, String> getParams() { |
| 30 |
return params; |
| 31 |
} |
| 32 |
|
| 33 |
public void setParams(Map<String, String> params) { |
| 34 |
this.params = params; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public static class JAXBMapAdapter extends XmlAdapter<List<KeyValuePair>,Map<String, String>> { |
| 39 |
|
| 40 |
@Override |
| 41 |
public List<KeyValuePair> marshal(Map<String, String> arg0) throws Exception { |
| 42 |
List<KeyValuePair> keyValueList = new ArrayList<KeyValuePair>(); |
| 43 |
for(Entry<String, String> entry : arg0.entrySet()) { |
| 44 |
KeyValuePair keyValuePair = new KeyValuePair(); |
| 45 |
keyValuePair.setKey(entry.getKey()); |
| 46 |
keyValuePair.setValue(entry.getValue()); |
| 47 |
keyValueList.add(keyValuePair); |
| 48 |
} |
| 49 |
return keyValueList; |
| 50 |
} |
| 51 |
@Override |
| 52 |
public Map<String, String> unmarshal(List<KeyValuePair> arg0) throws Exception |
| 53 |
{ |
| 54 |
HashMap<String, String> hashMap = new HashMap<String, String>(); |
| 55 |
for (int i = 0; i < arg0.size(); i++) { |
| 56 |
hashMap.put(arg0.get(i).getKey(), arg0.get(i).getValue()); |
| 57 |
} |
| 58 |
return hashMap; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// [JACKSON-722] |
| 63 |
|
| 64 |
public static class SillyAdapter extends XmlAdapter<String, Date> |
| 65 |
{ |
| 66 |
public SillyAdapter() { } |
| 67 |
|
| 68 |
@Override |
| 69 |
public Date unmarshal(String date) throws Exception { |
| 70 |
return new Date(29L); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public String marshal(Date date) throws Exception { |
| 75 |
return "XXX"; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
static class Wrapper { |
| 80 |
@XmlJavaTypeAdapter(SillyAdapter.class) |
| 81 |
public List<Date> values; |
| 82 |
|
| 83 |
public Wrapper() { } |
| 84 |
public Wrapper(long l) { |
| 85 |
values = new ArrayList<Date>(); |
| 86 |
values.add(new Date(l)); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
static class WrapperWithGetterAndSetter { |
| 91 |
private List<Date> values; |
| 92 |
|
| 93 |
public WrapperWithGetterAndSetter() { } |
| 94 |
public WrapperWithGetterAndSetter(long l) { |
| 95 |
values = new ArrayList<Date>(); |
| 96 |
values.add(new Date(l)); |
| 97 |
} |
| 98 |
|
| 99 |
@XmlJavaTypeAdapter(SillyAdapter.class) |
| 100 |
public List<Date> getValues() { |
| 101 |
return values; |
| 102 |
} |
| 103 |
|
| 104 |
public void setValues(List<Date> values) { |
| 105 |
this.values = values; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
/********************************************************** |
| 111 |
/* Unit tests, Lists |
| 112 |
/********************************************************** |
| 113 |
*/ |
| 114 |
|
| 115 |
public void testAdapterForList() throws Exception |
| 116 |
{ |
| 117 |
Wrapper w = new Wrapper(123L); |
| 118 |
assertEquals("{\"values\":[\"XXX\"]}", getJaxbMapper().writeValueAsString(w)); |
| 119 |
} |
| 120 |
|
| 121 |
public void testSimpleAdapterDeserialization() throws Exception |
| 122 |
{ |
| 123 |
Wrapper w = getJaxbMapper().readValue("{\"values\":[\"abc\"]}", Wrapper.class); |
| 124 |
assertNotNull(w); |
| 125 |
assertNotNull(w.values); |
| 126 |
assertEquals(1, w.values.size()); |
| 127 |
assertEquals(29L, w.values.get(0).getTime()); |
| 128 |
} |
| 129 |
|
| 130 |
public void testAdapterOnGetterSerialization() throws Exception |
| 131 |
{ |
| 132 |
WrapperWithGetterAndSetter w = new WrapperWithGetterAndSetter(123L); |
| 133 |
assertEquals("{\"values\":[\"XXX\"]}", getJaxbMapper().writeValueAsString(w)); |
| 134 |
} |
| 135 |
|
| 136 |
public void testAdapterOnGetterDeserialization() throws Exception |
| 137 |
{ |
| 138 |
WrapperWithGetterAndSetter w = getJaxbMapper().readValue("{\"values\":[\"abc\"]}", |
| 139 |
WrapperWithGetterAndSetter.class); |
| 140 |
assertNotNull(w); |
| 141 |
assertNotNull(w.values); |
| 142 |
assertEquals(1, w.values.size()); |
| 143 |
assertEquals(29L, w.values.get(0).getTime()); |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
/********************************************************** |
| 148 |
/* Unit tests, Map-related |
| 149 |
/********************************************************** |
| 150 |
*/ |
| 151 |
|
| 152 |
public void testAdapterForBeanWithMap() throws Exception |
| 153 |
{ |
| 154 |
ObjectMapper mapper = getJaxbMapper(); |
| 155 |
mapper.enable(SerializationFeature.INDENT_OUTPUT); |
| 156 |
ParentJAXBBean parentJaxbBean = new ParentJAXBBean(); |
| 157 |
HashMap<String, String> params = new HashMap<String, String>(); |
| 158 |
params.put("sampleKey", "sampleValue"); |
| 159 |
parentJaxbBean.setParams(params); |
| 160 |
|
| 161 |
String json = mapper.writeValueAsString(parentJaxbBean); |
| 162 |
|
| 163 |
// uncomment to see what the json looks like. |
| 164 |
//System.out.println(json); |
| 165 |
|
| 166 |
//now make sure it gets deserialized correctly. |
| 167 |
ParentJAXBBean readEx = mapper.readValue(json, ParentJAXBBean.class); |
| 168 |
assertEquals("sampleValue", readEx.getParams().get("sampleKey")); |
| 169 |
} |
| 170 |
|
| 171 |
} |