Skip to content

Commit 915512b

Browse files
committed
Test refactoring (Csv module)
1 parent 12ece23 commit 915512b

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvGenerator.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ public enum Feature
2828
* but when <code>false</code>, a faster but more conservative check
2929
* is made, and possibly quoting is used for values that might not need it.
3030
* Trade-offs is basically between optimal/minimal quoting (true), and
31-
* faster handling (false).
31+
* possibly faster handling (false).
3232
* Faster check involves only checking first N characters of value, as well
3333
* as possible looser checks.
34+
* However: strict check can also be more efficient in some cases when it
35+
* allows omitting quoting, so trade-off is not always simple.
3436
*<p>
3537
* Note, however, that regardless setting, all values that need to be quoted
3638
* will be: it is just that when set to <code>false</code>, other values may
@@ -75,10 +77,10 @@ public enum Feature
7577

7678
/**
7779
* Feature that determines whether quote characters within quoted String values are escaped
78-
* using configured escape character, instead of being "doubled up" (that is: a quote character
79-
* is written twice in a row).
80-
*<p>
81-
* Default value is false so that quotes are doubled as necessary, not escaped.
80+
* using configured escape character, instead of being "doubled up" (that is: a quote character
81+
* is written twice in a row).
82+
*<p>
83+
* Default value is false so that quotes are doubled as necessary, not escaped.
8284
*
8385
* @since 2.9.3
8486
*/

csv/src/test/java/com/fasterxml/jackson/dataformat/csv/ser/TestGenerator.java

+23-34
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ public Entry2(String id, float amount) {
4343
/**********************************************************************
4444
*/
4545

46+
private final CsvMapper MAPPER = mapperForCsv();
47+
4648
public void testSimpleExplicit() throws Exception
4749
{
48-
ObjectMapper mapper = mapperForCsv();
4950
CsvSchema schema = CsvSchema.builder()
5051
.addColumn("firstName")
5152
.addColumn("lastName")
@@ -59,7 +60,7 @@ public void testSimpleExplicit() throws Exception
5960

6061
FiveMinuteUser user = new FiveMinuteUser("Silu", "Seppala", false, Gender.MALE,
6162
new byte[] { 1, 2, 3, 4, 5});
62-
String csv = mapper.writer(schema).writeValueAsString(user);
63+
String csv = MAPPER.writer(schema).writeValueAsString(user);
6364
assertEquals("Silu,Seppala,MALE,AQIDBAU=,false\n", csv);
6465
}
6566

@@ -71,10 +72,9 @@ public void testSimpleWithAutoSchema() throws Exception
7172

7273
public void testWriteHeaders() throws Exception
7374
{
74-
CsvMapper mapper = mapperForCsv();
75-
CsvSchema schema = mapper.schemaFor(FiveMinuteUser.class).withHeader();
75+
CsvSchema schema = MAPPER.schemaFor(FiveMinuteUser.class).withHeader();
7676
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);
7878
assertEquals("firstName,lastName,gender,verified,userImage\n"
7979
+"Barbie,Benton,FEMALE,false,\n", result);
8080
}
@@ -85,11 +85,10 @@ public void testWriteHeaders() throws Exception
8585
*/
8686
public void testFailedWriteHeaders() throws Exception
8787
{
88-
CsvMapper mapper = mapperForCsv();
8988
CsvSchema schema = CsvSchema.builder().setUseHeader(true).build();
9089
FiveMinuteUser user = new FiveMinuteUser("Barbie", "Benton", false, Gender.FEMALE, null);
9190
try {
92-
mapper.writer(schema).writeValueAsString(user);
91+
MAPPER.writer(schema).writeValueAsString(user);
9392
fail("Should fail without columns");
9493
} catch (JsonMappingException e) {
9594
verifyException(e, "contains no column names");
@@ -98,19 +97,17 @@ public void testFailedWriteHeaders() throws Exception
9897

9998
public void testExplicitWithDouble() throws Exception
10099
{
101-
ObjectMapper mapper = mapperForCsv();
102100
CsvSchema schema = CsvSchema.builder()
103101
.addColumn("id")
104102
.addColumn("amount")
105103
.build();
106104

107-
String result = mapper.writer(schema).writeValueAsString(new Entry("abc", 1.25));
105+
String result = MAPPER.writer(schema).writeValueAsString(new Entry("abc", 1.25));
108106
assertEquals("abc,1.25\n", result);
109107
}
110108

111109
public void testExplicitWithFloat() throws Exception
112110
{
113-
ObjectMapper mapper = mapperForCsv();
114111
CsvSchema schema = CsvSchema.builder()
115112
.addColumn("id")
116113
.addColumn("amount")
@@ -119,27 +116,25 @@ public void testExplicitWithFloat() throws Exception
119116
float amount = 1.89f;
120117
//this value loses precision when converted
121118
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));
123120
assertEquals("abc,1.89\n", result);
124121
}
125122

126123
public void testExplicitWithQuoted() throws Exception
127124
{
128-
ObjectMapper mapper = mapperForCsv();
129125
CsvSchema schema = CsvSchema.builder()
130126
.addColumn("id")
131127
.addColumn("desc")
132128
.build();
133129

134-
String result = mapper.writer(schema).writeValueAsString(new IdDesc("id", "Some \"stuff\""));
130+
String result = MAPPER.writer(schema).writeValueAsString(new IdDesc("id", "Some \"stuff\""));
135131
// MUST use doubling for quotes!
136132
assertEquals("id,\"Some \"\"stuff\"\"\"\n", result);
137133
}
138134

