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.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          // arrange
    40          final Path file = Paths.get("some/path/test.txt");
    41
    42          // act
    43          final FileGeometryOutput out = new FileGeometryOutput(file);
    44
    45          // assert
    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          // arrange
    54          final Path file = Paths.get("TEST");
    55          final Charset charset = StandardCharsets.UTF_8;
    56
    57          // act
    58          final FileGeometryOutput out = new FileGeometryOutput(file, charset);
    59
    60          // assert
    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          // arrange
    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          // act/assert
    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          // arrange
    87          final FileGeometryOutput out = new FileGeometryOutput(Paths.get("some/path/test.txt"));
    88
    89          // act
    90          final String result = out.toString();
    91
    92          // assert
    93          Assertions.assertEquals("FileGeometryOutput[file= some/path/test.txt]",
    94                  result.replaceAll("\\\\", "/"));
    95      }
    96  }