3
3
import java .util .Arrays ;
4
4
import java .util .List ;
5
5
6
- import org .junit .Test ;
7
-
8
6
import com .fasterxml .jackson .annotation .*;
9
- import com .fasterxml .jackson .databind .BaseMapTest ;
10
- import com .fasterxml .jackson .databind .MapperFeature ;
11
- import com .fasterxml .jackson .databind .ObjectMapper ;
12
- import com .fasterxml .jackson .databind .PropertyNamingStrategy ;
7
+
8
+ import com .fasterxml .jackson .databind .*;
13
9
import com .fasterxml .jackson .databind .annotation .JsonNaming ;
14
10
import com .fasterxml .jackson .databind .introspect .TestNamingStrategyCustom .PersonBean ;
15
11
import com .fasterxml .jackson .databind .node .ObjectNode ;
16
12
17
13
/**
18
- * Unit tests to verify functioning of
19
- * {@link PropertyNamingStrategy#CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES}
20
- * and
21
- * {@link PropertyNamingStrategy#PASCAL_CASE_TO_CAMEL_CASE }
22
- * inside the context of an ObjectMapper.
23
- * PASCAL_CASE_TO_CAMEL_CASE was added in Jackson 2.1,
24
- * as per [JACKSON-63].
14
+ * Unit tests to verify functioning of standard {@link PropertyNamingStrategy}
15
+ * implementations Jackson includes out of the box.
25
16
*/
26
17
public class TestNamingStrategyStd extends BaseMapTest
27
18
{
@@ -110,14 +101,21 @@ static class ExplicitBean {
110
101
static class DefaultNaming {
111
102
public int someValue = 3 ;
112
103
}
113
-
104
+
105
+ static class FirstNameBean {
106
+ public String firstName ;
107
+
108
+ protected FirstNameBean () { }
109
+ public FirstNameBean (String n ) { firstName = n ; }
110
+ }
111
+
114
112
/*
115
113
/**********************************************************
116
114
/* Set up
117
115
/**********************************************************
118
116
*/
119
117
120
- public static List <Object []> NAME_TRANSLATIONS = Arrays .asList (new Object [][] {
118
+ public static List <Object []> SNAKE_CASE_NAME_TRANSLATIONS = Arrays .asList (new Object [][] {
121
119
{null , null },
122
120
{"" , "" },
123
121
{"a" , "a" },
@@ -176,32 +174,29 @@ public void setUp() throws Exception
176
174
{
177
175
super .setUp ();
178
176
_lcWithUndescoreMapper = new ObjectMapper ();
179
- _lcWithUndescoreMapper .setPropertyNamingStrategy (PropertyNamingStrategy .CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES );
177
+ _lcWithUndescoreMapper .setPropertyNamingStrategy (PropertyNamingStrategy .SNAKE_CASE );
180
178
}
181
179
182
180
/*
183
181
/**********************************************************
184
- /* Test methods for CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES
182
+ /* Test methods for SNAKE_CASE
185
183
/**********************************************************
186
184
*/
187
185
188
186
/**
189
187
* Unit test to verify translations of
190
- * {@link PropertyNamingStrategy#CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES }
188
+ * {@link PropertyNamingStrategy#SNAKE_CASE }
191
189
* outside the context of an ObjectMapper.
192
- * CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES was added in Jackson 1.9,
193
- * as per [JACKSON-598].
194
190
*/
195
- @ Test
196
191
public void testLowerCaseStrategyStandAlone ()
197
192
{
198
- for (Object [] pair : NAME_TRANSLATIONS ) {
199
- String translatedJavaName = PropertyNamingStrategy .CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES .nameForField (null , null ,
193
+ for (Object [] pair : SNAKE_CASE_NAME_TRANSLATIONS ) {
194
+ String translatedJavaName = PropertyNamingStrategy .SNAKE_CASE .nameForField (null , null ,
200
195
(String ) pair [0 ]);
201
196
assertEquals ((String ) pair [1 ], translatedJavaName );
202
197
}
203
198
}
204
-
199
+
205
200
public void testLowerCaseTranslations () throws Exception
206
201
{
207
202
// First serialize
@@ -256,34 +251,32 @@ public void testLowerCaseUnchangedNames() throws Exception
256
251
assertEquals ("from7user" , result .from7user );
257
252
assertEquals ("_x" , result ._x );
258
253
}
259
-
254
+
260
255
/*
261
256
/**********************************************************
262
- /* Test methods for PASCAL_CASE_TO_CAMEL_CASE (added in 2.1)
257
+ /* Test methods for UPPER_CAMEL_CASE
263
258
/**********************************************************
264
259
*/
265
260
266
261
/**
267
262
* Unit test to verify translations of
268
- * {@link PropertyNamingStrategy#PASCAL_CASE_TO_CAMEL_CASE }
263
+ * {@link PropertyNamingStrategy#UPPER_CAMEL_CASE }
269
264
* outside the context of an ObjectMapper.
270
- * PASCAL_CASE_TO_CAMEL_CASE was added in Jackson 2.1.0,
271
- * as per [JACKSON-63].
272
265
*/
273
266
public void testPascalCaseStandAlone ()
274
267
{
275
- String translatedJavaName = PropertyNamingStrategy .PASCAL_CASE_TO_CAMEL_CASE .nameForField
268
+ String translatedJavaName = PropertyNamingStrategy .UPPER_CAMEL_CASE .nameForField
276
269
(null , null , "userName" );
277
270
assertEquals ("UserName" , translatedJavaName );
278
271
279
- translatedJavaName = PropertyNamingStrategy .PASCAL_CASE_TO_CAMEL_CASE .nameForField
272
+ translatedJavaName = PropertyNamingStrategy .UPPER_CAMEL_CASE .nameForField
280
273
(null , null , "User" );
281
274
assertEquals ("User" , translatedJavaName );
282
275
283
- translatedJavaName = PropertyNamingStrategy .PASCAL_CASE_TO_CAMEL_CASE .nameForField
276
+ translatedJavaName = PropertyNamingStrategy .UPPER_CAMEL_CASE .nameForField
284
277
(null , null , "user" );
285
278
assertEquals ("User" , translatedJavaName );
286
- translatedJavaName = PropertyNamingStrategy .PASCAL_CASE_TO_CAMEL_CASE .nameForField
279
+ translatedJavaName = PropertyNamingStrategy .UPPER_CAMEL_CASE .nameForField
287
280
(null , null , "x" );
288
281
assertEquals ("X" , translatedJavaName );
289
282
}
@@ -294,14 +287,20 @@ public void testPascalCaseStandAlone()
294
287
public void testIssue428PascalWithOverrides () throws Exception {
295
288
296
289
String json = new ObjectMapper ()
297
- .setPropertyNamingStrategy (PropertyNamingStrategy .PASCAL_CASE_TO_CAMEL_CASE )
290
+ .setPropertyNamingStrategy (PropertyNamingStrategy .UPPER_CAMEL_CASE )
298
291
.writeValueAsString (new Bean428 ());
299
292
300
293
if (!json .contains (quote ("fooBar" ))) {
301
294
fail ("Should use name 'fooBar', does not: " +json );
302
295
}
303
296
}
304
297
298
+ /*
299
+ /**********************************************************
300
+ /* Test methods for LOWER_CASE
301
+ /**********************************************************
302
+ */
303
+
305
304
/**
306
305
* For [databind#461]
307
306
*/
@@ -314,13 +313,52 @@ public void testSimpleLowerCase() throws Exception
314
313
m .writeValueAsString (input ));
315
314
}
316
315
316
+ /*
317
+ /**********************************************************
318
+ /* Test methods for KEBAB_CASE
319
+ /**********************************************************
320
+ */
321
+
322
+ public void testKebabCaseStrategyStandAlone ()
323
+ {
324
+ assertEquals ("some-value" ,
325
+ PropertyNamingStrategy .KEBAB_CASE .nameForField (null , null , "someValue" ));
326
+ assertEquals ("some-value" ,
327
+ PropertyNamingStrategy .KEBAB_CASE .nameForField (null , null , "SomeValue" ));
328
+ assertEquals ("url" ,
329
+ PropertyNamingStrategy .KEBAB_CASE .nameForField (null , null , "URL" ));
330
+ assertEquals ("url-stuff" ,
331
+ PropertyNamingStrategy .KEBAB_CASE .nameForField (null , null , "URLStuff" ));
332
+ assertEquals ("some-url-stuff" ,
333
+ PropertyNamingStrategy .KEBAB_CASE .nameForField (null , null , "SomeURLStuff" ));
334
+ }
335
+
336
+ public void testSimpleKebabCase () throws Exception
337
+ {
338
+ final FirstNameBean input = new FirstNameBean ("Bob" );
339
+ ObjectMapper m = new ObjectMapper ()
340
+ .setPropertyNamingStrategy (PropertyNamingStrategy .KEBAB_CASE );
341
+
342
+ assertEquals (aposToQuotes ("{'first-name':'Bob'}" ), m .writeValueAsString (input ));
343
+
344
+ FirstNameBean result = m .readValue (aposToQuotes ("{'first-name':'Billy'}" ),
345
+ FirstNameBean .class );
346
+ assertEquals ("Billy" , result .firstName );
347
+ }
348
+
349
+ /*
350
+ /**********************************************************
351
+ /* Test methods, other
352
+ /**********************************************************
353
+ */
354
+
317
355
/**
318
356
* Test [databind#815], problems with ObjectNode, naming strategy
319
357
*/
320
358
public void testNamingWithObjectNode () throws Exception
321
359
{
322
- ObjectMapper m = new ObjectMapper ();
323
- m .setPropertyNamingStrategy (PropertyNamingStrategy .LOWER_CASE );
360
+ ObjectMapper m = new ObjectMapper ()
361
+ .setPropertyNamingStrategy (PropertyNamingStrategy .LOWER_CASE );
324
362
ClassWithObjectNodeField result =
325
363
m .readValue (
326
364
"{ \" id\" : \" 1\" , \" json\" : { \" foo\" : \" bar\" , \" baz\" : \" bing\" } }" ,
@@ -332,16 +370,17 @@ public void testNamingWithObjectNode() throws Exception
332
370
assertEquals ("bing" , result .json .path ("baz" ).asText ());
333
371
}
334
372
335
- public void testExplicitRename () throws Exception {
373
+ public void testExplicitRename () throws Exception
374
+ {
336
375
ObjectMapper m = new ObjectMapper ();
337
- m .setPropertyNamingStrategy (PropertyNamingStrategy .CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES );
376
+ m .setPropertyNamingStrategy (PropertyNamingStrategy .SNAKE_CASE );
338
377
m .enable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY );
339
378
// by default, renaming will not take place on explicitly named fields
340
379
assertEquals (aposToQuotes ("{'firstName':'Peter','lastName':'Venkman','user_age':'35'}" ),
341
380
m .writeValueAsString (new ExplicitBean ()));
342
381
343
382
m = new ObjectMapper ();
344
- m .setPropertyNamingStrategy (PropertyNamingStrategy .CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES );
383
+ m .setPropertyNamingStrategy (PropertyNamingStrategy .SNAKE_CASE );
345
384
m .enable (MapperFeature .SORT_PROPERTIES_ALPHABETICALLY );
346
385
m .enable (MapperFeature .ALLOW_EXPLICIT_PROPERTY_RENAMING );
347
386
// w/ feature enabled, ALL property names should get re-written
0 commit comments