View Javadoc
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.CSVPrinter;
25  import org.apache.commons.csv.QuoteMode;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * JIRA: <a href="https://issues.apache.org/jira/browse/CSV-203">withNullString value is printed without quotes when
30   * QuoteMode.ALL is specified</a>
31   */
32  public class JiraCsv203Test {
33  
34      @Test
35      public void testQuoteModeAll() throws Exception {
36          // @formatter:off
37          final CSVFormat format = CSVFormat.EXCEL.builder()
38                  .setNullString("N/A")
39                  .setIgnoreSurroundingSpaces(true)
40                  .setQuoteMode(QuoteMode.ALL)
41                  .get();
42          // @formatter:on
43          final StringBuilder buffer = new StringBuilder();
44          try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
45              printer.printRecord(null, "Hello", null, "World");
46          }
47          assertEquals("\"N/A\",\"Hello\",\"N/A\",\"World\"\r\n", buffer.toString());
48      }
49  
50      @Test
51      public void testQuoteModeAllNonNull() throws Exception {
52          // @formatter:off
53          final CSVFormat format = CSVFormat.EXCEL.builder()
54                  .setNullString("N/A")
55                  .setIgnoreSurroundingSpaces(true)
56                  .setQuoteMode(QuoteMode.ALL_NON_NULL)
57                  .get();
58          // @formatter:on
59          final StringBuilder buffer = new StringBuilder();
60          try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
61              printer.printRecord(null, "Hello", null, "World");
62          }
63          assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
64      }
65  
66      @Test
67      public void testQuoteModeMinimal() throws Exception {
68          // @formatter:off
69          final CSVFormat format = CSVFormat.EXCEL.builder()
70                  .setNullString("N/A")
71                  .setIgnoreSurroundingSpaces(true)
72                  .setQuoteMode(QuoteMode.MINIMAL)
73                  .get();
74          // @formatter:on
75          final StringBuilder buffer = new StringBuilder();
76          try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
77              printer.printRecord(null, "Hello", null, "World");
78          }
79          assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
80      }
81  
82      @Test
83      public void testQuoteModeNonNumeric() throws Exception {
84          // @formatter:off
85          final CSVFormat format = CSVFormat.EXCEL.builder()
86                  .setNullString("N/A")
87                  .setIgnoreSurroundingSpaces(true)
88                  .setQuoteMode(QuoteMode.NON_NUMERIC)
89                  .get();
90          // @formatter:on
91          final StringBuilder buffer = new StringBuilder();
92          try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
93              printer.printRecord(null, "Hello", null, "World");
94          }
95          assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString());
96      }
97  
98      @Test
99      public void testWithEmptyValues() throws Exception {
100         // @formatter:off
101         final CSVFormat format = CSVFormat.EXCEL.builder()
102                 .setNullString("N/A")
103                 .setIgnoreSurroundingSpaces(true)
104                 .setQuoteMode(QuoteMode.ALL)
105                 .get();
106         // @formatter:on
107         final StringBuilder buffer = new StringBuilder();
108         try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
109             printer.printRecord("", "Hello", "", "World");
110             // printer.printRecord(new Object[] { null, "Hello", null, "World" });
111         }
112         assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString());
113     }
114 
115     @Test
116     public void testWithoutNullString() throws Exception {
117         // @formatter:off
118         final CSVFormat format = CSVFormat.EXCEL.builder()
119                 //.setNullString("N/A")
120                 .setIgnoreSurroundingSpaces(true)
121                 .setQuoteMode(QuoteMode.ALL)
122                 .get();
123         // @formatter:on
124         final StringBuilder buffer = new StringBuilder();
125         try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
126             printer.printRecord(null, "Hello", null, "World");
127         }
128         assertEquals(",\"Hello\",,\"World\"\r\n", buffer.toString());
129     }
130 
131     @Test
132     public void testWithoutQuoteMode() throws Exception {
133         // @formatter:off
134         final CSVFormat format = CSVFormat.EXCEL.builder()
135                 .setNullString("N/A")
136                 .setIgnoreSurroundingSpaces(true)
137                 .get();
138         // @formatter:on
139         final StringBuilder buffer = new StringBuilder();
140         try (CSVPrinter printer = new CSVPrinter(buffer, format)) {
141             printer.printRecord(null, "Hello", null, "World");
142         }
143         assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString());
144     }
145 }