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