1
1
/*
2
- * Copyright 2012-2023 the original author or authors.
2
+ * Copyright 2012-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -59,7 +59,7 @@ void gsonRegistration() {
59
59
}
60
60
61
61
@ Test
62
- void generateNonExecutableJson () {
62
+ void generateNonExecutableJsonTrue () {
63
63
this .contextRunner .withPropertyValues ("spring.gson.generate-non-executable-json:true" ).run ((context ) -> {
64
64
Gson gson = context .getBean (Gson .class );
65
65
assertThat (gson .toJson (new DataObject ())).isNotEqualTo ("{\" data\" :1}" );
@@ -68,14 +68,31 @@ void generateNonExecutableJson() {
68
68
}
69
69
70
70
@ Test
71
- void excludeFieldsWithoutExposeAnnotation () {
71
+ void generateNonExecutableJsonFalse () {
72
+ this .contextRunner .withPropertyValues ("spring.gson.generate-non-executable-json:false" ).run ((context ) -> {
73
+ Gson gson = context .getBean (Gson .class );
74
+ assertThat (gson .toJson (new DataObject ())).isEqualTo ("{\" data\" :1}" );
75
+ });
76
+ }
77
+
78
+ @ Test
79
+ void excludeFieldsWithoutExposeAnnotationTrue () {
72
80
this .contextRunner .withPropertyValues ("spring.gson.exclude-fields-without-expose-annotation:true" )
73
81
.run ((context ) -> {
74
82
Gson gson = context .getBean (Gson .class );
75
83
assertThat (gson .toJson (new DataObject ())).isEqualTo ("{}" );
76
84
});
77
85
}
78
86
87
+ @ Test
88
+ void excludeFieldsWithoutExposeAnnotationFalse () {
89
+ this .contextRunner .withPropertyValues ("spring.gson.exclude-fields-without-expose-annotation:false" )
90
+ .run ((context ) -> {
91
+ Gson gson = context .getBean (Gson .class );
92
+ assertThat (gson .toJson (new DataObject ())).isEqualTo ("{\" data\" :1}" );
93
+ });
94
+ }
95
+
79
96
@ Test
80
97
void serializeNullsTrue () {
81
98
this .contextRunner .withPropertyValues ("spring.gson.serialize-nulls:true" ).run ((context ) -> {
@@ -93,7 +110,7 @@ void serializeNullsFalse() {
93
110
}
94
111
95
112
@ Test
96
- void enableComplexMapKeySerialization () {
113
+ void enableComplexMapKeySerializationTrue () {
97
114
this .contextRunner .withPropertyValues ("spring.gson.enable-complex-map-key-serialization:true" )
98
115
.run ((context ) -> {
99
116
Gson gson = context .getBean (Gson .class );
@@ -103,6 +120,17 @@ void enableComplexMapKeySerialization() {
103
120
});
104
121
}
105
122
123
+ @ Test
124
+ void enableComplexMapKeySerializationFalse () {
125
+ this .contextRunner .withPropertyValues ("spring.gson.enable-complex-map-key-serialization:false" )
126
+ .run ((context ) -> {
127
+ Gson gson = context .getBean (Gson .class );
128
+ Map <DataObject , String > original = new LinkedHashMap <>();
129
+ original .put (new DataObject (), "a" );
130
+ assertThat (gson .toJson (original )).contains (DataObject .class .getName ()).doesNotContain ("\" data\" :" );
131
+ });
132
+ }
133
+
106
134
@ Test
107
135
void notDisableInnerClassSerialization () {
108
136
this .contextRunner .run ((context ) -> {
@@ -113,14 +141,23 @@ void notDisableInnerClassSerialization() {
113
141
}
114
142
115
143
@ Test
116
- void disableInnerClassSerialization () {
144
+ void disableInnerClassSerializationTrue () {
117
145
this .contextRunner .withPropertyValues ("spring.gson.disable-inner-class-serialization:true" ).run ((context ) -> {
118
146
Gson gson = context .getBean (Gson .class );
119
147
WrapperObject wrapperObject = new WrapperObject ();
120
148
assertThat (gson .toJson (wrapperObject .new NestedObject ())).isEqualTo ("null" );
121
149
});
122
150
}
123
151
152
+ @ Test
153
+ void disableInnerClassSerializationFalse () {
154
+ this .contextRunner .withPropertyValues ("spring.gson.disable-inner-class-serialization:false" ).run ((context ) -> {
155
+ Gson gson = context .getBean (Gson .class );
156
+ WrapperObject wrapperObject = new WrapperObject ();
157
+ assertThat (gson .toJson (wrapperObject .new NestedObject ())).isEqualTo ("{\" data\" :\" nested\" }" );
158
+ });
159
+ }
160
+
124
161
@ Test
125
162
void withLongSerializationPolicy () {
126
163
this .contextRunner .withPropertyValues ("spring.gson.long-serialization-policy:" + LongSerializationPolicy .STRING )
@@ -156,13 +193,21 @@ void customGsonBuilder() {
156
193
}
157
194
158
195
@ Test
159
- void withPrettyPrinting () {
196
+ void withPrettyPrintingTrue () {
160
197
this .contextRunner .withPropertyValues ("spring.gson.pretty-printing:true" ).run ((context ) -> {
161
198
Gson gson = context .getBean (Gson .class );
162
199
assertThat (gson .toJson (new DataObject ())).isEqualTo ("{\n \" data\" : 1\n }" );
163
200
});
164
201
}
165
202
203
+ @ Test
204
+ void withPrettyPrintingFalse () {
205
+ this .contextRunner .withPropertyValues ("spring.gson.pretty-printing:false" ).run ((context ) -> {
206
+ Gson gson = context .getBean (Gson .class );
207
+ assertThat (gson .toJson (new DataObject ())).isEqualTo ("{\" data\" :1}" );
208
+ });
209
+ }
210
+
166
211
@ Test
167
212
void withoutLenient () {
168
213
this .contextRunner .run ((context ) -> {
@@ -172,28 +217,43 @@ void withoutLenient() {
172
217
}
173
218
174
219
@ Test
175
- void withLenient () {
220
+ void withLenientTrue () {
176
221
this .contextRunner .withPropertyValues ("spring.gson.lenient:true" ).run ((context ) -> {
177
222
Gson gson = context .getBean (Gson .class );
178
223
assertThat (gson ).hasFieldOrPropertyWithValue ("lenient" , true );
179
224
});
180
225
}
181
226
182
227
@ Test
183
- void withHtmlEscaping () {
228
+ void withLenientFalse () {
229
+ this .contextRunner .withPropertyValues ("spring.gson.lenient:false" ).run ((context ) -> {
230
+ Gson gson = context .getBean (Gson .class );
231
+ assertThat (gson ).hasFieldOrPropertyWithValue ("lenient" , false );
232
+ });
233
+ }
234
+
235
+ @ Test
236
+ void withoutDisableHtmlEscaping () {
184
237
this .contextRunner .run ((context ) -> {
185
238
Gson gson = context .getBean (Gson .class );
186
239
assertThat (gson .htmlSafe ()).isTrue ();
187
240
});
188
241
}
189
242
190
243
@ Test
191
- void withoutHtmlEscaping () {
244
+ void withDisableHtmlEscapingTrue () {
192
245
this .contextRunner .withPropertyValues ("spring.gson.disable-html-escaping:true" ).run ((context ) -> {
193
246
Gson gson = context .getBean (Gson .class );
194
- assertThat (gson .htmlSafe ()).isFalse ();
247
+ assertThat (gson .htmlSafe ()).isTrue ();
195
248
});
249
+ }
196
250
251
+ @ Test
252
+ void withDisableHtmlEscapingFalse () {
253
+ this .contextRunner .withPropertyValues ("spring.gson.disable-html-escaping:false" ).run ((context ) -> {
254
+ Gson gson = context .getBean (Gson .class );
255
+ assertThat (gson .htmlSafe ()).isFalse ();
256
+ });
197
257
}
198
258
199
259
@ Test
0 commit comments