Skip to content

Commit 46e99a4

Browse files
committed
More FileChannels testing
1 parent ae70118 commit 46e99a4

File tree

1 file changed

+51
-48
lines changed

1 file changed

+51
-48
lines changed

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

+51-48
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.junit.jupiter.api.Assertions.assertFalse;
2222
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
24-
import static org.junit.jupiter.api.Assertions.fail;
2524

2625
import java.io.File;
2726
import java.io.FileInputStream;
@@ -36,8 +35,6 @@
3635
import org.apache.commons.io.file.AbstractTempDirTest;
3736
import org.apache.commons.lang3.ArrayUtils;
3837
import org.apache.commons.lang3.StringUtils;
39-
import org.junit.jupiter.params.ParameterizedTest;
40-
import org.junit.jupiter.params.provider.MethodSource;
4138
import org.junitpioneer.jupiter.cartesian.CartesianTest;
4239
import org.junitpioneer.jupiter.cartesian.CartesianTest.Values;
4340

@@ -53,41 +50,43 @@ enum FileChannelType {
5350
private static final int SMALL_BUFFER_SIZE = 1024;
5451
private static final String CONTENT = StringUtils.repeat("x", SMALL_BUFFER_SIZE);
5552

56-
public static int[] getBufferSizes() {
57-
// 1 and 2 are unusual and slow edge cases, but edge cases nonetheless.
58-
return new int[] { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10, IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 };
53+
@SuppressWarnings("resource") // Caller closes
54+
private static FileChannel getChannel(final FileInputStream inNotEmpty, final FileChannelType fileChannelType) throws IOException {
55+
return wrap(inNotEmpty.getChannel(), fileChannelType);
5956
}
6057

58+
private static boolean isEmpty(final File empty) {
59+
return empty.length() == 0;
60+
}
61+
62+
@SuppressWarnings("resource") // Caller closes
6163
private static FileChannel open(final Path path, final FileChannelType fileChannelType) throws IOException {
62-
final FileChannel fc = FileChannel.open(path);
63-
if (fileChannelType != null) {
64-
switch (fileChannelType) {
65-
case NON_BLOCKING:
66-
return new NonBlockingFileChannelProxy(fc);
67-
case STOCK:
68-
return fc;
69-
case PROXY:
70-
return new FileChannelProxy(fc);
71-
default:
72-
fail("Unexpected FileChannelType " + fileChannelType);
73-
}
74-
}
75-
return FileChannel.open(path);
64+
return wrap(FileChannel.open(path), fileChannelType);
7665
}
7766

78-
private static byte reverse(final byte b) {
79-
return (byte) (~b & 0xff);
67+
private static FileChannel reset(final FileChannel fc) throws IOException {
68+
return fc.position(0);
8069
}
8170

82-
private boolean isEmpty(final File empty) {
83-
return empty.length() == 0;
71+
private static byte reverse(final byte b) {
72+
return (byte) (~b & 0xff);
8473
}
8574

86-
private FileChannel reset(final FileChannel fc) throws IOException {
87-
return fc.position(0);
75+
private static FileChannel wrap(final FileChannel fc, final FileChannelType fileChannelType) throws IOException {
76+
switch (fileChannelType) {
77+
case NON_BLOCKING:
78+
return new NonBlockingFileChannelProxy(fc);
79+
case STOCK:
80+
return fc;
81+
case PROXY:
82+
return new FileChannelProxy(fc);
83+
default:
84+
throw new UnsupportedOperationException("Unexpected FileChannelType " + fileChannelType);
85+
}
8886
}
8987

90-
private void testContentEquals(final String content1, final String content2, final int bufferSize) throws IOException {
88+
private void testContentEquals(final String content1, final String content2, final int bufferSize, final FileChannelType fileChannelType)
89+
throws IOException {
9190
assertTrue(FileChannels.contentEquals(null, null, bufferSize));
9291
// Prepare test files with same size but different content
9392
// (first 3 bytes are different, followed by a large amount of equal content)
@@ -99,34 +98,37 @@ private void testContentEquals(final String content1, final String content2, fin
9998
assertNotEquals(FileUtils.checksumCRC32(file1), FileUtils.checksumCRC32(file2));
10099
try (FileInputStream in1 = new FileInputStream(file1);
101100
FileInputStream in2 = new FileInputStream(file2);
102-
FileChannel channel1 = in1.getChannel();
103-
FileChannel channel2 = in2.getChannel()) {
101+
FileChannel channel1 = getChannel(in1, fileChannelType);
102+
FileChannel channel2 = getChannel(in2, fileChannelType)) {
104103
assertFalse(FileChannels.contentEquals(channel1, channel2, bufferSize));
105104
}
106105
try (FileInputStream in1 = new FileInputStream(file1);
107106
FileInputStream in2 = new FileInputStream(file2);
108-
FileChannel channel1 = in1.getChannel();
109-
FileChannel channel2 = in2.getChannel()) {
107+
FileChannel channel1 = getChannel(in1, fileChannelType);
108+
FileChannel channel2 = getChannel(in2, fileChannelType)) {
110109
assertTrue(FileChannels.contentEquals(channel1, channel1, bufferSize));
111110
assertTrue(FileChannels.contentEquals(channel2, channel2, bufferSize));
112111
}
113112
}
114113

115-
@ParameterizedTest()
116-
@MethodSource("getBufferSizes")
117-
public void testContentEqualsDifferentPostfix(final int bufferSize) throws IOException {
118-
testContentEquals(CONTENT + "ABC", CONTENT + "XYZ", bufferSize);
114+
@CartesianTest()
115+
public void testContentEqualsDifferentPostfix(
116+
@Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10, IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int bufferSize,
117+
@CartesianTest.Enum final FileChannelType fileChannelType) throws IOException {
118+
testContentEquals(CONTENT + "ABC", CONTENT + "XYZ", bufferSize, fileChannelType);
119119
}
120120

121-
@ParameterizedTest()
122-
@MethodSource("getBufferSizes")
123-
public void testContentEqualsDifferentPrefix(final int bufferSize) throws IOException {
124-
testContentEquals("ABC" + CONTENT, "XYZ" + CONTENT, bufferSize);
121+
@CartesianTest()
122+
public void testContentEqualsDifferentPrefix(
123+
@Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10, IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int bufferSize,
124+
@CartesianTest.Enum final FileChannelType fileChannelType) throws IOException {
125+
testContentEquals("ABC" + CONTENT, "XYZ" + CONTENT, bufferSize, fileChannelType);
125126
}
126127

127-
@ParameterizedTest()
128-
@MethodSource("getBufferSizes")
129-
public void testContentEqualsEmpty(final int bufferSize) throws IOException {
128+
@CartesianTest()
129+
public void testContentEqualsEmpty(
130+
@Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10, IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int bufferSize,
131+
@CartesianTest.Enum final FileChannelType fileChannelType) throws IOException {
130132
assertTrue(FileChannels.contentEquals(null, null, bufferSize));
131133
// setup fixtures
132134
final File empty = new File(tempDirFile, "empty.txt");
@@ -139,8 +141,8 @@ public void testContentEqualsEmpty(final int bufferSize) throws IOException {
139141
assertNotEquals(FileUtils.checksumCRC32(empty), FileUtils.checksumCRC32(notEmpty));
140142
try (FileInputStream inEmpty = new FileInputStream(empty);
141143
FileInputStream inNotEmpty = new FileInputStream(notEmpty);
142-
FileChannel channelEmpty = inEmpty.getChannel();
143-
FileChannel channelNotEmpty = inNotEmpty.getChannel()) {
144+
FileChannel channelEmpty = getChannel(inEmpty, fileChannelType);
145+
FileChannel channelNotEmpty = getChannel(inNotEmpty, fileChannelType)) {
144146
assertFalse(FileChannels.contentEquals(channelEmpty, channelNotEmpty, bufferSize));
145147
assertFalse(FileChannels.contentEquals(null, channelNotEmpty, bufferSize));
146148
assertFalse(FileChannels.contentEquals(channelNotEmpty, null, bufferSize));
@@ -155,6 +157,7 @@ public void testContentEqualsEmpty(final int bufferSize) throws IOException {
155157
public void testContentEqualsFileChannel(
156158
@Values(ints = { 1, 2, IOUtils.DEFAULT_BUFFER_SIZE / 10, IOUtils.DEFAULT_BUFFER_SIZE, IOUtils.DEFAULT_BUFFER_SIZE * 10 }) final int bufferSize)
157159
throws IOException {
160+
final FileChannelType fileChannelType = FileChannelType.STOCK;
158161
final Path bigFile1 = Files.createTempFile(getClass().getSimpleName(), "-1.bin");
159162
final Path bigFile2 = Files.createTempFile(getClass().getSimpleName(), "-2.bin");
160163
final Path bigFile3 = Files.createTempFile(getClass().getSimpleName(), "-3.bin");
@@ -170,7 +173,7 @@ public void testContentEqualsFileChannel(
170173
bytes2[0] = 2;
171174
Files.write(bigFile1, bytes1);
172175
Files.write(bigFile2, bytes2);
173-
try (FileChannel fc1 = open(bigFile1, null); FileChannel fc2 = open(bigFile2, null)) {
176+
try (FileChannel fc1 = open(bigFile1, fileChannelType); FileChannel fc2 = open(bigFile2, fileChannelType)) {
174177
assertFalse(FileChannels.contentEquals(fc1, fc2, bufferSize));
175178
assertFalse(FileChannels.contentEquals(reset(fc2), reset(fc1), bufferSize));
176179
assertTrue(FileChannels.contentEquals(reset(fc1), reset(fc1), bufferSize));
@@ -180,7 +183,7 @@ public void testContentEqualsFileChannel(
180183
final int last = bytes3.length - 1;
181184
bytes3[last] = reverse(bytes3[last]);
182185
Files.write(bigFile3, bytes3);
183-
try (FileChannel fc1 = open(bigFile1, null); FileChannel fc3 = open(bigFile3, null)) {
186+
try (FileChannel fc1 = open(bigFile1, fileChannelType); FileChannel fc3 = open(bigFile3, fileChannelType)) {
184187
assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
185188
assertFalse(FileChannels.contentEquals(reset(fc3), reset(fc1), bufferSize));
186189
// Test just the last byte
@@ -191,7 +194,7 @@ public void testContentEqualsFileChannel(
191194
// Make the LAST byte equal.
192195
bytes3 = bytes1.clone();
193196
Files.write(bigFile3, bytes3);
194-
try (FileChannel fc1 = open(bigFile1, null); FileChannel fc3 = open(bigFile3, null)) {
197+
try (FileChannel fc1 = open(bigFile1, fileChannelType); FileChannel fc3 = open(bigFile3, fileChannelType)) {
195198
// Test just the last byte
196199
fc1.position(last);
197200
fc3.position(last);
@@ -202,7 +205,7 @@ public void testContentEqualsFileChannel(
202205
final int middle = bytes3.length / 2;
203206
bytes3[middle] = reverse(bytes3[middle]);
204207
Files.write(bigFile3, bytes3);
205-
try (FileChannel fc1 = open(bigFile1, null); FileChannel fc3 = open(bigFile3, null)) {
208+
try (FileChannel fc1 = open(bigFile1, fileChannelType); FileChannel fc3 = open(bigFile3, fileChannelType)) {
206209
assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
207210
assertFalse(FileChannels.contentEquals(reset(fc3), reset(fc1), bufferSize));
208211
}

0 commit comments

Comments
 (0)