1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.build;
18
19 import static java.nio.file.StandardOpenOption.READ;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.mockito.Mockito.mock;
22
23 import java.io.IOException;
24 import java.nio.channels.Channel;
25 import java.nio.channels.ReadableByteChannel;
26 import java.nio.channels.WritableByteChannel;
27 import java.nio.file.Files;
28 import java.nio.file.OpenOption;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.nio.file.StandardOpenOption;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.HashSet;
35
36 import org.apache.commons.io.build.AbstractOrigin.ChannelOrigin;
37 import org.apache.commons.lang3.ArrayUtils;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.params.ParameterizedTest;
40 import org.junit.jupiter.params.provider.EnumSource;
41
42 class ChannelOriginTest extends AbstractOriginTest<Channel, ChannelOrigin> {
43 @Override
44 protected ChannelOrigin newOriginRo() throws IOException {
45 return new ChannelOrigin(Files.newByteChannel(Paths.get(FILE_NAME_RO), Collections.singleton(READ)));
46 }
47
48 @Override
49 protected ChannelOrigin newOriginRw() throws IOException {
50 return new ChannelOrigin(Files.newByteChannel(
51 tempPath.resolve(FILE_NAME_RW),
52 new HashSet<>(Arrays.asList(StandardOpenOption.READ, StandardOpenOption.WRITE))));
53 }
54
55 @Override
56 protected void resetOriginRw() throws IOException {
57
58 final Path rwPath = tempPath.resolve(FILE_NAME_RW);
59 Files.write(rwPath, ArrayUtils.EMPTY_BYTE_ARRAY, StandardOpenOption.CREATE);
60 }
61
62 @Override
63 @Test
64 void testGetFile() {
65
66 assertThrows(UnsupportedOperationException.class, super::testGetFile);
67 }
68
69 @Override
70 @Test
71 void testGetPath() {
72
73 assertThrows(UnsupportedOperationException.class, super::testGetPath);
74 }
75
76 @Override
77 @Test
78 void testGetRandomAccessFile() {
79
80 assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
81 }
82
83 @Override
84 @ParameterizedTest
85 @EnumSource(StandardOpenOption.class)
86 void testGetRandomAccessFile(final OpenOption openOption) {
87
88 assertThrows(UnsupportedOperationException.class, () -> super.testGetRandomAccessFile(openOption));
89 }
90
91 @Test
92 void testUnsupportedOperations_ReadableByteChannel() {
93 final ReadableByteChannel channel = mock(ReadableByteChannel.class);
94 final ChannelOrigin origin = new ChannelOrigin(channel);
95 assertThrows(UnsupportedOperationException.class, origin::getOutputStream);
96 assertThrows(UnsupportedOperationException.class, () -> origin.getWriter(null));
97 assertThrows(UnsupportedOperationException.class, () -> origin.getChannel(WritableByteChannel.class));
98 }
99
100 @Test
101 void testUnsupportedOperations_WritableByteChannel() {
102 final Channel channel = mock(WritableByteChannel.class);
103 final ChannelOrigin origin = new ChannelOrigin(channel);
104 assertThrows(UnsupportedOperationException.class, origin::getInputStream);
105 assertThrows(UnsupportedOperationException.class, () -> origin.getReader(null));
106 assertThrows(UnsupportedOperationException.class, () -> origin.getChannel(ReadableByteChannel.class));
107 }
108 }