139135
// [dataformat-csv#14]: String values that cross buffer boundary won't be quoted properly
140136
public void testLongerWithQuotes() throws Exception
141137
{
142-
ObjectMapper mapper = mapperForCsv();
143138
CsvSchema schema = CsvSchema.builder()
144139
.addColumn("id")
145140
.addColumn("desc")
@@ -160,58 +155,55 @@ public void testLongerWithQuotes() throws Exception
160155
final String inputDesc = sb.toString();
161156
String expOutputDesc = inputDesc.replace("\"", "\"\"");
162157
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();
164159
assertEquals(expOutput, result);
165160
}
166161

167162
public void testWriteInFile() throws Exception
168163
{
169-
ObjectMapper mapper = mapperForCsv();
170164
CsvSchema schema = CsvSchema.builder()
171165
.addColumn("firstName")
172166
.addColumn("lastName")
173167
.build();
174168

175-
ObjectNode node = mapper.createObjectNode()
169+
ObjectNode node = MAPPER.createObjectNode()
176170
.put("firstName", "David")
177171
.put("lastName", "Douillet");
178172

179173
File file = File.createTempFile("file", ".csv");
180174
try {
181-
mapper.writer(schema.withHeader()).writeValue(file, node);
175+
MAPPER.writer(schema.withHeader()).writeValue(file, node);
182176
} finally {
183177
file.delete();
184178
}
185179
}
186180

187181
public void testForcedQuoting60() throws Exception
188182
{
189-
CsvMapper mapper = mapperForCsv();
190183
CsvSchema schema = CsvSchema.builder()
191184
.addColumn("id")
192185
.addColumn("amount")
193186
.build();
194-
String result = mapper.writer(schema)
187+
String result = MAPPER.writer(schema)
195188
.with(CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS)
196189
.writeValueAsString(new Entry("abc", 1.25));
197190
assertEquals("\"abc\",1.25\n", result);
198191

199192
// Also, as per [dataformat-csv#81], should be possible to change dynamically
200-
result = mapper.writer(schema)
193+
result = MAPPER.writer(schema)
201194
.without(CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS)
202195
.writeValueAsString(new Entry("xyz", 2.5));
203196
assertEquals("xyz,2.5\n", result);
204197
}
205198

206199
public void testForcedQuotingWithQuoteEscapedWithBackslash() throws Exception
207200
{
208-
CsvMapper mapper = mapperForCsv();
209201
CsvSchema schema = CsvSchema.builder()
210202
.addColumn("id")
211203
.addColumn("amount")
212204
.setEscapeChar('\\')
213205
.build();
214-
String result = mapper.writer(schema)
206+
String result = MAPPER.writer(schema)
215207
.with(CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS)
216208
.with(CsvGenerator.Feature.ESCAPE_QUOTE_CHAR_WITH_ESCAPE_CHAR)
217209
.writeValueAsString(new Entry("\"abc\"", 1.25));
@@ -241,14 +233,13 @@ public void testForcedQuotingEmptyStrings() throws Exception
241233
public void testQuotingOfCommentChar() throws Exception
242234
{
243235
// 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)
247238
.writeValueAsString(new IdDesc("#123", "Foo"));
248239
assertEquals("\"#123\",Foo\n", csv);
249240

250241
// then with strict/optimal
251-
mapper = mapperForCsv();
242+
CsvMapper mapper = mapperForCsv();
252243
mapper.enable(CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING);
253244
csv = mapper.writer(schema)
254245
.writeValueAsString(new IdDesc("#123", "Foo"));
@@ -259,10 +250,9 @@ public void testQuotingOfCommentChar() throws Exception
259250
public void testBackslashEscape() throws Exception
260251
{
261252
// First, with default quoting
262-
CsvMapper mapper = mapperForCsv();
263-
final CsvSchema schema = mapper.schemaFor(IdDesc.class)
253+
final CsvSchema schema = MAPPER.schemaFor(IdDesc.class)
264254
.withEscapeChar('\\');
265-
String csv = mapper.writer(schema)
255+
String csv = MAPPER.writer(schema)
266256
.writeValueAsString(new IdDesc("123", "a\\b"));
267257
// Escaping also leads to quoting
268258
assertEquals("123,\"a\\\\b\"\n", csv);
@@ -325,16 +315,15 @@ private <T> void testSerializationOfPrimitiveToCsv(final CsvMapper mapper,
325315

326316
private void _testSimpleWithAutoSchema(boolean wrapAsArray) throws Exception
327317
{
328-
CsvMapper mapper = mapperForCsv();
329-
CsvSchema schema = mapper.schemaFor(FiveMinuteUser.class);
318+
CsvSchema schema = MAPPER.schemaFor(FiveMinuteUser.class);
330319
FiveMinuteUser user = new FiveMinuteUser("Veltto", "Virtanen", true, Gender.MALE,
331320
new byte[] { 3, 1 });
332321
String result;
333322
// having virtual root-level array should make no difference:
334323
if (wrapAsArray) {
335-
result = mapper.writer(schema).writeValueAsString(new FiveMinuteUser[] { user });
324+
result = MAPPER.writer(schema).writeValueAsString(new FiveMinuteUser[] { user });
336325
} else {
337-
result = mapper.writer(schema).writeValueAsString(user);
326+
result = MAPPER.writer(schema).writeValueAsString(user);
338327
}
339328
assertEquals("Veltto,Virtanen,MALE,true,AwE=\n", result);
340329
}

0 commit comments

Comments
 (0)