1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.commons.csv.issues;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import org.apache.commons.csv.CSVFormat;
24 import org.apache.commons.csv.QuoteMode;
25 import org.junit.jupiter.api.Test;
26
27 class JiraCsv148Test {
28
29 @Test
30 void testWithIgnoreSurroundingSpacesEmpty() {
31 // @formatter:off
32 final CSVFormat format = CSVFormat.DEFAULT.builder()
33 .setQuoteMode(QuoteMode.ALL)
34 .setIgnoreSurroundingSpaces(true)
35 .get();
36 // @formatter:on
37 assertEquals(
38 "\"\",\" \",\" Single space on the left\",\"Single space on the right \"," +
39 "\" Single spaces on both sides \",\" Multiple spaces on the left\"," +
40 "\"Multiple spaces on the right \",\" Multiple spaces on both sides \"",
41 format.format("", " ", " Single space on the left", "Single space on the right ", " Single spaces on both sides ",
42 " Multiple spaces on the left", "Multiple spaces on the right ", " Multiple spaces on both sides "));
43 }
44
45 /**
46 * The difference between withTrim()and withIgnoreSurroundingSpace(): difference: withTrim() can remove the leading and trailing spaces and newlines in
47 * quotation marks, while withIgnoreSurroundingSpace() cannot The same point: you can remove the leading and trailing spaces, tabs and other symbols.
48 */
49 @Test
50 void testWithTrimEmpty() {
51 // @formatter:off
52 final CSVFormat format = CSVFormat.DEFAULT.builder()
53 .setQuoteMode(QuoteMode.ALL)
54 .setTrim(true)
55 .get();
56 // @formatter:on
57 assertEquals(
58 "\"\",\"\",\"Single space on the left\",\"Single space on the right\",\"Single spaces on both sides\",\"Multiple spaces on the left\"," +
59 "\"Multiple spaces on the right\",\"Multiple spaces on both sides\"",
60 format.format("", " ", " Single space on the left", "Single space on the right ", " Single spaces on both sides ",
61 " Multiple spaces on the left", "Multiple spaces on the right ", " Multiple spaces on both sides "));
62 }
63 }