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 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
43
44
45
46
47
48 class URIOriginTest extends AbstractOriginTest<URI, URIOrigin> {
49
50
51 private static final URIOpenOption URI_OPEN_OPTION = URIOpenOption.builder()
52 .setConnectTimeout(Duration.ofSeconds(60))
53 .setReadTimeout(Duration.ofSeconds(60))
54 .get();
55
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
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 }