aboutsummaryrefslogtreecommitdiff
path: root/jimfs/src/test/java
diff options
context:
space:
mode:
authorcgdecker <cgdecker@google.com>2015-02-18 13:58:51 -0800
committerColin Decker <cgdecker@google.com>2015-02-18 17:03:16 -0500
commitf2503678be1a49023c27a023058f8202b9deea74 (patch)
tree431a7b6ea0204280fc607d2f0c7f033588ecc00e /jimfs/src/test/java
parent8794b21e764fc9defbe028c98bb4547951d4f639 (diff)
downloadjimfs-f2503678be1a49023c27a023058f8202b9deea74.tar.gz
Add support for "jimfs:" protocol URLs.
For URLs to work, Java has to be able to find a URLStreamHandler implementation for the URL protocol/scheme. There isn't any really ideal way to do this programatically, but what this CL does is add "com.google.common" to the "java.protocol.handler.pkgs" system property when JimfsFileSystemProvider is initialized. This means that when Java tries to create a URL with the "jimfs" protocol, it'll look for "jimfs.Handler" under "com.google.common", find it and use it. (Documentation of that behavior found here: http://docs.oracle.com/javase/8/docs/api/java/net/URL.html#URL-java.lang.String-java.lang.String-int-java.lang.String-) Fixes Github issue #13. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=86627713
Diffstat (limited to 'jimfs/src/test/java')
-rw-r--r--jimfs/src/test/java/com/google/common/jimfs/UrlTest.java134
1 files changed, 134 insertions, 0 deletions
diff --git a/jimfs/src/test/java/com/google/common/jimfs/UrlTest.java b/jimfs/src/test/java/com/google/common/jimfs/UrlTest.java
new file mode 100644
index 0000000..9b7ce42
--- /dev/null
+++ b/jimfs/src/test/java/com/google/common/jimfs/UrlTest.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.common.jimfs;
+
+import static com.google.common.base.StandardSystemProperty.LINE_SEPARATOR;
+import static com.google.common.truth.Truth.assertThat;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Range;
+import com.google.common.io.Resources;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.file.FileSystem;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileTime;
+
+/**
+ * Tests that {@link URL} instances can be created and used from jimfs URIs.
+ *
+ * @author Colin Decker
+ */
+@RunWith(JUnit4.class)
+public class UrlTest {
+
+ private final FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
+ private Path path = fs.getPath("foo");
+
+ @Test
+ public void creatUrl() throws MalformedURLException {
+ URL url = path.toUri().toURL();
+ assertThat(url).isNotNull();
+ }
+
+ @Test
+ public void readFromUrl() throws IOException {
+ Files.write(path, ImmutableList.of("Hello World"), UTF_8);
+
+ URL url = path.toUri().toURL();
+ assertThat(Resources.asCharSource(url, UTF_8).read())
+ .isEqualTo("Hello World" + LINE_SEPARATOR.value());
+ }
+
+ @Test
+ public void readDirectoryContents() throws IOException {
+ Files.createDirectory(path);
+ Files.createFile(path.resolve("a.txt"));
+ Files.createFile(path.resolve("b.txt"));
+ Files.createDirectory(path.resolve("c"));
+
+ URL url = path.toUri().toURL();
+ assertThat(Resources.asCharSource(url, UTF_8).read())
+ .isEqualTo(""
+ + "a.txt\n"
+ + "b.txt\n"
+ + "c\n");
+ }
+
+ @Test
+ public void headers() throws IOException {
+ byte[] bytes = {1, 2, 3};
+ Files.write(path, bytes);
+ FileTime lastModified = Files.getLastModifiedTime(path);
+
+ URL url = path.toUri().toURL();
+ URLConnection conn = url.openConnection();
+
+ // read header fields directly
+ assertThat(conn.getHeaderFields()).containsEntry("content-length",
+ ImmutableList.of("3"));
+ assertThat(conn.getHeaderFields()).containsEntry("content-type",
+ ImmutableList.of("application/octet-stream"));
+
+ if (lastModified != null) {
+ assertThat(conn.getHeaderFields()).containsKey("last-modified");
+ assertThat(conn.getHeaderFields()).hasSize(3);
+ } else {
+ assertThat(conn.getHeaderFields()).hasSize(2);
+ }
+
+ // use the specific methods for reading the expected headers
+ assertThat(conn.getContentLengthLong()).isEqualTo(Files.size(path));
+ assertThat(conn.getContentType()).isEqualTo("application/octet-stream");
+
+ if (lastModified != null) {
+ // The HTTP date format does not include milliseconds, which means that the last modified time
+ // returned from the connection may not be exactly the same as that of the file system itself.
+ // The difference should less than 1000ms though, and should never be greater.
+ long difference = lastModified.toMillis() - conn.getLastModified();
+ assertThat(difference).isIn(Range.closedOpen(0L, 1000L));
+ } else {
+ assertThat(conn.getLastModified()).isEqualTo(0L);
+ }
+ }
+
+ @Test
+ public void contentType() throws IOException {
+ path = fs.getPath("foo.txt");
+ Files.write(path, ImmutableList.of("Hello World"), UTF_8);
+
+ URL url = path.toUri().toURL();
+ URLConnection conn = url.openConnection();
+
+ // Should be text/plain, but this is entirely dependent on the installed FileTypeDetectors
+ String detectedContentType = Files.probeContentType(path);
+ if (detectedContentType == null) {
+ assertThat(conn.getContentType()).isEqualTo("application/octet-stream");
+ } else {
+ assertThat(conn.getContentType()).isEqualTo(detectedContentType);
+ }
+ }
+}