Debian Codegraph @

src/test/java/com/fasterxml/jackson/module/jaxb/introspect/TestPropertyOrdering.java

raw
1 package com.fasterxml.jackson.module.jaxb.introspect;
2
3 import javax.xml.bind.annotation.*;
4
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import com.fasterxml.jackson.module.jaxb.BaseJaxbTest;
7
8 public class TestPropertyOrdering
9 extends BaseJaxbTest
10 {
11 @XmlType(propOrder = {"cparty", "contacts"})
12 @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
13 public class BeanFor268
14 {
15 private String cpartyDto = "dto";
16 private int[] contacts = new int[] { 1, 2, 3 };
17
18 @XmlElement(name="cparty")
19 public String getCpartyDto() { return cpartyDto; }
20 public void setCpartyDto(String cpartyDto) { this.cpartyDto = cpartyDto; }
21
22 @XmlElement(name="contacts")
23 public int[] getContact() { return contacts; }
24 public void setContact(int[] contacts) { this.contacts = contacts; }
25 }
26
27 /* Also, considering that JAXB actually seems to expect original
28 * names for property ordering, let's see that alternative
29 * annotation also works
30 * (see [JACKSON-268] for more details)
31 */
32 @XmlType(propOrder = {"cpartyDto", "contacts"})
33 @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
34 public class BeanWithImplicitNames
35 {
36 private String cpartyDto = "dto";
37 private int[] contacts = new int[] { 1, 2, 3 };
38
39 @XmlElement(name="cparty")
40 public String getCpartyDto() { return cpartyDto; }
41 public void setCpartyDto(String cpartyDto) { this.cpartyDto = cpartyDto; }
42
43 @XmlElement(name="contacts")
44 public int[] getContact() { return contacts; }
45 public void setContact(int[] contacts) { this.contacts = contacts; }
46 }
47
48 @XmlType(propOrder={"b", "a", "c"})
49 public static class AlphaBean2
50 {
51 public int c = 3;
52 public int a = 1;
53 public int b = 2;
54 }
55
56 /*
57 /**********************************************************
58 /* Tests
59 /**********************************************************
60 */
61
62 public void testSerializationExplicitOrdering() throws Exception
63 {
64 ObjectMapper mapper = getJaxbMapper();
65 assertEquals("{\"b\":2,\"a\":1,\"c\":3}", serializeAsString(mapper, new AlphaBean2()));
66 }
67
68 // Trying to reproduce [JACKSON-268]
69 public void testOrderingWithRename() throws Exception
70 {
71 ObjectMapper mapper = getJaxbMapper();
72 assertEquals("{\"cparty\":\"dto\",\"contacts\":[1,2,3]}", mapper.writeValueAsString(new BeanFor268()));
73 }
74
75 public void testOrderingWithOriginalPropName() throws Exception
76 {
77 ObjectMapper mapper = getJaxbMapper();
78 assertEquals("{\"cparty\":\"dto\",\"contacts\":[1,2,3]}",
79 mapper.writeValueAsString(new BeanWithImplicitNames()));
80 }
81 }