|
17 | 17 | * under the License. |
18 | 18 | */ |
19 | 19 | package org.apache.commons.csv; |
20 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
21 | 20 |
|
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
22 | 22 |
|
23 | 23 | import java.io.IOException; |
24 | 24 | import java.io.InputStreamReader; |
|
27 | 27 |
|
28 | 28 | import org.junit.jupiter.api.Test; |
29 | 29 |
|
30 | | - |
31 | 30 | public class JiraCsv196Test { |
32 | | - @Test |
33 | | - public void parseThreeBytes() throws IOException { |
34 | | - final CSVFormat format = CSVFormat.Builder.create() |
35 | | - .setDelimiter(',') |
36 | | - .setQuote('\'') |
37 | | - .get(); |
38 | | - final CSVParser parser = new CSVParser.Builder() |
39 | | - .setFormat(format) |
40 | | - .setReader(getTestInput("org/apache/commons/csv/CSV-196/japanese.csv")) |
41 | | - .setCharset(StandardCharsets.UTF_8) |
42 | | - .setEnableByteTracking(true) |
43 | | - .get(); |
44 | | - final long[] charByteKey = {0, 89, 242, 395}; |
45 | | - int idx = 0; |
46 | | - for (CSVRecord record : parser) { |
47 | | - assertEquals(charByteKey[idx++], record.getBytePosition()); |
48 | | - } |
49 | | - parser.close(); |
50 | | - } |
51 | 31 |
|
| 32 | + private Reader getTestInput(String path) { |
| 33 | + return new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream(path)); |
| 34 | + } |
52 | 35 |
|
53 | 36 | @Test |
54 | | - public void parseFourBytes() throws IOException { |
55 | | - final CSVFormat format = CSVFormat.Builder.create() |
56 | | - .setDelimiter(',') |
57 | | - .setQuote('\'') |
58 | | - .get(); |
59 | | - final CSVParser parser = new CSVParser.Builder() |
60 | | - .setFormat(format) |
61 | | - .setReader(getTestInput("org/apache/commons/csv/CSV-196/emoji.csv")) |
62 | | - .setCharset(StandardCharsets.UTF_8) |
63 | | - .setEnableByteTracking(true) |
64 | | - .get(); |
65 | | - final long[] charByteKey = {0, 84, 701, 1318, 1935}; |
66 | | - int idx = 0; |
67 | | - for (CSVRecord record : parser) { |
68 | | - assertEquals(charByteKey[idx++], record.getBytePosition()); |
| 37 | + public void testParseFourBytes() throws IOException { |
| 38 | + final CSVFormat format = CSVFormat.Builder.create().setDelimiter(',').setQuote('\'').get(); |
| 39 | + try (CSVParser parser = new CSVParser.Builder().setFormat(format).setReader(getTestInput("org/apache/commons/csv/CSV-196/emoji.csv")) |
| 40 | + .setCharset(StandardCharsets.UTF_8).setEnableByteTracking(true).get()) { |
| 41 | + final long[] charByteKey = { 0, 84, 701, 1318, 1935 }; |
| 42 | + int idx = 0; |
| 43 | + for (CSVRecord record : parser) { |
| 44 | + assertEquals(charByteKey[idx++], record.getBytePosition()); |
| 45 | + } |
69 | 46 | } |
70 | | - parser.close(); |
71 | 47 | } |
72 | 48 |
|
73 | | - private Reader getTestInput(String path) { |
74 | | - return new InputStreamReader( |
75 | | - ClassLoader.getSystemClassLoader().getResourceAsStream(path)); |
| 49 | + @Test |
| 50 | + public void testParseThreeBytes() throws IOException { |
| 51 | + final CSVFormat format = CSVFormat.Builder.create().setDelimiter(',').setQuote('\'').get(); |
| 52 | + try (CSVParser parser = new CSVParser.Builder().setFormat(format).setReader(getTestInput("org/apache/commons/csv/CSV-196/japanese.csv")) |
| 53 | + .setCharset(StandardCharsets.UTF_8).setEnableByteTracking(true).get()) { |
| 54 | + final long[] charByteKey = { 0, 89, 242, 395 }; |
| 55 | + int idx = 0; |
| 56 | + for (CSVRecord record : parser) { |
| 57 | + assertEquals(charByteKey[idx++], record.getBytePosition()); |
| 58 | + } |
| 59 | + } |
76 | 60 | } |
77 | 61 | } |
0 commit comments