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 java.io.IOException;
24  import java.io.StringReader;
25  import java.util.Iterator;
26  
27  import org.apache.commons.csv.CSVFormat;
28  import org.apache.commons.csv.CSVParser;
29  import org.apache.commons.csv.CSVPrinter;
30  import org.apache.commons.csv.CSVRecord;
31  import org.junit.jupiter.api.Test;
32  
33  public class JiraCsv206Test {
34  
35      @Test
36      public void testJiraCsv206MultipleCharacterDelimiter() throws IOException {
37          // Read with multiple character delimiter
38          final String source = "FirstName[|]LastName[|]Address\r\nJohn[|]Smith[|]123 Main St.";
39          final StringReader reader = new StringReader(source);
40          final CSVFormat format = CSVFormat.DEFAULT.builder().setDelimiter("[|]").get();
41          CSVRecord record = null;
42          try (CSVParser csvParser = CSVParser.builder().setReader(reader).setFormat(format).get()) {
43              final Iterator<CSVRecord> iterator = csvParser.iterator();
44              record = iterator.next();
45              assertEquals("FirstName", record.get(0));
46              assertEquals("LastName", record.get(1));
47              assertEquals("Address", record.get(2));
48              record = iterator.next();
49              assertEquals("John", record.get(0));
50              assertEquals("Smith", record.get(1));
51              assertEquals("123 Main St.", record.get(2));
52          }
53          // Write with multiple character delimiter
54          // @formatter:off
55          final String outString =
56                  "# Change delimiter to [I]\r\n" +
57                  "first name[I]last name[I]address\r\n" +
58                  "John[I]Smith[I]123 Main St.";
59          // @formatter:on
60          final String comment = "Change delimiter to [I]";
61          // @formatter:off
62          final CSVFormat formatExcel = CSVFormat.EXCEL.builder()
63                  .setDelimiter("[I]").setHeader("first name", "last name", "address")
64                  .setCommentMarker('#')
65                  .setHeaderComments(comment).get();
66          // @formatter:on
67          final StringBuilder out = new StringBuilder();
68          try (CSVPrinter printer = formatExcel.print(out)) {
69              printer.print(record.get(0));
70              printer.print(record.get(1));
71              printer.print(record.get(2));
72          }
73          final String s = out.toString();
74          assertEquals(outString, s);
75      }
76  }