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  
18  package org.apache.commons.io.build;
19  
20  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.io.FileInputStream;
27  import java.io.RandomAccessFile;
28  import java.net.URI;
29  import java.net.URISyntaxException;
30  import java.nio.channels.ReadableByteChannel;
31  import java.nio.channels.SeekableByteChannel;
32  import java.nio.file.Files;
33  import java.nio.file.Path;
34  import java.nio.file.Paths;
35  import java.util.Arrays;
36  import java.util.Objects;
37  import java.util.stream.Stream;
38  
39  import org.apache.commons.io.function.IOConsumer;
40  import org.apache.commons.lang3.ArrayUtils;
41  import org.junit.jupiter.api.Test;
42  import org.junit.jupiter.params.ParameterizedTest;
43  import org.junit.jupiter.params.provider.MethodSource;
44  
45  /**
46   * Tests {@link AbstractStreamBuilder}.
47   */
48  class AbstractStreamBuilderTest {
49  
50      public static class Builder extends AbstractStreamBuilder<char[], Builder> {
51  
52          @Override
53          public char[] get() {
54              final char[] arr = new char[getBufferSize()];
55              Arrays.fill(arr, 'a');
56              return arr;
57          }
58  
59      }
60  
61      private static Stream<IOConsumer<Builder>> fileBasedConfigurers() throws URISyntaxException {
62          final URI uri = Objects.requireNonNull(AbstractStreamBuilderTest.class.getResource(AbstractOriginTest.FILE_RES_RO)).toURI();
63          final Path path = Paths.get(AbstractOriginTest.FILE_NAME_RO);
64          // @formatter:off
65          return Stream.of(
66                  b -> b.setByteArray(ArrayUtils.EMPTY_BYTE_ARRAY),
67                  b -> b.setFile(AbstractOriginTest.FILE_NAME_RO),
68                  b -> b.setFile(path.toFile()),
69                  b -> b.setPath(AbstractOriginTest.FILE_NAME_RO),
70                  b -> b.setPath(path),
71                  b -> b.setRandomAccessFile(new RandomAccessFile(AbstractOriginTest.FILE_NAME_RO, "r")),
72                  // We can convert FileInputStream to ReadableByteChannel, but not the reverse.
73                  // Therefore, we don't use Files.newInputStream.
74                  b -> b.setInputStream(new FileInputStream(AbstractOriginTest.FILE_NAME_RO)),
75                  b -> b.setChannel(Files.newByteChannel(path)),
76                  b -> b.setURI(uri));
77          // @formatter:on
78      }
79  
80      private void assertResult(final char[] arr, final int size) {
81          assertNotNull(arr);
82          assertEquals(size, arr.length);
83          for (final char c : arr) {
84              assertEquals('a', c);
85          }
86      }
87  
88      protected Builder builder() {
89          return new Builder();
90      }
91  
92      /**
93       * Tests various ways to obtain a {@link SeekableByteChannel}.
94       *
95       * @param configurer Lambda to configure the builder.
96       */
97      @ParameterizedTest
98      @MethodSource("fileBasedConfigurers")
99      void getGetSeekableByteChannel(final IOConsumer<Builder> configurer) throws Exception {
100         final Builder builder = builder();
101         configurer.accept(builder);
102         try (ReadableByteChannel channel = assertDoesNotThrow(() -> builder.getChannel(SeekableByteChannel.class))) {
103             assertTrue(channel.isOpen());
104         }
105     }
106 
107     @Test
108     void testBufferSizeChecker() {
109         // sanity
110         final Builder builder = builder();
111         assertResult(builder.get(), builder.getBufferSize());
112         // basic failure
113         assertThrows(IllegalArgumentException.class, () -> builder().setBufferSizeMax(2).setBufferSize(3));
114         // reset
115         assertResult(builder.setBufferSizeMax(2).setBufferSizeMax(0).setBufferSize(3).get(), 3);
116         // resize
117         assertResult(builder().setBufferSizeMax(2).setBufferSizeChecker(i -> 100).setBufferSize(3).get(), 100);
118     }
119 
120     /**
121      * Tests various ways to obtain a {@link java.io.InputStream}.
122      *
123      * @param configurer Lambda to configure the builder.
124      */
125     @ParameterizedTest
126     @MethodSource("fileBasedConfigurers")
127     void testGetInputStream(final IOConsumer<Builder> configurer) throws Exception {
128         final Builder builder = builder();
129         configurer.accept(builder);
130         assertNotNull(builder.getInputStream());
131     }
132 }