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    *      https://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.io.build;
18  
19  import static org.junit.jupiter.api.Assertions.assertThrows;
20  
21  import java.io.FileInputStream;
22  import java.io.FileNotFoundException;
23  import java.io.InputStream;
24  import java.nio.file.OpenOption;
25  import java.nio.file.StandardOpenOption;
26  
27  import org.apache.commons.io.build.AbstractOrigin.InputStreamOrigin;
28  import org.apache.commons.io.input.CharSequenceInputStream;
29  import org.junit.jupiter.api.Test;
30  import org.junit.jupiter.params.ParameterizedTest;
31  import org.junit.jupiter.params.provider.EnumSource;
32  
33  /**
34   * Tests {@link InputStreamOrigin}.
35   *
36   * A InputStreamOrigin can convert into some of the other aspects.
37   *
38   * @see InputStream
39   */
40  class InputStreamOriginTest extends AbstractOriginTest<InputStream, InputStreamOrigin> {
41  
42      @SuppressWarnings("resource")
43      @Override
44      protected InputStreamOrigin newOriginRo() throws FileNotFoundException {
45          return new InputStreamOrigin(new FileInputStream(FILE_NAME_RO));
46      }
47  
48      @SuppressWarnings("resource")
49      @Override
50      protected InputStreamOrigin newOriginRw() {
51          return new InputStreamOrigin(CharSequenceInputStream.builder().setCharSequence("World").get());
52      }
53  
54      @Override
55      @Test
56      void testGetFile() {
57          // Cannot convert a InputStream to a File.
58          assertThrows(UnsupportedOperationException.class, super::testGetFile);
59      }
60  
61      @Override
62      @Test
63      void testGetOutputStream() {
64          // Cannot convert a InputStream to an OutputStream.
65          assertThrows(UnsupportedOperationException.class, super::testGetOutputStream);
66      }
67  
68      @Override
69      @Test
70      void testGetPath() {
71          // Cannot convert a InputStream to a Path.
72          assertThrows(UnsupportedOperationException.class, super::testGetPath);
73      }
74  
75      @Override
76      @Test
77      void testGetRandomAccessFile() {
78          // Cannot convert a RandomAccessFile to a File.
79          assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
80      }
81  
82      @Override
83      @ParameterizedTest
84      @EnumSource(StandardOpenOption.class)
85      void testGetRandomAccessFile(final OpenOption openOption) {
86          // Cannot convert a RandomAccessFile to a File.
87          assertThrows(UnsupportedOperationException.class, () -> super.testGetRandomAccessFile(openOption));
88      }
89  
90      @Override
91      @Test
92      void testGetWriter() {
93          // Cannot convert a InputStream to a Writer.
94          assertThrows(UnsupportedOperationException.class, super::testGetWriter);
95      }
96  
97  }