/
FileUtilsTest.java
205 lines (178 loc) · 6.67 KB
/
FileUtilsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// FileUtilsTest.java
// Copyright 2016 by luccioman; https://github.com/luccioman
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package net.yacy.kelondro.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Unit tests for the {@link FileUtils} class.
*
*/
public class FileUtilsTest {
@BeforeClass
public static void beforeAll() {
/* Disable assertions here : success/failure of these tests should not rely on assertions that are likely to be disabled at runtime */
FileUtilsTest.class.getClassLoader().setDefaultAssertionStatus(false);
}
/**
* A test stream reading each time less than desired bytes.
* Simulates what can occur for example on real-world HTTP streams.
*/
private class LowerReadThanExpectedInputStream extends FilterInputStream {
protected LowerReadThanExpectedInputStream(InputStream in) {
super(in);
}
/**
* Reads less than specified len bytes
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
if(len > 10 ) {
return super.read(b, off, len - 10);
}
return super.read(b, off, len);
}
}
/**
* Copy stream : normal case
* @throws IOException when a read/write error occurred
*/
@Test
public void testCopyInputStreamOutputStream() throws IOException {
InputStream source = new ByteArrayInputStream("A test string".getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream dest = new ByteArrayOutputStream();
try {
FileUtils.copy(source, dest);
} finally {
source.close();
dest.close();
}
String resultStr = new String(dest.toByteArray(), StandardCharsets.UTF_8);
Assert.assertEquals("A test string", resultStr);
}
/**
* Copy stream : empty input
* @throws IOException when a read/write error occurred
*/
@Test
public void testCopyEmptyInputStreamOutputStream() throws IOException {
InputStream source = new ByteArrayInputStream(new byte[0]);
ByteArrayOutputStream dest = new ByteArrayOutputStream();
try {
FileUtils.copy(source, dest);
} finally {
source.close();
dest.close();
}
String resultStr = new String(dest.toByteArray(), StandardCharsets.UTF_8);
Assert.assertEquals("", resultStr);
}
/**
* Copy stream : output stream with existing content
* @throws IOException when a read/write error occurred
*/
@Test
public void testCopyInputStreamOutputStreamNotEmpty() throws IOException {
InputStream source = new ByteArrayInputStream("An input String".getBytes(StandardCharsets.UTF_8));
ByteArrayOutputStream dest = new ByteArrayOutputStream();
try {
dest.write("Non empty out stream.".getBytes(StandardCharsets.UTF_8));
FileUtils.copy(source, dest);
} finally {
source.close();
dest.close();
}
String resultStr = new String(dest.toByteArray(), StandardCharsets.UTF_8);
Assert.assertEquals("Non empty out stream.An input String", resultStr);
}
/**
* Partial Stream Copy
* @throws IOException when a read/write error occurred
*/
@Test
public void testPartialCopyInputStreamOutputStream() throws IOException {
/* Fill an input stream with more bytes than FileUtils.DEFAULT_BUFFER_SIZE */
byte[] sourceBytes = new byte[2000];
for(int i = 0; i < sourceBytes.length; i++) {
sourceBytes[i] = (byte)(i % Byte.MAX_VALUE);
}
final int COUNT = 1200;
try (InputStream source = new ByteArrayInputStream(sourceBytes);
ByteArrayOutputStream dest = new ByteArrayOutputStream();) {
FileUtils.copy(source, dest, COUNT);
Assert.assertEquals(COUNT, dest.size());
}
/* Copy from a stream reading less than desired bytes (can occurs for example on real-world HTTP streams) */
try (InputStream bufferedSource = new ByteArrayInputStream(sourceBytes);
InputStream source = new LowerReadThanExpectedInputStream(bufferedSource);
ByteArrayOutputStream dest = new ByteArrayOutputStream();) {
FileUtils.copy(source, dest, COUNT);
Assert.assertEquals(COUNT, dest.size());
}
}
/**
* Test reading n bytes in a stream
* @throws IOException when a read/write error occurred
*/
@Test
public void testReadInputStream() throws IOException {
/* Fill an input stream with more bytes than FileUtils.DEFAULT_BUFFER_SIZE */
byte[] sourceBytes = new byte[2000];
for(int i = 0; i < sourceBytes.length; i++) {
sourceBytes[i] = (byte)(i % Byte.MAX_VALUE);
}
/* Read all*/
try(InputStream source = new ByteArrayInputStream(sourceBytes);) {
Assert.assertEquals(sourceBytes.length, FileUtils.read(source, -1).length);
}
/* Read zero */
try(InputStream source = new ByteArrayInputStream(sourceBytes);) {
Assert.assertEquals(0, FileUtils.read(source, 0).length);
}
/* Read only one */
try(InputStream source = new ByteArrayInputStream(sourceBytes);) {
Assert.assertEquals(1, FileUtils.read(source, 1).length);
}
/* Read half */
try(InputStream source = new ByteArrayInputStream(sourceBytes);) {
Assert.assertEquals(sourceBytes.length / 2, FileUtils.read(source, sourceBytes.length / 2).length);
}
/* Read half on a stream reading each time less than desired bytes (can occurs for example on real-world HTTP streams) */
try(InputStream bufferedStream = new ByteArrayInputStream(sourceBytes);
InputStream source = new LowerReadThanExpectedInputStream(bufferedStream);) {
Assert.assertEquals(sourceBytes.length / 2, FileUtils.read(source, sourceBytes.length / 2).length);
}
/* Read exactly source bytes count */
try(InputStream source = new ByteArrayInputStream(sourceBytes);) {
Assert.assertEquals(sourceBytes.length, FileUtils.read(source, sourceBytes.length).length);
}
/* Trying to read more than source bytes count */
try(InputStream source = new ByteArrayInputStream(sourceBytes);) {
Assert.assertEquals(sourceBytes.length, FileUtils.read(source, sourceBytes.length + 10).length);
}
}
}