| 1 |
package com.fasterxml.jackson.module.jaxb.introspect; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
|
| 5 |
import javax.xml.bind.annotation.*; |
| 6 |
|
| 7 |
import com.fasterxml.jackson.databind.*; |
| 8 |
|
| 9 |
import com.fasterxml.jackson.module.jaxb.BaseJaxbTest; |
| 10 |
|
| 11 |
public class TestPropertyVisibility |
| 12 |
extends BaseJaxbTest |
| 13 |
{ |
| 14 |
@XmlAccessorType(XmlAccessType.NONE) |
| 15 |
protected static class Bean354 |
| 16 |
{ |
| 17 |
protected String name = "foo"; |
| 18 |
|
| 19 |
@XmlElement |
| 20 |
protected String getName() { return name; } |
| 21 |
|
| 22 |
public void setName(String s) { name = s; } |
| 23 |
} |
| 24 |
|
| 25 |
// Note: full example would be "Content"; but let's use simpler demonstration here, easier to debug |
| 26 |
@XmlAccessorType(XmlAccessType.PROPERTY) |
| 27 |
static class Jackson539Bean |
| 28 |
{ |
| 29 |
protected int type; |
| 30 |
|
| 31 |
@XmlTransient |
| 32 |
public String getType() { |
| 33 |
throw new UnsupportedOperationException(); |
| 34 |
} |
| 35 |
|
| 36 |
public void setType(String type) { |
| 37 |
throw new UnsupportedOperationException(); |
| 38 |
} |
| 39 |
|
| 40 |
@XmlAttribute(name = "type") |
| 41 |
public int getRawType() { |
| 42 |
return type; |
| 43 |
} |
| 44 |
|
| 45 |
public void setRawType(int type) { |
| 46 |
this.type = type; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/* |
| 51 |
/********************************************************** |
| 52 |
/* Unit tests |
| 53 |
/********************************************************** |
| 54 |
*/ |
| 55 |
|
| 56 |
// Verify serialization wrt [JACKSON-354] |
| 57 |
// |
| 58 |
// NOTE: fails currently because we use Bean Introspector which only sees public methods -- need to rewrite |
| 59 |
public void testJackson354Serialization() throws IOException |
| 60 |
{ |
| 61 |
ObjectMapper mapper = getJaxbMapper(); |
| 62 |
assertEquals("{\"name\":\"foo\"}", mapper.writeValueAsString(new Bean354())); |
| 63 |
} |
| 64 |
|
| 65 |
// For [JACKSON-539] |
| 66 |
public void testJacksonSerialization() |
| 67 |
throws Exception |
| 68 |
{ |
| 69 |
/* Earlier |
| 70 |
Content content = new Content(); |
| 71 |
content.setRawType("application/json"); |
| 72 |
String json = mapper.writeValueAsString(content); |
| 73 |
Content content2 = mapper.readValue(json, Content.class); // deserialize |
| 74 |
assertNotNull(content2); |
| 75 |
*/ |
| 76 |
|
| 77 |
Jackson539Bean input = new Jackson539Bean(); |
| 78 |
input.type = 123; |
| 79 |
ObjectMapper mapper = getJaxbMapper(); |
| 80 |
String json = mapper.writeValueAsString(input); |
| 81 |
Jackson539Bean result = mapper.readValue(json, Jackson539Bean.class); |
| 82 |
assertNotNull(result); |
| 83 |
assertEquals(123, result.type); |
| 84 |
} |
| 85 |
} |