| 1 |
package com.fasterxml.jackson.module.jaxb.introspect; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
import java.math.BigDecimal; |
| 5 |
import java.util.Map; |
| 6 |
|
| 7 |
import javax.xml.bind.annotation.*; |
| 8 |
|
| 9 |
import com.fasterxml.jackson.databind.*; |
| 10 |
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair; |
| 11 |
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; |
| 12 |
import com.fasterxml.jackson.databind.type.TypeFactory; |
| 13 |
|
| 14 |
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest; |
| 15 |
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector; |
| 16 |
|
| 17 |
/** |
| 18 |
* Tests for verifying auto-detection settings with JAXB annotations. |
| 19 |
* |
| 20 |
* @author Tatu Saloranta |
| 21 |
*/ |
| 22 |
public class TestJaxbAutoDetect extends BaseJaxbTest |
| 23 |
{ |
| 24 |
/* |
| 25 |
/********************************************************** |
| 26 |
/* Helper beans |
| 27 |
/********************************************************** |
| 28 |
*/ |
| 29 |
|
| 30 |
/* Bean for testing problem [JACKSON-183]: with normal |
| 31 |
* auto-detect enabled, 2 fields visible; if disabled, just 1. |
| 32 |
* NOTE: should NOT include "XmlAccessorType", since it will |
| 33 |
* have priority over global defaults |
| 34 |
*/ |
| 35 |
static class Jackson183Bean { |
| 36 |
public String getA() { return "a"; } |
| 37 |
|
| 38 |
@XmlElement public String getB() { return "b"; } |
| 39 |
|
| 40 |
// JAXB (or Bean introspection) mandates use of matching setters... |
| 41 |
public void setA(String str) { } |
| 42 |
public void setB(String str) { } |
| 43 |
} |
| 44 |
|
| 45 |
static class Identified |
| 46 |
{ |
| 47 |
Object id; |
| 48 |
|
| 49 |
@XmlAttribute(name="id") |
| 50 |
public Object getIdObject() { |
| 51 |
return id; |
| 52 |
} |
| 53 |
public void setId(Object id) { this.id = id; } |
| 54 |
} |
| 55 |
|
| 56 |
@XmlRootElement(name="bah") |
| 57 |
public static class JaxbAnnotatedObject { |
| 58 |
|
| 59 |
private BigDecimal number; |
| 60 |
|
| 61 |
public JaxbAnnotatedObject() { } |
| 62 |
|
| 63 |
public JaxbAnnotatedObject(String number) { |
| 64 |
this.number = new BigDecimal(number); |
| 65 |
} |
| 66 |
|
| 67 |
@XmlElement |
| 68 |
public void setNumber(BigDecimal number) { |
| 69 |
this.number = number; |
| 70 |
} |
| 71 |
|
| 72 |
@XmlTransient |
| 73 |
public BigDecimal getNumber() { |
| 74 |
return number; |
| 75 |
} |
| 76 |
|
| 77 |
@XmlElement(name = "number") |
| 78 |
public BigDecimal getNumberString() { |
| 79 |
return number; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@SuppressWarnings("serial") |
| 84 |
public static class DualAnnotationObjectMapper extends ObjectMapper { |
| 85 |
|
| 86 |
public DualAnnotationObjectMapper() { |
| 87 |
super(); |
| 88 |
AnnotationIntrospector primary = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); |
| 89 |
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); |
| 90 |
|
| 91 |
// make de/serializer use JAXB annotations first, then jackson ones |
| 92 |
AnnotationIntrospector pair = new AnnotationIntrospectorPair(primary, secondary); |
| 93 |
setAnnotationIntrospector(pair); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
/********************************************************** |
| 99 |
/* Unit tests |
| 100 |
/********************************************************** |
| 101 |
*/ |
| 102 |
|
| 103 |
public void testAutoDetectDisable() throws IOException |
| 104 |
{ |
| 105 |
ObjectMapper mapper = getJaxbMapper(); |
| 106 |
Jackson183Bean bean = new Jackson183Bean(); |
| 107 |
Map<String,Object> result; |
| 108 |
|
| 109 |
// Ok: by default, should see 2 fields: |
| 110 |
result = writeAndMap(mapper, bean); |
| 111 |
assertEquals(2, result.size()); |
| 112 |
assertEquals("a", result.get("a")); |
| 113 |
assertEquals("b", result.get("b")); |
| 114 |
|
| 115 |
// But when disabling auto-detection, just one |
| 116 |
mapper = getJaxbMapper(); |
| 117 |
mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false); |
| 118 |
result = writeAndMap(mapper, bean); |
| 119 |
assertEquals(1, result.size()); |
| 120 |
assertNull(result.get("a")); |
| 121 |
assertEquals("b", result.get("b")); |
| 122 |
} |
| 123 |
|
| 124 |
public void testIssue246() throws IOException |
| 125 |
{ |
| 126 |
ObjectMapper mapper = getJaxbMapper(); |
| 127 |
Identified id = new Identified(); |
| 128 |
id.id = "123"; |
| 129 |
assertEquals("{\"id\":\"123\"}", mapper.writeValueAsString(id)); |
| 130 |
} |
| 131 |
|
| 132 |
// [JACKSON-556] |
| 133 |
public void testJaxbAnnotatedObject() throws Exception |
| 134 |
{ |
| 135 |
JaxbAnnotatedObject original = new JaxbAnnotatedObject("123"); |
| 136 |
ObjectMapper mapper = new DualAnnotationObjectMapper(); |
| 137 |
String json = mapper.writeValueAsString(original); |
| 138 |
assertFalse("numberString field in JSON", json.contains("numberString")); // kinda hack-y :) |
| 139 |
JaxbAnnotatedObject result = mapper.readValue(json, JaxbAnnotatedObject.class); |
| 140 |
assertEquals(new BigDecimal("123"), result.number); |
| 141 |
} |
| 142 |
|
| 143 |
/* |
| 144 |
public void testJaxbAnnotatedObjectXML() throws Exception |
| 145 |
{ |
| 146 |
JAXBContext ctxt = JAXBContext.newInstance(JaxbAnnotatedObject.class); |
| 147 |
JaxbAnnotatedObject original = new JaxbAnnotatedObject("123"); |
| 148 |
StringWriter sw = new StringWriter(); |
| 149 |
ctxt.createMarshaller().marshal(original, sw); |
| 150 |
String xml = sw.toString(); |
| 151 |
JaxbAnnotatedObject result = (JaxbAnnotatedObject) ctxt.createUnmarshaller().unmarshal(new StringReader(xml)); |
| 152 |
assertEquals(new BigDecimal("123"), result.number); |
| 153 |
} |
| 154 |
*/ |
| 155 |
} |