@@ -43,9 +43,10 @@ public Entry2(String id, float amount) {
43
43
/**********************************************************************
44
44
*/
45
45
46
+ private final CsvMapper MAPPER = mapperForCsv ();
47
+
46
48
public void testSimpleExplicit () throws Exception
47
49
{
48
- ObjectMapper mapper = mapperForCsv ();
49
50
CsvSchema schema = CsvSchema .builder ()
50
51
.addColumn ("firstName" )
51
52
.addColumn ("lastName" )
@@ -59,7 +60,7 @@ public void testSimpleExplicit() throws Exception
59
60
60
61
FiveMinuteUser user = new FiveMinuteUser ("Silu" , "Seppala" , false , Gender .MALE ,
61
62
new byte [] { 1 , 2 , 3 , 4 , 5 });
62
- String csv = mapper .writer (schema ).writeValueAsString (user );
63
+ String csv = MAPPER .writer (schema ).writeValueAsString (user );
63
64
assertEquals ("Silu,Seppala,MALE,AQIDBAU=,false\n " , csv );
64
65
}
65
66
@@ -71,10 +72,9 @@ public void testSimpleWithAutoSchema() throws Exception
71
72
72
73
public void testWriteHeaders () throws Exception
73
74
{
74
- CsvMapper mapper = mapperForCsv ();
75
- CsvSchema schema = mapper .schemaFor (FiveMinuteUser .class ).withHeader ();
75
+ CsvSchema schema = MAPPER .schemaFor (FiveMinuteUser .class ).withHeader ();
76
76
FiveMinuteUser user = new FiveMinuteUser ("Barbie" , "Benton" , false , Gender .FEMALE , null );
77
- String result = mapper .writer (schema ).writeValueAsString (user );
77
+ String result = MAPPER .writer (schema ).writeValueAsString (user );
78
78
assertEquals ("firstName,lastName,gender,verified,userImage\n "
79
79
+"Barbie,Benton,FEMALE,false,\n " , result );
80
80
}
@@ -85,11 +85,10 @@ public void testWriteHeaders() throws Exception
85
85
*/
86
86
public void testFailedWriteHeaders () throws Exception
87
87
{
88
- CsvMapper mapper = mapperForCsv ();
89
88
CsvSchema schema = CsvSchema .builder ().setUseHeader (true ).build ();
90
89
FiveMinuteUser user = new FiveMinuteUser ("Barbie" , "Benton" , false , Gender .FEMALE , null );
91
90
try {
92
- mapper .writer (schema ).writeValueAsString (user );
91
+ MAPPER .writer (schema ).writeValueAsString (user );
93
92
fail ("Should fail without columns" );
94
93
} catch (JsonMappingException e ) {
95
94
verifyException (e , "contains no column names" );
@@ -98,19 +97,17 @@ public void testFailedWriteHeaders() throws Exception
98
97
99
98
public void testExplicitWithDouble () throws Exception
100
99
{
101
- ObjectMapper mapper = mapperForCsv ();
102
100
CsvSchema schema = CsvSchema .builder ()
103
101
.addColumn ("id" )
104
102
.addColumn ("amount" )
105
103
.build ();
106
104
107
- String result = mapper .writer (schema ).writeValueAsString (new Entry ("abc" , 1.25 ));
105
+ String result = MAPPER .writer (schema ).writeValueAsString (new Entry ("abc" , 1.25 ));
108
106
assertEquals ("abc,1.25\n " , result );
109
107
}
110
108
111
109
public void testExplicitWithFloat () throws Exception
112
110
{
113
- ObjectMapper mapper = mapperForCsv ();
114
111
CsvSchema schema = CsvSchema .builder ()
115
112
.addColumn ("id" )
116
113
.addColumn ("amount" )
@@ -119,27 +116,25 @@ public void testExplicitWithFloat() throws Exception
119
116
float amount = 1.89f ;
120
117
//this value loses precision when converted
121
118
assertFalse (Double .toString ((double )amount ).equals ("1.89" ));
122
- String result = mapper .writer (schema ).writeValueAsString (new Entry2 ("abc" , amount ));
119
+ String result = MAPPER .writer (schema ).writeValueAsString (new Entry2 ("abc" , amount ));
123
120
assertEquals ("abc,1.89\n " , result );
124
121
}
125
122
126
123
public void testExplicitWithQuoted () throws Exception
127
124
{
128
- ObjectMapper mapper = mapperForCsv ();
129
125
CsvSchema schema = CsvSchema .builder ()
130
126
.addColumn ("id" )
131
127
.addColumn ("desc" )
132
128
.build ();
133
129
134
- String result = mapper .writer (schema ).writeValueAsString (new IdDesc ("id" , "Some \" stuff\" " ));
130
+ String result = MAPPER .writer (schema ).writeValueAsString (new IdDesc ("id" , "Some \" stuff\" " ));
135
131
// MUST use doubling for quotes!
136
132
assertEquals ("id,\" Some \" \" stuff\" \" \" \n " , result );
137
133
}
138
134
139
135
// [dataformat-csv#14]: String values that cross buffer boundary won't be quoted properly
140
136
public void testLongerWithQuotes () throws Exception
141
137
{
142
- ObjectMapper mapper = mapperForCsv ();
143
138
CsvSchema schema = CsvSchema .builder ()
144
139
.addColumn ("id" )
145
140
.addColumn ("desc" )
@@ -160,58 +155,55 @@ public void testLongerWithQuotes() throws Exception
160
155
final String inputDesc = sb .toString ();
161
156
String expOutputDesc = inputDesc .replace ("\" " , "\" \" " );
162
157
String expOutput = "id,\" " +expOutputDesc +"\" " ;
163
- String result = mapper .writer (schema ).writeValueAsString (new IdDesc ("id" , inputDesc )).trim ();
158
+ String result = MAPPER .writer (schema ).writeValueAsString (new IdDesc ("id" , inputDesc )).trim ();
164
159
assertEquals (expOutput , result );
165
160
}
166
161
167
162
public void testWriteInFile () throws Exception
168
163
{
169
- ObjectMapper mapper = mapperForCsv ();
170
164
CsvSchema schema = CsvSchema .builder ()
171
165
.addColumn ("firstName" )
172
166
.addColumn ("lastName" )
173
167
.build ();
174
168
175
- ObjectNode node = mapper .createObjectNode ()
169
+ ObjectNode node = MAPPER .createObjectNode ()
176
170
.put ("firstName" , "David" )
177
171
.put ("lastName" , "Douillet" );
178
172
179
173
File file = File .createTempFile ("file" , ".csv" );
180
174
try {
181
- mapper .writer (schema .withHeader ()).writeValue (file , node );
175
+ MAPPER .writer (schema .withHeader ()).writeValue (file , node );
182
176
} finally {
183
177
file .delete ();
184
178
}
185
179
}
186
180
187
181
public void testForcedQuoting60 () throws Exception
188
182
{
189
- CsvMapper mapper = mapperForCsv ();
190
183
CsvSchema schema = CsvSchema .builder ()
191
184
.addColumn ("id" )
192
185
.addColumn ("amount" )
193
186
.build ();
194
- String result = mapper .writer (schema )
187
+ String result = MAPPER .writer (schema )
195
188
.with (CsvGenerator .Feature .ALWAYS_QUOTE_STRINGS )
196
189
.writeValueAsString (new Entry ("abc" , 1.25 ));
197
190
assertEquals ("\" abc\" ,1.25\n " , result );
198
191
199
192
// Also, as per [dataformat-csv#81], should be possible to change dynamically
200
- result = mapper .writer (schema )
193
+ result = MAPPER .writer (schema )
201
194
.without (CsvGenerator .Feature .ALWAYS_QUOTE_STRINGS )
202
195
.writeValueAsString (new Entry ("xyz" , 2.5 ));
203
196
assertEquals ("xyz,2.5\n " , result );
204
197
}
205
198
206
199
public void testForcedQuotingWithQuoteEscapedWithBackslash () throws Exception
207
200
{
208
- CsvMapper mapper = mapperForCsv ();
209
201
CsvSchema schema = CsvSchema .builder ()
210
202
.addColumn ("id" )
211
203
.addColumn ("amount" )
212
204
.setEscapeChar ('\\' )
213
205
.build ();
214
- String result = mapper .writer (schema )
206
+ String result = MAPPER .writer (schema )
215
207
.with (CsvGenerator .Feature .ALWAYS_QUOTE_STRINGS )
216
208
.with (CsvGenerator .Feature .ESCAPE_QUOTE_CHAR_WITH_ESCAPE_CHAR )
217
209
.writeValueAsString (new Entry ("\" abc\" " , 1.25 ));
@@ -241,14 +233,13 @@ public void testForcedQuotingEmptyStrings() throws Exception
241
233
public void testQuotingOfCommentChar () throws Exception
242
234
{
243
235
// First, with default quoting
244
- CsvMapper mapper = mapperForCsv ();
245
- final CsvSchema schema = mapper .schemaFor (IdDesc .class );
246
- String csv = mapper .writer (schema )
236
+ final CsvSchema schema = MAPPER .schemaFor (IdDesc .class );
237
+ String csv = MAPPER .writer (schema )
247
238
.writeValueAsString (new IdDesc ("#123" , "Foo" ));
248
239
assertEquals ("\" #123\" ,Foo\n " , csv );
249
240
250
241
// then with strict/optimal
251
- mapper = mapperForCsv ();
242
+ CsvMapper mapper = mapperForCsv ();
252
243
mapper .enable (CsvGenerator .Feature .STRICT_CHECK_FOR_QUOTING );
253
244
csv = mapper .writer (schema )
254
245
.writeValueAsString (new IdDesc ("#123" , "Foo" ));
@@ -259,10 +250,9 @@ public void testQuotingOfCommentChar() throws Exception
259
250
public void testBackslashEscape () throws Exception
260
251
{
261
252
// First, with default quoting
262
- CsvMapper mapper = mapperForCsv ();
263
- final CsvSchema schema = mapper .schemaFor (IdDesc .class )
253
+ final CsvSchema schema = MAPPER .schemaFor (IdDesc .class )
264
254
.withEscapeChar ('\\' );
265
- String csv = mapper .writer (schema )
255
+ String csv = MAPPER .writer (schema )
266
256
.writeValueAsString (new IdDesc ("123" , "a\\ b" ));
267
257
// Escaping also leads to quoting
268
258
assertEquals ("123,\" a\\ \\ b\" \n " , csv );
@@ -325,16 +315,15 @@ private <T> void testSerializationOfPrimitiveToCsv(final CsvMapper mapper,
325
315
326
316
private void _testSimpleWithAutoSchema (boolean wrapAsArray ) throws Exception
327
317
{
328
- CsvMapper mapper = mapperForCsv ();
329
- CsvSchema schema = mapper .schemaFor (FiveMinuteUser .class );
318
+ CsvSchema schema = MAPPER .schemaFor (FiveMinuteUser .class );
330
319
FiveMinuteUser user = new FiveMinuteUser ("Veltto" , "Virtanen" , true , Gender .MALE ,
331
320
new byte [] { 3 , 1 });
332
321
String result ;
333
322
// having virtual root-level array should make no difference:
334
323
if (wrapAsArray ) {
335
- result = mapper .writer (schema ).writeValueAsString (new FiveMinuteUser [] { user });
324
+ result = MAPPER .writer (schema ).writeValueAsString (new FiveMinuteUser [] { user });
336
325
} else {
337
- result = mapper .writer (schema ).writeValueAsString (user );
326
+ result = MAPPER .writer (schema ).writeValueAsString (user );
338
327
}
339
328
assertEquals ("Veltto,Virtanen,MALE,true,AwE=\n " , result );
340
329
}
0 commit comments