8
8
* Unit tests for verifying that constraints on ordering of serialized
9
9
* properties are held.
10
10
*/
11
- public class TestSerializationOrder
11
+ public class SerializationOrderTest
12
12
extends BaseMapTest
13
13
{
14
14
static class BeanWithCreator
@@ -68,7 +68,7 @@ static class BeanFor459 {
68
68
69
69
// For [databind#311]
70
70
@ JsonPropertyOrder (alphabetic = true )
71
- public class BeanForGH311 {
71
+ static class BeanForGH311 {
72
72
private final int a ;
73
73
private final int b ;
74
74
@@ -82,6 +82,21 @@ public BeanForGH311(@JsonProperty("b") int b, @JsonProperty("a") int a) { //b an
82
82
public int getB () { return b ; }
83
83
}
84
84
85
+ // We'll expect ordering of "FUBAR"
86
+ @ JsonPropertyOrder ({ "f" })
87
+ static class OrderingByIndexBean {
88
+ public int r ;
89
+ public int a ;
90
+
91
+ @ JsonProperty (index = 1 )
92
+ public int b ;
93
+
94
+ @ JsonProperty (index = 0 )
95
+ public int u ;
96
+
97
+ public int f ;
98
+ }
99
+
85
100
/*
86
101
/*********************************************
87
102
/* Unit tests
@@ -90,19 +105,23 @@ public BeanForGH311(@JsonProperty("b") int b, @JsonProperty("a") int a) { //b an
90
105
91
106
private final ObjectMapper MAPPER = newJsonMapper ();
92
107
93
- public void testImplicitOrderByCreator () throws Exception
94
- {
95
- assertEquals ("{\" c\" :1,\" a\" :2,\" b\" :0}" , MAPPER .writeValueAsString (new BeanWithCreator (1 , 2 )));
108
+ private final ObjectMapper ALPHA_MAPPER = jsonMapperBuilder ()
109
+ .enable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY )
110
+ .build ();
111
+
112
+ public void testImplicitOrderByCreator () throws Exception {
113
+ assertEquals ("{\" c\" :1,\" a\" :2,\" b\" :0}" ,
114
+ MAPPER .writeValueAsString (new BeanWithCreator (1 , 2 )));
96
115
}
97
116
98
- public void testExplicitOrder () throws Exception
99
- {
100
- assertEquals ( "{ \" c \" :3, \" a \" :1, \" b \" :2, \" d \" :4}" , MAPPER .writeValueAsString (new BeanWithOrder (1 , 2 , 3 , 4 )));
117
+ public void testExplicitOrder () throws Exception {
118
+ assertEquals ( "{ \" c \" :3, \" a \" :1, \" b \" :2, \" d \" :4}" ,
119
+ MAPPER .writeValueAsString (new BeanWithOrder (1 , 2 , 3 , 4 )));
101
120
}
102
121
103
- public void testAlphabeticOrder () throws Exception
104
- {
105
- assertEquals ( "{ \" d \" :4, \" a \" :1, \" b \" :2, \" c \" :3}" , MAPPER .writeValueAsString (new SubBeanWithOrder (1 , 2 , 3 , 4 )));
122
+ public void testAlphabeticOrder () throws Exception {
123
+ assertEquals ( "{ \" d \" :4, \" a \" :1, \" b \" :2, \" c \" :3}" ,
124
+ MAPPER .writeValueAsString (new SubBeanWithOrder (1 , 2 , 3 , 4 )));
106
125
}
107
126
108
127
public void testOrderWithMixins () throws Exception
@@ -122,20 +141,23 @@ public void testOrderWrt268() throws Exception
122
141
123
142
public void testOrderWithFeature () throws Exception
124
143
{
125
- ObjectMapper mapper = jsonMapperBuilder ()
126
- .enable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY )
127
- .build ();
128
144
assertEquals ("{\" a\" :1,\" b\" :2,\" c\" :3,\" d\" :4}" ,
129
- mapper .writeValueAsString (new BeanFor459 ()));
145
+ ALPHA_MAPPER .writeValueAsString (new BeanFor459 ()));
130
146
}
131
147
132
148
// [databind#311]
133
149
public void testAlphaAndCreatorOrdering () throws Exception
134
150
{
135
- ObjectMapper mapper = jsonMapperBuilder ()
136
- .enable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY )
137
- .build ();
138
- String json = mapper .writeValueAsString (new BeanForGH311 (2 , 1 ));
151
+ String json = ALPHA_MAPPER .writeValueAsString (new BeanForGH311 (2 , 1 ));
139
152
assertEquals ("{\" a\" :1,\" b\" :2}" , json );
140
153
}
154
+
155
+ // [databind#2555]
156
+ public void testOrderByIndexEtc () throws Exception
157
+ {
158
+ // since "default" order can actually vary with later JDKs, only verify
159
+ // case of alphabetic-as-default
160
+ assertEquals (aposToQuotes ("{'f':0,'u':0,'b':0,'a':0,'r':0}" ),
161
+ ALPHA_MAPPER .writeValueAsString (new OrderingByIndexBean ()));
162
+ }
141
163
}
0 commit comments