diff --git a/gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/gcloud/storage/contrib/nio/CloudStorageFileSystem.java b/gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/gcloud/storage/contrib/nio/CloudStorageFileSystem.java index 5ae2ca6aee80..84dd0eb1cd42 100644 --- a/gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/gcloud/storage/contrib/nio/CloudStorageFileSystem.java +++ b/gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/gcloud/storage/contrib/nio/CloudStorageFileSystem.java @@ -103,7 +103,7 @@ private static CloudStorageFileSystemProvider getProvider() { if (provider != null) { return (CloudStorageFileSystemProvider) provider; } - logger.warning("Could not find CloudStorageFileSystemProvider via the SPI"); + logger.warning("Could not find CloudStorageFileSystemProvider via SPI"); return new CloudStorageFileSystemProvider(); } diff --git a/gcloud-java-contrib/gcloud-java-nio/src/test/java/com/google/gcloud/storage/contrib/nio/it/ITGcsNio.java b/gcloud-java-contrib/gcloud-java-nio/src/test/java/com/google/gcloud/storage/contrib/nio/it/ITGcsNio.java index 2b5206b121fd..34b762c5f2b4 100644 --- a/gcloud-java-contrib/gcloud-java-nio/src/test/java/com/google/gcloud/storage/contrib/nio/it/ITGcsNio.java +++ b/gcloud-java-contrib/gcloud-java-nio/src/test/java/com/google/gcloud/storage/contrib/nio/it/ITGcsNio.java @@ -8,7 +8,6 @@ import com.google.gcloud.storage.BucketInfo; import com.google.gcloud.storage.Storage; import com.google.gcloud.storage.StorageOptions; -import com.google.gcloud.storage.contrib.nio.CloudStorageConfiguration; import com.google.gcloud.storage.contrib.nio.CloudStorageFileSystem; import com.google.gcloud.storage.testing.RemoteGcsHelper; @@ -24,12 +23,14 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.net.URI; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.channels.SeekableByteChannel; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Arrays; import java.util.List; @@ -55,6 +56,7 @@ * to your browsers is your "Service Account JSON Key". */ @RunWith(JUnit4.class) +@SuppressWarnings("resource") public class ITGcsNio { private static final List FILE_CONTENTS = @@ -63,28 +65,28 @@ public class ITGcsNio { "Ils sont doués de raison et de conscience et doivent agir ", "les uns envers les autres dans un esprit de fraternité."); - private static final Logger log = Logger.getLogger(ITGcsNio.class.getName()); - private static final String BUCKET = RemoteGcsHelper.generateBucketName(); private static final String SML_FILE = "tmp-test-small-file.txt"; private static final int SML_SIZE = 100; - // it's big, relatively speaking. - private static final String BIG_FILE = "tmp-test-big-file.txt"; - // arbitrary size that's not too round. - private static final int BIG_SIZE = 2 * 1024 * 1024 - 50; + private static final String BIG_FILE = "tmp-test-big-file.txt"; // it's big, relatively speaking. + private static final int BIG_SIZE = 2 * 1024 * 1024 - 50; // arbitrary size that's not too round. private static final String PREFIX = "tmp-test-file"; + + private static final Logger logger = Logger.getLogger(ITGcsNio.class.getName()); + private static final Random random = new Random(); + + private static String bucket; private static Storage storage; private static StorageOptions storageOptions; - private final Random rnd = new Random(); - @BeforeClass - public static void beforeClass() throws IOException { + public static void beforeClass() { + bucket = RemoteGcsHelper.generateBucketName(); // loads the credentials from local disk as par README RemoteGcsHelper gcsHelper = RemoteGcsHelper.create(); storageOptions = gcsHelper.options(); storage = storageOptions.service(); // create and populate test bucket - storage.create(BucketInfo.of(BUCKET)); + storage.create(BucketInfo.of(bucket)); fillFile(storage, SML_FILE, SML_SIZE); fillFile(storage, BIG_FILE, BIG_SIZE); } @@ -92,9 +94,9 @@ public static void beforeClass() throws IOException { @AfterClass public static void afterClass() throws ExecutionException, InterruptedException { if (storage != null - && !RemoteGcsHelper.forceDelete(storage, BUCKET, 5, TimeUnit.SECONDS) - && log.isLoggable(Level.WARNING)) { - log.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", BUCKET); + && !RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS) + && logger.isLoggable(Level.WARNING)) { + logger.log(Level.WARNING, "Deletion of bucket {0} timed out, bucket is not empty", bucket); } } @@ -104,27 +106,33 @@ private static byte[] randomContents(int size) { return bytes; } - private static void fillFile(Storage storage, String fname, int size) throws IOException { - storage.create(BlobInfo.builder(BUCKET, fname).build(), randomContents(size)); + private static void fillFile(Storage storage, String fname, int size) { + storage.create(BlobInfo.builder(bucket, fname).build(), randomContents(size)); } @Test - public void testFileExists() throws IOException { - CloudStorageFileSystem testBucket = getTestBucket(); + public void testFileExists() { + CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket); Path path = testBucket.getPath(SML_FILE); assertThat(Files.exists(path)).isTrue(); } + @Test + public void testFileExistsUsingSpi() { + Path path = Paths.get(URI.create(String.format("gs://%s/%s", bucket, SML_FILE))); + assertThat(Files.exists(path)).isTrue(); + } + @Test public void testFileSize() throws IOException { - CloudStorageFileSystem testBucket = getTestBucket(); + CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket); Path path = testBucket.getPath(SML_FILE); assertThat(Files.size(path)).isEqualTo(SML_SIZE); } @Test(timeout = 60_000) public void testReadByteChannel() throws IOException { - CloudStorageFileSystem testBucket = getTestBucket(); + CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket); Path path = testBucket.getPath(SML_FILE); long size = Files.size(path); SeekableByteChannel chan = Files.newByteChannel(path, StandardOpenOption.READ); @@ -150,7 +158,7 @@ public void testReadByteChannel() throws IOException { @Test public void testSeek() throws IOException { - CloudStorageFileSystem testBucket = getTestBucket(); + CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket); Path path = testBucket.getPath(BIG_FILE); int size = BIG_SIZE; byte[] contents = randomContents(size); @@ -179,7 +187,7 @@ public void testSeek() throws IOException { @Test public void testCreate() throws IOException { - CloudStorageFileSystem testBucket = getTestBucket(); + CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket); Path path = testBucket.getPath(PREFIX + randomSuffix()); // file shouldn't exist initially. If it does it's either because it's a leftover // from a previous run (so we should delete the file) @@ -201,7 +209,7 @@ public void testCreate() throws IOException { @Test public void testWrite() throws IOException { - CloudStorageFileSystem testBucket = getTestBucket(); + CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket); Path path = testBucket.getPath(PREFIX + randomSuffix()); // file shouldn't exist initially. If it does it's either because it's a leftover // from a previous run (so we should delete the file) @@ -233,7 +241,7 @@ public void testWrite() throws IOException { @Test public void testCreateAndWrite() throws IOException { - CloudStorageFileSystem testBucket = getTestBucket(); + CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket); Path path = testBucket.getPath(PREFIX + randomSuffix()); // file shouldn't exist initially (see above). assertThat(Files.exists(path)).isFalse(); @@ -262,7 +270,7 @@ public void testCreateAndWrite() throws IOException { @Test public void testWriteOnClose() throws Exception { - CloudStorageFileSystem testBucket = getTestBucket(); + CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket); Path path = testBucket.getPath(PREFIX + randomSuffix()); // file shouldn't exist initially (see above) assertThat(Files.exists(path)).isFalse(); @@ -296,7 +304,7 @@ public void testWriteOnClose() throws Exception { @Test public void testCopy() throws IOException { - CloudStorageFileSystem testBucket = getTestBucket(); + CloudStorageFileSystem testBucket = CloudStorageFileSystem.forBucket(bucket); Path src = testBucket.getPath(SML_FILE); Path dst = testBucket.getPath(PREFIX + randomSuffix()); // file shouldn't exist initially (see above). @@ -330,19 +338,6 @@ private int readFully(ReadableByteChannel chan, byte[] outputBuf) throws IOExcep } private String randomSuffix() { - return "-" + rnd.nextInt(99999); - } - - private CloudStorageFileSystem getTestBucket() throws IOException { - // in typical usage we use the single-argument version of forBucket - // and rely on the user being logged into their project with the - // gcloud tool, and then everything authenticates automagically - // (or we just use paths that start with "gs://" and rely on NIO's magic). - // - // However for the tests we want to be able to run in automated environments - // where we can set environment variables but not necessarily install gcloud - // or run it. That's why we're setting the credentials programmatically. - return CloudStorageFileSystem.forBucket( - BUCKET, CloudStorageConfiguration.DEFAULT, storageOptions); + return "-" + random.nextInt(99999); } }