View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // Reset the file
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          // A FileByteChannel cannot be converted into a File.
66          assertThrows(UnsupportedOperationException.class, super::testGetFile);
67      }
68  
69      @Override
70      @Test
71      void testGetPath() {
72          // A FileByteChannel cannot be converted into a Path.
73          assertThrows(UnsupportedOperationException.class, super::testGetPath);
74      }
75  
76      @Override
77      @Test
78      void testGetRandomAccessFile() {
79          // A FileByteChannel cannot be converted into a RandomAccessFile.
80          assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
81      }
82  
83      @Override
84      @ParameterizedTest
85      @EnumSource(StandardOpenOption.class)
86      void testGetRandomAccessFile(final OpenOption openOption) {
87          // A FileByteChannel cannot be converted into a RandomAccessFile.
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 }