diff options
author | Eike Stepper | 2015-12-07 11:42:12 +0000 |
---|---|---|
committer | Eike Stepper | 2015-12-07 11:42:12 +0000 |
commit | 2867f6d2afd6d241a648ed12b707bfbb7fc27da8 (patch) | |
tree | ebf0528695e25010761af4e7b8c5a6fbfe825bbd | |
parent | f409f87e9082d1afe3daaf6f0e064d3fcd98be89 (diff) | |
download | uss-2867f6d2afd6d241a648ed12b707bfbb7fc27da8.tar.gz uss-2867f6d2afd6d241a648ed12b707bfbb7fc27da8.tar.xz uss-2867f6d2afd6d241a648ed12b707bfbb7fc27da8.zip |
Return the delete() result as a boolean
7 files changed, 187 insertions, 28 deletions
diff --git a/org.eclipse.userstorage.tests/src/org/eclipse/userstorage/tests/Example.java b/org.eclipse.userstorage.tests/src/org/eclipse/userstorage/tests/Example.java index 357ebe1..b8e41af 100644 --- a/org.eclipse.userstorage.tests/src/org/eclipse/userstorage/tests/Example.java +++ b/org.eclipse.userstorage.tests/src/org/eclipse/userstorage/tests/Example.java @@ -29,7 +29,7 @@ public class Example { IStorage storage = StorageFactory.DEFAULT.create("pDKTqBfDuNxlAKydhEwxBZPxa4q", new FileStorageCache()); - IBlob blob = storage.getBlob("user_setup"); + IBlob blob = storage.getBlob("test_blob"); blob.setContentsUTF("A short UTF-8 string value"); InputStream in = blob.getContents(); diff --git a/org.eclipse.userstorage.tests/src/org/eclipse/userstorage/tests/StorageTests.java b/org.eclipse.userstorage.tests/src/org/eclipse/userstorage/tests/StorageTests.java index ab118a3..ee7a11e 100644 --- a/org.eclipse.userstorage.tests/src/org/eclipse/userstorage/tests/StorageTests.java +++ b/org.eclipse.userstorage.tests/src/org/eclipse/userstorage/tests/StorageTests.java @@ -60,6 +60,8 @@ public final class StorageTests extends AbstractTest private static final String KEY = "test_blob"; + private static final String INVALID_ETAG = "<invalid_etag>"; + private ServerFixture serverFixture; private ClientFixture clientFixture; @@ -219,6 +221,49 @@ public final class StorageTests extends AbstractTest } @Test + public void testCreateWithInvalidETag() throws Exception + { + String key = makeKey(); + + IBlob blob1 = factory.create(APPLICATION_TOKEN).getBlob(key); + blob1.setContentsUTF("A short UTF-8 string value"); + blob1.delete(); + + IBlob blob2 = factory.create(APPLICATION_TOKEN).getBlob(key); + blob2.setContentsUTF("A short UTF-8 string value"); + } + + @Test + public void testCreateAfterRetrieve() throws Exception + { + String key = makeKey(); + + IBlob blob1 = factory.create(APPLICATION_TOKEN).getBlob(key); + blob1.setContentsUTF("A short UTF-8 string value"); + blob1.delete(); + + IBlob blob2 = factory.create(APPLICATION_TOKEN).getBlob(key); + + try + { + blob2.getContentsUTF(); + fail("NotFoundException expected"); + } + catch (NotFoundException expected) + { + // SUCCESS + } + + String value = "A short UTF-8 string value"; + blob2.setContentsUTF(value); + + BlobInfo blobInfo = serverFixture.readServer(blob2); + assertThat(blobInfo.contents, is(value)); + assertThat(blobInfo.eTag, is(blob2.getETag())); + assertThat(blobInfo.eTag, is(not(Session.NOT_FOUND_ETAG))); + } + + @Test public void testUpdateWithCache() throws Exception { IStorage storage = factory.create(APPLICATION_TOKEN, cache); @@ -291,11 +336,11 @@ public final class StorageTests extends AbstractTest } @Test - public void testRetrieveNotFound() throws Exception + public void testRetrieveNotExistent() throws Exception { IStorage storage = factory.create(APPLICATION_TOKEN); IBlob blob = storage.getBlob("aaaaaaaaaa"); - blob.setETag("<invalid_etag>"); + blob.setETag(INVALID_ETAG); try { @@ -581,6 +626,34 @@ public final class StorageTests extends AbstractTest } @Test + public void testAuthenticateFailure() throws Exception + { + IStorage storage = factory.create(APPLICATION_TOKEN); + IBlob blob = storage.getBlob(makeKey()); + + Credentials credentials = new Credentials("abcd", "wrong123"); + Credentials oldCredentials = FixedCredentialsProvider.setCredentials(credentials); + + try + { + ((StorageService)storage.getService()).setCredentials(credentials); + + String value1 = "A short UTF-8 string value"; + assertThat(blob.setContentsUTF(value1), is(true)); + + fail("ProtocolException: HTTP/1.1 401 Unauthorized expected"); + } + catch (ProtocolException expected) + { + assertThat(expected.getStatusCode(), is(401)); + } + finally + { + FixedCredentialsProvider.setCredentials(oldCredentials); + } + } + + @Test public void testReauthenticate() throws Exception { IStorage storage = factory.create(APPLICATION_TOKEN); @@ -636,6 +709,33 @@ public final class StorageTests extends AbstractTest } @Test + public void testDeleteWithoutETag() throws Exception + { + IStorage storage = factory.create(APPLICATION_TOKEN); + IBlob blob = storage.getBlob(makeKey()); + + String value = "A short UTF-8 string value"; + blob.setContentsUTF(value); + assertThat(blob.getContentsUTF(), is(value)); + + blob.setETag(null); + blob.delete(); + + BlobInfo blobInfo = serverFixture.readServer(blob); + assertThat(blobInfo, isNull()); + + try + { + blob.getContentsUTF(); + fail("NotFoundException expected"); + } + catch (NotFoundException expected) + { + // SUCCESS + } + } + + @Test public void testDeleteWithCache() throws Exception { IStorage storage = factory.create(APPLICATION_TOKEN, cache); @@ -710,6 +810,54 @@ public final class StorageTests extends AbstractTest assertThat(storage.getBlobs().iterator().hasNext(), is(false)); } + @Test + public void testDeleteNotExistent() throws Exception + { + IStorage storage = factory.create(APPLICATION_TOKEN); + IBlob blob = storage.getBlob(makeKey()); + + try + { + blob.getContentsUTF(); + + // Do not fail if the blob exists. + // Just skip this test in the unlikely case. + return; + } + catch (NotFoundException expected) + { + // SUCCESS + } + + boolean deleted = blob.delete(); + assertThat(deleted, is(false)); + } + + @Test + public void testDeleteNotExistentWithInvalidETag() throws Exception + { + IStorage storage = factory.create(APPLICATION_TOKEN); + IBlob blob = storage.getBlob(makeKey()); + + try + { + blob.getContentsUTF(); + + // Do not fail if the blob exists. + // Just skip this test in the unlikely case. + return; + } + catch (NotFoundException expected) + { + // SUCCESS + } + + blob.setETag(INVALID_ETAG); + + boolean deleted = blob.delete(); + assertThat(deleted, is(false)); + } + private String makeKey() { if (serverFixture.hasLocalServer()) diff --git a/org.eclipse.userstorage/src/org/eclipse/userstorage/IBlob.java b/org.eclipse.userstorage/src/org/eclipse/userstorage/IBlob.java index c3842f7..baa5df0 100644 --- a/org.eclipse.userstorage/src/org/eclipse/userstorage/IBlob.java +++ b/org.eclipse.userstorage/src/org/eclipse/userstorage/IBlob.java @@ -313,6 +313,7 @@ public interface IBlob * the cache will be deleted, too. * <p> * + * @return <code>true</code> if this blob was successfully deleted from the server, <code>false</code> if it did not exist.<p> * @throws IOException if remote I/O was unsuccessful. A {@link ProtocolException} may contain more information about protocol-specific problems.<p> * @throws ConflictException if the server detected a conflict and did not update the blob.<p> * @throws NoServiceException if the {@link #getStorage() storage} of this blob has no {@link IStorageService service} assigned.<p> @@ -320,7 +321,7 @@ public interface IBlob * * @see #setETag(String) */ - public void delete() throws IOException, ConflictException, NoServiceException, IllegalStateException; + public boolean delete() throws IOException, ConflictException, NoServiceException, IllegalStateException; /** * Returns <code>true</code> if this blob is disposed, <code>false</code> otherwise. diff --git a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Blob.java b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Blob.java index 42bbce8..e7dc0f0 100644 --- a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Blob.java +++ b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Blob.java @@ -171,10 +171,10 @@ public class Blob implements IBlob } @Override - public void delete() throws IOException, ConflictException, NoServiceException, IllegalStateException + public boolean delete() throws IOException, ConflictException, NoServiceException, IllegalStateException { checkNotDisposed(); - storage.deleteBlob(key, properties); + return storage.deleteBlob(key, properties); } @Override diff --git a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Session.java b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Session.java index 4055422..a03e024 100644 --- a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Session.java +++ b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Session.java @@ -57,7 +57,7 @@ public class Session implements Headers, Codes public static final String USER_AGENT_PROPERTY = Session.class.getName() + ".userAgent"; - public static final String NOT_FOUND_ETAG = "<not_found>"; + public static final String NOT_FOUND_ETAG = "not_found"; private static final int AUTHORIZATION_ATTEMPTS = 3; @@ -210,7 +210,12 @@ public class Session implements Headers, Codes } // Blob wasn't found. - properties.put(Blob.ETAG, NOT_FOUND_ETAG); + + // TODO This does not work because of bug 483775. + // properties.put(Blob.ETAG, NOT_FOUND_ETAG); + + int xxx; + properties.remove(Blob.ETAG); StatusLine statusLine = response.getStatusLine(); throw new NotFoundException("GET", uri, getProtocolVersion(statusLine), statusLine.getReasonPhrase()); @@ -264,12 +269,12 @@ public class Session implements Headers, Codes }.send(credentialsProvider); } - public void deleteBlob(String applicationToken, String key, final Map<String, String> properties, ICredentialsProvider credentialsProvider) + public boolean deleteBlob(String applicationToken, String key, final Map<String, String> properties, ICredentialsProvider credentialsProvider) throws IOException, ConflictException { URI uri = StringUtil.newURI(service.getServiceURI(), "api/blob/" + applicationToken + "/" + key); - new RequestTemplate<Boolean>(uri) + boolean deleted = new RequestTemplate<Boolean>(uri) { @Override protected Request prepareRequest() throws IOException @@ -288,7 +293,7 @@ public class Session implements Headers, Codes @Override protected Boolean handleResponse(HttpResponse response, HttpEntity responseEntity) throws IOException { - int statusCode = getStatusCode("DELETE", uri, response, NO_CONTENT, CONFLICT); + int statusCode = getStatusCode("DELETE", uri, response, NO_CONTENT, CONFLICT, NOT_FOUND); String eTag = getETag(response); if (statusCode == CONFLICT) @@ -298,7 +303,7 @@ public class Session implements Headers, Codes } properties.put(Blob.ETAG, "<deleted_etag>"); - return true; + return statusCode == NO_CONTENT; } }.send(credentialsProvider); @@ -323,6 +328,18 @@ public class Session implements Headers, Codes sessionID = null; csrfToken = null; } + + return deleted; + } + + private void debugResponseEntity(HttpEntity responseEntity) throws IOException + { + if (DEBUG && responseEntity != null) + { + responseEntity.writeTo(System.out); + System.out.println(); + System.out.println(); + } } private static String getETag(HttpResponse response) @@ -392,10 +409,7 @@ public class Session implements Headers, Codes } catch (IOException ex) { - if (DEBUG && responseEntity != null) - { - responseEntity.writeTo(System.out); - } + debugResponseEntity(responseEntity); if (ex instanceof ProtocolException) { @@ -461,10 +475,7 @@ public class Session implements Headers, Codes sessionID = null; csrfToken = null; - if (DEBUG && responseEntity != null) - { - responseEntity.writeTo(System.out); - } + debugResponseEntity(responseEntity); throw ex; } @@ -503,10 +514,7 @@ public class Session implements Headers, Codes { csrfToken = null; - if (DEBUG && responseEntity != null) - { - responseEntity.writeTo(System.out); - } + debugResponseEntity(responseEntity); throw ex; } diff --git a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Storage.java b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Storage.java index e7c7230..34289b3 100644 --- a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Storage.java +++ b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/Storage.java @@ -406,15 +406,17 @@ public final class Storage implements IStorage return created; } - public void deleteBlob(String key, Map<String, String> properties) throws IOException, ConflictException, NoServiceException + public boolean deleteBlob(String key, Map<String, String> properties) throws IOException, ConflictException, NoServiceException { StorageService service = getServiceSafe(); - service.deleteBlob(credentialsProvider, applicationToken, key, properties); + boolean deleted = service.deleteBlob(credentialsProvider, applicationToken, key, properties); if (cache != null) { cache.internalDelete(applicationToken, key); } + + return deleted; } @Override diff --git a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/StorageService.java b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/StorageService.java index eb5dcb1..20e17e6 100644 --- a/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/StorageService.java +++ b/org.eclipse.userstorage/src/org/eclipse/userstorage/internal/StorageService.java @@ -283,7 +283,7 @@ public class StorageService implements IStorageService return session.updateBlob(applicationToken, key, properties, in, credentialsProvider); } - public synchronized void deleteBlob(ICredentialsProvider credentialsProvider, String applicationToken, String key, Map<String, String> properties) + public synchronized boolean deleteBlob(ICredentialsProvider credentialsProvider, String applicationToken, String key, Map<String, String> properties) throws IOException, ConflictException { if (credentialsProvider == null) @@ -292,7 +292,7 @@ public class StorageService implements IStorageService } Session session = getSession(); - session.deleteBlob(applicationToken, key, properties, credentialsProvider); + return session.deleteBlob(applicationToken, key, properties, credentialsProvider); } @Override |