Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand All @@ -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());
}
}