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.net.URL;
23 import java.nio.charset.Charset;
24 import java.nio.charset.StandardCharsets;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.io.TempDir;
32
33 class UrlGeometryInputTest {
34
35 @TempDir
36 Path tempDir;
37
38 @Test
39 void testCtor_fileOnly() throws IOException {
40
41 final URL url = Paths.get("some/path/test.txt").toUri().toURL();
42
43
44 final UrlGeometryInput in = new UrlGeometryInput(url);
45
46
47 Assertions.assertEquals(url, in.getUrl());
48 Assertions.assertEquals("test.txt", in.getFileName());
49 Assertions.assertNull(in.getCharset());
50 }
51
52 @Test
53 void testCtor_fileAndCharset() {
54
55 final URL url = getClass().getResource("/java/lang/String.class");
56 final Charset charset = StandardCharsets.UTF_8;
57
58
59 final UrlGeometryInput in = new UrlGeometryInput(url, charset);
60
61
62 Assertions.assertEquals(url, in.getUrl());
63 Assertions.assertEquals("String.class", in.getFileName());
64 Assertions.assertEquals(charset, in.getCharset());
65 }
66
67 @Test
68 void testGetInputStream() throws IOException {
69
70 final Path file = tempDir.resolve("test");
71 final byte[] bytes = "abc".getBytes(StandardCharsets.UTF_8);
72 Files.write(file, bytes);
73
74 final UrlGeometryInput input = new UrlGeometryInput(file.toUri().toURL());
75
76
77 try (InputStream in = input.getInputStream()) {
78 Assertions.assertEquals(BufferedInputStream.class, in.getClass());
79
80 final byte[] readBytes = new byte[3];
81 in.read(readBytes);
82
83 Assertions.assertArrayEquals(bytes, readBytes);
84 }
85 }
86
87 @Test
88 void testToString() throws IOException {
89
90 final UrlGeometryInput in = new UrlGeometryInput(Paths.get("some/path/test.txt").toUri().toURL());
91
92
93 final String result = in.toString();
94
95
96 Assertions.assertTrue(result.startsWith("UrlGeometryInput[url= file:"));
97 }
98 }