From 2d1e83693589a1b0658a50d044c8928430df90f0 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 11 Jan 2016 16:20:55 -0800 Subject: [PATCH 1/3] Set default retry strategy --- .../java/com/google/gcloud/bigquery/BigQueryImplTest.java | 1 + .../src/main/java/com/google/gcloud/ServiceOptions.java | 5 +++-- .../test/java/com/google/gcloud/datastore/DatastoreTest.java | 1 + .../main/java/com/google/gcloud/examples/StorageExample.java | 4 +--- .../gcloud/resourcemanager/ResourceManagerImplTest.java | 3 --- .../com/google/gcloud/storage/BlobReadChannelImplTest.java | 2 ++ .../com/google/gcloud/storage/BlobWriteChannelImplTest.java | 2 ++ .../test/java/com/google/gcloud/storage/CopyWriterTest.java | 2 ++ .../test/java/com/google/gcloud/storage/StorageImplTest.java | 1 + 9 files changed, 13 insertions(+), 8 deletions(-) diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java index 6a6a1c7cd6af..ed54e6a94111 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/BigQueryImplTest.java @@ -262,6 +262,7 @@ public void setUp() { options = BigQueryOptions.builder() .projectId(PROJECT) .serviceRpcFactory(rpcFactoryMock) + .retryParams(RetryParams.noRetries()) .build(); } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index 6ed1d50ba364..84bb07195806 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -264,7 +264,8 @@ public B authCredentials(AuthCredentials authCredentials) { /** * Sets configuration parameters for request retries. If no configuration is set - * {@link RetryParams#noRetries()} is used. + * {@link RetryParams#defaultInstance()} is used. To disable retries, supply + * {@link RetryParams#noRetries()} here. * * @return the builder */ @@ -325,7 +326,7 @@ protected ServiceOptions(Class> ser authCredentials = builder.authCredentials != null ? builder.authCredentials : defaultAuthCredentials(); authCredentialsState = authCredentials != null ? authCredentials.capture() : null; - retryParams = builder.retryParams; + retryParams = firstNonNull(builder.retryParams, RetryParams.defaultInstance()); serviceFactory = firstNonNull(builder.serviceFactory, getFromServiceLoader(serviceFactoryClass, defaultServiceFactory())); serviceFactoryClassName = serviceFactory.getClass().getName(); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java index 029b161c1c38..0e9b993483b0 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java @@ -125,6 +125,7 @@ public void setUp() { options = DatastoreOptions.builder() .projectId(PROJECT_ID) .host("http://localhost:" + PORT) + .retryParams(RetryParams.noRetries()) .build(); datastore = options.service(); StructuredQuery query = Query.keyQueryBuilder().build(); diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java index a6f65ec705e4..dc3dba6c72ab 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java @@ -18,7 +18,6 @@ import com.google.gcloud.AuthCredentials; import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials; -import com.google.gcloud.RetryParams; import com.google.gcloud.spi.StorageRpc.Tuple; import com.google.gcloud.storage.Blob; import com.google.gcloud.storage.BlobId; @@ -549,8 +548,7 @@ public static void main(String... args) throws Exception { printUsage(); return; } - StorageOptions.Builder optionsBuilder = - StorageOptions.builder().retryParams(RetryParams.defaultInstance()); + StorageOptions.Builder optionsBuilder = StorageOptions.builder(); StorageAction action; String actionName; if (args.length >= 2 && !ACTIONS.containsKey(args[0])) { diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java index 1210e4ec81a7..fedd10eacdc6 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceManagerImplTest.java @@ -25,7 +25,6 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.Page; -import com.google.gcloud.RetryParams; import com.google.gcloud.resourcemanager.ProjectInfo.ResourceId; import com.google.gcloud.resourcemanager.ResourceManager.ProjectField; import com.google.gcloud.resourcemanager.ResourceManager.ProjectGetOption; @@ -271,7 +270,6 @@ public void testRetryableException() { EasyMock.replay(rpcFactoryMock); ResourceManager resourceManagerMock = ResourceManagerOptions.builder() .serviceRpcFactory(rpcFactoryMock) - .retryParams(RetryParams.defaultInstance()) .build() .service(); EasyMock.expect(resourceManagerRpcMock.get(PARTIAL_PROJECT.projectId(), EMPTY_RPC_OPTIONS)) @@ -291,7 +289,6 @@ public void testNonRetryableException() { EasyMock.replay(rpcFactoryMock); ResourceManager resourceManagerMock = ResourceManagerOptions.builder() .serviceRpcFactory(rpcFactoryMock) - .retryParams(RetryParams.defaultInstance()) .build() .service(); EasyMock.expect(resourceManagerRpcMock.get(PARTIAL_PROJECT.projectId(), EMPTY_RPC_OPTIONS)) diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelImplTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelImplTest.java index d943715580c9..7daf4a6fb468 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelImplTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelImplTest.java @@ -28,6 +28,7 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.RestorableState; +import com.google.gcloud.RetryParams; import com.google.gcloud.spi.StorageRpc; import com.google.gcloud.spi.StorageRpcFactory; @@ -65,6 +66,7 @@ public void setUp() { options = StorageOptions.builder() .projectId("projectId") .serviceRpcFactory(rpcFactoryMock) + .retryParams(RetryParams.noRetries()) .build(); } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelImplTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelImplTest.java index 952be543a913..518ba8e14c65 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelImplTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelImplTest.java @@ -32,6 +32,7 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.RestorableState; +import com.google.gcloud.RetryParams; import com.google.gcloud.spi.StorageRpc; import com.google.gcloud.spi.StorageRpcFactory; @@ -74,6 +75,7 @@ public void setUp() { options = StorageOptions.builder() .projectId("projectid") .serviceRpcFactory(rpcFactoryMock) + .retryParams(RetryParams.noRetries()) .build(); } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java index f14ee934638f..1b1ffd987de6 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/CopyWriterTest.java @@ -26,6 +26,7 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.RestorableState; +import com.google.gcloud.RetryParams; import com.google.gcloud.spi.StorageRpc; import com.google.gcloud.spi.StorageRpc.RewriteRequest; import com.google.gcloud.spi.StorageRpc.RewriteResponse; @@ -72,6 +73,7 @@ public void setUp() { options = StorageOptions.builder() .projectId("projectid") .serviceRpcFactory(rpcFactoryMock) + .retryParams(RetryParams.noRetries()) .build(); } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java index 007ac4b2a15a..3aaec047714f 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java @@ -261,6 +261,7 @@ public void setUp() { .projectId("projectId") .clock(TIME_SOURCE) .serviceRpcFactory(rpcFactoryMock) + .retryParams(RetryParams.noRetries()) .build(); } From 3ddbcf1f80fc497a155f0950b10eb58199f78d9d Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 12 Jan 2016 15:16:57 -0800 Subject: [PATCH 2/3] ServiceOptions tests + fix for default retry params --- .../com/google/gcloud/ServiceOptions.java | 6 +- .../com/google/gcloud/ServiceOptionsTest.java | 234 ++++++++++++++++++ 2 files changed, 237 insertions(+), 3 deletions(-) create mode 100644 gcloud-java-core/src/test/java/com/google/gcloud/ServiceOptionsTest.java diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java index 84bb07195806..31e543809464 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java @@ -511,11 +511,11 @@ public AuthCredentials authCredentials() { } /** - * Returns configuration parameters for request retries. By default requests are not retried: - * {@link RetryParams#noRetries()} is used. + * Returns configuration parameters for request retries. By default requests are retried: + * {@link RetryParams#defaultInstance()} is used. */ public RetryParams retryParams() { - return retryParams != null ? retryParams : RetryParams.noRetries(); + return retryParams; } /** diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/ServiceOptionsTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/ServiceOptionsTest.java new file mode 100644 index 000000000000..9b118197a5db --- /dev/null +++ b/gcloud-java-core/src/test/java/com/google/gcloud/ServiceOptionsTest.java @@ -0,0 +1,234 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.gcloud.ServiceOptions.Clock; +import com.google.gcloud.ServiceOptions.DefaultHttpTransportFactory; +import com.google.gcloud.ServiceOptions.HttpTransportFactory; +import com.google.gcloud.spi.ServiceRpcFactory; + +import org.easymock.EasyMock; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Set; + +@RunWith(JUnit4.class) +public class ServiceOptionsTest { + private static final String JSON_KEY = + "{\n" + + " \"private_key_id\": \"somekeyid\",\n" + + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" + + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" + + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" + + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" + + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" + + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" + + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" + + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" + + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" + + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" + + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" + + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" + + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" + + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" + + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" + + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" + + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" + + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" + + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" + + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" + + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" + + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" + + " \"type\": \"service_account\"\n" + + "}"; + private static final InputStream JSON_KEY_STREAM = new ByteArrayInputStream(JSON_KEY.getBytes()); + private static AuthCredentials authCredentials; + static { + try { + authCredentials = AuthCredentials.createForJson(JSON_KEY_STREAM); + } catch (IOException e) { + fail("Couldn't create fake JSON credentials."); + } + } + private static final HttpTransportFactory MOCK_HTTP_TRANSPORT_FACTORY = + EasyMock.createMock(HttpTransportFactory.class); + private static final TestServiceOptions OPTIONS = + TestServiceOptions.builder() + .authCredentials(authCredentials) + .connectTimeout(1234) + .host("host") + .httpTransportFactory(MOCK_HTTP_TRANSPORT_FACTORY) + .projectId("project-id") + .readTimeout(5678) + .retryParams(RetryParams.noRetries()) + .build(); + private static final TestServiceOptions DEFAULT_OPTIONS = + TestServiceOptions.builder().projectId("project-id").build(); + private static final TestServiceOptions OPTIONS_COPY = OPTIONS.toBuilder().build(); + + interface TestService extends Service {} + + static class TestServiceImpl + extends BaseService implements TestService { + TestServiceImpl(TestServiceOptions options) { + super(options); + } + } + + interface TestServiceFactory extends ServiceFactory {} + + static class DefaultTestServiceFactory implements TestServiceFactory { + private static final TestServiceFactory INSTANCE = new DefaultTestServiceFactory(); + + @Override + public TestService create(TestServiceOptions options) { + return new TestServiceImpl(options); + } + } + + interface TestServiceRpcFactory + extends ServiceRpcFactory {} + + static class DefaultTestServiceRpcFactory implements TestServiceRpcFactory { + private static final TestServiceRpcFactory INSTANCE = new DefaultTestServiceRpcFactory(); + + @Override + public TestServiceRpc create(TestServiceOptions options) { + return new DefaultTestServiceRpc(options); + } + } + + interface TestServiceRpc {} + + static class DefaultTestServiceRpc implements TestServiceRpc { + DefaultTestServiceRpc(TestServiceOptions options) {} + } + + static class TestServiceOptions extends ServiceOptions { + static class Builder + extends ServiceOptions.Builder { + private Builder() {} + + private Builder(TestServiceOptions options) { + super(options); + } + + @Override + protected TestServiceOptions build() { + return new TestServiceOptions(this); + } + } + + protected TestServiceOptions(Builder builder) { + super(TestServiceFactory.class, TestServiceRpcFactory.class, builder); + } + + public static TestServiceOptions defaultInstance() { + return builder().build(); + } + + @Override + protected TestServiceFactory defaultServiceFactory() { + return DefaultTestServiceFactory.INSTANCE; + } + + @Override + protected TestServiceRpcFactory defaultRpcFactory() { + return DefaultTestServiceRpcFactory.INSTANCE; + } + + @Override + protected Set scopes() { + return null; + } + + @Override + public Builder toBuilder() { + return new Builder(this); + } + + public static Builder builder() { + return new Builder(); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof TestServiceOptions && baseEquals((TestServiceOptions) obj); + } + + @Override + public int hashCode() { + return baseHashCode(); + } + } + + @Test + public void testBuilder() { + assertEquals(authCredentials, OPTIONS.authCredentials()); + assertEquals(Clock.defaultClock(), OPTIONS.clock()); + assertEquals(1234, OPTIONS.connectTimeout()); + assertEquals("host", OPTIONS.host()); + assertEquals(MOCK_HTTP_TRANSPORT_FACTORY, OPTIONS.httpTransportFactory()); + assertEquals("project-id", OPTIONS.projectId()); + assertEquals(5678, OPTIONS.readTimeout()); + assertEquals(RetryParams.noRetries(), OPTIONS.retryParams()); + + assertEquals(Clock.defaultClock(), DEFAULT_OPTIONS.clock()); + assertEquals(-1, DEFAULT_OPTIONS.connectTimeout()); + assertEquals("https://www.googleapis.com", DEFAULT_OPTIONS.host()); + assertTrue(DEFAULT_OPTIONS.httpTransportFactory() instanceof DefaultHttpTransportFactory); + assertEquals(-1, DEFAULT_OPTIONS.readTimeout()); + assertEquals(RetryParams.defaultInstance(), DEFAULT_OPTIONS.retryParams()); + } + + @Test + public void testGetProjectIdRequired() { + assertTrue(OPTIONS.projectIdRequired()); + } + + @Test + public void testService() { + assertTrue(OPTIONS.service() instanceof TestServiceImpl); + } + + @Test + public void testRpc() { + assertTrue(OPTIONS.rpc() instanceof DefaultTestServiceRpc); + } + + @Test + public void testBaseEquals() { + assertEquals(OPTIONS, OPTIONS_COPY); + assertNotEquals(DEFAULT_OPTIONS, OPTIONS); + } + + @Test + public void testBaseHashCode() { + assertEquals(OPTIONS.hashCode(), OPTIONS_COPY.hashCode()); + assertNotEquals(DEFAULT_OPTIONS.hashCode(), OPTIONS.hashCode()); + } +} From b0d4c57e2b44b97b79c0dc50f54655849199d8e7 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 12 Jan 2016 15:46:53 -0800 Subject: [PATCH 3/3] Fix ServiceOptions tests --- .../com/google/gcloud/ServiceOptionsTest.java | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/ServiceOptionsTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/ServiceOptionsTest.java index 9b118197a5db..d0e3db2d2a55 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/ServiceOptionsTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/ServiceOptionsTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -76,9 +77,11 @@ public class ServiceOptionsTest { } private static final HttpTransportFactory MOCK_HTTP_TRANSPORT_FACTORY = EasyMock.createMock(HttpTransportFactory.class); + private static final Clock TEST_CLOCK = new TestClock(); private static final TestServiceOptions OPTIONS = TestServiceOptions.builder() .authCredentials(authCredentials) + .clock(TEST_CLOCK) .connectTimeout(1234) .host("host") .httpTransportFactory(MOCK_HTTP_TRANSPORT_FACTORY) @@ -90,18 +93,25 @@ public class ServiceOptionsTest { TestServiceOptions.builder().projectId("project-id").build(); private static final TestServiceOptions OPTIONS_COPY = OPTIONS.toBuilder().build(); - interface TestService extends Service {} + private static class TestClock extends Clock { + @Override + public long millis() { + return 123456789L; + } + } - static class TestServiceImpl + private interface TestService extends Service {} + + private static class TestServiceImpl extends BaseService implements TestService { - TestServiceImpl(TestServiceOptions options) { + private TestServiceImpl(TestServiceOptions options) { super(options); } } - interface TestServiceFactory extends ServiceFactory {} + private interface TestServiceFactory extends ServiceFactory {} - static class DefaultTestServiceFactory implements TestServiceFactory { + private static class DefaultTestServiceFactory implements TestServiceFactory { private static final TestServiceFactory INSTANCE = new DefaultTestServiceFactory(); @Override @@ -110,10 +120,10 @@ public TestService create(TestServiceOptions options) { } } - interface TestServiceRpcFactory + private interface TestServiceRpcFactory extends ServiceRpcFactory {} - static class DefaultTestServiceRpcFactory implements TestServiceRpcFactory { + private static class DefaultTestServiceRpcFactory implements TestServiceRpcFactory { private static final TestServiceRpcFactory INSTANCE = new DefaultTestServiceRpcFactory(); @Override @@ -122,14 +132,15 @@ public TestServiceRpc create(TestServiceOptions options) { } } - interface TestServiceRpc {} + private interface TestServiceRpc {} - static class DefaultTestServiceRpc implements TestServiceRpc { + private static class DefaultTestServiceRpc implements TestServiceRpc { DefaultTestServiceRpc(TestServiceOptions options) {} } - static class TestServiceOptions extends ServiceOptions { - static class Builder + private static class TestServiceOptions + extends ServiceOptions { + private static class Builder extends ServiceOptions.Builder { private Builder() {} @@ -143,14 +154,10 @@ protected TestServiceOptions build() { } } - protected TestServiceOptions(Builder builder) { + private TestServiceOptions(Builder builder) { super(TestServiceFactory.class, TestServiceRpcFactory.class, builder); } - public static TestServiceOptions defaultInstance() { - return builder().build(); - } - @Override protected TestServiceFactory defaultServiceFactory() { return DefaultTestServiceFactory.INSTANCE; @@ -171,7 +178,7 @@ public Builder toBuilder() { return new Builder(this); } - public static Builder builder() { + private static Builder builder() { return new Builder(); } @@ -188,21 +195,21 @@ public int hashCode() { @Test public void testBuilder() { - assertEquals(authCredentials, OPTIONS.authCredentials()); - assertEquals(Clock.defaultClock(), OPTIONS.clock()); + assertSame(authCredentials, OPTIONS.authCredentials()); + assertSame(TEST_CLOCK, OPTIONS.clock()); assertEquals(1234, OPTIONS.connectTimeout()); assertEquals("host", OPTIONS.host()); - assertEquals(MOCK_HTTP_TRANSPORT_FACTORY, OPTIONS.httpTransportFactory()); + assertSame(MOCK_HTTP_TRANSPORT_FACTORY, OPTIONS.httpTransportFactory()); assertEquals("project-id", OPTIONS.projectId()); assertEquals(5678, OPTIONS.readTimeout()); - assertEquals(RetryParams.noRetries(), OPTIONS.retryParams()); + assertSame(RetryParams.noRetries(), OPTIONS.retryParams()); - assertEquals(Clock.defaultClock(), DEFAULT_OPTIONS.clock()); + assertSame(Clock.defaultClock(), DEFAULT_OPTIONS.clock()); assertEquals(-1, DEFAULT_OPTIONS.connectTimeout()); assertEquals("https://www.googleapis.com", DEFAULT_OPTIONS.host()); assertTrue(DEFAULT_OPTIONS.httpTransportFactory() instanceof DefaultHttpTransportFactory); assertEquals(-1, DEFAULT_OPTIONS.readTimeout()); - assertEquals(RetryParams.defaultInstance(), DEFAULT_OPTIONS.retryParams()); + assertSame(RetryParams.defaultInstance(), DEFAULT_OPTIONS.retryParams()); } @Test