View Javadoc
1   /* Licensed to the Apache Software Foundation (ASF) under one or more
2    * contributor license agreements. See the NOTICE file distributed with
3    * this work for additional information regarding copyright ownership.
4    * The ASF licenses this file to You under the Apache License, Version 2.0
5    * (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License. */
13  package org.apache.commons.io;
14  
15  import static org.junit.jupiter.api.Assertions.assertFalse;
16  import static org.junit.jupiter.api.Assertions.assertTrue;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  
23  import org.apache.commons.io.test.TestUtils;
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  import org.junit.jupiter.api.io.TempDir;
27  
28  /**
29   * Tests {@link FileUtils}.
30   */
31  public class FileUtilsCopyToFileTest {
32  
33      private final class CheckingInputStream extends ByteArrayInputStream {
34          private boolean closed;
35  
36          public CheckingInputStream(final byte[] data) {
37              super(data);
38              closed = false;
39          }
40  
41          @Override
42          public void close() throws IOException {
43              super.close();
44              closed = true;
45          }
46  
47          public boolean isClosed() {
48              return closed;
49          }
50      }
51  
52      @TempDir
53      public File temporaryFolder;
54  
55      private File testFile;
56  
57      private byte[] testData;
58  
59      @BeforeEach
60      public void setUp() throws Exception {
61          testFile = new File(temporaryFolder, "file1-test.txt");
62          if (!testFile.getParentFile().exists()) {
63              throw new IOException("Cannot create file " + testFile +
64                  " as the parent directory does not exist");
65          }
66  
67          testData = TestUtils.generateTestData(1024);
68      }
69  
70      /**
71       * Tests that {@code copyInputStreamToFile(InputStream, File)} closes the input stream.
72       *
73       * @throws IOException
74       * @see FileUtils#copyInputStreamToFile(InputStream, File)
75       * @see FileUtils#copyToFile(InputStream, File)
76       */
77      @Test
78      public void testCopyInputStreamToFile() throws IOException {
79          try (CheckingInputStream inputStream = new CheckingInputStream(testData)) {
80              FileUtils.copyInputStreamToFile(inputStream, testFile);
81              assertTrue(inputStream.isClosed(), "inputStream should be closed");
82          }
83      }
84  
85      /**
86       * Tests that {@code copyToFile(InputStream, File)} does not close the input stream.
87       *
88       * @throws IOException
89       * @see FileUtils#copyToFile(InputStream, File)
90       * @see FileUtils#copyInputStreamToFile(InputStream, File)
91       */
92      @Test
93      public void testCopyToFile() throws IOException {
94          try (CheckingInputStream inputStream = new CheckingInputStream(testData)) {
95              FileUtils.copyToFile(inputStream, testFile);
96              assertFalse(inputStream.isClosed(), "inputStream should NOT be closed");
97          }
98      }
99  }