4747import java .nio .channels .SeekableByteChannel ;
4848import java .nio .channels .WritableByteChannel ;
4949import java .nio .file .Path ;
50+ import java .util .List ;
5051import java .util .stream .Stream ;
5152
5253import org .apache .commons .io .FileUtils ;
54+ import org .apache .commons .lang3 .ClassUtils ;
5355import org .junit .jupiter .api .Test ;
5456import org .junit .jupiter .api .io .TempDir ;
5557import org .junit .jupiter .params .ParameterizedTest ;
6062 */
6163class CloseShieldChannelTest {
6264
63- static Stream <Class <? extends Channel >> testedInterfaces () {
65+ /**
66+ * JRE {@link Channel} interfaces.
67+ */
68+ static Stream <Class <? extends Channel >> channelInterfaces () {
6469 // @formatter:off
6570 return Stream .of (
6671 AsynchronousChannel .class ,
@@ -76,8 +81,15 @@ static Stream<Class<? extends Channel>> testedInterfaces() {
7681 // @formatter:on
7782 }
7883
84+ /**
85+ * Gets all interfaces implemented by the class {@link FileChannel}.
86+ */
87+ static List <Class <?>> fileChannelInterfaces () {
88+ return ClassUtils .getAllInterfaces (FileChannel .class );
89+ }
90+
7991 @ ParameterizedTest
80- @ MethodSource ("testedInterfaces " )
92+ @ MethodSource ("channelInterfaces " )
8193 void testCloseDoesNotCloseDelegate (final Class <? extends Channel > channelClass ) throws Exception {
8294 final Channel channel = mock (channelClass );
8395 final Channel shield = CloseShieldChannel .wrap (channel );
@@ -86,7 +98,7 @@ void testCloseDoesNotCloseDelegate(final Class<? extends Channel> channelClass)
8698 }
8799
88100 @ ParameterizedTest
89- @ MethodSource ("testedInterfaces " )
101+ @ MethodSource ("channelInterfaces " )
90102 void testCloseIsIdempotent (final Class <? extends Channel > channelClass ) throws Exception {
91103 final Channel channel = mock (channelClass );
92104 final Channel shield = CloseShieldChannel .wrap (channel );
@@ -98,9 +110,9 @@ void testCloseIsIdempotent(final Class<? extends Channel> channelClass) throws E
98110 }
99111
100112 @ ParameterizedTest
101- @ MethodSource ("testedInterfaces " )
102- void testCloseIsShielded (final Class <? extends Channel > channelClass ) throws Exception {
103- final Channel channel = mock (channelClass );
113+ @ MethodSource ("channelInterfaces " )
114+ void testCloseIsShielded (final Class <? extends Channel > channelInterface ) throws Exception {
115+ final Channel channel = mock (channelInterface );
104116 when (channel .isOpen ()).thenReturn (true , false , true , false );
105117 final Channel shield = CloseShieldChannel .wrap (channel );
106118 // Reflects delegate state initially
@@ -115,19 +127,12 @@ void testCloseIsShielded(final Class<? extends Channel> channelClass) throws Exc
115127 }
116128
117129 @ Test
118- void testCorrectlyDetectsInterfaces ( @ TempDir Path tempDir ) throws IOException {
130+ void testWrapFileChannel ( final @ TempDir Path tempDir ) throws IOException {
119131 final Path testFile = tempDir .resolve ("test.txt" );
120132 FileUtils .touch (testFile .toFile ());
121133 try (FileChannel channel = FileChannel .open (testFile ); Channel shield = CloseShieldChannel .wrap (channel )) {
122- assertInstanceOf (SeekableByteChannel .class , shield );
123- assertInstanceOf (GatheringByteChannel .class , shield );
124- assertInstanceOf (WritableByteChannel .class , shield );
125- assertInstanceOf (ScatteringByteChannel .class , shield );
126- assertInstanceOf (ReadableByteChannel .class , shield );
127- assertInstanceOf (InterruptibleChannel .class , shield );
128- assertInstanceOf (ByteChannel .class , shield );
129- assertInstanceOf (Channel .class , shield );
130- // These are not interfaces, so can not be implemented
134+ fileChannelInterfaces ().forEach (iface -> assertInstanceOf (iface , shield ));
135+ // FileChannel is not an interface, so can not be implemented.
131136 assertFalse (shield instanceof FileChannel , "not FileChannel" );
132137 }
133138 }
@@ -141,7 +146,7 @@ void testDoesNotDoubleWrap() {
141146 }
142147
143148 @ ParameterizedTest
144- @ MethodSource ("testedInterfaces " )
149+ @ MethodSource ("channelInterfaces " )
145150 void testEquals (final Class <? extends Channel > channelClass ) throws Exception {
146151 final Channel channel = mock (channelClass );
147152 final Channel shield = CloseShieldChannel .wrap (channel );
@@ -168,7 +173,7 @@ void testGatheringByteChannelMethods() throws Exception {
168173 }
169174
170175 @ ParameterizedTest
171- @ MethodSource ("testedInterfaces " )
176+ @ MethodSource ("channelInterfaces " )
172177 void testHashCode (final Class <? extends Channel > channelClass ) throws Exception {
173178 final Channel channel = mock (channelClass );
174179 final Channel shield = CloseShieldChannel .wrap (channel );
@@ -208,7 +213,7 @@ void testNetworkChannelMethods() throws Exception {
208213 }
209214
210215 @ ParameterizedTest
211- @ MethodSource ("testedInterfaces " )
216+ @ MethodSource ("channelInterfaces " )
212217 void testPreservesInterfaces (final Class <? extends Channel > channelClass ) {
213218 final Channel channel = mock (channelClass );
214219 final Channel shield = CloseShieldChannel .wrap (channel );
@@ -275,7 +280,7 @@ void testSeekableByteChannelMethods() throws Exception {
275280 }
276281
277282 @ ParameterizedTest
278- @ MethodSource ("testedInterfaces " )
283+ @ MethodSource ("channelInterfaces " )
279284 void testToString (final Class <? extends Channel > channelClass ) throws Exception {
280285 final Channel channel = mock (channelClass );
281286 when (channel .toString ()).thenReturn ("MyChannel" );
0 commit comments