• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
30  * Tests {@link FileUtils}.
31  */
32 public class FileUtilsCopyToFileTest {
33 
34     private class CheckingInputStream extends ByteArrayInputStream {
35         private boolean closed;
36 
CheckingInputStream(final byte[] data)37         public CheckingInputStream(final byte[] data) {
38             super(data);
39             closed = false;
40         }
41 
42         @Override
close()43         public void close() throws IOException {
44             super.close();
45             closed = true;
46         }
47 
isClosed()48         public boolean isClosed() {
49             return closed;
50         }
51     }
52 
53     @TempDir
54     public File temporaryFolder;
55 
56     private File testFile;
57 
58     private byte[] testData;
59 
60     @BeforeEach
setUp()61     public void setUp() throws Exception {
62         testFile = new File(temporaryFolder, "file1-test.txt");
63         if (!testFile.getParentFile().exists()) {
64             throw new IOException("Cannot create file " + testFile +
65                 " as the parent directory does not exist");
66         }
67 
68         testData = TestUtils.generateTestData(1024);
69     }
70 
71     /**
72      * Tests that {@code copyInputStreamToFile(InputStream, File)} closes the input stream.
73      *
74      * @throws IOException
75      * @see FileUtils#copyInputStreamToFile(InputStream, File)
76      * @see FileUtils#copyToFile(InputStream, File)
77      */
78     @Test
testCopyInputStreamToFile()79     public void testCopyInputStreamToFile() throws IOException {
80         try(CheckingInputStream inputStream = new CheckingInputStream(testData)) {
81             FileUtils.copyInputStreamToFile(inputStream, testFile);
82             assertTrue(inputStream.isClosed(), "inputStream should be closed");
83         }
84     }
85 
86     /**
87      * Tests that {@code copyToFile(InputStream, File)} does not close the input stream.
88      *
89      * @throws IOException
90      * @see FileUtils#copyToFile(InputStream, File)
91      * @see FileUtils#copyInputStreamToFile(InputStream, File)
92      */
93     @Test
testCopyToFile()94     public void testCopyToFile() throws IOException {
95         try(CheckingInputStream inputStream = new CheckingInputStream(testData)) {
96             FileUtils.copyToFile(inputStream, testFile);
97             assertFalse(inputStream.isClosed(), "inputStream should NOT be closed");
98         }
99     }
100 }
101