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 org.junit.jupiter.api.Assertions.assertNotEquals;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.URI;
24  import java.nio.ByteBuffer;
25  import java.nio.channels.Channel;
26  import java.nio.channels.ReadableByteChannel;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.nio.file.Paths;
30  import java.nio.file.StandardOpenOption;
31  import java.time.Duration;
32  import java.util.function.Supplier;
33  
34  import org.apache.commons.io.build.AbstractOrigin.URIOrigin;
35  import org.apache.commons.io.build.AbstractOrigin.URIOrigin.URIOpenOption;
36  import org.apache.commons.lang3.ArrayUtils;
37  import org.junit.jupiter.api.Test;
38  import org.junit.jupiter.params.ParameterizedTest;
39  import org.junit.jupiter.params.provider.MethodSource;
40  
41  /**
42   * Tests {@link URIOrigin}.
43   *
44   * A URIOrigin can convert into all other aspects.
45   *
46   * @see URI
47   */
48  class URIOriginTest extends AbstractOriginTest<URI, URIOrigin> {
49  
50      // @formatter:off
51      private static final URIOpenOption URI_OPEN_OPTION = URIOpenOption.builder()
52              .setConnectTimeout(Duration.ofSeconds(60))
53              .setReadTimeout(Duration.ofSeconds(60))
54              .get();
55      // @formatter:on
56  
57      static String[] fixtures() {
58          return new String[] { "http://1.1.1.1", // IP
59                  "http://google.com", // HTTP
60                  "https://apache.org" // HTTPS
61          };
62      }
63  
64      private void checkRead(final Channel in, final Supplier<String> message) throws IOException {
65          if (in instanceof ReadableByteChannel) {
66              final ReadableByteChannel rbc = (ReadableByteChannel) in;
67              assertNotEquals(-1, rbc.read(ByteBuffer.allocate(1)), message);
68          }
69      }
70  
71      private void checkRead(final InputStream in) throws IOException {
72          assertNotEquals(-1, in.read());
73      }
74  
75      @Override
76      protected URIOrigin newOriginRo() {
77          return new URIOrigin(Paths.get(FILE_NAME_RO).toUri());
78      }
79  
80      @Override
81      protected URIOrigin newOriginRw() {
82          return new URIOrigin(tempPath.resolve(FILE_NAME_RW).toUri());
83      }
84  
85      @Override
86      protected void resetOriginRw() throws IOException {
87          // Reset the file
88          final Path rwPath = tempPath.resolve(FILE_NAME_RW);
89          Files.write(rwPath, ArrayUtils.EMPTY_BYTE_ARRAY, StandardOpenOption.CREATE);
90      }
91  
92      @Test
93      void testGetChannelFileURI() throws Exception {
94          final AbstractOrigin.URIOrigin origin = getOriginRo().asThis();
95          try (Channel in = origin.getChannel()) {
96              checkRead(in, origin::toString);
97          }
98      }
99  
100     @ParameterizedTest
101     @MethodSource("fixtures")
102     void testGetInputStrea(final String uri) throws Exception {
103         final AbstractOrigin.URIOrigin origin = new AbstractOrigin.URIOrigin(new URI(uri));
104         try (Channel in = origin.getChannel(URI_OPEN_OPTION)) {
105             checkRead(in, uri::toString);
106         }
107     }
108 
109     @ParameterizedTest
110     @MethodSource("fixtures")
111     void testGetInputStream(final String uri) throws Exception {
112         final AbstractOrigin.URIOrigin origin = new AbstractOrigin.URIOrigin(new URI(uri));
113         try (InputStream in = origin.getInputStream(URI_OPEN_OPTION)) {
114             checkRead(in);
115         }
116     }
117     @Test
118     void testGetInputStreamFileURI() throws Exception {
119         final AbstractOrigin.URIOrigin origin = getOriginRo().asThis();
120         try (InputStream in = origin.getInputStream()) {
121             checkRead(in);
122         }
123     }
124 }