From 575fa1f50ee34aa68d277cd4b09a054982b871cc Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 29 Sep 2015 12:18:58 +0200 Subject: [PATCH 1/3] Add test for blob channels, remove duplicated call in BlobWriterChannelImpl --- .../gcloud/storage/BlobWriterChannelImpl.java | 2 +- .../gcloud/storage/BlobReadChannelTest.java | 194 ++++++++++++++++++ .../gcloud/storage/BlobWriterChannelTest.java | 166 +++++++++++++++ 3 files changed, 361 insertions(+), 1 deletion(-) create mode 100644 gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java create mode 100644 gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java index 1eefe87702e6..612e5d7551dc 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java @@ -55,7 +55,7 @@ class BlobWriterChannelImpl implements BlobWriteChannel { this.options = options; this.blobInfo = blobInfo; initTransients(); - uploadId = options.storageRpc().open(storageObject, optionsMap); + uploadId = storageRpc.open(storageObject, optionsMap); } private void writeObject(ObjectOutputStream out) throws IOException { diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java new file mode 100644 index 000000000000..f54c02c6eb83 --- /dev/null +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java @@ -0,0 +1,194 @@ +/* + * 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.storage; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import com.google.common.collect.ImmutableMap; +import com.google.gcloud.RetryParams; +import com.google.gcloud.spi.StorageRpc; + +import org.easymock.EasyMock; +import org.junit.Test; +import org.junit.Before; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Map; +import java.util.Random; + +public class BlobReadChannelTest { + + private static final String BUCKET_NAME = "b"; + private static final String BLOB_NAME = "n"; + private static final BlobInfo BLOB_INFO = BlobInfo.of(BUCKET_NAME, BLOB_NAME); + private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); + private static final int DEFAULT_CHUNK_SIZE = 2 * 1024 * 1024; + private static final int CUSTOM_CHUNK_SIZE = 2 * 1024 * 1024; + private static final Random RANDOM = new Random(); + + private StorageOptions optionsMock; + private StorageRpc storageRpcMock; + private BlobReadChannelImpl reader; + + @Before + public void setUp() throws IOException, InterruptedException { + optionsMock = EasyMock.createMock(StorageOptions.class); + storageRpcMock = EasyMock.createMock(StorageRpc.class); + } + + @Test + public void testCreate() { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.replay(optionsMock); + reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + assertTrue(reader.isOpen()); + } + + @Test + public void testReadSmall() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); + EasyMock.replay(optionsMock); + reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE); + ByteBuffer readBuffer = ByteBuffer.allocate(42); + EasyMock + .expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) + .andReturn(result); + EasyMock.replay(storageRpcMock); + reader.read(readBuffer); + assertArrayEquals(Arrays.copyOf(result, readBuffer.capacity()), readBuffer.array()); + } + + @Test + public void testReadBuffered() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); + EasyMock.replay(optionsMock); + reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE); + ByteBuffer firstReadBuffer = ByteBuffer.allocate(42); + ByteBuffer secondReadBuffer = ByteBuffer.allocate(42); + EasyMock + .expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) + .andReturn(result); + EasyMock.replay(storageRpcMock); + reader.read(firstReadBuffer); + reader.read(secondReadBuffer); + assertArrayEquals(Arrays.copyOf(result, firstReadBuffer.capacity()), firstReadBuffer.array()); + assertArrayEquals( + Arrays.copyOfRange(result, firstReadBuffer.capacity(), firstReadBuffer.capacity() + + secondReadBuffer.capacity()), + secondReadBuffer.array()); + } + + @Test + public void testReadBig() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()).times(2); + EasyMock.replay(optionsMock); + reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + reader.chunkSize(CUSTOM_CHUNK_SIZE); + byte[] firstResult = randomByteArray(DEFAULT_CHUNK_SIZE); + byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE); + ByteBuffer firstReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); + ByteBuffer secondReadBuffer = ByteBuffer.allocate(42); + EasyMock + .expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) + .andReturn(firstResult); + EasyMock + .expect( + storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE, + CUSTOM_CHUNK_SIZE)) + .andReturn(secondResult); + EasyMock.expectLastCall(); + EasyMock.replay(storageRpcMock); + reader.read(firstReadBuffer); + reader.read(secondReadBuffer); + assertArrayEquals(firstResult, firstReadBuffer.array()); + assertArrayEquals(Arrays.copyOf(secondResult, secondReadBuffer.capacity()), + secondReadBuffer.array()); + } + + @Test + public void testReadFinish() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); + EasyMock.replay(optionsMock); + reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + byte[] result = {}; + ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); + EasyMock + .expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) + .andReturn(result); + EasyMock.replay(storageRpcMock); + assertEquals(-1, reader.read(readBuffer)); + } + + @Test + public void testSeek() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); + EasyMock.replay(optionsMock); + reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + reader.seek(42); + byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE); + ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); + EasyMock + .expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 42, DEFAULT_CHUNK_SIZE)) + .andReturn(result); + EasyMock.replay(storageRpcMock); + reader.read(readBuffer); + assertArrayEquals(result, readBuffer.array()); + } + + @Test + public void testClose() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.replay(optionsMock); + reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + assertTrue(reader.isOpen()); + reader.close(); + assertTrue(!reader.isOpen()); + } + + @Test + public void testReadClosed() { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.replay(optionsMock); + reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + reader.close(); + try { + ByteBuffer readBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE); + reader.read(readBuffer); + fail("Expected BlobReadChannel read to throw IOException"); + } catch (IOException ex) { + // expected + } + } + + private static byte[] randomByteArray(int size) { + byte[] byteArray = new byte[size]; + RANDOM.nextBytes(byteArray); + return byteArray; + } +} diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java new file mode 100644 index 000000000000..65997bf86228 --- /dev/null +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java @@ -0,0 +1,166 @@ +/* + * 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.storage; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import com.google.common.collect.ImmutableMap; +import com.google.gcloud.RetryParams; +import com.google.gcloud.spi.StorageRpc; + +import org.easymock.Capture; +import org.easymock.EasyMock; +import org.junit.Test; +import org.junit.Before; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Map; +import java.util.Random; + +public class BlobWriterChannelTest { + + private static final String BUCKET_NAME = "b"; + private static final String BLOB_NAME = "n"; + private static final String UPLOAD_ID = "uploadid"; + private static final BlobInfo BLOB_INFO = BlobInfo.of(BUCKET_NAME, BLOB_NAME); + private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); + private static final int MIN_CHUNK_SIZE = 256 * 1024; + private static final int DEFAULT_CHUNK_SIZE = 8 * MIN_CHUNK_SIZE; + private static final int CUSTOM_CHUNK_SIZE = 4 * MIN_CHUNK_SIZE; + private static final Random RANDOM = new Random(); + + private StorageOptions optionsMock; + private StorageRpc storageRpcMock; + private BlobWriterChannelImpl writer; + + @Before + public void setUp() throws IOException, InterruptedException { + optionsMock = EasyMock.createMock(StorageOptions.class); + storageRpcMock = EasyMock.createMock(StorageRpc.class); + } + + @Test + public void testCreate() { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.replay(optionsMock); + EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); + EasyMock.replay(storageRpcMock); + writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + assertTrue(writer.isOpen()); + } + + @Test + public void testWriteWithoutFlush() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.replay(optionsMock); + EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); + EasyMock.replay(storageRpcMock); + writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + assertEquals(MIN_CHUNK_SIZE, writer.write(ByteBuffer.allocate(MIN_CHUNK_SIZE))); + } + + @Test + public void testWriteWithFlush() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); + EasyMock.replay(optionsMock); + EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); + Capture capturedBuffer = Capture.newInstance(); + storageRpcMock.write(EasyMock.eq(UPLOAD_ID), EasyMock.capture(capturedBuffer), EasyMock.eq(0), + EasyMock.eq(BLOB_INFO.toPb()), EasyMock.eq(0L), EasyMock.eq(CUSTOM_CHUNK_SIZE), + EasyMock.eq(false)); + EasyMock.expectLastCall(); + EasyMock.replay(storageRpcMock); + writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + writer.chunkSize(CUSTOM_CHUNK_SIZE); + ByteBuffer buffer = randomBuffer(CUSTOM_CHUNK_SIZE); + assertEquals(CUSTOM_CHUNK_SIZE, writer.write(buffer)); + assertArrayEquals(buffer.array(), capturedBuffer.getValue()); + } + + @Test + public void testCloseWithoutFlush() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); + EasyMock.replay(optionsMock); + EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); + Capture capturedBuffer = Capture.newInstance(); + storageRpcMock.write(EasyMock.eq(UPLOAD_ID), EasyMock.capture(capturedBuffer), EasyMock.eq(0), + EasyMock.eq(BLOB_INFO.toPb()), EasyMock.eq(0L), EasyMock.eq(0), EasyMock.eq(true)); + EasyMock.expectLastCall(); + EasyMock.replay(storageRpcMock); + writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + assertTrue(writer.isOpen()); + writer.close(); + assertArrayEquals(new byte[0], capturedBuffer.getValue()); + assertTrue(!writer.isOpen()); + } + + @Test + public void testCloseWithFlush() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); + EasyMock.replay(optionsMock); + EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); + Capture capturedBuffer = Capture.newInstance(); + ByteBuffer buffer = randomBuffer(MIN_CHUNK_SIZE); + storageRpcMock.write(EasyMock.eq(UPLOAD_ID), EasyMock.capture(capturedBuffer), EasyMock.eq(0), + EasyMock.eq(BLOB_INFO.toPb()), EasyMock.eq(0L), EasyMock.eq(MIN_CHUNK_SIZE), + EasyMock.eq(true)); + EasyMock.expectLastCall(); + EasyMock.replay(storageRpcMock); + writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + assertTrue(writer.isOpen()); + writer.write(buffer); + writer.close(); + assertEquals(DEFAULT_CHUNK_SIZE, capturedBuffer.getValue().length); + assertArrayEquals(buffer.array(), Arrays.copyOf(capturedBuffer.getValue(), MIN_CHUNK_SIZE)); + assertTrue(!writer.isOpen()); + } + + @Test + public void testWriteClosed() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); + EasyMock.replay(optionsMock); + EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); + Capture capturedBuffer = Capture.newInstance(); + storageRpcMock.write(EasyMock.eq(UPLOAD_ID), EasyMock.capture(capturedBuffer), EasyMock.eq(0), + EasyMock.eq(BLOB_INFO.toPb()), EasyMock.eq(0L), EasyMock.eq(0), EasyMock.eq(true)); + EasyMock.expectLastCall(); + EasyMock.replay(storageRpcMock); + writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + writer.close(); + try { + writer.write(ByteBuffer.allocate(MIN_CHUNK_SIZE)); + fail("Expected BlobWriteChannel write to throw IOException"); + } catch (IOException ex) { + // expected + } + } + + private static ByteBuffer randomBuffer(int size) { + byte[] byteArray = new byte[size]; + RANDOM.nextBytes(byteArray); + return ByteBuffer.wrap(byteArray); + } +} From 967c4292b8320ed5d85700b080d7795a6b0370cd Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 29 Sep 2015 22:36:56 +0200 Subject: [PATCH 2/3] Remove redundant read test, add buffered write test, add EasyMock.verify --- .../gcloud/storage/BlobReadChannelTest.java | 28 ++++++--------- .../gcloud/storage/BlobWriterChannelTest.java | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java index f54c02c6eb83..dd2426416d19 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java @@ -34,6 +34,8 @@ import java.util.Arrays; import java.util.Map; import java.util.Random; +import static org.easymock.EasyMock.verify; +import org.junit.After; public class BlobReadChannelTest { @@ -55,28 +57,19 @@ public void setUp() throws IOException, InterruptedException { storageRpcMock = EasyMock.createMock(StorageRpc.class); } - @Test - public void testCreate() { - EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); - EasyMock.replay(optionsMock); - reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); - assertTrue(reader.isOpen()); + @After + public void tearDown() throws Exception { + verify(optionsMock); + verify(storageRpcMock); } @Test - public void testReadSmall() throws IOException { + public void testCreate() { EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); - EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); EasyMock.replay(optionsMock); - reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); - byte[] result = randomByteArray(DEFAULT_CHUNK_SIZE); - ByteBuffer readBuffer = ByteBuffer.allocate(42); - EasyMock - .expect(storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) - .andReturn(result); EasyMock.replay(storageRpcMock); - reader.read(readBuffer); - assertArrayEquals(Arrays.copyOf(result, readBuffer.capacity()), readBuffer.array()); + reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + assertTrue(reader.isOpen()); } @Test @@ -120,7 +113,6 @@ public void testReadBig() throws IOException { storageRpcMock.read(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE, CUSTOM_CHUNK_SIZE)) .andReturn(secondResult); - EasyMock.expectLastCall(); EasyMock.replay(storageRpcMock); reader.read(firstReadBuffer); reader.read(secondReadBuffer); @@ -165,6 +157,7 @@ public void testSeek() throws IOException { public void testClose() throws IOException { EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); EasyMock.replay(optionsMock); + EasyMock.replay(storageRpcMock); reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); assertTrue(reader.isOpen()); reader.close(); @@ -175,6 +168,7 @@ public void testClose() throws IOException { public void testReadClosed() { EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); EasyMock.replay(optionsMock); + EasyMock.replay(storageRpcMock); reader = new BlobReadChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); reader.close(); try { diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java index 65997bf86228..bff9106db75b 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java @@ -35,6 +35,8 @@ import java.util.Arrays; import java.util.Map; import java.util.Random; +import static org.easymock.EasyMock.verify; +import org.junit.After; public class BlobWriterChannelTest { @@ -58,6 +60,12 @@ public void setUp() throws IOException, InterruptedException { storageRpcMock = EasyMock.createMock(StorageRpc.class); } + @After + public void tearDown() throws Exception { + verify(optionsMock); + verify(storageRpcMock); + } + @Test public void testCreate() { EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); @@ -97,6 +105,32 @@ public void testWriteWithFlush() throws IOException { assertArrayEquals(buffer.array(), capturedBuffer.getValue()); } + @Test + public void testWritesAndFlush() throws IOException { + EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); + EasyMock.expect(optionsMock.retryParams()).andReturn(RetryParams.noRetries()); + EasyMock.replay(optionsMock); + EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); + Capture capturedBuffer = Capture.newInstance(); + storageRpcMock.write(EasyMock.eq(UPLOAD_ID), EasyMock.capture(capturedBuffer), EasyMock.eq(0), + EasyMock.eq(BLOB_INFO.toPb()), EasyMock.eq(0L), EasyMock.eq(DEFAULT_CHUNK_SIZE), + EasyMock.eq(false)); + EasyMock.expectLastCall(); + EasyMock.replay(storageRpcMock); + writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + ByteBuffer[] buffers = new ByteBuffer[DEFAULT_CHUNK_SIZE / MIN_CHUNK_SIZE]; + for (int i = 0; i < buffers.length; i++) { + buffers[i] = randomBuffer(MIN_CHUNK_SIZE); + assertEquals(MIN_CHUNK_SIZE, writer.write(buffers[i])); + } + for (int i = 0; i < buffers.length; i++) { + assertArrayEquals( + buffers[i].array(), + Arrays.copyOfRange( + capturedBuffer.getValue(), MIN_CHUNK_SIZE * i, MIN_CHUNK_SIZE * (i + 1))); + } + } + @Test public void testCloseWithoutFlush() throws IOException { EasyMock.expect(optionsMock.storageRpc()).andReturn(storageRpcMock); From 7145030ddb913c9ec3c9fb9418943ffe0af631e5 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 29 Sep 2015 22:40:27 +0200 Subject: [PATCH 3/3] Renaming BlobWriterChannelImpl to BlobWriteChannelImpl, renaming tests --- ...nelImpl.java => BlobWriteChannelImpl.java} | 4 ++-- .../google/gcloud/storage/StorageImpl.java | 2 +- ...Test.java => BlobReadChannelImplTest.java} | 4 ++-- ...est.java => BlobWriteChannelImplTest.java} | 20 +++++++++---------- 4 files changed, 15 insertions(+), 15 deletions(-) rename gcloud-java-storage/src/main/java/com/google/gcloud/storage/{BlobWriterChannelImpl.java => BlobWriteChannelImpl.java} (97%) rename gcloud-java-storage/src/test/java/com/google/gcloud/storage/{BlobReadChannelTest.java => BlobReadChannelImplTest.java} (99%) rename gcloud-java-storage/src/test/java/com/google/gcloud/storage/{BlobWriterChannelTest.java => BlobWriteChannelImplTest.java} (92%) diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java similarity index 97% rename from gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java rename to gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java index 612e5d7551dc..48d60dc8e24d 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java @@ -32,7 +32,7 @@ /** * Default implementation for BlobWriteChannel. */ -class BlobWriterChannelImpl implements BlobWriteChannel { +class BlobWriteChannelImpl implements BlobWriteChannel { private static final long serialVersionUID = 8675286882724938737L; private static final int MIN_CHUNK_SIZE = 256 * 1024; @@ -50,7 +50,7 @@ class BlobWriterChannelImpl implements BlobWriteChannel { private transient StorageRpc storageRpc; private transient StorageObject storageObject; - BlobWriterChannelImpl(StorageOptions options, BlobInfo blobInfo, + BlobWriteChannelImpl(StorageOptions options, BlobInfo blobInfo, Map optionsMap) { this.options = options; this.blobInfo = blobInfo; diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java index 4c80c6741559..5f2ee7e30d71 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java @@ -443,7 +443,7 @@ public BlobReadChannel reader(String bucket, String blob, BlobSourceOption... op @Override public BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options) { final Map optionsMap = optionMap(blobInfo, options); - return new BlobWriterChannelImpl(options(), blobInfo, optionsMap); + return new BlobWriteChannelImpl(options(), blobInfo, optionsMap); } @Override diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelImplTest.java similarity index 99% rename from gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java rename to gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelImplTest.java index dd2426416d19..2a43753e6178 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobReadChannelImplTest.java @@ -16,6 +16,7 @@ package com.google.gcloud.storage; +import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; @@ -34,10 +35,9 @@ import java.util.Arrays; import java.util.Map; import java.util.Random; -import static org.easymock.EasyMock.verify; import org.junit.After; -public class BlobReadChannelTest { +public class BlobReadChannelImplTest { private static final String BUCKET_NAME = "b"; private static final String BLOB_NAME = "n"; diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelImplTest.java similarity index 92% rename from gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java rename to gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelImplTest.java index bff9106db75b..cc43f8f6e1f7 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriterChannelTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobWriteChannelImplTest.java @@ -16,6 +16,7 @@ package com.google.gcloud.storage; +import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertTrue; @@ -35,10 +36,9 @@ import java.util.Arrays; import java.util.Map; import java.util.Random; -import static org.easymock.EasyMock.verify; import org.junit.After; -public class BlobWriterChannelTest { +public class BlobWriteChannelImplTest { private static final String BUCKET_NAME = "b"; private static final String BLOB_NAME = "n"; @@ -52,7 +52,7 @@ public class BlobWriterChannelTest { private StorageOptions optionsMock; private StorageRpc storageRpcMock; - private BlobWriterChannelImpl writer; + private BlobWriteChannelImpl writer; @Before public void setUp() throws IOException, InterruptedException { @@ -72,7 +72,7 @@ public void testCreate() { EasyMock.replay(optionsMock); EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); EasyMock.replay(storageRpcMock); - writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + writer = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); assertTrue(writer.isOpen()); } @@ -82,7 +82,7 @@ public void testWriteWithoutFlush() throws IOException { EasyMock.replay(optionsMock); EasyMock.expect(storageRpcMock.open(BLOB_INFO.toPb(), EMPTY_RPC_OPTIONS)).andReturn(UPLOAD_ID); EasyMock.replay(storageRpcMock); - writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + writer = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); assertEquals(MIN_CHUNK_SIZE, writer.write(ByteBuffer.allocate(MIN_CHUNK_SIZE))); } @@ -98,7 +98,7 @@ public void testWriteWithFlush() throws IOException { EasyMock.eq(false)); EasyMock.expectLastCall(); EasyMock.replay(storageRpcMock); - writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + writer = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); writer.chunkSize(CUSTOM_CHUNK_SIZE); ByteBuffer buffer = randomBuffer(CUSTOM_CHUNK_SIZE); assertEquals(CUSTOM_CHUNK_SIZE, writer.write(buffer)); @@ -117,7 +117,7 @@ public void testWritesAndFlush() throws IOException { EasyMock.eq(false)); EasyMock.expectLastCall(); EasyMock.replay(storageRpcMock); - writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + writer = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); ByteBuffer[] buffers = new ByteBuffer[DEFAULT_CHUNK_SIZE / MIN_CHUNK_SIZE]; for (int i = 0; i < buffers.length; i++) { buffers[i] = randomBuffer(MIN_CHUNK_SIZE); @@ -142,7 +142,7 @@ public void testCloseWithoutFlush() throws IOException { EasyMock.eq(BLOB_INFO.toPb()), EasyMock.eq(0L), EasyMock.eq(0), EasyMock.eq(true)); EasyMock.expectLastCall(); EasyMock.replay(storageRpcMock); - writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + writer = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); assertTrue(writer.isOpen()); writer.close(); assertArrayEquals(new byte[0], capturedBuffer.getValue()); @@ -162,7 +162,7 @@ public void testCloseWithFlush() throws IOException { EasyMock.eq(true)); EasyMock.expectLastCall(); EasyMock.replay(storageRpcMock); - writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + writer = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); assertTrue(writer.isOpen()); writer.write(buffer); writer.close(); @@ -182,7 +182,7 @@ public void testWriteClosed() throws IOException { EasyMock.eq(BLOB_INFO.toPb()), EasyMock.eq(0L), EasyMock.eq(0), EasyMock.eq(true)); EasyMock.expectLastCall(); EasyMock.replay(storageRpcMock); - writer = new BlobWriterChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); + writer = new BlobWriteChannelImpl(optionsMock, BLOB_INFO, EMPTY_RPC_OPTIONS); writer.close(); try { writer.write(ByteBuffer.allocate(MIN_CHUNK_SIZE));