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.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          // arrange
    41          final URL url = Paths.get("some/path/test.txt").toUri().toURL();
    42
    43          // act
    44          final UrlGeometryInput in = new UrlGeometryInput(url);
    45
    46          // assert
    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          // arrange
    55          final URL url = getClass().getResource("/java/lang/String.class");
    56          final Charset charset = StandardCharsets.UTF_8;
    57
    58          // act
    59          final UrlGeometryInput in = new UrlGeometryInput(url, charset);
    60
    61          // assert
    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          // arrange
    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          // act/assert
    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          // arrange
    90          final UrlGeometryInput in = new UrlGeometryInput(Paths.get("some/path/test.txt").toUri().toURL());
    91
    92          // act
    93          final String result = in.toString();
    94
    95          // assert
    96          Assertions.assertTrue(result.startsWith("UrlGeometryInput[url= file:"));
    97      }
    98  }