Skip to content

Commit 55da87d

Browse files
committed
Lighten test set up
Only create the file test fixture once per class run
1 parent e48e3e3 commit 55da87d

File tree

3 files changed

+64
-59
lines changed

3 files changed

+64
-59
lines changed

src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
import org.apache.commons.io.IOUtils;
3131
import org.apache.commons.lang3.RandomUtils;
3232
import org.junit.jupiter.api.AfterEach;
33-
import org.junit.jupiter.api.BeforeEach;
33+
import org.junit.jupiter.api.BeforeAll;
3434
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.io.TempDir;
36+
import org.junit.jupiter.params.ParameterizedTest;
37+
import org.junit.jupiter.params.provider.MethodSource;
3538

3639
/**
3740
* Tests {@link InputStream} subclasses.
@@ -50,23 +53,29 @@ static int[] getArrayLengths() {
5053
return ARRAY_LENGTHS;
5154
}
5255

53-
protected byte[] actualRandomBytes;
54-
protected byte[] expectedRandomBytes;
55-
protected Path inputFile;
56+
protected static byte[] ActualBytes;
57+
protected static byte[] ExpectedBytes;
58+
protected static Path InputPath;
59+
60+
/**
61+
* Set in subclasses.
62+
*/
5663
protected InputStream[] inputStreams;
5764

