| 1 |
package com.fasterxml.jackson.module.jaxb.adapters; |
| 2 |
|
| 3 |
import java.util.*; |
| 4 |
|
| 5 |
import javax.xml.bind.annotation.*; |
| 6 |
import javax.xml.bind.annotation.adapters.XmlAdapter; |
| 7 |
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
| 8 |
|
| 9 |
import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 |
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest; |
| 11 |
|
| 12 |
/** |
| 13 |
* Unit tests for checking that JAXB type adapters work (to some |
| 14 |
* degree, anyway). |
| 15 |
* Related to issues [JACKSON-288], [JACKSON-411] |
| 16 |
* |
| 17 |
* @author tatu |
| 18 |
*/ |
| 19 |
public class TestAdapters extends BaseJaxbTest |
| 20 |
{ |
| 21 |
public static class SillyAdapter extends XmlAdapter<String, Date> |
| 22 |
{ |
| 23 |
public SillyAdapter() { } |
| 24 |
|
| 25 |
@Override |
| 26 |
public Date unmarshal(String date) throws Exception { |
| 27 |
return new Date(29L); |
| 28 |
} |
| 29 |
|
| 30 |
@Override |
| 31 |
public String marshal(Date date) throws Exception { |
| 32 |
return "XXX"; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
static class Bean |
| 37 |
{ |
| 38 |
@XmlJavaTypeAdapter(SillyAdapter.class) |
| 39 |
public Date value; |
| 40 |
|
| 41 |
public Bean() { } |
| 42 |
public Bean(long l) { value = new Date(l); } |
| 43 |
} |
| 44 |
|
| 45 |
// For [JACKSON-288] |
| 46 |
|
| 47 |
static class Bean288 { |
| 48 |
public List<Person> persons; |
| 49 |
|
| 50 |
public Bean288() { } |
| 51 |
public Bean288(String str) { |
| 52 |
persons = new ArrayList<Person>(); |
| 53 |
persons.add(new Person(str)); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static class Person |
| 58 |
{ |
| 59 |
public String name; |
| 60 |
|
| 61 |
@XmlElement(required = true, type = String.class) |
| 62 |
@XmlJavaTypeAdapter(DateAdapter.class) |
| 63 |
protected Calendar date; |
| 64 |
|
| 65 |
public Person() { } |
| 66 |
public Person(String n) { |
| 67 |
name = n; |
| 68 |
date = Calendar.getInstance(); |
| 69 |
date.setTime(new Date(0L)); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public static class DateAdapter |
| 74 |
extends XmlAdapter<String, Calendar> |
| 75 |
{ |
| 76 |
public DateAdapter() { } |
| 77 |
|
| 78 |
@Override |
| 79 |
public Calendar unmarshal(String value) { |
| 80 |
return (javax.xml.bind.DatatypeConverter.parseDateTime(value)); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public String marshal(Calendar value) { |
| 85 |
if (value == null) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
return (javax.xml.bind.DatatypeConverter.printDateTime(value)); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// [JACKSON-656] |
| 93 |
|
| 94 |
@XmlAccessorType(XmlAccessType.FIELD) |
| 95 |
@XmlType(name = "Paging", propOrder = { "numFound" }) |
| 96 |
public static class Bean656 { |
| 97 |
@XmlElement(type = String.class) |
| 98 |
@XmlJavaTypeAdapter(Adapter1.class) |
| 99 |
@XmlSchemaType(name = "long") |
| 100 |
protected Long numFound; |
| 101 |
|
| 102 |
public Long getNumFound() { |
| 103 |
return numFound; |
| 104 |
} |
| 105 |
|
| 106 |
public void setNumFound(Long value) { |
| 107 |
this.numFound = value; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
public static class Adapter1 extends XmlAdapter<String, Long> { |
| 112 |
@Override |
| 113 |
public Long unmarshal(String value) { |
| 114 |
return ((long) javax.xml.bind.DatatypeConverter.parseLong(value)); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public String marshal(Long value) { |
| 119 |
if (value == null) { |
| 120 |
return null; |
| 121 |
} |
| 122 |
return (javax.xml.bind.DatatypeConverter.printLong((long) (long) value)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// [Issue-10]: Infinite recursion in "self" adapters |
| 127 |
public static class IdentityAdapter extends XmlAdapter<IdentityAdapterBean, IdentityAdapterBean> { |
| 128 |
@Override |
| 129 |
public IdentityAdapterBean unmarshal(IdentityAdapterBean b) { |
| 130 |
b.value += "U"; |
| 131 |
return b; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public IdentityAdapterBean marshal(IdentityAdapterBean b) { |
| 136 |
if (b != null) { |
| 137 |
b.value += "M"; |
| 138 |
} |
| 139 |
return b; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
@XmlJavaTypeAdapter(IdentityAdapter.class) |
| 144 |
static class IdentityAdapterBean |
| 145 |
{ |
| 146 |
public String value; |
| 147 |
|
| 148 |
public IdentityAdapterBean() { } |
| 149 |
public IdentityAdapterBean(String s) { value = s; } |
| 150 |
} |
| 151 |
|
| 152 |
static class IdentityAdapterPropertyBean |
| 153 |
{ |
| 154 |
@XmlJavaTypeAdapter(IdentityAdapter.class) |
| 155 |
public String value; |
| 156 |
|
| 157 |
public IdentityAdapterPropertyBean() { } |
| 158 |
public IdentityAdapterPropertyBean(String s) { value = s; } |
| 159 |
} |
| 160 |
|
| 161 |
/* |
| 162 |
/********************************************************** |
| 163 |
/* Unit tests |
| 164 |
/********************************************************** |
| 165 |
*/ |
| 166 |
|
| 167 |
public void testSimpleAdapterSerialization() throws Exception |
| 168 |
{ |
| 169 |
Bean bean = new Bean(123L); |
| 170 |
assertEquals("{\"value\":\"XXX\"}", getJaxbMapper().writeValueAsString(bean)); |
| 171 |
} |
| 172 |
|
| 173 |
public void testSimpleAdapterDeserialization() throws Exception |
| 174 |
{ |
| 175 |
Bean bean = getJaxbMapper().readValue("{\"value\":\"abc\"}", Bean.class); |
| 176 |
assertNotNull(bean.value); |
| 177 |
assertEquals(29L, bean.value.getTime()); |
| 178 |
} |
| 179 |
|
| 180 |
// [JACKSON-288] |
| 181 |
public void testDateAdapter() throws Exception |
| 182 |
{ |
| 183 |
Bean288 input = new Bean288("test"); |
| 184 |
ObjectMapper mapper = getJaxbMapper(); |
| 185 |
String json = mapper.writeValueAsString(input); |
| 186 |
Bean288 output = mapper.readValue(json, Bean288.class); |
| 187 |
assertNotNull(output); |
| 188 |
} |
| 189 |
|
| 190 |
// [JACKSON-656] |
| 191 |
|
| 192 |
public void testJackson656() throws Exception |
| 193 |
{ |
| 194 |
Bean656 bean = new Bean656(); |
| 195 |
bean.setNumFound(3232l); |
| 196 |
ObjectMapper mapper = getJaxbMapper(); |
| 197 |
String json = mapper.writeValueAsString(bean); |
| 198 |
assertEquals("{\"numFound\":\"3232\"}", json); |
| 199 |
} |
| 200 |
} |