Skip to content

Commit 863c572

Browse files
author
Ajay Kannan
committed
Create simple forceDelete that can be used on AppEngine
1 parent 5a23e57 commit 863c572

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

gcloud-java-storage/src/main/java/com/google/gcloud/storage/testing/RemoteGcsHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ public static Boolean forceDelete(Storage storage, String bucket, long timeout,
8888
}
8989
}
9090

91+
/**
92+
* Deletes a bucket, even if non-empty. Objects in the bucket are listed and deleted until bucket
93+
* deletion succeeds. This method can be used to delete buckets from within App Engine. Note that
94+
* this method does not set a timeout.
95+
*
96+
* @param storage the storage service to be used to issue requests
97+
* @param bucket the bucket to be deleted
98+
* @throws StorageException if an exception is encountered during bucket deletion
99+
*/
100+
public static void forceDelete(Storage storage, String bucket) throws StorageException {
101+
try {
102+
new DeleteBucketTask(storage, bucket).call();
103+
} catch (Exception e) {
104+
throw (StorageException) e;
105+
}
106+
}
107+
91108
/**
92109
* Returns a bucket name generated using a random UUID.
93110
*/

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,37 @@ public void testForceDeleteFail() throws InterruptedException, ExecutionExceptio
140140
}
141141
}
142142

143+
144+
@Test
145+
public void testForceDeleteNoTimeout() throws Exception {
146+
Storage storageMock = EasyMock.createMock(Storage.class);
147+
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(BLOB_PAGE);
148+
for (BlobInfo info : BLOB_LIST) {
149+
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
150+
}
151+
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
152+
EasyMock.replay(storageMock);
153+
RemoteGcsHelper.forceDelete(storageMock, BUCKET_NAME);
154+
EasyMock.verify(storageMock);
155+
}
156+
157+
@Test
158+
public void testForceDeleteNoTimeoutFail() throws Exception {
159+
Storage storageMock = EasyMock.createMock(Storage.class);
160+
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(BLOB_PAGE);
161+
for (BlobInfo info : BLOB_LIST) {
162+
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
163+
}
164+
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION);
165+
EasyMock.replay(storageMock);
166+
thrown.expect(StorageException.class);
167+
try {
168+
RemoteGcsHelper.forceDelete(storageMock, BUCKET_NAME);
169+
} finally {
170+
EasyMock.verify(storageMock);
171+
}
172+
}
173+
143174
@Test
144175
public void testCreateFromStream() {
145176
RemoteGcsHelper helper = RemoteGcsHelper.create(PROJECT_ID, JSON_KEY_STREAM);

0 commit comments

Comments
 (0)