Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ public final class CloseShieldChannel {
private static final Class<?>[] EMPTY = {};

private static Set<Class<?>> collectChannelInterfaces(final Class<?> type, final Set<Class<?>> out) {
Class<?> currentType = type;
// Visit interfaces
for (final Class<?> iface : type.getInterfaces()) {
if (Channel.class.isAssignableFrom(iface) && out.add(iface)) {
collectChannelInterfaces(iface, out);
while (currentType != null) {
for (final Class<?> iface : currentType.getInterfaces()) {
if (Channel.class.isAssignableFrom(iface) && out.add(iface)) {
collectChannelInterfaces(iface, out);
}
}
currentType = currentType.getSuperclass();
}
return out;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -32,11 +33,13 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.nio.channels.AsynchronousByteChannel;
import java.nio.channels.AsynchronousChannel;
import java.nio.channels.ByteChannel;
import java.nio.channels.Channel;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.InterruptibleChannel;
import java.nio.channels.MulticastChannel;
Expand All @@ -45,9 +48,12 @@
import java.nio.channels.ScatteringByteChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Path;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand Down Expand Up @@ -280,4 +286,22 @@ void testWritableByteChannelMethods() throws Exception {
assertThrows(ClosedChannelException.class, () -> shield.write(null));
verifyNoMoreInteractions(channel);
}

@Test
void testCorrectlyDetectsInterfaces(@TempDir Path tempDir) throws IOException {
final Path testFile = tempDir.resolve("test.txt");
FileUtils.touch(testFile.toFile());
try (FileChannel channel = FileChannel.open(testFile); Channel shield = CloseShieldChannel.wrap(channel)) {
assertInstanceOf(SeekableByteChannel.class, shield);
assertInstanceOf(GatheringByteChannel.class, shield);
assertInstanceOf(WritableByteChannel.class, shield);
assertInstanceOf(ScatteringByteChannel.class, shield);
assertInstanceOf(ReadableByteChannel.class, shield);
assertInstanceOf(InterruptibleChannel.class, shield);
assertInstanceOf(ByteChannel.class, shield);
assertInstanceOf(Channel.class, shield);
// These are not interfaces, so can not be implemented
assertFalse(shield instanceof FileChannel, "not FileChannel");
}
}
}