| 1 |
package com.fasterxml.jackson.module.jaxb.adapters; |
| 2 |
|
| 3 |
import javax.xml.bind.annotation.adapters.XmlAdapter; |
| 4 |
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
| 5 |
|
| 6 |
import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 |
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest; |
| 8 |
|
| 9 |
/** |
| 10 |
* Failing unit tests related to Adapter handling. |
| 11 |
*/ |
| 12 |
public class TestIdentityAdapters extends BaseJaxbTest |
| 13 |
{ |
| 14 |
// [Issue-10]: Infinite recursion in "self" adapters |
| 15 |
public static class IdentityAdapter extends XmlAdapter<IdentityAdapterBean, IdentityAdapterBean> { |
| 16 |
@Override |
| 17 |
public IdentityAdapterBean unmarshal(IdentityAdapterBean b) { |
| 18 |
return new IdentityAdapterBean(b.value + "U"); |
| 19 |
} |
| 20 |
|
| 21 |
@Override |
| 22 |
public IdentityAdapterBean marshal(IdentityAdapterBean b) { |
| 23 |
return new IdentityAdapterBean(b.value + "M"); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public static class IdentityStringAdapter extends XmlAdapter<String, String> { |
| 28 |
@Override |
| 29 |
public String unmarshal(String b) { |
| 30 |
return b + "U"; |
| 31 |
} |
| 32 |
@Override |
| 33 |
public String marshal(String b) { |
| 34 |
return b + "M"; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
@XmlJavaTypeAdapter(IdentityAdapter.class) |
| 39 |
static class IdentityAdapterBean |
| 40 |
{ |
| 41 |
public String value; |
| 42 |
|
| 43 |
protected IdentityAdapterBean() { } |
| 44 |
public IdentityAdapterBean(String s) { value = s; } |
| 45 |
} |
| 46 |
|
| 47 |
static class IdentityAdapterPropertyBean |
| 48 |
{ |
| 49 |
@XmlJavaTypeAdapter(IdentityStringAdapter.class) |
| 50 |
public String value; |
| 51 |
|
| 52 |
public IdentityAdapterPropertyBean() { } |
| 53 |
public IdentityAdapterPropertyBean(String s) { value = s; } |
| 54 |
} |
| 55 |
|
| 56 |
/* |
| 57 |
/********************************************************** |
| 58 |
/* Unit tests |
| 59 |
/********************************************************** |
| 60 |
*/ |
| 61 |
|
| 62 |
// [Issue-10] |
| 63 |
public void testIdentityAdapterForClass() throws Exception |
| 64 |
{ |
| 65 |
IdentityAdapterBean input = new IdentityAdapterBean("A"); |
| 66 |
ObjectMapper mapper = getJaxbMapper(); |
| 67 |
|
| 68 |
String json = mapper.writeValueAsString(input); |
| 69 |
assertEquals("{\"value\":\"AM\"}", json); |
| 70 |
|
| 71 |
IdentityAdapterBean result = mapper.readValue(json, IdentityAdapterBean.class); |
| 72 |
assertEquals("AMU", result.value); |
| 73 |
} |
| 74 |
|
| 75 |
// [Issue-10] |
| 76 |
public void testIdentityAdapterForProperty() throws Exception |
| 77 |
{ |
| 78 |
IdentityAdapterPropertyBean input = new IdentityAdapterPropertyBean("B"); |
| 79 |
ObjectMapper mapper = getJaxbMapper(); |
| 80 |
String json = mapper.writeValueAsString(input); |
| 81 |
assertEquals("{\"value\":\"BM\"}", json); |
| 82 |
IdentityAdapterPropertyBean result = mapper.readValue(json, IdentityAdapterPropertyBean.class); |
| 83 |
assertEquals("BMU", result.value); |
| 84 |
} |
| 85 |
} |