diff --git a/TESTING.md b/TESTING.md index 8b4372e9b2ac..bb17d20cb25b 100644 --- a/TESTING.md +++ b/TESTING.md @@ -193,7 +193,22 @@ You can test against an in-memory local Resource Manager by following these step ### Testing code that uses Storage -Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteStorageHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below: +#### On your machine + +You can test against an in-memory local Storage by following these steps: + +1. Follow the [Quickstart instructions][cloud-nio] to add the nio dependency to your project. +2. In your program, create and use a fake Storage service object. For example: + + ```java + Storage storage = LocalStorageHelper.getOptions().getService(); + ``` + +3. Run your tests. + +#### Remote + +The alternative way of testing is to create a test project. `RemoteStorageHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below: 1. Create a test Google Cloud project. @@ -266,6 +281,8 @@ Here is an example that uses the `RemoteSpannerHelper` to create a database. 6. Clean up the test project by using `cleanUp` to clear any databases created. ```java RemoteSpannerHelper.cleanUp(); + ``` [cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts [create-service-account]:https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount +[cloud-nio]:https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-contrib/google-cloud-nio diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/FakeStorageRpc.java b/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java similarity index 99% rename from google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/FakeStorageRpc.java rename to google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java index d985c91adabf..a5d1a281a724 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/FakeStorageRpc.java +++ b/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/FakeStorageRpc.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.cloud.storage.contrib.nio; +package com.google.cloud.storage.contrib.nio.testing; import com.google.api.services.storage.model.Bucket; import com.google.api.services.storage.model.BucketAccessControl; diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/LocalStorageHelper.java b/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelper.java similarity index 94% rename from google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/LocalStorageHelper.java rename to google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelper.java index b9ee4489f9e0..293791f70860 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/LocalStorageHelper.java +++ b/google-cloud-contrib/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/testing/LocalStorageHelper.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.cloud.storage.contrib.nio; +package com.google.cloud.storage.contrib.nio.testing; import com.google.cloud.spi.ServiceRpcFactory; import com.google.cloud.storage.spi.v1.StorageRpc; @@ -24,11 +24,13 @@ * Utility to create an in-memory storage configuration for testing. Storage options can be * obtained via the {@link #getOptions()} method. Returned options will point to FakeStorageRpc. */ -class LocalStorageHelper { +public final class LocalStorageHelper { // used for testing. Will throw if you pass it an option. private static final FakeStorageRpc instance = new FakeStorageRpc(true); + private LocalStorageHelper() {} + /** * Returns a {@link StorageOptions} that use the static FakeStorageRpc instance, and resets it * first so you start from a clean slate. That instance will throw if you pass it any option. diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributeViewTest.java b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributeViewTest.java index 30bfe1244b82..f3f362c5b727 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributeViewTest.java +++ b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributeViewTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static java.nio.charset.StandardCharsets.UTF_8; +import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper; import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributesTest.java b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributesTest.java index 9a6ca7339151..5549d6fe7baa 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributesTest.java +++ b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileAttributesTest.java @@ -20,6 +20,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.google.cloud.storage.Acl; +import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper; import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java index 6411a96a0a2d..1f867450ce4e 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java +++ b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java @@ -27,6 +27,7 @@ import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; import static java.nio.file.StandardOpenOption.WRITE; +import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper; import com.google.common.collect.ImmutableList; import com.google.common.testing.NullPointerTester; diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java index 3a679eb025aa..9d9e864968ab 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java +++ b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemTest.java @@ -20,6 +20,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.google.cloud.storage.StorageOptions; +import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper; import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester; @@ -185,7 +186,7 @@ public void testMatcher() throws IOException { assertMatches(fs, javaFileMatcher, "a.text", false); assertMatches(fs, javaFileMatcher, "folder/c.java", true); assertMatches(fs, javaFileMatcher, "d", false); - + String pattern2 = "glob:*.{java,text}"; PathMatcher javaAndTextFileMatcher = fs.getPathMatcher(pattern2); assertMatches(fs, javaAndTextFileMatcher, "a.java", true); diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageOptionsTest.java b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageOptionsTest.java index d5dd2592c39c..146270484f86 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageOptionsTest.java +++ b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageOptionsTest.java @@ -20,6 +20,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.google.cloud.storage.Acl; +import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper; import com.google.common.testing.NullPointerTester; import org.junit.Before; diff --git a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStoragePathTest.java b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStoragePathTest.java index 6b4af923545f..8fd5ba626f17 100644 --- a/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStoragePathTest.java +++ b/google-cloud-contrib/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStoragePathTest.java @@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper; import com.google.common.collect.Iterables; import com.google.common.testing.EqualsTester; import com.google.common.testing.NullPointerTester;