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