From 5e0de55a546770228b07fe82029b7d822efe91b7 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 18 Apr 2016 18:40:21 +0200 Subject: [PATCH] Add options(namespace) method to LocalDatastoreHelper (#936) Add options(namespace) method to LocalDatastoreHelper --- .../cloud/datastore/DatastoreOptions.java | 6 +++++ .../testing/LocalDatastoreHelper.java | 23 ++++++++++++++----- .../testing/LocalDatastoreHelperTest.java | 7 ++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java b/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java index d456af51bde1..77e716474e41 100644 --- a/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java +++ b/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/DatastoreOptions.java @@ -75,6 +75,9 @@ public DatastoreOptions build() { return new DatastoreOptions(this); } + /** + * Sets the default namespace to be used by the datastore service. + */ public Builder namespace(String namespace) { this.namespace = validateNamespace(namespace); return this; @@ -112,6 +115,9 @@ protected DatastoreRpcFactory defaultRpcFactory() { return DefaultDatastoreRpcFactory.INSTANCE; } + /** + * Returns the default namespace to be used by the datastore service. + */ public String namespace() { return namespace; } diff --git a/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java b/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java index 4da7e0d27e75..906e3ccb300e 100644 --- a/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java +++ b/gcloud-java-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java @@ -556,17 +556,28 @@ private static int findAvailablePort() { } } + private DatastoreOptions.Builder optionsBuilder() { + return DatastoreOptions.builder() + .projectId(projectId) + .host("localhost:" + Integer.toString(port)) + .authCredentials(AuthCredentials.noAuth()) + .retryParams(RetryParams.noRetries()); + } + /** * Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on * localhost. */ public DatastoreOptions options() { - return DatastoreOptions.builder() - .projectId(projectId) - .host("localhost:" + Integer.toString(port)) - .authCredentials(AuthCredentials.noAuth()) - .retryParams(RetryParams.noRetries()) - .build(); + return optionsBuilder().build(); + } + + /** + * Returns a {@link DatastoreOptions} instance that sets the host to use the Datastore emulator on + * localhost. The default namespace is set to {@code namespace}. + */ + public DatastoreOptions options(String namespace) { + return optionsBuilder().namespace(namespace).build(); } /** diff --git a/gcloud-java-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java b/gcloud-java-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java index 73844d2e19b3..a5a49e472ae3 100644 --- a/gcloud-java-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/cloud/datastore/testing/LocalDatastoreHelperTest.java @@ -16,6 +16,7 @@ package com.google.cloud.datastore.testing; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; @@ -31,6 +32,7 @@ public class LocalDatastoreHelperTest { private static final double TOLERANCE = 0.00001; private static final String PROJECT_ID_PREFIX = "test-project-"; + private static final String NAMESPACE = "namespace"; @Test public void testCreate() { @@ -49,5 +51,10 @@ public void testOptions() { assertTrue(options.projectId().startsWith(PROJECT_ID_PREFIX)); assertTrue(options.host().startsWith("localhost:")); assertSame(AuthCredentials.noAuth(), options.authCredentials()); + options = helper.options(NAMESPACE); + assertTrue(options.projectId().startsWith(PROJECT_ID_PREFIX)); + assertTrue(options.host().startsWith("localhost:")); + assertSame(AuthCredentials.noAuth(), options.authCredentials()); + assertEquals(NAMESPACE, options.namespace()); } }