1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.io.core.input;
18
19 import java.io.BufferedInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.nio.charset.Charset;
23 import java.nio.charset.StandardCharsets;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.io.TempDir;
31
32 class FileGeometryInputTest {
33
34 @TempDir
35 Path tempDir;
36
37 @Test
38 void testCtor_fileOnly() {
39
40 final Path file = Paths.get("some/path/test.txt");
41
42
43 final FileGeometryInput in = new FileGeometryInput(file);
44
45
46 Assertions.assertEquals(file, in.getFile());
47 Assertions.assertEquals("test.txt", in.getFileName());
48 Assertions.assertNull(in.getCharset());
49 }
50
51 @Test
52 void testCtor_fileAndCharset() {
53
54 final Path file = Paths.get("TEST");
55 final Charset charset = StandardCharsets.UTF_8;
56
57
58 final FileGeometryInput in = new FileGeometryInput(file, charset);
59
60
61 Assertions.assertEquals(file, in.getFile());
62 Assertions.assertEquals("TEST", in.getFileName());
63 Assertions.assertEquals(charset, in.getCharset());
64 }
65
66 @Test
67 void testGetInputStream() throws IOException {
68
69 final Path file = tempDir.resolve("test");
70 final byte[] bytes = "abc".getBytes(StandardCharsets.UTF_8);
71 Files.write(file, bytes);
72
73 final FileGeometryInput input = new FileGeometryInput(file);
74
75
76 try (InputStream in = input.getInputStream()) {
77 Assertions.assertEquals(BufferedInputStream.class, in.getClass());
78
79 final byte[] readBytes = new byte[3];
80 in.read(readBytes);
81
82 Assertions.assertArrayEquals(bytes, readBytes);
83 }
84 }
85
86 @Test
87 void testToString() {
88
89 final FileGeometryInput in = new FileGeometryInput(Paths.get("some/path/test.txt"));
90
91
92 final String result = in.toString();
93
94
95 Assertions.assertEquals("FileGeometryInput[file= some/path/test.txt]",
96 result.replaceAll("\\\\", "/"));
97 }
98 }