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