|
26 | 26 | import org.apache.hadoop.fs.Path; |
27 | 27 | import org.apache.hadoop.io.IOUtils; |
28 | 28 | import org.junit.Test; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
29 | 30 |
|
30 | 31 | import java.io.IOException; |
31 | 32 | import java.io.InputStream; |
32 | 33 | import java.net.URI; |
33 | 34 | import java.net.URISyntaxException; |
34 | 35 | import java.net.URL; |
35 | 36 | import java.nio.charset.StandardCharsets; |
| 37 | +import java.util.stream.IntStream; |
36 | 38 |
|
37 | 39 | import static org.junit.Assert.assertEquals; |
38 | 40 |
|
39 | 41 | /** |
40 | 42 | * Testing HttpFileSystem. |
41 | 43 | */ |
42 | 44 | public class TestHttpFileSystem { |
| 45 | + private final Configuration conf = new Configuration(false); |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + public void setUp() { |
| 49 | + conf.set("fs.http.impl", HttpFileSystem.class.getCanonicalName()); |
| 50 | + } |
| 51 | + |
43 | 52 | @Test |
44 | 53 | public void testHttpFileSystem() throws IOException, URISyntaxException, |
45 | 54 | InterruptedException { |
46 | | - Configuration conf = new Configuration(false); |
47 | | - conf.set("fs.http.impl", HttpFileSystem.class.getCanonicalName()); |
48 | 55 | final String data = "foo"; |
49 | | - |
50 | 56 | try (MockWebServer server = new MockWebServer()) { |
51 | | - server.enqueue(new MockResponse().setBody(data)); |
| 57 | + IntStream.rangeClosed(1, 3).forEach(i -> server.enqueue(new MockResponse().setBody(data))); |
52 | 58 | server.start(); |
53 | 59 | URI uri = URI.create(String.format("http://%s:%d", server.getHostName(), |
54 | 60 | server.getPort())); |
55 | 61 | FileSystem fs = FileSystem.get(uri, conf); |
56 | | - try (InputStream is = fs.open( |
57 | | - new Path(new URL(uri.toURL(), "/foo").toURI()), |
58 | | - 4096)) { |
59 | | - byte[] buf = new byte[data.length()]; |
60 | | - IOUtils.readFully(is, buf, 0, buf.length); |
61 | | - assertEquals(data, new String(buf, StandardCharsets.UTF_8)); |
62 | | - } |
| 62 | + assertSameData(fs, new Path(new URL(uri.toURL(), "/foo").toURI()), data); |
| 63 | + assertSameData(fs, new Path("/foo"), data); |
| 64 | + assertSameData(fs, new Path("foo"), data); |
63 | 65 | RecordedRequest req = server.takeRequest(); |
64 | 66 | assertEquals("/foo", req.getPath()); |
65 | 67 | } |
66 | 68 | } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testHttpFileStatus() throws IOException, URISyntaxException, InterruptedException { |
| 72 | + URI uri = new URI("http://www.example.com"); |
| 73 | + FileSystem fs = FileSystem.get(uri, conf); |
| 74 | + URI expectedUri = uri.resolve("/foo"); |
| 75 | + assertEquals(fs.getFileStatus(new Path(new Path(uri), "/foo")).getPath().toUri(), expectedUri); |
| 76 | + assertEquals(fs.getFileStatus(new Path("/foo")).getPath().toUri(), expectedUri); |
| 77 | + assertEquals(fs.getFileStatus(new Path("foo")).getPath().toUri(), expectedUri); |
| 78 | + } |
| 79 | + |
| 80 | + private void assertSameData(FileSystem fs, Path path, String data) throws IOException { |
| 81 | + try (InputStream is = fs.open( |
| 82 | + path, |
| 83 | + 4096)) { |
| 84 | + byte[] buf = new byte[data.length()]; |
| 85 | + IOUtils.readFully(is, buf, 0, buf.length); |
| 86 | + assertEquals(data, new String(buf, StandardCharsets.UTF_8)); |
| 87 | + } |
| 88 | + } |
67 | 89 | } |
0 commit comments