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