Skip to content

Commit e036335

Browse files
committed
use static constant for default factories, add example to Restorable and use mock instead of niceMock
1 parent 8f5dee2 commit e036335

10 files changed

Lines changed: 145 additions & 120 deletions

File tree

gcloud-java-core/src/main/java/com/google/gcloud/Restorable.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@
22

33
/**
44
* Implementation of this interface can persist their state and restore from it.
5+
*
6+
* <p>
7+
* A typical capture usage:
8+
* <pre> {@code
9+
* X restorableObj; // X instanceof Restorable<X>
10+
* RestorableState<X> state = restorableObj.capture();
11+
* .. persist state
12+
* }</pre>
13+
*
14+
* A typical restore usage:
15+
* <pre> {@code
16+
* RestorableState<X> state = ... // read from persistence
17+
* X restorableObj = state.restore();
18+
* ...
19+
* }</pre>
520
*/
621
public interface Restorable<T extends Restorable<T>> {
722

823
/**
9-
* Capture the state of this object.
24+
* Captures the state of this object.
1025
*
1126
* @return a {@link RestorableState} instance that contains the state for this object and can
1227
* restore it afterwards.

gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
/*
22
* Copyright 2015 Google Inc. All Rights Reserved.
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
76
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
98
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
1513
*/
1614

1715
package com.google.gcloud;
@@ -28,7 +26,15 @@
2826
import com.google.common.collect.Iterables;
2927
import com.google.gcloud.spi.ServiceRpcFactory;
3028

31-
import java.io.*;
29+
import java.io.BufferedReader;
30+
import java.io.File;
31+
import java.io.FileReader;
32+
import java.io.IOException;
33+
import java.io.InputStream;
34+
import java.io.InputStreamReader;
35+
import java.io.ObjectInputStream;
36+
import java.io.ObjectStreamException;
37+
import java.io.Serializable;
3238
import java.lang.reflect.Method;
3339
import java.net.HttpURLConnection;
3440
import java.net.URL;
@@ -70,8 +76,8 @@ public abstract class ServiceOptions<
7076
/**
7177
* A base interface for all {@link HttpTransport} factories.
7278
*
73-
* Implementation must provide a public no-arg constructor.
74-
* Loading of a factory implementation is done via {@link java.util.ServiceLoader}.
79+
* Implementation must provide a public no-arg constructor. Loading of a factory implementation is
80+
* done via {@link java.util.ServiceLoader}.
7581
*/
7682
public interface HttpTransportFactory {
7783
HttpTransport create();
@@ -264,7 +270,7 @@ public B serviceRpcFactory(ServiceRpcFactory<ServiceRpcT, OptionsT> serviceRpcFa
264270
* Sets the timeout in milliseconds to establish a connection.
265271
*
266272
* @param connectTimeout connection timeout in milliseconds. 0 for an infinite timeout, a
267-
* negative number for the default value (20000).
273+
* negative number for the default value (20000).
268274
* @return the builder.
269275
*/
270276
public B connectTimeout(int connectTimeout) {
@@ -275,8 +281,8 @@ public B connectTimeout(int connectTimeout) {
275281
/**
276282
* Sets the timeout in milliseconds to read data from an established connection.
277283
*
278-
* @param readTimeout read timeout in milliseconds. 0 for an infinite timeout, a
279-
* negative number for the default value (20000).
284+
* @param readTimeout read timeout in milliseconds. 0 for an infinite timeout, a negative number
285+
* for the default value (20000).
280286
* @return the builder.
281287
*/
282288
public B readTimeout(int readTimeout) {
@@ -362,10 +368,10 @@ protected static String googleCloudProjectId() {
362368
} catch (IOException ignore) {
363369
// ignore
364370
}
365-
File configDir;
371+
File configDir;
366372
if (System.getenv().containsKey("CLOUDSDK_CONFIG")) {
367373
configDir = new File(System.getenv("CLOUDSDK_CONFIG"));
368-
} else if (isWindows() && System.getenv().containsKey("APPDATA")) {
374+
} else if (isWindows() && System.getenv().containsKey("APPDATA")) {
369375
configDir = new File(System.getenv("APPDATA"), "gcloud");
370376
} else {
371377
configDir = new File(System.getProperty("user.home"), ".config/gcloud");
@@ -509,8 +515,8 @@ public int readTimeout() {
509515
}
510516

511517
/**
512-
* Returns the service's clock. Default time source uses {@link System#currentTimeMillis()} to
513-
* get current time.
518+
* Returns the service's clock. Default time source uses {@link System#currentTimeMillis()} to get
519+
* current time.
514520
*/
515521
public Clock clock() {
516522
return clock;

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreRpc, Da
4949

5050
public static class DefaultDatastoreFactory implements DatastoreFactory {
5151

52+
private static final DatastoreFactory INSTANCE = new DefaultDatastoreFactory();
53+
5254
@Override
5355
public Datastore create(DatastoreOptions options) {
5456
return new DatastoreImpl(options);
@@ -57,6 +59,8 @@ public Datastore create(DatastoreOptions options) {
5759

5860
public static class DefaultDatastoreRpcFactory implements DatastoreRpcFactory {
5961

62+
private static final DatastoreRpcFactory INSTANCE = new DefaultDatastoreRpcFactory();
63+
6064
@Override
6165
public DatastoreRpc create(DatastoreOptions options) {
6266
return new DefaultDatastoreRpc(options);
@@ -155,12 +159,12 @@ protected String defaultProject() {
155159

156160
@Override
157161
protected DatastoreFactory defaultServiceFactory() {
158-
return new DefaultDatastoreFactory();
162+
return DefaultDatastoreFactory.INSTANCE;
159163
}
160164

161165
@Override
162166
protected DatastoreRpcFactory defaultRpcFactory() {
163-
return new DefaultDatastoreRpcFactory();
167+
return DefaultDatastoreRpcFactory.INSTANCE;
164168
}
165169

166170
public String namespace() {

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobReadChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public interface BlobReadChannel extends ReadableByteChannel, Closeable,
5050
void chunkSize(int chunkSize);
5151

5252
/**
53-
* Capture the read channel state so that it can be saved and restored afterwards.
53+
* Captures the read channel state so that it can be saved and restored afterwards.
5454
*
5555
* @return a {@link RestorableState} object that contains the read channel state and can restore
5656
* it afterwards.

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public interface BlobWriteChannel extends WritableByteChannel, Closeable,
3939
void chunkSize(int chunkSize);
4040

4141
/**
42-
* Capture the write channel state so that it can be saved and restored afterwards. The original
42+
* Captures the write channel state so that it can be saved and restored afterwards. The original
4343
* {@code BlobWriteChannel} and the restored one should not both be used. Closing one channel
4444
* causes the other channel to close, subsequent writes will fail.
4545
*

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class StorageOptions extends ServiceOptions<Storage, StorageRpc, StorageO
3737

3838
public static class DefaultStorageFactory implements StorageFactory {
3939

40+
private static final StorageFactory INSTANCE = new DefaultStorageFactory();
41+
4042
@Override
4143
public Storage create(StorageOptions options) {
4244
return new StorageImpl(options);
@@ -45,6 +47,8 @@ public Storage create(StorageOptions options) {
4547

4648
public static class DefaultStorageRpcFactory implements StorageRpcFactory {
4749

50+
private static final StorageRpcFactory INSTANCE = new DefaultStorageRpcFactory();
51+
4852
@Override
4953
public StorageRpc create(StorageOptions options) {
5054
return new DefaultStorageRpc(options);
@@ -87,12 +91,12 @@ private StorageOptions(Builder builder) {
8791

8892
@Override
8993
protected StorageFactory defaultServiceFactory() {
90-
return new DefaultStorageFactory();
94+
return DefaultStorageFactory.INSTANCE;
9195
}
9296

9397
@Override
9498
protected StorageRpcFactory defaultRpcFactory() {
95-
return new DefaultStorageRpcFactory();
99+
return DefaultStorageRpcFactory.INSTANCE;
96100
}
97101

98102
@Override

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelImplTest.java

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
package com.google.gcloud.storage;
1818

19+
import static org.easymock.EasyMock.anyObject;
20+
import static org.easymock.EasyMock.createMock;
21+
import static org.easymock.EasyMock.expect;
22+
import static org.easymock.EasyMock.expectLastCall;
23+
import static org.easymock.EasyMock.replay;
1924
import static org.easymock.EasyMock.verify;
2025
import static org.junit.Assert.assertArrayEquals;
2126
import static org.junit.Assert.assertEquals;
@@ -27,7 +32,6 @@
2732
import com.google.gcloud.spi.StorageRpc;
2833
import com.google.gcloud.spi.StorageRpcFactory;
2934

30-
import org.easymock.EasyMock;
3135
import org.junit.After;
3236
import org.junit.Before;
3337
import org.junit.Test;
@@ -49,16 +53,16 @@ public class BlobReadChannelImplTest {
4953
private static final Random RANDOM = new Random();
5054

5155
private StorageOptions options;
56+
private StorageRpcFactory rpcFactoryMock;
5257
private StorageRpc storageRpcMock;
5358
private BlobReadChannelImpl reader;
5459

5560
@Before
5661
public void setUp() throws IOException, InterruptedException {
57-
StorageRpcFactory rpcFactoryMock = EasyMock.createNiceMock(StorageRpcFactory.class);
58-
storageRpcMock = EasyMock.createMock(StorageRpc.class);
59-
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(StorageOptions.class)))
60-
.andReturn(storageRpcMock);
61-
EasyMock.replay(rpcFactoryMock);
62+
rpcFactoryMock = createMock(StorageRpcFactory.class);
63+
storageRpcMock = createMock(StorageRpc.class);
64+
expect(rpcFactoryMock.create(anyObject(StorageOptions.class))).andReturn(storageRpcMock);
65+
replay(rpcFactoryMock);
6266
options = StorageOptions.builder()
6367
.projectId("projectId")
6468
.serviceRpcFactory(rpcFactoryMock)
@@ -67,12 +71,12 @@ public void setUp() throws IOException, InterruptedException {
6771

6872
@After
6973
public void tearDown() throws Exception {
70-
verify(storageRpcMock);
74+
verify(rpcFactoryMock, storageRpcMock);
7175
}
7276

7377
@Test
7478
public void testCreate() {
75-
EasyMock.replay(storageRpcMock);
79+
replay(storageRpcMock);
7680
reader = new BlobReadChannelImpl(options, BLOB_ID, EMPTY_RPC_OPTIONS);
7781
assertTrue(reader.isOpen());
7882
}
@@ -83,10 +87,9 @@ public void testReadBuffered() throws IOException {
8387
byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE);
8488
ByteBuffer firstReadBuffer = ByteBuffer.allocate(42);
8589
ByteBuffer secondReadBuffer = ByteBuffer.allocate(42);
86-
EasyMock
87-
.expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
90+
expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
8891
.andReturn(result);
89-
EasyMock.replay(storageRpcMock);
92+
replay(storageRpcMock);
9093
reader.read(firstReadBuffer);
9194
reader.read(secondReadBuffer);
9295
assertArrayEquals(Arrays.copyOf(result, firstReadBuffer.capacity()), firstReadBuffer.array());
@@ -104,15 +107,11 @@ public void testReadBig() throws IOException {
104107
byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE);
105108
ByteBuffer firstReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
106109
ByteBuffer secondReadBuffer = ByteBuffer.allocate(42);
107-
EasyMock
108-
.expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
109-
.andReturn(firstResult);
110-
EasyMock
111-
.expect(
112-
storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE,
113-
CUSTOM_CHUNK_SIZE))
114-
.andReturn(secondResult);
115-
EasyMock.replay(storageRpcMock);
110+
storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE);
111+
expectLastCall().andReturn(firstResult);
112+
storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE, CUSTOM_CHUNK_SIZE);
113+
expectLastCall().andReturn(secondResult);
114+
replay(storageRpcMock);
116115
reader.read(firstReadBuffer);
117116
reader.read(secondReadBuffer);
118117
assertArrayEquals(firstResult, firstReadBuffer.array());
@@ -125,10 +124,9 @@ public void testReadFinish() throws IOException {
125124
reader = new BlobReadChannelImpl(options, BLOB_ID, EMPTY_RPC_OPTIONS);
126125
byte[] result = {};
127126
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
128-
EasyMock
129-
.expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
127+
expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
130128
.andReturn(result);
131-
EasyMock.replay(storageRpcMock);
129+
replay(storageRpcMock);
132130
assertEquals(-1, reader.read(readBuffer));
133131
}
134132

@@ -138,17 +136,16 @@ public void testSeek() throws IOException {
138136
reader.seek(42);
139137
byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE);
140138
ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
141-
EasyMock
142-
.expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE))
139+
expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE))
143140
.andReturn(result);
144-
EasyMock.replay(storageRpcMock);
141+
replay(storageRpcMock);
145142
reader.read(readBuffer);
146143
assertArrayEquals(result, readBuffer.array());
147144
}
148145

149146
@Test
150147
public void testClose() throws IOException {
151-
EasyMock.replay(storageRpcMock);
148+
replay(storageRpcMock);
152149
reader = new BlobReadChannelImpl(options, BLOB_ID, EMPTY_RPC_OPTIONS);
153150
assertTrue(reader.isOpen());
154151
reader.close();
@@ -157,7 +154,7 @@ public void testClose() throws IOException {
157154

158155
@Test
159156
public void testReadClosed() {
160-
EasyMock.replay(storageRpcMock);
157+
replay(storageRpcMock);
161158
reader = new BlobReadChannelImpl(options, BLOB_ID, EMPTY_RPC_OPTIONS);
162159
reader.close();
163160
try {
@@ -175,13 +172,11 @@ public void testSaveAndRestore() throws IOException, ClassNotFoundException {
175172
byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE);
176173
ByteBuffer firstReadBuffer = ByteBuffer.allocate(42);
177174
ByteBuffer secondReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
178-
EasyMock
179-
.expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
175+
expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
180176
.andReturn(firstResult);
181-
EasyMock
182-
.expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE))
177+
expect(storageRpcMock.read(BLOB_ID.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE))
183178
.andReturn(secondResult);
184-
EasyMock.replay(storageRpcMock);
179+
replay(storageRpcMock);
185180
reader = new BlobReadChannelImpl(options, BLOB_ID, EMPTY_RPC_OPTIONS);
186181
reader.read(firstReadBuffer);
187182
RestorableState<BlobReadChannel> readerState = reader.capture();
@@ -194,7 +189,7 @@ public void testSaveAndRestore() throws IOException, ClassNotFoundException {
194189

195190
@Test
196191
public void testStateEquals() {
197-
EasyMock.replay(storageRpcMock);
192+
replay(storageRpcMock);
198193
reader = new BlobReadChannelImpl(options, BLOB_ID, EMPTY_RPC_OPTIONS);
199194
BlobReadChannel secondReader = new BlobReadChannelImpl(options, BLOB_ID, EMPTY_RPC_OPTIONS);
200195
RestorableState<BlobReadChannel> state = reader.capture();

0 commit comments

Comments
 (0)