1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.io.core.output;
18
19 import java.io.BufferedOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStream;
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 FileGeometryOutputTest {
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 FileGeometryOutput out = new FileGeometryOutput(file);
44
45
46 Assertions.assertEquals(file, out.getFile());
47 Assertions.assertEquals("test.txt", out.getFileName());
48 Assertions.assertNull(out.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 FileGeometryOutput out = new FileGeometryOutput(file, charset);
59
60
61 Assertions.assertEquals(file, out.getFile());
62 Assertions.assertEquals("TEST", out.getFileName());
63 Assertions.assertEquals(charset, out.getCharset());
64 }
65
66 @Test
67 void testGetOutputStream() throws IOException {
68
69 final Path file = tempDir.resolve("test");
70 final byte[] bytes = "abc".getBytes(StandardCharsets.UTF_8);
71
72 final FileGeometryOutput output = new FileGeometryOutput(file);
73
74
75 try (OutputStream out = output.getOutputStream()) {
76 out.write(bytes);
77
78 Assertions.assertEquals(BufferedOutputStream.class, out.getClass());
79 }
80
81 Assertions.assertArrayEquals(bytes, Files.readAllBytes(file));
82 }
83
84 @Test
85 void testToString() {
86
87 final FileGeometryOutput out = new FileGeometryOutput(Paths.get("some/path/test.txt"));
88
89
90 final String result = out.toString();
91
92
93 Assertions.assertEquals("FileGeometryOutput[file= some/path/test.txt]",
94 result.replaceAll("\\\\", "/"));
95 }
96 }