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  
18  package org.apache.commons.io.input;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  
30  import org.apache.commons.io.IOUtils;
31  import org.apache.commons.lang3.StringUtils;
32  import org.junit.jupiter.api.AfterEach;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Tests {@link UnsynchronizedFilterInputStream}.
38   * <p>
39   * Provenance: Apache Harmony and modified.
40   * </p>
41   */
42  public class UnsynchronizedFilterInputStreamTest {
43  
44      public static final String DATA = StringUtils.repeat("This is a test.", 500);
45  
46      private Path fileName;
47  
48      private InputStream is;
49  
50      byte[] ibuf = new byte[4096];
51  
52      /**
53       * Sets up the fixture, for example, open a network connection. This method is called before a test is executed.
54       *
55       * @throws IOException Thrown on test failure.
56       */
57      @SuppressWarnings("resource") // See @AfterEach tearDown() method
58      @BeforeEach
59      protected void setUp() throws IOException {
60          fileName = Files.createTempFile(getClass().getSimpleName(), ".tst");
61          Files.write(fileName, DATA.getBytes(StandardCharsets.UTF_8));
62          is = UnsynchronizedFilterInputStream.builder().setInputStream(Files.newInputStream(fileName)).get();
63      }
64  
65      /**
66       * Tears down the fixture, for example, close a network connection. This method is called after a test is executed.
67       *
68       * @throws IOException Thrown on test failure.
69       */
70      @AfterEach
71      protected void tearDown() throws IOException {
72          IOUtils.closeQuietly(is);
73          Files.deleteIfExists(fileName);
74      }
75  
76      /**
77       * Tests java.io.FilterInputStream#available()
78       *
79       * @throws IOException Thrown on test failure.
80       */
81      @Test
82      public void test_available() throws IOException {
83          assertEquals(DATA.length(), is.available(), "Returned incorrect number of available bytes");
84      }
85  
86      /**
87       * Tests java.io.FilterInputStream#close()
88       *
89       * @throws IOException Thrown on test failure.
90       */
91      @Test
92      public void test_close() throws IOException {
93          is.close();
94          assertThrows(IOException.class, () -> is.read(), "Able to read from closed stream");
95      }
96  
97      /**
98       * Tests java.io.FilterInputStream#mark(int)
99       */
100     @Test
101     public void test_markI() {
102         assertTrue(true, "Mark not supported by parent InputStream");
103     }
104 
105     /**
106      * Tests java.io.FilterInputStream#markSupported()
107      */
108     @Test
109     public void test_markSupported() {
110         assertTrue(!is.markSupported(), "markSupported returned true");
111     }
112 
113     /**
114      * Tests java.io.FilterInputStream#read()
115      *
116      * @throws IOException Thrown on test failure.
117      */
118     @Test
119     public void test_read() throws IOException {
120         final int c = is.read();
121         assertEquals(DATA.charAt(0), c, "read returned incorrect char");
122     }
123 
124     /**
125      * Tests java.io.FilterInputStream#read(byte[])
126      *
127      * @throws IOException Thrown on test failure.
128      */
129     @Test
130     public void test_read$B() throws IOException {
131         final byte[] buf1 = new byte[100];
132         assertEquals(buf1.length, is.read(buf1));
133         assertTrue(new String(buf1, 0, buf1.length, StandardCharsets.UTF_8).equals(DATA.substring(0, 100)), "Failed to read correct data");
134     }
135 
136     /**
137      * Tests java.io.FilterInputStream#read(byte[], int, int)
138      *
139      * @throws IOException Thrown on test failure.
140      */
141     @Test
142     public void test_read$BII() throws IOException {
143         final byte[] buf1 = new byte[100];
144         is.skip(3000);
145         is.mark(1000);
146         is.read(buf1, 0, buf1.length);
147         assertTrue(new String(buf1, 0, buf1.length, StandardCharsets.UTF_8).equals(DATA.substring(3000, 3100)), "Failed to read correct data");
148     }
149 
150     /**
151      * Tests java.io.FilterInputStream#reset()
152      */
153     @Test
154     public void test_reset() {
155         assertThrows(IOException.class, () -> is.reset(), "should throw IOException");
156 
157     }
158 
159     /**
160      * Tests java.io.FilterInputStream#skip(long)
161      *
162      * @throws IOException Thrown on test failure.
163      */
164     @Test
165     public void test_skipJ() throws IOException {
166         final byte[] buf1 = new byte[10];
167         is.skip(1000);
168         is.read(buf1, 0, buf1.length);
169         assertTrue(new String(buf1, 0, buf1.length, StandardCharsets.UTF_8).equals(DATA.substring(1000, 1010)), "Failed to skip to correct position");
170     }
171 }