From 91bd4603ba8585ab075c3d6def7c2747f874fd5c Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 18 Apr 2016 11:28:09 +0200 Subject: [PATCH] Add options(namespace) method to LocalDatastoreHelper --- .../google/cloud/datastore/DatastoreOptions.java | 6 ++++++ .../datastore/testing/LocalDatastoreHelper.java | 14 ++++++++++++++ .../testing/LocalDatastoreHelperTest.java | 7 +++++++ 3 files changed, 27 insertions(+) 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..9e9613ccf725 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 @@ -569,6 +569,20 @@ public DatastoreOptions options() { .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 DatastoreOptions.builder() + .projectId(projectId) + .host("localhost:" + Integer.toString(port)) + .authCredentials(AuthCredentials.noAuth()) + .retryParams(RetryParams.noRetries()) + .namespace(namespace) + .build(); + } + /** * Returns the project ID associated with this local Datastore emulator. */ 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()); } }