Skip to content

Commit 77a1915

Browse files
committed
Simplify test
1 parent e7a7542 commit 77a1915

1 file changed

Lines changed: 79 additions & 79 deletions

File tree

src/test/java/org/apache/commons/io/channels/FileChannelsTest.java

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.FileInputStream;
2828
import java.io.IOException;
2929
import java.nio.channels.FileChannel;
30+
import java.nio.channels.SeekableByteChannel;
3031
import java.nio.file.Files;
3132
import java.nio.file.Path;
3233
import java.time.Duration;
@@ -64,81 +65,6 @@ public static Object[][] getBufferSizesAndTimeouts() {
6465
{ IOUtils.DEFAULT_BUFFER_SIZE, Duration.ZERO }, { IOUtils.DEFAULT_BUFFER_SIZE * 10, Duration.ZERO } };
6566
}
6667

67-
private static byte reverse(final byte b) {
68-
return (byte) (~b & 0xff);
69-
}
70-
71-
private boolean isEmpty(final File empty) {
72-
return empty.length() == 0;
73-
}
74-
75-
private FileChannel reset(final FileChannel fc) throws IOException {
76-
return fc.position(0);
77-
}
78-
79-
@CartesianTest()
80-
public void testContentEquals(
81-
@Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10, IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int bufferSize,
82-
@CartesianTest.Enum final FileChannelType fileChannelType1, @CartesianTest.Enum final FileChannelType fileChannelType2) throws IOException {
83-
final Path bigFile1 = Files.createTempFile(getClass().getSimpleName(), "-1.bin");
84-
final Path bigFile2 = Files.createTempFile(getClass().getSimpleName(), "-2.bin");
85-
final Path bigFile3 = Files.createTempFile(getClass().getSimpleName(), "-3.bin");
86-
try {
87-
// This length must match any restriction from the Surefire configuration.
88-
final int newLength = 1_000_000;
89-
final byte[] bytes1 = new byte[newLength];
90-
final byte[] bytes2 = new byte[newLength];
91-
// Make sure bytes1 and bytes2 are different despite the shuffle
92-
ArrayUtils.shuffle(bytes1);
93-
bytes1[0] = 1;
94-
ArrayUtils.shuffle(bytes2);
95-
bytes2[0] = 2;
96-
Files.write(bigFile1, bytes1);
97-
Files.write(bigFile2, bytes2);
98-
try (FileChannel fc1 = open(bigFile1, fileChannelType1); FileChannel fc2 = open(bigFile2, fileChannelType2)) {
99-
assertFalse(FileChannels.contentEquals(fc1, fc2, bufferSize));
100-
assertFalse(FileChannels.contentEquals(reset(fc2), reset(fc1), bufferSize));
101-
assertTrue(FileChannels.contentEquals(reset(fc1), reset(fc1), bufferSize));
102-
}
103-
// Make the LAST byte different.
104-
byte[] bytes3 = bytes1.clone();
105-
final int last = bytes3.length - 1;
106-
bytes3[last] = reverse(bytes3[last]);
107-
Files.write(bigFile3, bytes3);
108-
try (FileChannel fc1 = open(bigFile1, fileChannelType1); FileChannel fc3 = open(bigFile3, fileChannelType2)) {
109-
assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
110-
assertFalse(FileChannels.contentEquals(reset(fc3), reset(fc1), bufferSize));
111-
// Test just the last byte
112-
fc1.position(last);
113-
fc3.position(last);
114-
assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
115-
}
116-
// Make the LAST byte equal.
117-
bytes3 = bytes1.clone();
118-
Files.write(bigFile3, bytes3);
119-
try (FileChannel fc1 = open(bigFile1, fileChannelType1); FileChannel fc3 = open(bigFile3, fileChannelType2)) {
120-
// Test just the last byte
121-
fc1.position(last);
122-
fc3.position(last);
123-
assertTrue(FileChannels.contentEquals(fc1, fc3, bufferSize));
124-
}
125-
// Make a byte in the middle different
126-
bytes3 = bytes1.clone();
127-
final int middle = bytes3.length / 2;
128-
bytes3[middle] = reverse(bytes3[middle]);
129-
Files.write(bigFile3, bytes3);
130-
try (FileChannel fc1 = open(bigFile1, fileChannelType1); FileChannel fc3 = open(bigFile3, fileChannelType2)) {
131-
assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
132-
assertFalse(FileChannels.contentEquals(reset(fc3), reset(fc1), bufferSize));
133-
}
134-
} finally {
135-
// Delete ASAP
136-
Files.deleteIfExists(bigFile1);
137-
Files.deleteIfExists(bigFile2);
138-
Files.deleteIfExists(bigFile3);
139-
}
140-
}
141-
14268
private static FileChannel open(final Path path, final FileChannelType fileChannelType) throws IOException {
14369
final FileChannel fc = FileChannel.open(path);
14470
if (fileChannelType != null) {
@@ -156,6 +82,18 @@ private static FileChannel open(final Path path, final FileChannelType fileChann
15682
return FileChannel.open(path);
15783
}
15884

85+
private static byte reverse(final byte b) {
86+
return (byte) (~b & 0xff);
87+
}
88+
89+
private boolean isEmpty(final File empty) {
90+
return empty.length() == 0;
91+
}
92+
93+
private FileChannel reset(final FileChannel fc) throws IOException {
94+
return fc.position(0);
95+
}
96+
15997
private void testContentEquals(final String content1, final String content2, final int bufferSize) throws IOException {
16098
assertTrue(FileChannels.contentEquals(null, null, bufferSize));
16199
// Prepare test files with same size but different content
@@ -221,10 +159,9 @@ public void testContentEqualsEmpty(final int bufferSize) throws IOException {
221159
}
222160

223161
@CartesianTest
224-
public void testContentEqualsTimeout(
225-
@Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10, IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int bufferSize,
226-
@Values(ints = { 0, 1 }) final int timeoutMillis) throws IOException {
227-
final Duration timeout = Duration.ofMillis(timeoutMillis);
162+
public void testContentEqualsFileChannel(
163+
@Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10, IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int bufferSize)
164+
throws IOException {
228165
final Path bigFile1 = Files.createTempFile(getClass().getSimpleName(), "-1.bin");
229166
final Path bigFile2 = Files.createTempFile(getClass().getSimpleName(), "-2.bin");
230167
final Path bigFile3 = Files.createTempFile(getClass().getSimpleName(), "-3.bin");
@@ -283,4 +220,67 @@ public void testContentEqualsTimeout(
283220
Files.deleteIfExists(bigFile3);
284221
}
285222
}
223+
224+
@CartesianTest()
225+
public void testContentEqualsSeekableByteChannel(
226+
@Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10, IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int bufferSize,
227+
@CartesianTest.Enum final FileChannelType fileChannelType1, @CartesianTest.Enum final FileChannelType fileChannelType2) throws IOException {
228+
final Path bigFile1 = Files.createTempFile(getClass().getSimpleName(), "-1.bin");
229+
final Path bigFile2 = Files.createTempFile(getClass().getSimpleName(), "-2.bin");
230+
final Path bigFile3 = Files.createTempFile(getClass().getSimpleName(), "-3.bin");
231+
try {
232+
// This length must match any restriction from the Surefire configuration.
233+
final int newLength = 1_000_000;
234+
final byte[] bytes1 = new byte[newLength];
235+
final byte[] bytes2 = new byte[newLength];
236+
// Make sure bytes1 and bytes2 are different despite the shuffle
237+
ArrayUtils.shuffle(bytes1);
238+
bytes1[0] = 1;
239+
ArrayUtils.shuffle(bytes2);
240+
bytes2[0] = 2;
241+
Files.write(bigFile1, bytes1);
242+
Files.write(bigFile2, bytes2);
243+
try (FileChannel fc1 = open(bigFile1, fileChannelType1); FileChannel fc2 = open(bigFile2, fileChannelType2)) {
244+
assertFalse(FileChannels.contentEquals((SeekableByteChannel) fc1, fc2, bufferSize));
245+
assertFalse(FileChannels.contentEquals((SeekableByteChannel) reset(fc2), reset(fc1), bufferSize));
246+
assertTrue(FileChannels.contentEquals((SeekableByteChannel) reset(fc1), reset(fc1), bufferSize));
247+
}
248+
// Make the LAST byte different.
249+
byte[] bytes3 = bytes1.clone();
250+
final int last = bytes3.length - 1;
251+
bytes3[last] = reverse(bytes3[last]);
252+
Files.write(bigFile3, bytes3);
253+
try (FileChannel fc1 = open(bigFile1, fileChannelType1); FileChannel fc3 = open(bigFile3, fileChannelType2)) {
254+
assertFalse(FileChannels.contentEquals((SeekableByteChannel) fc1, fc3, bufferSize));
255+
assertFalse(FileChannels.contentEquals((SeekableByteChannel) reset(fc3), reset(fc1), bufferSize));
256+
// Test just the last byte
257+
fc1.position(last);
258+
fc3.position(last);
259+
assertFalse(FileChannels.contentEquals((SeekableByteChannel) fc1, fc3, bufferSize));
260+
}
261+
// Make the LAST byte equal.
262+
bytes3 = bytes1.clone();
263+
Files.write(bigFile3, bytes3);
264+
try (FileChannel fc1 = open(bigFile1, fileChannelType1); FileChannel fc3 = open(bigFile3, fileChannelType2)) {
265+
// Test just the last byte
266+
fc1.position(last);
267+
fc3.position(last);
268+
assertTrue(FileChannels.contentEquals((SeekableByteChannel) fc1, fc3, bufferSize));
269+
}
270+
// Make a byte in the middle different
271+
bytes3 = bytes1.clone();
272+
final int middle = bytes3.length / 2;
273+
bytes3[middle] = reverse(bytes3[middle]);
274+
Files.write(bigFile3, bytes3);
275+
try (FileChannel fc1 = open(bigFile1, fileChannelType1); FileChannel fc3 = open(bigFile3, fileChannelType2)) {
276+
assertFalse(FileChannels.contentEquals((SeekableByteChannel) fc1, fc3, bufferSize));
277+
assertFalse(FileChannels.contentEquals((SeekableByteChannel) reset(fc3), reset(fc1), bufferSize));
278+
}
279+
} finally {
280+
// Delete ASAP
281+
Files.deleteIfExists(bigFile1);
282+
Files.deleteIfExists(bigFile2);
283+
Files.deleteIfExists(bigFile3);
284+
}
285+
}
286286
}

0 commit comments

Comments
 (0)