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;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.IOException;
24  import java.io.InputStreamReader;
25  import java.io.Reader;
26  import java.io.UnsupportedEncodingException;
27  import java.nio.charset.StandardCharsets;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  
31  import org.apache.commons.io.input.BOMInputStream;
32  import org.junit.jupiter.api.Test;
33  import org.junit.jupiter.api.io.TempDir;
34  
35  /**
36   * Tests for the user guide.
37   */
38  public class UserGuideTest {
39  
40      @TempDir
41      Path tempDir;
42  
43      /**
44       * Creates a reader capable of handling BOMs.
45       *
46       * @param path The path to read.
47       * @return a new InputStreamReader for UTF-8 bytes.
48       * @throws IOException if an I/O error occurs.
49       */
50      public InputStreamReader newReader(final Path path) throws IOException {
51          return new InputStreamReader(BOMInputStream.builder()
52                  .setPath(path)
53                  .get(), StandardCharsets.UTF_8);
54      }
55  
56      @Test
57      public void testBomFull() throws UnsupportedEncodingException, IOException {
58          final Path path = tempDir.resolve("test1.csv");
59          Files.copy(Utils.createUtf8Input("ColumnA, ColumnB, ColumnC\r\nA, B, C\r\n".getBytes(StandardCharsets.UTF_8), true), path);
60          // @formatter:off
61          try (Reader reader = new InputStreamReader(BOMInputStream.builder()
62                  .setPath(path)
63                  .get(), "UTF-8");
64                  CSVParser parser = CSVFormat.EXCEL.builder()
65                          .setHeader()
66                          .get()
67                          .parse(reader)) {
68              // @formatter:off
69              for (final CSVRecord record : parser) {
70                  final String string = record.get("ColumnA");
71                  assertEquals("A", string);
72              }
73          }
74      }
75  
76      @Test
77      public void testBomUtil() throws UnsupportedEncodingException, IOException {
78          final Path path = tempDir.resolve("test2.csv");
79          Files.copy(Utils.createUtf8Input("ColumnA, ColumnB, ColumnC\r\nA, B, C\r\n".getBytes(StandardCharsets.UTF_8), true), path);
80          try (Reader reader = newReader(path);
81                  // @formatter:off
82                  CSVParser parser = CSVFormat.EXCEL.builder()
83                          .setHeader()
84                          .get()
85                          .parse(reader)) {
86              // @formatter:off
87              for (final CSVRecord record : parser) {
88                  final String string = record.get("ColumnA");
89                  assertEquals("A", string);
90              }
91          }
92      }
93  
94  }