Skip to content

Commit

Permalink
Move test helpers' tests to testing package (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard authored and aozarov committed Apr 11, 2016
1 parent af18b50 commit 5dadf6a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
* limitations under the License.
*/

package com.google.gcloud.bigquery;
package com.google.gcloud.bigquery.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.gcloud.bigquery.BigQuery;
import com.google.gcloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.gcloud.bigquery.testing.RemoteBigQueryHelper;
import com.google.gcloud.bigquery.BigQueryOptions;

import org.easymock.EasyMock;
import org.junit.Rule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

package com.google.gcloud.datastore;
package com.google.gcloud.datastore.testing;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.google.gcloud.AuthCredentials;
import com.google.gcloud.datastore.testing.LocalDatastoreHelper;
import com.google.gcloud.datastore.DatastoreOptions;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.google.gcloud.resourcemanager;
package com.google.gcloud.resourcemanager.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -11,10 +11,10 @@
import com.google.api.services.cloudresourcemanager.model.Binding;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gcloud.resourcemanager.ResourceManagerException;
import com.google.gcloud.resourcemanager.spi.DefaultResourceManagerRpc;
import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc;
import com.google.gcloud.resourcemanager.spi.ResourceManagerRpc.Tuple;
import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper;

import org.junit.AfterClass;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,9 @@ public Builder toBuilder() {

@Override
public final boolean equals(Object obj) {
return obj instanceof Blob && Objects.equals(toPb(), ((Blob) obj).toPb())
return this == obj
|| obj instanceof Blob
&& Objects.equals(toPb(), ((Blob) obj).toPb())
&& Objects.equals(options, ((Blob) obj).options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
* limitations under the License.
*/

package com.google.gcloud.storage;
package com.google.gcloud.storage.testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableList;
import com.google.gcloud.Page;
import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.Storage.BlobListOption;
import com.google.gcloud.storage.testing.RemoteStorageHelper;
import com.google.gcloud.storage.StorageException;
import com.google.gcloud.storage.StorageOptions;

import org.easymock.EasyMock;
import org.junit.Before;
Expand Down Expand Up @@ -71,8 +75,11 @@ public class RemoteStorageHelperTest {
private static final InputStream JSON_KEY_STREAM = new ByteArrayInputStream(JSON_KEY.getBytes());
private static final StorageException RETRYABLE_EXCEPTION = new StorageException(409, "");
private static final StorageException FATAL_EXCEPTION = new StorageException(500, "");
private static final BlobId BLOB_ID1 = BlobId.of(BUCKET_NAME, "n1");
private static final BlobId BLOB_ID2 = BlobId.of(BUCKET_NAME, "n2");

private static Storage serviceMockReturnsOptions;
private Blob blob1;
private Blob blob2;
private List<Blob> blobList;
private Page<Blob> blobPage;

Expand All @@ -81,18 +88,9 @@ public class RemoteStorageHelperTest {

@Before
public void setUp() {
serviceMockReturnsOptions = EasyMock.createMock(Storage.class);
EasyMock.expect(serviceMockReturnsOptions.options())
.andReturn(EasyMock.createMock(StorageOptions.class))
.times(2);
EasyMock.replay(serviceMockReturnsOptions);
blobList = ImmutableList.of(
new Blob(
serviceMockReturnsOptions,
new BlobInfo.BuilderImpl(BlobInfo.builder(BUCKET_NAME, "n1").build())),
new Blob(
serviceMockReturnsOptions,
new BlobInfo.BuilderImpl(BlobInfo.builder(BUCKET_NAME, "n2").build())));
blob1 = EasyMock.createMock(Blob.class);
blob2 = EasyMock.createMock(Blob.class);
blobList = ImmutableList.of(blob1, blob2);
blobPage = new Page<Blob>() {
@Override
public String nextPageCursor() {
Expand All @@ -119,27 +117,29 @@ public Iterator<Blob> iterateAll() {
@Test
public void testForceDelete() throws InterruptedException, ExecutionException {
Storage storageMock = EasyMock.createMock(Storage.class);
EasyMock.expect(blob1.blobId()).andReturn(BLOB_ID1);
EasyMock.expect(storageMock.delete(BLOB_ID1)).andReturn(true);
EasyMock.expect(blob2.blobId()).andReturn(BLOB_ID2);
EasyMock.expect(storageMock.delete(BLOB_ID2)).andReturn(true);
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
.andReturn(blobPage);
for (BlobInfo info : blobList) {
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
}
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
EasyMock.replay(storageMock);
EasyMock.replay(storageMock, blob1, blob2);
assertTrue(RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME, 5, TimeUnit.SECONDS));
EasyMock.verify(storageMock);
EasyMock.verify(storageMock, blob1, blob2);
}

@Test
public void testForceDeleteTimeout() throws InterruptedException, ExecutionException {
Storage storageMock = EasyMock.createMock(Storage.class);
EasyMock.expect(blob1.blobId()).andReturn(BLOB_ID1).anyTimes();
EasyMock.expect(storageMock.delete(BLOB_ID1)).andReturn(true).anyTimes();
EasyMock.expect(blob2.blobId()).andReturn(BLOB_ID2).anyTimes();
EasyMock.expect(storageMock.delete(BLOB_ID2)).andReturn(true).anyTimes();
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
.andReturn(blobPage).anyTimes();
for (BlobInfo info : blobList) {
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true).anyTimes();
}
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(RETRYABLE_EXCEPTION).anyTimes();
EasyMock.replay(storageMock);
EasyMock.replay(storageMock, blob1, blob2);
assertFalse(
RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME, 50, TimeUnit.MICROSECONDS));
EasyMock.verify(storageMock);
Expand All @@ -148,13 +148,14 @@ public void testForceDeleteTimeout() throws InterruptedException, ExecutionExcep
@Test
public void testForceDeleteFail() throws InterruptedException, ExecutionException {
Storage storageMock = EasyMock.createMock(Storage.class);
EasyMock.expect(blob1.blobId()).andReturn(BLOB_ID1);
EasyMock.expect(storageMock.delete(BLOB_ID1)).andReturn(true);
EasyMock.expect(blob2.blobId()).andReturn(BLOB_ID2);
EasyMock.expect(storageMock.delete(BLOB_ID2)).andReturn(true);
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
.andReturn(blobPage);
for (BlobInfo info : blobList) {
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
}
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION);
EasyMock.replay(storageMock);
EasyMock.replay(storageMock, blob1, blob2);
thrown.expect(ExecutionException.class);
try {
RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME, 5, TimeUnit.SECONDS);
Expand All @@ -166,27 +167,29 @@ public void testForceDeleteFail() throws InterruptedException, ExecutionExceptio
@Test
public void testForceDeleteNoTimeout() {
Storage storageMock = EasyMock.createMock(Storage.class);
EasyMock.expect(blob1.blobId()).andReturn(BLOB_ID1);
EasyMock.expect(storageMock.delete(BLOB_ID1)).andReturn(true);
EasyMock.expect(blob2.blobId()).andReturn(BLOB_ID2);
EasyMock.expect(storageMock.delete(BLOB_ID2)).andReturn(true);
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
.andReturn(blobPage);
for (BlobInfo info : blobList) {
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
}
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
EasyMock.replay(storageMock);
EasyMock.replay(storageMock, blob1, blob2);
RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME);
EasyMock.verify(storageMock);
}

@Test
public void testForceDeleteNoTimeoutFail() {
Storage storageMock = EasyMock.createMock(Storage.class);
EasyMock.expect(blob1.blobId()).andReturn(BLOB_ID1);
EasyMock.expect(storageMock.delete(BLOB_ID1)).andReturn(true);
EasyMock.expect(blob2.blobId()).andReturn(BLOB_ID2);
EasyMock.expect(storageMock.delete(BLOB_ID2)).andReturn(true);
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
.andReturn(blobPage);
for (BlobInfo info : blobList) {
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
}
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION);
EasyMock.replay(storageMock);
EasyMock.replay(storageMock, blob1, blob2);
thrown.expect(StorageException.class);
try {
RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME);
Expand Down

0 comments on commit 5dadf6a

Please sign in to comment.