1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38 public class UserGuideTest {
39
40 @TempDir
41 Path tempDir;
42
43
44
45
46
47
48
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
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
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
82 CSVParser parser = CSVFormat.EXCEL.builder()
83 .setHeader()
84 .get()
85 .parse(reader)) {
86
87 for (final CSVRecord record : parser) {
88 final String string = record.get("ColumnA");
89 assertEquals("A", string);
90 }
91 }
92 }
93
94 }