Skip to content

Commit f1cbfbe

Browse files
committed
Make quote character in @CsvSource configurable
Prior to this commit, the quote character for quoted strings in @CsvSource was hard coded to a single quote (') and could not be changed; however, with the recently added support for text blocks, it may be desirable to change the quote character to something else. This commit introduces a new quoteCharacter attribute in @CsvSource that allows the user to change the quote character. The quoteCharacter defaults to a single quote for backward compatibility. Closes #2735
1 parent 10f8ceb commit f1cbfbe

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.8.2.adoc

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ GitHub.
3939

4040
==== New Features and Improvements
4141

42-
* ❓
42+
* The quote character for _quoted strings_ in `@CsvSource` is now configurable via the new
43+
`quoteCharacter` attribute, which defaults to a single quote (`'`) for backward
44+
compatibility.
4345

4446

4547
[[release-notes-5.8.2-junit-vintage]]

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

+8-7
Original file line numberDiff line numberDiff line change
@@ -1363,13 +1363,14 @@ The default delimiter is a comma (`,`), but you can use another character by set
13631363
`String` delimiter instead of a single character. However, both delimiter attributes
13641364
cannot be set simultaneously.
13651365

1366-
`@CsvSource` uses a single quote `'` as its quote character. See the `'lemon, lime'` value
1367-
in the example above and in the table below. An empty, quoted value `''` results in an
1368-
empty `String` unless the `emptyValue` attribute is set; whereas, an entirely _empty_
1369-
value is interpreted as a `null` reference. By specifying one or more `nullValues`, a
1370-
custom value can be interpreted as a `null` reference (see the `NIL` example in the table
1371-
below). An `ArgumentConversionException` is thrown if the target type of a `null`
1372-
reference is a primitive type.
1366+
By default, `@CsvSource` uses a single quote `'` as its quote character, but this can be
1367+
changed via the `quoteCharacter` attribute. See the `'lemon, lime'` value in the example
1368+
above and in the table below. An empty, quoted value `''` results in an empty `String`
1369+
unless the `emptyValue` attribute is set; whereas, an entirely _empty_ value is
1370+
interpreted as a `null` reference. By specifying one or more `nullValues`, a custom value
1371+
can be interpreted as a `null` reference (see the `NIL` example in the table below). An
1372+
`ArgumentConversionException` is thrown if the target type of a `null` reference is a
1373+
primitive type.
13731374

13741375
NOTE: An _unquoted_ empty value will always be converted to a `null` reference regardless
13751376
of any custom values configured via the `nullValues` attribute.

junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/CsvParserFactory.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ class CsvParserFactory {
2424

2525
private static final String DEFAULT_DELIMITER = ",";
2626
private static final String LINE_SEPARATOR = "\n";
27-
private static final char SINGLE_QUOTE = '\'';
2827
private static final char DOUBLE_QUOTE = '"';
2928
private static final char EMPTY_CHAR = '\0';
3029
private static final boolean COMMENT_PROCESSING_FOR_CSV_SOURCE = false;
3130
private static final boolean COMMENT_PROCESSING_FOR_CSV_FILE_SOURCE = true;
3231

3332
static CsvParser createParserFor(CsvSource annotation) {
3433
String delimiter = selectDelimiter(annotation, annotation.delimiter(), annotation.delimiterString());
35-
return createParser(delimiter, LINE_SEPARATOR, SINGLE_QUOTE, annotation.emptyValue(),
34+
return createParser(delimiter, LINE_SEPARATOR, annotation.quoteCharacter(), annotation.emptyValue(),
3635
annotation.maxCharsPerColumn(), COMMENT_PROCESSING_FOR_CSV_SOURCE,
3736
annotation.ignoreLeadingAndTrailingWhitespace());
3837
}

junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/CsvSource.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
* <p>The column delimiter (defaults to comma) can be customized with either
3333
* {@link #delimiter} or {@link #delimiterString}.
3434
*
35-
* <p>{@code @CsvSource} uses a single quote ({@code '}) as its quote character.
36-
* See the {@code 'lemon, lime'} examples in the documentation for the {@link #value}
35+
* <p>By default, {@code @CsvSource} uses a single quote ({@code '}) as its quote
36+
* character, but this can be changed via {@link #quoteCharacter}. See the
37+
* {@code 'lemon, lime'} examples in the documentation for the {@link #value}
3738
* and {@link #textBlock} attributes. An empty, quoted value ({@code ''}) results
3839
* in an empty {@link String} unless the {@link #emptyValue} attribute is set;
3940
* whereas, an entirely <em>empty</em> value is interpreted as a {@code null} reference.
@@ -126,6 +127,21 @@
126127
@API(status = EXPERIMENTAL, since = "5.8.1")
127128
String textBlock() default "";
128129

130+
/**
131+
* The quote character to use for <em>quoted strings</em>.
132+
*
133+
* <p>Defaults to a single quote ({@code '}).
134+
*
135+
* <p>You may change the quote character to anything that makes sense for
136+
* your use case; however, the primary use case is to allow you to use double
137+
* quotes in {@link #textBlock}.
138+
*
139+
* @since 5.8.2
140+
* @see #textBlock
141+
*/
142+
@API(status = EXPERIMENTAL, since = "5.8.2")
143+
char quoteCharacter() default '\'';
144+
129145
/**
130146
* The column delimiter character to use when reading the {@linkplain #value lines}.
131147
*

junit-jupiter-params/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@
9191
class ParameterizedTestIntegrationTests {
9292

9393
@ParameterizedTest
94-
@CsvSource(textBlock = """
94+
@CsvSource(quoteCharacter = '"', textBlock = """
9595
apple, 1
9696
banana, 2
97-
'lemon, lime', 0xF1
97+
"lemon, lime", 0xF1
9898
strawberry, 700_000
9999
""")
100100
void executesLinesFromTextBlock(String fruit, int rank) {

junit-jupiter-params/src/test/java/org/junit/jupiter/params/provider/MockCsvAnnotationBuilder.java

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ CsvSource build() {
115115
// @CsvSource
116116
when(annotation.value()).thenReturn(this.lines);
117117
when(annotation.textBlock()).thenReturn(this.textBlock);
118+
when(annotation.quoteCharacter()).thenReturn('\'');
118119

119120
return annotation;
120121
}

0 commit comments

Comments
 (0)