Debian Codegraph @

src/test/java/com/fasterxml/jackson/module/jaxb/misc/TestXmlAnyElementWithElementRef.java

raw
1 package com.fasterxml.jackson.module.jaxb.misc;
2
3 import java.util.*;
4
5 import javax.xml.bind.annotation.*;
6
7 import com.fasterxml.jackson.databind.ObjectMapper;
8 import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
9
10 public class TestXmlAnyElementWithElementRef
11 extends BaseJaxbTest
12 {
13 static class Bean {
14 @XmlAnyElement(lax=true)
15 @XmlElementRefs({
16 @XmlElementRef(name="a", type=Name.class),
17 @XmlElementRef(name="b", type=Count.class)
18 })
19 public List<Object> others;
20
21 public Bean() { }
22 public Bean(Object ob) {
23 others = new ArrayList<Object>();
24 others.add(ob);
25 }
26 }
27
28 static class Name {
29 public String name;
30 }
31
32 static class Count {
33 public int count;
34 }
35
36 /*
37 /**********************************************************
38 /* Test methods
39 /**********************************************************
40 */
41
42 // [JACKSON-254]: verify that things do work
43 public void testXmlAnyElementWithElementRef() throws Exception
44 {
45 ObjectMapper mapper = getJaxbMapper();
46
47 Count count = new Count();
48 count.count = 8;
49 Bean value = new Bean(count);
50
51 // typed handling should be triggered by annotation on property, so
52 String json = mapper.writeValueAsString(value);
53 assertEquals("{\"others\":[{\"b\":{\"count\":8}}]}", json);
54
55 Bean result = mapper.readValue(json, Bean.class);
56 assertNotNull(result);
57 assertEquals(1, result.others.size());
58 Object resultOb = result.others.get(0);
59 assertSame(Count.class, resultOb.getClass());
60 assertEquals(8, ((Count) resultOb).count);
61 }
62
63 }