58-
@BeforeEach
59-
public void setUp() throws IOException {
65+
@TempDir
66+
static Path tempDir;
67+
68+
@BeforeAll
69+
public static void setUp() throws IOException {
6070
// Create a byte array of size 2 MB with random bytes
61-
actualRandomBytes = RandomUtils.insecure().randomBytes(2 * 1024 * 1024);
62-
expectedRandomBytes = actualRandomBytes;
63-
inputFile = Files.createTempFile("temp-file", ".tmp");
64-
Files.write(inputFile, actualRandomBytes);
71+
ActualBytes = RandomUtils.insecure().randomBytes(2 * 1024 * 1024);
72+
ExpectedBytes = ActualBytes.clone();
73+
InputPath = tempDir.resolve(AbstractInputStreamTest.class.getSimpleName() + ".tmp");
74+
Files.write(InputPath, ActualBytes);
6575
}
6676

6777
@AfterEach
6878
public void tearDown() throws IOException {
69-
Files.delete(inputFile);
7079
IOUtils.close(inputStreams);
7180
}
7281

@@ -81,7 +90,7 @@ public void testAvailableAfterClose() throws Exception {
8190
@Test
8291
public void testAvailableAfterOpen() throws Exception {
8392
for (final InputStream inputStream : inputStreams) {
84-
assertEquals(0, inputStream.available());
93+
assertTrue(inputStream.available() >= 0);
8594
}
8695
}
8796

@@ -105,16 +114,16 @@ public void testAvailableAtEnd() throws Exception {
105114
public void testBytesSkipped() throws IOException {
106115
for (final InputStream inputStream : inputStreams) {
107116
assertEquals(1024, inputStream.skip(1024));
108-
for (int i = 1024; i < expectedRandomBytes.length; i++) {
109-
assertEquals(expectedRandomBytes[i], (byte) inputStream.read());
117+
for (int i = 1024; i < ExpectedBytes.length; i++) {
118+
assertEquals(ExpectedBytes[i], (byte) inputStream.read());
110119
}
111120
}
112121
}
113122

114123
@Test
115124
public void testBytesSkippedAfterEOF() throws IOException {
116125
for (final InputStream inputStream : inputStreams) {
117-
assertEquals(expectedRandomBytes.length, inputStream.skip(actualRandomBytes.length + 1));
126+
assertEquals(ExpectedBytes.length, inputStream.skip(ActualBytes.length + 1));
118127
assertEquals(-1, inputStream.read());
119128
}
120129
}
@@ -123,11 +132,11 @@ public void testBytesSkippedAfterEOF() throws IOException {
123132
public void testBytesSkippedAfterRead() throws IOException {
124133
for (final InputStream inputStream : inputStreams) {
125134
for (int i = 0; i < 1024; i++) {
126-
assertEquals(expectedRandomBytes[i], (byte) inputStream.read());
135+
assertEquals(ExpectedBytes[i], (byte) inputStream.read());
127136
}
128137
assertEquals(1024, inputStream.skip(1024));
129-
for (int i = 2048; i < expectedRandomBytes.length; i++) {
130-
assertEquals(expectedRandomBytes[i], (byte) inputStream.read());
138+
for (int i = 2048; i < ExpectedBytes.length; i++) {
139+
assertEquals(ExpectedBytes[i], (byte) inputStream.read());
131140
}
132141
}
133142
}
@@ -136,28 +145,29 @@ public void testBytesSkippedAfterRead() throws IOException {
136145
public void testNegativeBytesSkippedAfterRead() throws IOException {
137146
for (final InputStream inputStream : inputStreams) {
138147
for (int i = 0; i < 1024; i++) {
139-
assertEquals(expectedRandomBytes[i], (byte) inputStream.read());
148+
assertEquals(ExpectedBytes[i], (byte) inputStream.read());
140149
}
141150
// Skipping negative bytes should essential be a no-op
142151
assertEquals(0, inputStream.skip(-1));
143152
assertEquals(0, inputStream.skip(-1024));
144153
assertEquals(0, inputStream.skip(Long.MIN_VALUE));
145154
assertEquals(1024, inputStream.skip(1024));
146-
for (int i = 2048; i < expectedRandomBytes.length; i++) {
147-
assertEquals(expectedRandomBytes[i], (byte) inputStream.read());
155+
for (int i = 2048; i < ExpectedBytes.length; i++) {
156+
assertEquals(ExpectedBytes[i], (byte) inputStream.read());
148157
}
149158
}
150159
}
151160

152-
@Test
153-
public void testReadMultipleBytes() throws IOException {
161+
@ParameterizedTest
162+
@MethodSource("org.apache.commons.io.input.ReversedLinesFileReaderParamBlockSizeTest#blockSizes")
163+
public void testReadMultipleBytes(final int bufferSize) throws IOException {
154164
for (final InputStream inputStream : inputStreams) {
155-
final byte[] readBytes = new byte[8 * 1024];
165+
final byte[] readBytes = new byte[bufferSize];
156166
int i = 0;
157-
while (i < expectedRandomBytes.length) {
158-
final int read = inputStream.read(readBytes, 0, 8 * 1024);
167+
while (i < ExpectedBytes.length) {
168+
final int read = inputStream.read(readBytes, 0, readBytes.length);
159169
for (int j = 0; j < read; j++) {
160-
assertEquals(expectedRandomBytes[i], readBytes[j]);
170+
assertEquals(ExpectedBytes[i], readBytes[j]);
161171
i++;
162172
}
163173
}
@@ -167,7 +177,7 @@ public void testReadMultipleBytes() throws IOException {
167177
@Test
168178
public void testReadOneByOne() throws IOException {
169179
for (final InputStream inputStream : inputStreams) {
170-
for (final byte randomByte : expectedRandomBytes) {
180+
for (final byte randomByte : ExpectedBytes) {
171181
assertEquals(randomByte, (byte) inputStream.read());
172182
}
173183
}
@@ -181,9 +191,9 @@ public void testReadOneByOneCheckAvailable() throws IOException {
181191
final AtomicInteger refIB = new AtomicInteger();
182192
@SuppressWarnings("resource")
183193
final InputStream inputStream = inputStreams[idxInputs];
184-
for (int idxBytes = 0; idxBytes < expectedRandomBytes.length; idxBytes++) {
194+
for (int idxBytes = 0; idxBytes < ExpectedBytes.length; idxBytes++) {
185195
refIB.set(idxBytes);
186-
final byte randomByte = expectedRandomBytes[idxBytes];
196+
final byte randomByte = ExpectedBytes[idxBytes];
187197
// Check that available() doesn't have a side effect on read()
188198
final int available = inputStream.available();
189199
final Supplier<String> messageSupplier = () -> String.format("idxInputs = %,d, idxBytes = %,d, available = %,d", refII.get(), refIB.get(),
@@ -195,13 +205,12 @@ public void testReadOneByOneCheckAvailable() throws IOException {
195205
}
196206

197207
@Test
198-
public void testReadPastEOF() throws IOException {
208+
public void testReadPastEof() throws IOException {
199209
final InputStream is = inputStreams[0];
200210
final byte[] buf = new byte[1024];
201211
while (is.read(buf, 0, buf.length) != -1) {
202212
// empty
203213
}
204-
205214
final int readAfterEOF = is.read(buf, 0, buf.length);
206215
assertEquals(-1, readAfterEOF);
207216
}
@@ -213,13 +222,13 @@ public void testSkipFromFileChannel() throws IOException {
213222
// we skip from underlying file channel.
214223
assertEquals(1024, inputStream.skip(1024));
215224
for (int i = 1024; i < 2048; i++) {
216-
assertEquals(expectedRandomBytes[i], (byte) inputStream.read());
225+
assertEquals(ExpectedBytes[i], (byte) inputStream.read());
217226
}
218227
assertEquals(256, inputStream.skip(256));
219228
assertEquals(256, inputStream.skip(256));
220229
assertEquals(512, inputStream.skip(512));
221-
for (int i = 3072; i < expectedRandomBytes.length; i++) {
222-
assertEquals(expectedRandomBytes[i], (byte) inputStream.read());
230+
for (int i = 3072; i < ExpectedBytes.length; i++) {
231+
assertEquals(ExpectedBytes[i], (byte) inputStream.read());
223232
}
224233
}
225234
}

src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,17 @@
3636
public class BufferedFileChannelInputStreamTest extends AbstractInputStreamTest {
3737

3838
@SuppressWarnings("resource")
39-
@Override
4039
@BeforeEach
41-
public void setUp() throws IOException {
42-
super.setUp();
40+
public void setUpInputStreams() throws IOException {
4341
// @formatter:off
4442
inputStreams = new InputStream[] {
45-
new BufferedFileChannelInputStream(inputFile), // default
46-
new BufferedFileChannelInputStream(inputFile, 123), // small, unaligned buffer size
47-
BufferedFileChannelInputStream.builder().setPath(inputFile).get(), // default
48-
BufferedFileChannelInputStream.builder().setPath(inputFile).setBufferSize(123).get(), // small, unaligned buffer size
49-
BufferedFileChannelInputStream.builder().setURI(inputFile.toUri()).setBufferSize(1024).get(), // URI and buffer size
50-
BufferedFileChannelInputStream.builder().setPath(inputFile).setOpenOptions(StandardOpenOption.READ).get(), // open options
51-
BufferedFileChannelInputStream.builder().setFileChannel(FileChannel.open(inputFile)).get(), // FileChannel
43+
new BufferedFileChannelInputStream(InputPath), // default
44+
new BufferedFileChannelInputStream(InputPath, 123), // small, unaligned buffer size
45+
BufferedFileChannelInputStream.builder().setPath(InputPath).get(), // default
46+
BufferedFileChannelInputStream.builder().setPath(InputPath).setBufferSize(123).get(), // small, unaligned buffer size
47+
BufferedFileChannelInputStream.builder().setURI(InputPath.toUri()).setBufferSize(1024).get(), // URI and buffer size
48+
BufferedFileChannelInputStream.builder().setPath(InputPath).setOpenOptions(StandardOpenOption.READ).get(), // open options
49+
BufferedFileChannelInputStream.builder().setFileChannel(FileChannel.open(InputPath)).get(), // FileChannel
5250
};
5351
//@formatter:on
5452
}

src/test/java/org/apache/commons/io/input/ReadAheadInputStreamTest.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,31 @@
3030
public class ReadAheadInputStreamTest extends AbstractInputStreamTest {
3131

3232
@SuppressWarnings("resource")
33-
@Override
3433
@BeforeEach
35-
public void setUp() throws IOException {
36-
super.setUp();
34+
public void setUpInputStreams() throws IOException {
3735
inputStreams = new InputStream[] {
3836
// Tests equal and aligned buffers of wrapped an outer stream.
39-
new ReadAheadInputStream(new BufferedFileChannelInputStream(inputFile, 8 * 1024), 8 * 1024),
37+
new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 8 * 1024), 8 * 1024),
4038
// Tests aligned buffers, wrapped bigger than outer.
41-
new ReadAheadInputStream(new BufferedFileChannelInputStream(inputFile, 3 * 1024), 2 * 1024),
39+
new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 3 * 1024), 2 * 1024),
4240
// Tests aligned buffers, wrapped smaller than outer.
43-
new ReadAheadInputStream(new BufferedFileChannelInputStream(inputFile, 2 * 1024), 3 * 1024),
41+
new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 2 * 1024), 3 * 1024),
4442
// Tests unaligned buffers, wrapped bigger than outer.
45-
new ReadAheadInputStream(new BufferedFileChannelInputStream(inputFile, 321), 123),
43+
new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 321), 123),
4644
// Tests unaligned buffers, wrapped smaller than outer.
47-
new ReadAheadInputStream(new BufferedFileChannelInputStream(inputFile, 123), 321),
45+
new ReadAheadInputStream(new BufferedFileChannelInputStream(InputPath, 123), 321),
4846
//
4947
// Tests equal and aligned buffers of wrapped an outer stream.
50-
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(inputFile, 8 * 1024)).setBufferSize(8 * 1024).get(),
48+
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 8 * 1024)).setBufferSize(8 * 1024).get(),
5149
// Tests aligned buffers, wrapped bigger than outer.
52-
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(inputFile, 3 * 1024)).setBufferSize(2 * 1024).get(),
50+
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 3 * 1024)).setBufferSize(2 * 1024).get(),
5351
// Tests aligned buffers, wrapped smaller than outer.
54-
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(inputFile, 2 * 1024)).setBufferSize(3 * 1024).get(),
52+
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 2 * 1024)).setBufferSize(3 * 1024).get(),
5553
// Tests unaligned buffers, wrapped bigger than outer.
56-
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(inputFile, 321)).setBufferSize(123).get(),
54+
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 321)).setBufferSize(123).get(),
5755
// Tests unaligned buffers, wrapped smaller than outer.
58-
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(inputFile, 123)).setBufferSize(321).get(),
59-
ReadAheadInputStream.builder().setPath(inputFile).setOpenOptions(StandardOpenOption.READ).get() };
56+
ReadAheadInputStream.builder().setInputStream(new BufferedFileChannelInputStream(InputPath, 123)).setBufferSize(321).get(),
57+
ReadAheadInputStream.builder().setPath(InputPath).setOpenOptions(StandardOpenOption.READ).get() };
6058
}
6159

6260
}

0 commit comments

Comments
 (0)