View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // arrange
40          final Path file = Paths.get("some/path/test.txt");
41  
42          // act
43          final FileGeometryInput in = new FileGeometryInput(file);
44  
45          // assert
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          // arrange
54          final Path file = Paths.get("TEST");
55          final Charset charset = StandardCharsets.UTF_8;
56  
57          // act
58          final FileGeometryInput in = new FileGeometryInput(file, charset);
59  
60          // assert
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          // arrange
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          // act/assert
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          // arrange
89          final FileGeometryInput in = new FileGeometryInput(Paths.get("some/path/test.txt"));
90  
91          // act
92          final String result = in.toString();
93  
94          // assert
95          Assertions.assertEquals("FileGeometryInput[file= some/path/test.txt]",
96                  result.replaceAll("\\\\", "/"));
97      }
98  }