Issue 45: re-tooled tests to share 20 buckets, which are created at startup and assigned inside each test method

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1446 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-06-19 00:49:53 +00:00
parent 852374b35a
commit b11148a974
21 changed files with 1620 additions and 1216 deletions

View File

@ -47,13 +47,15 @@ import com.google.common.collect.ImmutableSet;
@Test
public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
public abstract void testPutAll() throws InterruptedException;
public abstract void testPutAll() throws InterruptedException, ExecutionException,
TimeoutException;
public abstract void testEntrySet() throws IOException, InterruptedException;
public abstract void testEntrySet() throws IOException, InterruptedException,
ExecutionException, TimeoutException;
public abstract void testValues() throws IOException, InterruptedException;
public abstract void testValues() throws IOException, InterruptedException, ExecutionException,
TimeoutException;
protected BaseS3Map<T> map;
protected Map<String, String> fiveStrings = ImmutableMap.of("one", "apple", "two", "bear",
"three", "candy", "four", "dogma", "five", "emma");
@ -68,7 +70,7 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
protected Map<String, File> fiveFiles;
String tmpDirectory;
@BeforeMethod(dependsOnMethods = "setUpBucket", groups = { "integration", "live" })
@BeforeMethod(groups = { "integration", "live" })
@Parameters( { "basedir" })
protected void setUpTempDir(String basedir) throws InterruptedException, ExecutionException,
FileNotFoundException, IOException, TimeoutException {
@ -87,34 +89,45 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
fiveInputs = ImmutableMap.of("one", IOUtils.toInputStream("apple"), "two", IOUtils
.toInputStream("bear"), "three", IOUtils.toInputStream("candy"), "four", IOUtils
.toInputStream("dogma"), "five", IOUtils.toInputStream("emma"));
map = createMap(context, bucketName);
map.clear();
}
protected abstract BaseS3Map<T> createMap(S3Context context, String bucket);
@Test(groups = { "integration", "live" })
public void testClear() throws InterruptedException {
map.clear();
assertEventuallyMapSize(0);
putString("one", "apple");
assertEventuallyMapSize(1);
map.clear();
assertEventuallyMapSize(0);
public void testClear() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<T> map = createMap(context, bucketName);
assertEventuallyMapSize(map, 0);
putString(map, "one", "apple");
assertEventuallyMapSize(map, 1);
map.clear();
assertEventuallyMapSize(map, 0);
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public abstract void testRemove() throws IOException, InterruptedException;
public abstract void testRemove() throws IOException, InterruptedException, ExecutionException,
TimeoutException;
@Test(groups = { "integration", "live" })
public void testKeySet() throws InterruptedException {
assertEventuallyKeySize(0);
putString("one", "two");
assertEventuallyKeySize(1);
assertEventuallyKeySetEquals(ImmutableSet.of("one"));
public void testKeySet() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<T> map = createMap(context, bucketName);
assertEventuallyKeySize(map, 0);
putString(map, "one", "two");
assertEventuallyKeySize(map, 1);
assertEventuallyKeySetEquals(map, ImmutableSet.of("one"));
} finally {
returnBucket(bucketName);
}
}
protected void assertEventuallyKeySetEquals(final Object toEqual) throws InterruptedException {
protected void assertEventuallyKeySetEquals(final BaseS3Map<T> map, final Object toEqual)
throws InterruptedException {
assertEventually(new Runnable() {
public void run() {
assertEquals(map.keySet(), toEqual);
@ -122,7 +135,8 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
});
}
protected void assertEventuallyKeySize(final int size) throws InterruptedException {
protected void assertEventuallyKeySize(final BaseS3Map<T> map, final int size)
throws InterruptedException {
assertEventually(new Runnable() {
public void run() {
assertEquals(map.keySet().size(), size);
@ -131,17 +145,24 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
}
@Test(groups = { "integration", "live" })
public void testContainsKey() throws InterruptedException {
assertEventuallyDoesntContainKey();
putString("one", "apple");
assertEventuallyContainsKey();
public void testContainsKey() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<T> map = createMap(context, bucketName);
assertEventuallyDoesntContainKey(map);
putString(map, "one", "apple");
assertEventuallyContainsKey(map);
} finally {
returnBucket(bucketName);
}
}
/**
* containsValue() uses md5 comparison to bucket contents, so this can be subject to eventual
* consistency problems.
*/
protected void assertEventuallyContainsValue(final Object value) throws InterruptedException {
protected void assertEventuallyContainsValue(final BaseS3Map<T> map, final Object value)
throws InterruptedException {
assertEventually(new Runnable() {
public void run() {
assert map.containsValue(value);
@ -149,7 +170,7 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
});
}
protected void assertEventuallyContainsKey() throws InterruptedException {
protected void assertEventuallyContainsKey(final BaseS3Map<T> map) throws InterruptedException {
assertEventually(new Runnable() {
public void run() {
assert map.containsKey("one");
@ -157,7 +178,8 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
});
}
protected void assertEventuallyDoesntContainKey() throws InterruptedException {
protected void assertEventuallyDoesntContainKey(final BaseS3Map<T> map)
throws InterruptedException {
assertEventually(new Runnable() {
public void run() {
assert !map.containsKey("one");
@ -166,13 +188,19 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
}
@Test(groups = { "integration", "live" })
public void testIsEmpty() throws InterruptedException {
assertEventuallyEmpty();
putString("one", "apple");
assertEventuallyNotEmpty();
public void testIsEmpty() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<T> map = createMap(context, bucketName);
assertEventuallyEmpty(map);
putString(map, "one", "apple");
assertEventuallyNotEmpty(map);
} finally {
returnBucket(bucketName);
}
}
protected void assertEventuallyNotEmpty() throws InterruptedException {
protected void assertEventuallyNotEmpty(final BaseS3Map<T> map) throws InterruptedException {
assertEventually(new Runnable() {
public void run() {
assert !map.isEmpty();
@ -180,7 +208,7 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
});
}
protected void assertEventuallyEmpty() throws InterruptedException {
protected void assertEventuallyEmpty(final BaseS3Map<T> map) throws InterruptedException {
assertEventually(new Runnable() {
public void run() {
assert map.isEmpty();
@ -188,16 +216,19 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
});
}
abstract protected void putString(String key, String value);
abstract protected void putString(BaseS3Map<T> map, String key, String value)
throws InterruptedException, ExecutionException, TimeoutException;
protected void fourLeftRemovingOne() throws InterruptedException {
protected void fourLeftRemovingOne(BaseS3Map<T> map) throws InterruptedException,
ExecutionException, TimeoutException {
map.remove("one");
assertEventuallyMapSize(4);
assertEventuallyKeySetEquals(new TreeSet<String>(ImmutableSet.of("two", "three", "four",
assertEventuallyMapSize(map, 4);
assertEventuallyKeySetEquals(map, new TreeSet<String>(ImmutableSet.of("two", "three", "four",
"five")));
}
protected void assertEventuallyMapSize(final int size) throws InterruptedException {
protected void assertEventuallyMapSize(final BaseS3Map<T> map, final int size)
throws InterruptedException {
assertEventually(new Runnable() {
public void run() {
assertEquals(map.size(), size);
@ -206,14 +237,22 @@ public abstract class BaseS3MapIntegrationTest<T> extends S3IntegrationTest {
}
@Test(groups = { "integration", "live" })
public abstract void testPut() throws IOException, InterruptedException;
public abstract void testPut() throws IOException, InterruptedException, ExecutionException,
TimeoutException;
@Test(groups = { "integration", "live" })
public void testGetBucket() throws InterruptedException {
assertEventuallyBucketNameCorrect();
public void testGetBucket() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<T> map = createMap(context, bucketName);
assertEventuallyBucketNameCorrect(map, bucketName);
} finally {
returnBucket(bucketName);
}
}
protected void assertEventuallyBucketNameCorrect() throws InterruptedException {
protected void assertEventuallyBucketNameCorrect(final BaseS3Map<T> map, final String bucketName)
throws InterruptedException {
assertEventually(new Runnable() {
public void run() {
assertEquals(map.getBucket().getName(), bucketName);

View File

@ -63,8 +63,6 @@ public class S3ConnectionIntegrationTest extends S3IntegrationTest {
httpStreamMd5 = checkNotNull(httpStreamMd5 != null ? httpStreamMd5 : sysHttpStreamMd5,
"httpStreamMd5");
String bucketName = bucketPrefix + "tcu";
createBucketAndEnsureEmpty(bucketName);
String key = "hello";
URL url = new URL(httpStreamUrl);
@ -78,8 +76,12 @@ public class S3ConnectionIntegrationTest extends S3IntegrationTest {
object.setContentLength(length);
object.getMetadata().setMd5(md5);
object.getMetadata().setSize(length);
byte[] newMd5 = client.putObject(bucketName, object).get(30, TimeUnit.SECONDS);
assertEquals(newMd5, md5);
String bucketName = getBucketName();
try {
byte[] newMd5 = client.putObject(bucketName, object).get(30, TimeUnit.SECONDS);
assertEquals(newMd5, md5);
} finally {
returnBucket(bucketName);
}
}
}

View File

@ -32,6 +32,8 @@ import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import org.apache.commons.io.IOUtils;
import org.jclouds.aws.s3.internal.BaseS3Map;
@ -45,166 +47,275 @@ import org.testng.annotations.Test;
*/
@Test(testName = "s3.S3InputStreamMapIntegrationTest")
public class S3InputStreamMapIntegrationTest extends BaseS3MapIntegrationTest<InputStream> {
S3InputStreamMap map = null;
@SuppressWarnings("unchecked")
protected BaseS3Map<InputStream> createMap(S3Context context, String bucket) {
map = context.createInputStreamMap(bucket);
S3InputStreamMap map = context.createInputStreamMap(bucket);
return (BaseS3Map<InputStream>) map;
}
@Override
@Test(groups = { "integration", "live" })
public void testValues() throws IOException, InterruptedException {
map.putAll(this.fiveInputs);
// this will cause us to block until the bucket updates.
assertEventuallyKeySize(5);
Collection<InputStream> values = map.values();
assertEquals(values.size(), 5);
Set<String> valuesAsString = new HashSet<String>();
for (InputStream stream : values) {
valuesAsString.add(Utils.toStringAndClose(stream));
public void testValues() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
map.putAll(this.fiveInputs);
// this will cause us to block until the bucket updates.
assertEventuallyKeySize(map, 5);
Collection<InputStream> values = map.values();
assertEquals(values.size(), 5);
Set<String> valuesAsString = new HashSet<String>();
for (InputStream stream : values) {
valuesAsString.add(Utils.toStringAndClose(stream));
}
valuesAsString.removeAll(fiveStrings.values());
assert valuesAsString.size() == 0;
} finally {
returnBucket(bucketName);
}
valuesAsString.removeAll(fiveStrings.values());
assert valuesAsString.size() == 0;
}
@Test(groups = { "integration", "live" })
public void testRemove() throws IOException, InterruptedException {
putString("one", "two");
InputStream old = map.remove("one");
assertEquals(Utils.toStringAndClose(old), "two");
old = map.remove("one");
assert old == null;
old = map.get("one");
assert old == null;
assertEventuallyKeySize(0);
public void testRemove() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
putString(map, "one", "two");
InputStream old = map.remove("one");
assertEquals(Utils.toStringAndClose(old), "two");
old = map.remove("one");
assert old == null;
old = map.get("one");
assert old == null;
assertEventuallyKeySize(map, 0);
} finally {
returnBucket(bucketName);
}
}
@Override
@Test(groups = { "integration", "live" })
public void testEntrySet() throws IOException, InterruptedException {
map.putAllStrings(this.fiveStrings);
// this will cause us to block until the bucket updates.
assertEventuallyKeySize(5);
Set<Entry<String, InputStream>> entries = map.entrySet();
assertEquals(entries.size(), 5);
for (Entry<String, InputStream> entry : entries) {
assertEquals(IOUtils.toString(entry.getValue()), fiveStrings.get(entry.getKey()));
entry.setValue(IOUtils.toInputStream(""));
}
assertEventuallyMapSize(5);
for (InputStream value : map.values()) {
assertEquals(IOUtils.toString(value), "");
public void testEntrySet() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
((S3InputStreamMap) map).putAllStrings(this.fiveStrings);
// this will cause us to block until the bucket updates.
assertEventuallyKeySize(map, 5);
Set<Entry<String, InputStream>> entries = map.entrySet();
assertEquals(entries.size(), 5);
for (Entry<String, InputStream> entry : entries) {
assertEquals(IOUtils.toString(entry.getValue()), fiveStrings.get(entry.getKey()));
entry.setValue(IOUtils.toInputStream(""));
}
assertEventuallyMapSize(map, 5);
for (InputStream value : map.values()) {
assertEquals(IOUtils.toString(value), "");
}
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testContainsStringValue() throws InterruptedException {
map.putString("one", "apple");
assertEventuallyContainsValue(fiveStrings.get("one"));
public void testContainsStringValue() throws InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
((S3InputStreamMap) map).putString("one", "apple");
assertEventuallyContainsValue(map, fiveStrings.get("one"));
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testContainsFileValue() throws InterruptedException {
map.putString("one", "apple");
assertEventuallyContainsValue(fiveFiles.get("one"));
public void testContainsFileValue() throws InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
((S3InputStreamMap) map).putString("one", "apple");
assertEventuallyContainsValue(map, fiveFiles.get("one"));
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testContainsInputStreamValue() throws InterruptedException {
map.putString("one", "apple");
assertEventuallyContainsValue(this.fiveInputs.get("one"));
public void testContainsInputStreamValue() throws InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
((S3InputStreamMap) map).putString("one", "apple");
assertEventuallyContainsValue(map, this.fiveInputs.get("one"));
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testContainsBytesValue() throws InterruptedException {
map.putString("one", "apple");
assertEventuallyContainsValue(this.fiveBytes.get("one"));
public void testContainsBytesValue() throws InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
((S3InputStreamMap) map).putString("one", "apple");
assertEventuallyContainsValue(map, this.fiveBytes.get("one"));
} finally {
returnBucket(bucketName);
}
}
@Override
@Test(groups = { "integration", "live" })
public void testPutAll() throws InterruptedException {
map.putAll(this.fiveInputs);
assertEventuallyMapSize(5);
assertEventuallyKeySetEquals(new TreeSet<String>(fiveInputs.keySet()));
fourLeftRemovingOne();
public void testPutAll() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
map.putAll(this.fiveInputs);
assertEventuallyMapSize(map, 5);
assertEventuallyKeySetEquals(map, new TreeSet<String>(fiveInputs.keySet()));
fourLeftRemovingOne(map);
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testPutAllBytes() throws InterruptedException {
map.putAllBytes(this.fiveBytes);
assertEventuallyMapSize(5);
assertEventuallyKeySetEquals(new TreeSet<String>(fiveBytes.keySet()));
fourLeftRemovingOne();
public void testPutAllBytes() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
((S3InputStreamMap) map).putAllBytes(this.fiveBytes);
assertEventuallyMapSize(map, 5);
assertEventuallyKeySetEquals(map, new TreeSet<String>(fiveBytes.keySet()));
fourLeftRemovingOne(map);
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testPutAllFiles() throws InterruptedException {
map.putAllFiles(this.fiveFiles);
assertEventuallyMapSize(5);
assertEventuallyKeySetEquals(new TreeSet<String>(fiveFiles.keySet()));
fourLeftRemovingOne();
public void testPutAllFiles() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
((S3InputStreamMap) map).putAllFiles(this.fiveFiles);
assertEventuallyMapSize(map, 5);
assertEventuallyKeySetEquals(map, new TreeSet<String>(fiveFiles.keySet()));
fourLeftRemovingOne(map);
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testPutAllStrings() throws InterruptedException {
map.putAllStrings(this.fiveStrings);
assertEventuallyMapSize(5);
assertEventuallyKeySetEquals(new TreeSet<String>(fiveStrings.keySet()));
fourLeftRemovingOne();
public void testPutAllStrings() throws InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
((S3InputStreamMap) map).putAllStrings(this.fiveStrings);
assertEventuallyMapSize(map, 5);
assertEventuallyKeySetEquals(map, new TreeSet<String>(fiveStrings.keySet()));
fourLeftRemovingOne(map);
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testPutString() throws IOException, InterruptedException {
InputStream old = map.putString("one", "apple");
getOneReturnsAppleAndOldValueIsNull(old);
InputStream apple = map.putString("one", "bear");
getOneReturnsBearAndOldValueIsApple(apple);
public void testPutString() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
InputStream old = ((S3InputStreamMap) map).putString("one", "apple");
getOneReturnsAppleAndOldValueIsNull(map, old);
InputStream apple = ((S3InputStreamMap) map).putString("one", "bear");
getOneReturnsBearAndOldValueIsApple(map, apple);
} finally {
returnBucket(bucketName);
}
}
void getOneReturnsAppleAndOldValueIsNull(InputStream old) throws IOException,
InterruptedException {
void getOneReturnsAppleAndOldValueIsNull(BaseS3Map<InputStream> map, InputStream old)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
assert old == null;
assertEquals(Utils.toStringAndClose(map.get("one")), "apple");
assertEventuallyMapSize(1);
assertEventuallyMapSize(map, 1);
}
void getOneReturnsBearAndOldValueIsApple(InputStream oldValue) throws IOException,
InterruptedException {
void getOneReturnsBearAndOldValueIsApple(BaseS3Map<InputStream> map, InputStream oldValue)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
assertEquals(Utils.toStringAndClose(map.get("one")), "bear");
assertEquals(Utils.toStringAndClose(oldValue), "apple");
assertEventuallyMapSize(1);
assertEventuallyMapSize(map, 1);
}
@Test(groups = { "integration", "live" })
public void testPutFile() throws IOException, InterruptedException {
InputStream old = map.putFile("one", fiveFiles.get("one"));
getOneReturnsAppleAndOldValueIsNull(old);
InputStream apple = map.putFile("one", fiveFiles.get("two"));
getOneReturnsBearAndOldValueIsApple(apple);
public void testPutFile() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
InputStream old = ((S3InputStreamMap) map).putFile("one", fiveFiles.get("one"));
getOneReturnsAppleAndOldValueIsNull(map, old);
InputStream apple = ((S3InputStreamMap) map).putFile("one", fiveFiles.get("two"));
getOneReturnsBearAndOldValueIsApple(map, apple);
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testPutBytes() throws IOException, InterruptedException {
InputStream old = map.putBytes("one", "apple".getBytes());
getOneReturnsAppleAndOldValueIsNull(old);
InputStream apple = map.putBytes("one", "bear".getBytes());
getOneReturnsBearAndOldValueIsApple(apple);
public void testPutBytes() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
InputStream old = ((S3InputStreamMap) map).putBytes("one", "apple".getBytes());
getOneReturnsAppleAndOldValueIsNull(map, old);
InputStream apple = ((S3InputStreamMap) map).putBytes("one", "bear".getBytes());
getOneReturnsBearAndOldValueIsApple(map, apple);
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testPut() throws IOException, InterruptedException {
InputStream old = map.put("one", IOUtils.toInputStream("apple"));
getOneReturnsAppleAndOldValueIsNull(old);
InputStream apple = map.put("one", IOUtils.toInputStream("bear"));
getOneReturnsBearAndOldValueIsApple(apple);
public void testPut() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<InputStream> map = createMap(context, bucketName);
InputStream old = map.put("one", IOUtils.toInputStream("apple"));
getOneReturnsAppleAndOldValueIsNull(map, old);
InputStream apple = map.put("one", IOUtils.toInputStream("bear"));
getOneReturnsBearAndOldValueIsApple(map, apple);
} finally {
returnBucket(bucketName);
}
}
@Override
protected void putString(String key, String value) {
map.putString(key, value);
protected void putString(BaseS3Map<InputStream> map, String key, String value)
throws InterruptedException, ExecutionException, TimeoutException {
((S3InputStreamMap) map).putString(key, value);
}
}

View File

@ -23,14 +23,16 @@
*/
package org.jclouds.aws.s3;
import static org.jclouds.aws.s3.commands.options.PutBucketOptions.Builder.createIn;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@ -46,15 +48,14 @@ import java.util.logging.Logger;
import org.jclouds.aws.s3.config.StubS3ConnectionModule;
import org.jclouds.aws.s3.domain.S3Bucket;
import org.jclouds.aws.s3.domain.S3Object;
import org.jclouds.aws.s3.domain.S3Bucket.Metadata.LocationConstraint;
import org.jclouds.aws.s3.reference.S3Constants;
import org.jclouds.aws.s3.util.S3Utils;
import org.jclouds.http.config.JavaUrlHttpFutureCommandClientModule;
import org.jclouds.util.Utils;
import org.testng.ITestContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
@ -85,13 +86,12 @@ public class S3IntegrationTest {
protected byte[] goodMd5;
protected byte[] badMd5;
protected String bucketName;
protected void createBucketAndEnsureEmpty(String sourceBucket) throws InterruptedException,
protected void createBucketAndEnsureEmpty(String bucketName) throws InterruptedException,
ExecutionException, TimeoutException {
deleteBucket(sourceBucket);
client.putBucketIfNotExists(sourceBucket).get(10, TimeUnit.SECONDS);
assertEventuallyBucketEmpty(sourceBucket);
client.putBucketIfNotExists(bucketName).get(10, TimeUnit.SECONDS);
emptyBucket(bucketName);
assertEventuallyBucketEmpty(bucketName);
}
protected void assertEventuallyBucketEmpty(final String bucketName) throws InterruptedException {
@ -143,7 +143,7 @@ public class S3IntegrationTest {
});
}
@BeforeClass(groups = { "integration", "live" })
@BeforeGroups(groups = { "integration", "live" })
protected void enableDebug() {
if (debugEnabled()) {
Handler HANDLER = new ConsoleHandler() {
@ -170,15 +170,12 @@ public class S3IntegrationTest {
protected S3Connection client;
protected S3Context context = null;
protected String bucketPrefix = (System.getProperty("user.name") + "." + this.getClass()
.getSimpleName()).toLowerCase();
protected static final String sysAWSAccessKeyId = System
.getProperty(S3Constants.PROPERTY_AWS_ACCESSKEYID);
protected static final String sysAWSSecretAccessKey = System
.getProperty(S3Constants.PROPERTY_AWS_SECRETACCESSKEY);
@BeforeClass(inheritGroups = false, groups = { "integration", "live" })
@BeforeGroups(groups = { "integration", "live" })
@Parameters( { S3Constants.PROPERTY_AWS_ACCESSKEYID, S3Constants.PROPERTY_AWS_SECRETACCESSKEY })
protected void setUpCredentials(@Optional String AWSAccessKeyId,
@Optional String AWSSecretAccessKey, ITestContext testContext) throws Exception {
@ -190,7 +187,7 @@ public class S3IntegrationTest {
testContext.setAttribute(S3Constants.PROPERTY_AWS_SECRETACCESSKEY, AWSSecretAccessKey);
}
@BeforeClass(dependsOnMethods = { "setUpCredentials" }, groups = { "integration", "live" })
@BeforeGroups(dependsOnMethods = { "setUpCredentials" }, groups = { "integration", "live" })
protected void setUpClient(ITestContext testContext) throws Exception {
if (testContext.getAttribute(S3Constants.PROPERTY_AWS_ACCESSKEYID) != null) {
String AWSAccessKeyId = (String) testContext
@ -203,7 +200,6 @@ public class S3IntegrationTest {
}
client = context.getConnection();
assert client != null;
deleteEverything();
goodMd5 = S3Utils.md5(TEST_STRING);
badMd5 = S3Utils.md5("alf");
}
@ -218,20 +214,64 @@ public class S3IntegrationTest {
createHttpModule()).build();
}
@BeforeMethod(dependsOnMethods = "deleteBucket", groups = { "integration", "live" })
public void setUpBucket(Method method) throws TimeoutException, ExecutionException,
InterruptedException {
bucketName = (bucketPrefix + method.getName()).toLowerCase();
if (bucketName.length() > 63)
bucketName = bucketName.substring(0, 62);
createBucketAndEnsureEmpty(bucketName);
public String getBucketName() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = bucketNames.poll(30, TimeUnit.SECONDS);
emptyBucket(bucketName);
assert bucketName != null : "unable to get a bucket for the test";
return bucketName;
}
@BeforeMethod(groups = { "integration", "live" })
@AfterMethod(groups = { "integration", "live" })
public void deleteBucket() throws TimeoutException, ExecutionException, InterruptedException {
if (bucketName != null)
deleteBucket(bucketName);
/**
* a bucket that should be deleted and recreated after the test is complete. This is due to
* having an ACL or otherwise that makes it not compatible with normal buckets
*/
public String getScratchBucketName() throws InterruptedException, ExecutionException,
TimeoutException {
return getBucketName();
}
public void returnBucket(String bucketName) throws InterruptedException, ExecutionException,
TimeoutException {
if (bucketName != null) {
bucketNames.add(bucketName);
bucketName = null;
}
}
/**
* abandon old bucket name instead of waiting for the bucket to be created.
*/
public void returnScratchBucket(String scratchBucket) throws InterruptedException,
ExecutionException, TimeoutException {
if (scratchBucket != null) {
deleteBucket(scratchBucket);
String newScratchBucket = bucketPrefix + (++bucketIndex);
createBucketAndEnsureEmpty(newScratchBucket);
returnBucket(newScratchBucket);
}
}
protected static int bucketCount = 20;
protected static volatile int bucketIndex = 0;
/**
* two test groups integration and live.
*/
private static final BlockingQueue<String> bucketNames = new ArrayBlockingQueue<String>(
bucketCount);
@BeforeGroups(dependsOnMethods = { "setUpClient" }, groups = { "live" })
public void setUpBuckets(ITestContext context) throws Exception {
synchronized (bucketNames) {
if (bucketNames.peek() == null) {
this.deleteEverything();
for (; bucketIndex < bucketCount; bucketIndex++) {
String bucketName = bucketPrefix + bucketIndex;
bucketNames.put(bucketName);
createBucketAndEnsureEmpty(bucketName);
}
}
}
}
protected boolean debugEnabled() {
@ -247,6 +287,8 @@ public class S3IntegrationTest {
return new JavaUrlHttpFutureCommandClientModule();
}
private String bucketPrefix = System.getProperty("user.name") + ".s3int";
protected void deleteEverything() throws Exception {
try {
List<S3Bucket.Metadata> metadata = client.listOwnedBuckets().get(10, TimeUnit.SECONDS);
@ -286,6 +328,15 @@ public class S3IntegrationTest {
}
}
protected String createScratchBucketInEU() throws InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getScratchBucketName();
deleteBucket(bucketName);
client.putBucketIfNotExists(bucketName, createIn(LocationConstraint.EU)).get(10,
TimeUnit.SECONDS);
return bucketName;
}
/**
* Empty and delete a bucket.
*
@ -302,9 +353,8 @@ public class S3IntegrationTest {
}
}
@AfterClass
@AfterGroups(groups = { "integration", "live" })
protected void tearDownClient() throws Exception {
deleteEverything();
context.close();
context = null;
}

View File

@ -33,6 +33,8 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import org.apache.commons.io.IOUtils;
import org.jclouds.aws.s3.domain.S3Object;
@ -47,117 +49,158 @@ import org.testng.annotations.Test;
*/
@Test(testName = "s3.S3ObjectMapIntegrationTest")
public class S3ObjectMapIntegrationTest extends BaseS3MapIntegrationTest<S3Object> {
S3ObjectMap map = null;
@SuppressWarnings("unchecked")
protected BaseS3Map<S3Object> createMap(S3Context context, String bucket) {
map = context.createS3ObjectMap(bucket);
S3ObjectMap map = context.createS3ObjectMap(bucket);
return (BaseS3Map<S3Object>) map;
}
@Override
@Test(groups = { "integration", "live" })
public void testValues() throws IOException, InterruptedException {
putFiveStrings();
Collection<S3Object> values = map.values();
assertEventuallyMapSize(5);
Set<String> valuesAsString = new HashSet<String>();
for (S3Object object : values) {
valuesAsString.add(S3Utils.getContentAsStringAndClose(object));
public void testValues() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<S3Object> map = createMap(context, bucketName);
putFiveStrings(map);
Collection<S3Object> values = map.values();
assertEventuallyMapSize(map, 5);
Set<String> valuesAsString = new HashSet<String>();
for (S3Object object : values) {
valuesAsString.add(S3Utils.getContentAsStringAndClose(object));
}
valuesAsString.removeAll(fiveStrings.values());
assert valuesAsString.size() == 0;
} finally {
returnBucket(bucketName);
}
valuesAsString.removeAll(fiveStrings.values());
assert valuesAsString.size() == 0;
}
@Test(groups = { "integration", "live" })
public void testRemove() throws IOException, InterruptedException {
putString("one", "two");
S3Object old = map.remove("one");
assertEquals(S3Utils.getContentAsStringAndClose(old), "two");
old = map.remove("one");
assert old == S3Object.NOT_FOUND;
old = map.get("one");
assert old == S3Object.NOT_FOUND;
assertEventuallyKeySize(0);
public void testRemove() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<S3Object> map = createMap(context, bucketName);
putString(map, "one", "two");
S3Object old = map.remove("one");
assertEquals(S3Utils.getContentAsStringAndClose(old), "two");
old = map.remove("one");
assert old == S3Object.NOT_FOUND;
old = map.get("one");
assert old == S3Object.NOT_FOUND;
assertEventuallyKeySize(map, 0);
} finally {
returnBucket(bucketName);
}
}
@Override
@Test(groups = { "integration", "live" })
public void testEntrySet() throws IOException, InterruptedException {
putFiveStrings();
Set<Entry<String, S3Object>> entries = map.entrySet();
assertEquals(entries.size(), 5);
for (Entry<String, S3Object> entry : entries) {
assertEquals(S3Utils.getContentAsStringAndClose(entry.getValue()), fiveStrings.get(entry
.getKey()));
S3Object value = entry.getValue();
value.setData("");
value.generateMd5();
entry.setValue(value);
}
assertEventuallyMapSize(5);
for (S3Object value : map.values()) {
assertEquals(S3Utils.getContentAsStringAndClose(value), "");
public void testEntrySet() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<S3Object> map = createMap(context, bucketName);
putFiveStrings(map);
Set<Entry<String, S3Object>> entries = map.entrySet();
assertEquals(entries.size(), 5);
for (Entry<String, S3Object> entry : entries) {
assertEquals(S3Utils.getContentAsStringAndClose(entry.getValue()), fiveStrings
.get(entry.getKey()));
S3Object value = entry.getValue();
value.setData("");
value.generateMd5();
entry.setValue(value);
}
assertEventuallyMapSize(map, 5);
for (S3Object value : map.values()) {
assertEquals(S3Utils.getContentAsStringAndClose(value), "");
}
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testContains() throws InterruptedException {
putString("one", "apple");
S3Object object = new S3Object("one");
object.setData("apple");
assertEventuallyContainsValue(object);
public void testContains() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<S3Object> map = createMap(context, bucketName);
putString(map, "one", "apple");
S3Object object = new S3Object("one");
object.setData("apple");
assertEventuallyContainsValue(map, object);
} finally {
returnBucket(bucketName);
}
}
void getOneReturnsAppleAndOldValueIsNull(S3Object old) throws IOException, InterruptedException {
void getOneReturnsAppleAndOldValueIsNull(BaseS3Map<S3Object> map, S3Object old)
throws IOException, InterruptedException {
assert old == S3Object.NOT_FOUND;
assertEquals(S3Utils.getContentAsStringAndClose(map.get("one")), "apple");
assertEventuallyMapSize(1);
assertEventuallyMapSize(map, 1);
}
void getOneReturnsBearAndOldValueIsApple(S3Object oldValue) throws IOException,
InterruptedException {
void getOneReturnsBearAndOldValueIsApple(BaseS3Map<S3Object> map, S3Object oldValue)
throws IOException, InterruptedException {
assertEquals(S3Utils.getContentAsStringAndClose(map.get("one")), "bear");
assertEquals(S3Utils.getContentAsStringAndClose(oldValue), "apple");
assertEventuallyMapSize(1);
assertEventuallyMapSize(map, 1);
}
@Test(groups = { "integration", "live" })
public void testPut() throws IOException, InterruptedException {
S3Object object = new S3Object("one");
object.setData(IOUtils.toInputStream("apple"));
object.generateMd5();
S3Object old = map.put(object.getKey(), object);
getOneReturnsAppleAndOldValueIsNull(old);
object.setData(IOUtils.toInputStream("bear"));
object.generateMd5();
S3Object apple = map.put(object.getKey(), object);
getOneReturnsBearAndOldValueIsApple(apple);
}
@Test(groups = { "integration", "live" })
public void testPutAll() throws InterruptedException {
Map<String, S3Object> newMap = new HashMap<String, S3Object>();
for (String key : fiveInputs.keySet()) {
S3Object object = new S3Object(key);
object.setData(fiveInputs.get(key));
object.getMetadata().setSize(fiveBytes.get(key).length);
newMap.put(key, object);
public void testPut() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<S3Object> map = createMap(context, bucketName);
S3Object object = new S3Object("one");
object.setData(IOUtils.toInputStream("apple"));
object.generateMd5();
S3Object old = map.put(object.getKey(), object);
getOneReturnsAppleAndOldValueIsNull(map, old);
object.setData(IOUtils.toInputStream("bear"));
object.generateMd5();
S3Object apple = map.put(object.getKey(), object);
getOneReturnsBearAndOldValueIsApple(map, apple);
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
public void testPutAll() throws InterruptedException, ExecutionException, TimeoutException {
String bucketName = getBucketName();
try {
BaseS3Map<S3Object> map = createMap(context, bucketName);
Map<String, S3Object> newMap = new HashMap<String, S3Object>();
for (String key : fiveInputs.keySet()) {
S3Object object = new S3Object(key);
object.setData(fiveInputs.get(key));
object.getMetadata().setSize(fiveBytes.get(key).length);
newMap.put(key, object);
}
map.putAll(newMap);
assertEventuallyMapSize(map, 5);
assertEventuallyKeySetEquals(map, new TreeSet<String>(fiveInputs.keySet()));
fourLeftRemovingOne(map);
} finally {
returnBucket(bucketName);
}
map.putAll(newMap);
assertEventuallyMapSize(5);
assertEventuallyKeySetEquals(new TreeSet<String>(fiveInputs.keySet()));
fourLeftRemovingOne();
}
@Override
protected void putString(String key, String value) {
protected void putString(BaseS3Map<S3Object> map, String key, String value) {
S3Object object = new S3Object(key);
object.setData(value);
map.put(key, object);
}
protected void putFiveStrings() {
protected void putFiveStrings(BaseS3Map<S3Object> map) {
Map<String, S3Object> newMap = new HashMap<String, S3Object>();
for (Map.Entry<String, String> entry : fiveStrings.entrySet()) {
S3Object object = new S3Object(entry.getKey());

View File

@ -502,15 +502,19 @@ public class StubS3Connection implements S3Connection {
}
if (options.getIfModifiedSince() != null) {
DateTime modifiedSince = dateService.rfc822DateParse(options.getIfModifiedSince());
if (modifiedSince.isAfter(object.getMetadata().getLastModified()))
throw new ExecutionException(new RuntimeException("after"));
if (object.getMetadata().getLastModified().isBefore(modifiedSince))
throw new ExecutionException(new RuntimeException(String.format(
"%1$s is before %2$s", object.getMetadata().getLastModified(),
modifiedSince)));
}
if (options.getIfUnmodifiedSince() != null) {
DateTime unmodifiedSince = dateService.rfc822DateParse(options
.getIfUnmodifiedSince());
if (unmodifiedSince.isAfter(object.getMetadata().getLastModified()))
throw new ExecutionException(new RuntimeException("after"));
if (object.getMetadata().getLastModified().isAfter(unmodifiedSince))
throw new ExecutionException(new RuntimeException(String.format(
"%1$s is after %2$s", object.getMetadata().getLastModified(),
unmodifiedSince)));
}
S3Object returnVal = new S3Object(copy(object.getMetadata()), object.getData());
if (options.getRange() != null) {

View File

@ -40,15 +40,16 @@ public class BucketExistsIntegrationTest extends S3IntegrationTest {
@Test
void bucketDoesntExist() throws Exception {
String bucketName = bucketPrefix + "be";
assert !client.bucketExists(bucketName).get(10, TimeUnit.SECONDS);
assert !client.bucketExists("be").get(10, TimeUnit.SECONDS);
}
@Test
void bucketExists() throws Exception {
String bucketName = bucketPrefix + "bde";
assert client.putBucketIfNotExists(bucketName).get(10, TimeUnit.SECONDS);
assert client.bucketExists(bucketName).get(10, TimeUnit.SECONDS);
String bucketName = getBucketName();
try {
assert client.bucketExists(bucketName).get(10, TimeUnit.SECONDS);
} finally {
returnBucket(bucketName);
}
}
}

View File

@ -59,17 +59,21 @@ public class CopyObjectIntegrationTest extends S3IntegrationTest {
@Test(groups = { "integration", "live" })
void testCopyObject() throws Exception {
String bucketName = getBucketName();
String destinationBucket = getScratchBucketName();
String destinationBucket = bucketName + "dest";
try {
addToBucketAndValidate(bucketName, sourceKey);
addToBucketAndValidate(bucketName, sourceKey);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey).get(10,
TimeUnit.SECONDS);
createBucketAndEnsureEmpty(destinationBucket);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey).get(10,
TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
validateContent(destinationBucket, destinationKey);
} finally {
returnBucket(bucketName);
returnScratchBucket(destinationBucket);
}
}
private void addToBucketAndValidate(String bucketName, String sourceKey)
@ -82,24 +86,28 @@ public class CopyObjectIntegrationTest extends S3IntegrationTest {
// TODO: fails on linux and windows
void testCopyIfModifiedSince() throws InterruptedException, ExecutionException,
TimeoutException, IOException {
String destinationBucket = bucketName + "dest";
DateTime before = new DateTime();
addToBucketAndValidate(bucketName, sourceKey);
DateTime after = new DateTime().plusSeconds(1);
createBucketAndEnsureEmpty(destinationBucket);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceModifiedSince(before)).get(10, TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
String bucketName = getBucketName();
String destinationBucket = getScratchBucketName();
try {
DateTime before = new DateTime();
addToBucketAndValidate(bucketName, sourceKey);
DateTime after = new DateTime().plusSeconds(1);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceModifiedSince(after)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
ifSourceModifiedSince(before)).get(10, TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
try {
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceModifiedSince(after)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
}
} finally {
returnBucket(bucketName);
returnScratchBucket(destinationBucket);
}
}
@ -107,92 +115,106 @@ public class CopyObjectIntegrationTest extends S3IntegrationTest {
// TODO: fails on linux and windows
void testCopyIfUnmodifiedSince() throws InterruptedException, ExecutionException,
TimeoutException, IOException {
String destinationBucket = bucketName + "dest";
DateTime before = new DateTime();
addToBucketAndValidate(bucketName, sourceKey);
DateTime after = new DateTime().plusSeconds(1);
createBucketAndEnsureEmpty(destinationBucket);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceUnmodifiedSince(after)).get(10, TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
String bucketName = getBucketName();
String destinationBucket = getScratchBucketName();
try {
DateTime before = new DateTime();
addToBucketAndValidate(bucketName, sourceKey);
DateTime after = new DateTime().plusSeconds(1);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceModifiedSince(before)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
ifSourceUnmodifiedSince(after)).get(10, TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
try {
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceModifiedSince(before)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
}
} finally {
returnBucket(bucketName);
returnScratchBucket(destinationBucket);
}
}
@Test(groups = { "integration", "live" })
void testCopyIfMatch() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String destinationBucket = bucketName + "dest";
addToBucketAndValidate(bucketName, sourceKey);
createBucketAndEnsureEmpty(destinationBucket);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceMd5Matches(goodMd5)).get(10, TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
String bucketName = getBucketName();
String destinationBucket = getScratchBucketName();
try {
addToBucketAndValidate(bucketName, sourceKey);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceMd5Matches(badMd5)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
ifSourceMd5Matches(goodMd5)).get(10, TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
try {
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceMd5Matches(badMd5)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
}
} finally {
returnBucket(bucketName);
returnScratchBucket(destinationBucket);
}
}
@Test(groups = { "integration", "live" })
void testCopyIfNoneMatch() throws IOException, InterruptedException, ExecutionException,
TimeoutException {
String destinationBucket = bucketName + "dest";
addToBucketAndValidate(bucketName, sourceKey);
createBucketAndEnsureEmpty(destinationBucket);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceMd5DoesntMatch(badMd5)).get(10, TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
String bucketName = getBucketName();
String destinationBucket = getScratchBucketName();
try {
addToBucketAndValidate(bucketName, sourceKey);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceMd5DoesntMatch(goodMd5)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
ifSourceMd5DoesntMatch(badMd5)).get(10, TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
try {
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
ifSourceMd5DoesntMatch(goodMd5)).get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
}
} finally {
returnBucket(bucketName);
returnScratchBucket(destinationBucket);
}
}
@Test(groups = { "integration", "live" })
void testCopyWithMetadata() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String bucketName = getBucketName();
String destinationBucket = getScratchBucketName();
try {
addToBucketAndValidate(bucketName, sourceKey);
String destinationBucket = bucketName + "dest";
Multimap<String, String> metadata = HashMultimap.create();
metadata.put(S3Headers.USER_METADATA_PREFIX + "adrian", "cole");
addToBucketAndValidate(bucketName, sourceKey);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
overrideMetadataWith(metadata)).get(10, TimeUnit.SECONDS);
Multimap<String, String> metadata = HashMultimap.create();
metadata.put(S3Headers.USER_METADATA_PREFIX + "adrian", "cole");
validateContent(destinationBucket, destinationKey);
createBucketAndEnsureEmpty(destinationBucket);
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
overrideMetadataWith(metadata)).get(10, TimeUnit.SECONDS);
S3Object.Metadata objectMeta = client.headObject(destinationBucket, destinationKey).get(
10, TimeUnit.SECONDS);
validateContent(destinationBucket, destinationKey);
assertEquals(objectMeta.getUserMetadata(), metadata);
} finally {
returnBucket(bucketName);
returnScratchBucket(destinationBucket);
S3Object.Metadata objectMeta = client.headObject(destinationBucket, destinationKey).get(10,
TimeUnit.SECONDS);
assertEquals(objectMeta.getUserMetadata(), metadata);
}
}
}

View File

@ -35,34 +35,37 @@ import java.util.concurrent.TimeUnit;
/**
* Tests integrated functionality of all copyObject commands.
* <p/>
* Each test uses a different bucket name, so it should be perfectly fine to run
* in parallel.
*
* Each test uses a different bucket name, so it should be perfectly fine to run in parallel.
*
* @author Adrian Cole
*/
@Test(testName = "s3.CopyObjectLiveTest")
public class CopyObjectLiveTest extends S3IntegrationTest {
String sourceKey = "apples";
String destinationKey = "pears";
String sourceKey = "apples";
String destinationKey = "pears";
@Test(groups = "live")
void testCannedAccessPolicyPublic() throws Exception {
String bucketName = getBucketName();
String destinationBucket = getScratchBucketName();
try {
addObjectToBucket(bucketName, sourceKey);
validateContent(bucketName, sourceKey);
@Test(groups = "live")
void testCannedAccessPolicyPublic() throws Exception {
String destinationBucket = bucketName + "dest";
client.copyObject(bucketName, sourceKey, destinationBucket, destinationKey,
overrideAcl(CannedAccessPolicy.PUBLIC_READ)).get(10, TimeUnit.SECONDS);
addObjectToBucket(bucketName, sourceKey);
validateContent(bucketName, sourceKey);
validateContent(destinationBucket, destinationKey);
createBucketAndEnsureEmpty(destinationBucket);
client.copyObject(bucketName, sourceKey, destinationBucket,
destinationKey, overrideAcl(CannedAccessPolicy.PUBLIC_READ)).get(10, TimeUnit.SECONDS);
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com/%2$s", destinationBucket,
destinationKey));
S3Utils.toStringAndClose(url.openStream());
validateContent(destinationBucket, destinationKey);
} finally {
returnBucket(bucketName);
returnScratchBucket(destinationBucket);
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com/%2$s",
destinationBucket, destinationKey));
S3Utils.toStringAndClose(url.openStream());
}
}
}
}

View File

@ -31,32 +31,40 @@ import java.util.concurrent.TimeUnit;
/**
* Tests integrated functionality of all deleteBucket commands.
* <p/>
* Each test uses a different bucket name, so it should be perfectly fine to run
* in parallel.
*
* Each test uses a different bucket name, so it should be perfectly fine to run in parallel.
*
* @author Adrian Cole
*/
@Test(groups = {"integration", "live"}, testName = "s3.DeleteBucketIntegrationTest")
@Test(groups = { "integration", "live" }, testName = "s3.DeleteBucketIntegrationTest")
public class DeleteBucketIntegrationTest extends S3IntegrationTest {
@Test
/**
* this method overrides bucketName to ensure it isn't found
*/
void deleteBucketIfEmptyNotFound() throws Exception {
String bucketName = bucketPrefix + "dbienf";
assert client.deleteBucketIfEmpty(bucketName).get(10, TimeUnit.SECONDS);
}
/**
* this method overrides bucketName to ensure it isn't found
*/
@Test
void deleteBucketIfEmptyNotFound() throws Exception {
assert client.deleteBucketIfEmpty("dbienf").get(10, TimeUnit.SECONDS);
}
@Test()
void deleteBucketIfEmptyButHasContents() throws Exception {
addObjectToBucket(bucketName, "test");
assert !client.deleteBucketIfEmpty(bucketName).get(10, TimeUnit.SECONDS);
}
@Test
void deleteBucketIfEmptyButHasContents() throws Exception {
String bucketName = getBucketName();
try {
addObjectToBucket(bucketName, "test");
assert !client.deleteBucketIfEmpty(bucketName).get(10, TimeUnit.SECONDS);
} finally {
returnBucket(bucketName);
}
}
@Test()
void deleteBucketIfEmpty() throws Exception {
assert client.deleteBucketIfEmpty(bucketName).get(10, TimeUnit.SECONDS);
assert !client.bucketExists(bucketName).get(10, TimeUnit.SECONDS);
}
@Test
void deleteBucketIfEmpty() throws Exception {
String bucketName = getBucketName();
try {
assert client.deleteBucketIfEmpty(bucketName).get(10, TimeUnit.SECONDS);
assert !client.bucketExists(bucketName).get(10, TimeUnit.SECONDS);
} finally {
returnBucket(bucketName);
}
}
}

View File

@ -43,32 +43,37 @@ import org.testng.annotations.Test;
@Test(groups = { "integration", "live" }, testName = "s3.DeleteObjectIntegrationTest")
public class DeleteObjectIntegrationTest extends S3IntegrationTest {
@Test()
@Test
void deleteObjectNotFound() throws Exception {
String bucketName = bucketPrefix + "donf";
createBucketAndEnsureEmpty(bucketName);
addObjectToBucket(bucketName, "test");
assert client.deleteObject(bucketName, "test").get(10, TimeUnit.SECONDS);
String bucketName = getBucketName();
try {
addObjectToBucket(bucketName, "test");
assert client.deleteObject(bucketName, "test").get(10, TimeUnit.SECONDS);
} finally {
returnBucket(bucketName);
}
}
@Test
void deleteObjectNoBucket() throws Exception {
String bucketName = bucketPrefix + "donb";
try {
client.deleteObject(bucketName, "test").get(10, TimeUnit.SECONDS);
client.deleteObject("donb", "test").get(10, TimeUnit.SECONDS);
} catch (ExecutionException e) {
assert e.getCause() instanceof AWSResponseException;
assertEquals(((AWSResponseException) e.getCause()).getResponse().getStatusCode(), 404);
}
}
@Test()
@Test
void deleteObject() throws Exception {
String bucketName = bucketPrefix + "do";
createBucketAndEnsureEmpty(bucketName);
addObjectToBucket(bucketName, "test");
assert client.deleteObject(bucketName, "test").get(10, TimeUnit.SECONDS);
assert client.headObject(bucketName, "test").get(10, TimeUnit.SECONDS) == S3Object.Metadata.NOT_FOUND;
String bucketName = getBucketName();
try {
addObjectToBucket(bucketName, "test");
assert client.deleteObject(bucketName, "test").get(10, TimeUnit.SECONDS);
assert client.headObject(bucketName, "test").get(10, TimeUnit.SECONDS) == S3Object.Metadata.NOT_FOUND;
} finally {
returnBucket(bucketName);
}
}
}

View File

@ -23,6 +23,10 @@
*/
package org.jclouds.aws.s3.commands;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
@ -37,88 +41,102 @@ import org.jclouds.aws.s3.domain.AccessControlList.Permission;
import org.jclouds.aws.s3.domain.acl.CannedAccessPolicy;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
/**
* Tests integrated functionality of all commands that retrieve Access Control Lists (ACLs).
*
*
* @author James Murty
* @author Adrian Cole
*/
@Test(groups = {"integration", "live"}, testName = "s3.GetAccessControlListIntegrationTest")
@Test(groups = { "integration", "live" }, testName = "s3.GetAccessControlListIntegrationTest")
public class GetAccessControlListIntegrationTest extends S3IntegrationTest {
@Test
void testPrivateBucketACL() throws InterruptedException, ExecutionException,
TimeoutException, IOException
{
bucketName = bucketPrefix + ".testPrivateBucketACL".toLowerCase();
createBucketAndEnsureEmpty(bucketName);
AccessControlList acl = client.getBucketACL(bucketName).get(10, TimeUnit.SECONDS);
assertEquals(acl.getGrants().size(), 1);
assertTrue(acl.getOwner() != null);
String ownerId = acl.getOwner().getId();
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
void testPrivateAclIsDefaultForBucket() throws InterruptedException, ExecutionException,
TimeoutException, IOException {
String bucketName = getBucketName();
try {
AccessControlList acl = client.getBucketACL(bucketName).get(10, TimeUnit.SECONDS);
assertEquals(acl.getGrants().size(), 1);
assertTrue(acl.getOwner() != null);
String ownerId = acl.getOwner().getId();
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
} finally {
returnBucket(bucketName);
}
}
@Test
void testObjectACL() throws InterruptedException, ExecutionException,
TimeoutException, IOException
{
bucketName = bucketPrefix + ".testObjectACL".toLowerCase();
createBucketAndEnsureEmpty(bucketName);
void testPrivateAclIsDefaultForObject() throws InterruptedException, ExecutionException,
TimeoutException, IOException {
String privateObjectKey = "pr“vate-acl";
String publicReadObjectKey = "pŸblic-read-acl";
String publicReadWriteObjectKey = "pŸblic-read-write-acl";
// Private object
addObjectToBucket(bucketName, privateObjectKey);
AccessControlList acl = client.getObjectACL(bucketName, privateObjectKey)
.get(10, TimeUnit.SECONDS);
assertEquals(acl.getGrants().size(), 1);
assertTrue(acl.getOwner() != null);
String ownerId = acl.getOwner().getId();
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
String bucketName = getBucketName();
try {
// Private object
addObjectToBucket(bucketName, privateObjectKey);
AccessControlList acl = client.getObjectACL(bucketName, privateObjectKey).get(10,
TimeUnit.SECONDS);
// Public Read object
client.putObject(bucketName, new S3Object(publicReadObjectKey, ""),
new PutObjectOptions().withAcl(CannedAccessPolicy.PUBLIC_READ));
acl = client.getObjectACL(bucketName, publicReadObjectKey)
.get(10, TimeUnit.SECONDS);
assertEquals(acl.getGrants().size(), 2);
assertEquals(acl.getPermissions(GroupGranteeURI.ALL_USERS).size(), 1);
assertTrue(acl.getOwner() != null);
ownerId = acl.getOwner().getId();
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ));
assertEquals(acl.getGrants().size(), 1);
assertTrue(acl.getOwner() != null);
String ownerId = acl.getOwner().getId();
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
} finally {
returnBucket(bucketName);
}
// Public Read-Write object
client.putObject(bucketName, new S3Object(publicReadWriteObjectKey, ""),
new PutObjectOptions().withAcl(CannedAccessPolicy.PUBLIC_READ_WRITE));
acl = client.getObjectACL(bucketName, publicReadWriteObjectKey)
.get(10, TimeUnit.SECONDS);
assertEquals(acl.getGrants().size(), 3);
assertEquals(acl.getPermissions(GroupGranteeURI.ALL_USERS).size(), 2);
assertTrue(acl.getOwner() != null);
ownerId = acl.getOwner().getId();
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ));
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.WRITE));
assertFalse(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ_ACP));
assertFalse(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.WRITE_ACP));
assertFalse(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.FULL_CONTROL));
emptyBucket(bucketName);
}
@Test
void testPublicReadOnObject() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String publicReadObjectKey = "pŸblic-read-acl";
String bucketName = getBucketName();
try {
client.putObject(bucketName, new S3Object(publicReadObjectKey, ""), new PutObjectOptions()
.withAcl(CannedAccessPolicy.PUBLIC_READ));
AccessControlList acl = client.getObjectACL(bucketName, publicReadObjectKey).get(10,
TimeUnit.SECONDS);
assertEquals(acl.getGrants().size(), 2);
assertEquals(acl.getPermissions(GroupGranteeURI.ALL_USERS).size(), 1);
assertTrue(acl.getOwner() != null);
String ownerId = acl.getOwner().getId();
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ));
} finally {
returnBucket(bucketName);
}
}
@Test
void testPublicWriteOnObject() throws InterruptedException, ExecutionException,
TimeoutException, IOException {
String publicReadWriteObjectKey = "pŸblic-read-write-acl";
String bucketName = getBucketName();
try {
// Public Read-Write object
client.putObject(bucketName, new S3Object(publicReadWriteObjectKey, ""),
new PutObjectOptions().withAcl(CannedAccessPolicy.PUBLIC_READ_WRITE));
AccessControlList acl = client.getObjectACL(bucketName, publicReadWriteObjectKey).get(10,
TimeUnit.SECONDS);
assertEquals(acl.getGrants().size(), 3);
assertEquals(acl.getPermissions(GroupGranteeURI.ALL_USERS).size(), 2);
assertTrue(acl.getOwner() != null);
String ownerId = acl.getOwner().getId();
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ));
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.WRITE));
assertFalse(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ_ACP));
assertFalse(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.WRITE_ACP));
assertFalse(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.FULL_CONTROL));
} finally {
returnBucket(bucketName);
}
}
}

View File

@ -54,166 +54,207 @@ import org.testng.annotations.Test;
@Test(groups = { "integration", "live" }, testName = "s3.GetObjectIntegrationTest")
public class GetObjectIntegrationTest extends S3IntegrationTest {
@Test(enabled = false)
// TODO: fails on linux and windows
@Test
void testGetIfModifiedSince() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String key = "apples";
DateTime before = new DateTime();
addObjectAndValidateContent(bucketName, key);
DateTime after = new DateTime().plusSeconds(1);
client.getObject(bucketName, key, ifModifiedSince(before)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
String bucketName = getBucketName();
try {
client.getObject(bucketName, key, ifModifiedSince(after)).get(10, TimeUnit.SECONDS);
String key = "apples";
DateTime before = new DateTime();
addObjectAndValidateContent(bucketName, key);
DateTime after = new DateTime().plusSeconds(1);
client.getObject(bucketName, key, ifModifiedSince(before)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
} catch (ExecutionException e) {
if (e.getCause() instanceof HttpResponseException) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 304);
} else {
throw e;
}
}
}
@Test(enabled = false)
// TODO: fails on linux and windows
void testGetIfUnmodifiedSince() throws InterruptedException, ExecutionException,
TimeoutException, IOException {
String key = "apples";
DateTime before = new DateTime();
addObjectAndValidateContent(bucketName, key);
DateTime after = new DateTime().plusSeconds(1);
client.getObject(bucketName, key, ifUnmodifiedSince(after)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
try {
client.getObject(bucketName, key, ifUnmodifiedSince(before)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
} catch (ExecutionException e) {
if (e.getCause() instanceof HttpResponseException) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
} else {
throw e;
try {
client.getObject(bucketName, key, ifModifiedSince(after)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
} catch (ExecutionException e) {
if (e.getCause() instanceof HttpResponseException) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 304);
} else if (e.getCause() instanceof RuntimeException) {
// TODO enhance stub connection so that it throws the correct error
} else {
throw e;
}
}
} finally {
returnBucket(bucketName);
}
}
@Test
void testGetIfUnmodifiedSince() throws InterruptedException, ExecutionException,
TimeoutException, IOException {
String bucketName = getBucketName();
try {
String key = "apples";
DateTime before = new DateTime();
addObjectAndValidateContent(bucketName, key);
DateTime after = new DateTime().plusSeconds(1);
client.getObject(bucketName, key, ifUnmodifiedSince(after)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
try {
client.getObject(bucketName, key, ifUnmodifiedSince(before)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
} catch (ExecutionException e) {
if (e.getCause() instanceof HttpResponseException) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
} else if (e.getCause() instanceof RuntimeException) {
// TODO enhance stub connection so that it throws the correct error
} else {
throw e;
}
}
} finally {
returnBucket(bucketName);
}
}
@Test
void testGetIfMatch() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String key = "apples";
addObjectAndValidateContent(bucketName, key);
client.getObject(bucketName, key, ifMd5Matches(goodMd5)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
String bucketName = getBucketName();
try {
client.getObject(bucketName, key, ifMd5Matches(badMd5)).get(10, TimeUnit.SECONDS);
String key = "apples";
addObjectAndValidateContent(bucketName, key);
client.getObject(bucketName, key, ifMd5Matches(goodMd5)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
} catch (ExecutionException e) {
if (e.getCause() instanceof HttpResponseException) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
} else {
throw e;
try {
client.getObject(bucketName, key, ifMd5Matches(badMd5)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
} catch (ExecutionException e) {
if (e.getCause() instanceof HttpResponseException) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 412);
} else if (e.getCause() instanceof RuntimeException) {
// TODO enhance stub connection so that it throws the correct error
} else {
throw e;
}
}
} finally {
returnBucket(bucketName);
}
}
@Test
void testGetIfNoneMatch() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String key = "apples";
addObjectAndValidateContent(bucketName, key);
client.getObject(bucketName, key, ifMd5DoesntMatch(badMd5)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
String bucketName = getBucketName();
try {
client.getObject(bucketName, key, ifMd5DoesntMatch(goodMd5)).get(10, TimeUnit.SECONDS);
String key = "apples";
addObjectAndValidateContent(bucketName, key);
client.getObject(bucketName, key, ifMd5DoesntMatch(badMd5)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
} catch (ExecutionException e) {
if (e.getCause() instanceof HttpResponseException) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 304);
} else {
throw e;
try {
client.getObject(bucketName, key, ifMd5DoesntMatch(goodMd5)).get(10, TimeUnit.SECONDS);
validateContent(bucketName, key);
} catch (ExecutionException e) {
if (e.getCause() instanceof HttpResponseException) {
HttpResponseException ex = (HttpResponseException) e.getCause();
assertEquals(ex.getResponse().getStatusCode(), 304);
} else {
throw e;
}
}
} finally {
returnBucket(bucketName);
}
}
@Test
void testGetRange() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String bucketName = getBucketName();
try {
String key = "apples";
String key = "apples";
addObjectAndValidateContent(bucketName, key);
S3Object object1 = client.getObject(bucketName, key, range(0, 5)).get(10, TimeUnit.SECONDS);
assertEquals(S3Utils.getContentAsStringAndClose(object1), TEST_STRING.substring(0, 6));
addObjectAndValidateContent(bucketName, key);
S3Object object1 = client.getObject(bucketName, key, range(0, 5))
.get(10, TimeUnit.SECONDS);
assertEquals(S3Utils.getContentAsStringAndClose(object1), TEST_STRING.substring(0, 6));
S3Object object2 = client.getObject(bucketName, key, range(6, TEST_STRING.length())).get(10,
TimeUnit.SECONDS);
assertEquals(S3Utils.getContentAsStringAndClose(object2), TEST_STRING.substring(6,
TEST_STRING.length()));
S3Object object2 = client.getObject(bucketName, key, range(6, TEST_STRING.length())).get(
10, TimeUnit.SECONDS);
assertEquals(S3Utils.getContentAsStringAndClose(object2), TEST_STRING.substring(6,
TEST_STRING.length()));
} finally {
returnBucket(bucketName);
}
}
@Test
void testGetTwoRanges() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String bucketName = getBucketName();
try {
String key = "apples";
String key = "apples";
addObjectAndValidateContent(bucketName, key);
S3Object object = client.getObject(bucketName, key,
range(0, 5).range(6, TEST_STRING.length())).get(10, TimeUnit.SECONDS);
addObjectAndValidateContent(bucketName, key);
S3Object object = client.getObject(bucketName, key,
range(0, 5).range(6, TEST_STRING.length())).get(10, TimeUnit.SECONDS);
assertEquals(S3Utils.getContentAsStringAndClose(object), TEST_STRING);
assertEquals(S3Utils.getContentAsStringAndClose(object), TEST_STRING);
} finally {
returnBucket(bucketName);
}
}
@Test
void testGetTail() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String bucketName = getBucketName();
try {
String key = "apples";
addObjectAndValidateContent(bucketName, key);
S3Object object = client.getObject(bucketName, key, tail(5)).get(10, TimeUnit.SECONDS);
assertEquals(S3Utils.getContentAsStringAndClose(object), TEST_STRING.substring(TEST_STRING
.length() - 5));
assertEquals(object.getContentLength(), 5);
assertEquals(object.getMetadata().getSize(), TEST_STRING.length());
String key = "apples";
addObjectAndValidateContent(bucketName, key);
S3Object object = client.getObject(bucketName, key, tail(5)).get(10, TimeUnit.SECONDS);
assertEquals(S3Utils.getContentAsStringAndClose(object), TEST_STRING.substring(TEST_STRING
.length() - 5));
assertEquals(object.getContentLength(), 5);
assertEquals(object.getMetadata().getSize(), TEST_STRING.length());
} finally {
returnBucket(bucketName);
}
}
@Test
void testGetStartAt() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String bucketName = getBucketName();
try {
String key = "apples";
String key = "apples";
addObjectAndValidateContent(bucketName, key);
S3Object object = client.getObject(bucketName, key, startAt(5)).get(10, TimeUnit.SECONDS);
assertEquals(S3Utils.getContentAsStringAndClose(object), TEST_STRING.substring(5, TEST_STRING
.length()));
assertEquals(object.getContentLength(), TEST_STRING.length() - 5);
assertEquals(object.getMetadata().getSize(), TEST_STRING.length());
addObjectAndValidateContent(bucketName, key);
S3Object object = client.getObject(bucketName, key, startAt(5)).get(10, TimeUnit.SECONDS);
assertEquals(S3Utils.getContentAsStringAndClose(object), TEST_STRING.substring(5,
TEST_STRING.length()));
assertEquals(object.getContentLength(), TEST_STRING.length() - 5);
assertEquals(object.getMetadata().getSize(), TEST_STRING.length());
} finally {
returnBucket(bucketName);
}
}
private void addObjectAndValidateContent(String sourcebucketName, String sourceKey)

View File

@ -38,97 +38,118 @@ import java.util.concurrent.TimeoutException;
/**
* Tests integrated functionality of all getBucket commands.
* <p/>
* Each test uses a different bucket name, so it should be perfectly fine to run
* in parallel.
*
* Each test uses a different bucket name, so it should be perfectly fine to run in parallel.
*
* @author Adrian Cole
*/
@Test(groups = {"integration", "live"}, testName = "s3.ListBucketIntegrationTest")
@Test(groups = { "integration", "live" }, testName = "s3.ListBucketIntegrationTest")
public class ListBucketIntegrationTest extends S3IntegrationTest {
@Test()
void testListBucketDelimiter() throws InterruptedException,
ExecutionException, TimeoutException, UnsupportedEncodingException {
String prefix = "apps";
addTenObjectsUnderPrefix(bucketName, prefix);
add15UnderRoot(bucketName);
S3Bucket bucket = client.listBucket(bucketName, delimiter("/")).get(10,
TimeUnit.SECONDS);
assertEquals(bucket.getDelimiter(), "/");
assert !bucket.isTruncated();
assertEquals(bucket.getContents().size(), 15);
assertEquals(bucket.getCommonPrefixes().size(), 1);
}
@Test()
void testListBucketDelimiter() throws InterruptedException, ExecutionException,
TimeoutException, UnsupportedEncodingException {
String bucketName = getBucketName();
try {
String prefix = "apps";
addTenObjectsUnderPrefix(bucketName, prefix);
add15UnderRoot(bucketName);
S3Bucket bucket = client.listBucket(bucketName, delimiter("/")).get(10, TimeUnit.SECONDS);
assertEquals(bucket.getDelimiter(), "/");
assert !bucket.isTruncated();
assertEquals(bucket.getContents().size(), 15);
assertEquals(bucket.getCommonPrefixes().size(), 1);
} finally {
returnBucket(bucketName);
}
private void addAlphabetUnderRoot(String bucketName)
throws InterruptedException, ExecutionException, TimeoutException {
for (char letter = 'a'; letter <= 'z'; letter++) {
client.putObject(bucketName,
new S3Object(letter + "", letter + "content")).get(10,
TimeUnit.SECONDS);
}
}
}
@Test
void testListBucketMarker() throws InterruptedException,
ExecutionException, TimeoutException, UnsupportedEncodingException {
addAlphabetUnderRoot(bucketName);
S3Bucket bucket = client.listBucket(bucketName, afterMarker("y")).get(
10, TimeUnit.SECONDS);
assertEquals(bucket.getMarker(), "y");
assert !bucket.isTruncated();
assertEquals(bucket.getContents().size(), 1);
}
@Test
void testListBucketMaxResults() throws InterruptedException,
ExecutionException, TimeoutException, UnsupportedEncodingException {
addAlphabetUnderRoot(bucketName);
S3Bucket bucket = client.listBucket(bucketName, maxResults(5)).get(10,
TimeUnit.SECONDS);
assertEquals(bucket.getMaxKeys(), 5);
assert bucket.isTruncated();
assertEquals(bucket.getContents().size(), 5);
}
@Test()
void testListBucketPrefix() throws InterruptedException,
ExecutionException, TimeoutException, UnsupportedEncodingException {
String prefix = "apps";
addTenObjectsUnderPrefix(bucketName, prefix);
add15UnderRoot(bucketName);
S3Bucket bucket = client.listBucket(bucketName, withPrefix("apps/"))
.get(10, TimeUnit.SECONDS);
assert !bucket.isTruncated();
assertEquals(bucket.getContents().size(), 10);
assertEquals(bucket.getPrefix(), "apps/");
}
@Test()
void testListBucket() throws InterruptedException,
ExecutionException, TimeoutException, UnsupportedEncodingException {
String prefix = "apps";
addTenObjectsUnderPrefix(bucketName, prefix);
S3Bucket bucket = client.listBucket(bucketName)
.get(10, TimeUnit.SECONDS);
assertEquals(bucket.getContents().size(), 10);
}
private void add15UnderRoot(String bucketName) throws InterruptedException,
private void addAlphabetUnderRoot(String bucketName) throws InterruptedException,
ExecutionException, TimeoutException {
for (int i = 0; i < 15; i++)
client.putObject(bucketName, new S3Object(i + "", i + "content"))
.get(10, TimeUnit.SECONDS);
}
for (char letter = 'a'; letter <= 'z'; letter++) {
client.putObject(bucketName, new S3Object(letter + "", letter + "content")).get(10,
TimeUnit.SECONDS);
}
}
private void addTenObjectsUnderPrefix(String bucketName, String prefix)
@Test
void testListBucketMarker() throws InterruptedException, ExecutionException, TimeoutException,
UnsupportedEncodingException {
String bucketName = getBucketName();
try {
addAlphabetUnderRoot(bucketName);
S3Bucket bucket = client.listBucket(bucketName, afterMarker("y"))
.get(10, TimeUnit.SECONDS);
assertEquals(bucket.getMarker(), "y");
assert !bucket.isTruncated();
assertEquals(bucket.getContents().size(), 1);
} finally {
returnBucket(bucketName);
}
}
@Test
void testListBucketMaxResults() throws InterruptedException, ExecutionException,
TimeoutException, UnsupportedEncodingException {
String bucketName = getBucketName();
try {
addAlphabetUnderRoot(bucketName);
S3Bucket bucket = client.listBucket(bucketName, maxResults(5)).get(10, TimeUnit.SECONDS);
assertEquals(bucket.getMaxKeys(), 5);
assert bucket.isTruncated();
assertEquals(bucket.getContents().size(), 5);
} finally {
returnBucket(bucketName);
}
}
@Test()
void testListBucketPrefix() throws InterruptedException, ExecutionException, TimeoutException,
UnsupportedEncodingException {
String bucketName = getBucketName();
try {
String prefix = "apps";
addTenObjectsUnderPrefix(bucketName, prefix);
add15UnderRoot(bucketName);
S3Bucket bucket = client.listBucket(bucketName, withPrefix("apps/")).get(10,
TimeUnit.SECONDS);
assert !bucket.isTruncated();
assertEquals(bucket.getContents().size(), 10);
assertEquals(bucket.getPrefix(), "apps/");
} finally {
returnBucket(bucketName);
}
}
@Test()
void testListBucket() throws InterruptedException, ExecutionException, TimeoutException,
UnsupportedEncodingException {
String bucketName = getBucketName();
try {
String prefix = "apps";
addTenObjectsUnderPrefix(bucketName, prefix);
S3Bucket bucket = client.listBucket(bucketName).get(10, TimeUnit.SECONDS);
assertEquals(bucket.getContents().size(), 10);
} finally {
returnBucket(bucketName);
}
}
private void add15UnderRoot(String bucketName) throws InterruptedException, ExecutionException,
TimeoutException {
for (int i = 0; i < 15; i++)
client.putObject(bucketName, new S3Object(i + "", i + "content"))
.get(10, TimeUnit.SECONDS);
}
private void addTenObjectsUnderPrefix(String bucketName, String prefix)
throws InterruptedException, ExecutionException, TimeoutException {
for (int i = 0; i < 10; i++)
client.putObject(bucketName,
new S3Object(prefix + "/" + i, i + "content")).get(10,
TimeUnit.SECONDS);
}
for (int i = 0; i < 10; i++)
client.putObject(bucketName, new S3Object(prefix + "/" + i, i + "content")).get(10,
TimeUnit.SECONDS);
}
}

View File

@ -23,43 +23,40 @@
*/
package org.jclouds.aws.s3.commands;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.jclouds.aws.s3.S3IntegrationTest;
import org.jclouds.aws.s3.domain.S3Bucket;
import org.testng.annotations.Test;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Tests integrated functionality of all listOwnedBucket commands.
* <p/>
* Each test uses a different bucket name, so it should be perfectly fine to run
* in parallel.
*
* Each test uses a different bucket name, so it should be perfectly fine to run in parallel.
*
* @author Adrian Cole
*/
@Test(groups = {"integration", "live"}, testName = "s3.ListOwnedBucketsIntegrationTest")
@Test(groups = { "integration", "live" }, testName = "s3.ListOwnedBucketsIntegrationTest")
public class ListOwnedBucketsIntegrationTest extends S3IntegrationTest {
@Test()
void bucketDoesntExist() throws Exception {
String bucketName = bucketPrefix + "shouldntexist";
List<S3Bucket.Metadata> list = client.listOwnedBuckets().get(10,
TimeUnit.SECONDS);
assert !list.contains(new S3Bucket(bucketName));
}
@Test()
void bucketDoesntExist() throws Exception {
List<S3Bucket.Metadata> list = client.listOwnedBuckets().get(10, TimeUnit.SECONDS);
assert !list.contains(new S3Bucket("shouldntexist"));
}
@Test()
void bucketExists() throws Exception {
String bucketName = bucketPrefix + "needstoexist";
assert client.putBucketIfNotExists(bucketName)
.get(10, TimeUnit.SECONDS);
List<S3Bucket.Metadata> list = client.listOwnedBuckets().get(10,
TimeUnit.SECONDS);
S3Bucket.Metadata firstBucket = list.get(0);
S3Bucket.Metadata toMatch = new S3Bucket.Metadata(bucketName);
toMatch.setOwner(firstBucket.getOwner());
assert list.contains(toMatch);
}
@Test()
void bucketExists() throws Exception {
String bucketName = getBucketName();
try {
List<S3Bucket.Metadata> list = client.listOwnedBuckets().get(10, TimeUnit.SECONDS);
S3Bucket.Metadata firstBucket = list.get(0);
S3Bucket.Metadata toMatch = new S3Bucket.Metadata(bucketName);
toMatch.setOwner(firstBucket.getOwner());
assert list.contains(toMatch);
} finally {
returnBucket(bucketName);
}
}
}

View File

@ -51,60 +51,73 @@ public class PutAccessControlListIntegrationTest extends S3IntegrationTest {
@Test
void testUpdateBucketACL() throws InterruptedException, ExecutionException, TimeoutException,
IOException, Exception {
// Confirm the bucket is private
AccessControlList acl = client.getBucketACL(bucketName).get(10, TimeUnit.SECONDS);
String ownerId = acl.getOwner().getId();
assertEquals(acl.getGrants().size(), 1);
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
String bucketName = getScratchBucketName();
try {
// Confirm the bucket is private
AccessControlList acl = client.getBucketACL(bucketName).get(10, TimeUnit.SECONDS);
String ownerId = acl.getOwner().getId();
assertEquals(acl.getGrants().size(), 1);
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
addGrantsToACL(acl);
assertEquals(acl.getGrants().size(), 4);
assertTrue(client.putBucketACL(bucketName, acl).get(10, TimeUnit.SECONDS));
addGrantsToACL(acl);
assertEquals(acl.getGrants().size(), 4);
assertTrue(client.putBucketACL(bucketName, acl).get(10, TimeUnit.SECONDS));
// Confirm that the updated ACL has stuck.
acl = client.getBucketACL(bucketName).get(10, TimeUnit.SECONDS);
checkGrants(acl);
} finally {
returnScratchBucket(bucketName);
}
// Confirm that the updated ACL has stuck.
acl = client.getBucketACL(bucketName).get(10, TimeUnit.SECONDS);
checkGrants(acl);
}
@Test
void testUpdateObjectACL() throws InterruptedException, ExecutionException, TimeoutException,
IOException {
String objectKey = "pr“vate-acl";
String bucketName = getBucketName();
try {
String objectKey = "pr“vate-acl";
// Private object
addObjectToBucket(bucketName, objectKey);
AccessControlList acl = client.getObjectACL(bucketName, objectKey).get(10, TimeUnit.SECONDS);
String ownerId = acl.getOwner().getId();
// Private object
addObjectToBucket(bucketName, objectKey);
AccessControlList acl = client.getObjectACL(bucketName, objectKey).get(10,
TimeUnit.SECONDS);
String ownerId = acl.getOwner().getId();
assertEquals(acl.getGrants().size(), 1);
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
assertEquals(acl.getGrants().size(), 1);
assertTrue(acl.hasPermission(ownerId, Permission.FULL_CONTROL));
addGrantsToACL(acl);
assertEquals(acl.getGrants().size(), 4);
assertTrue(client.putObjectACL(bucketName, objectKey, acl).get(10, TimeUnit.SECONDS));
addGrantsToACL(acl);
assertEquals(acl.getGrants().size(), 4);
assertTrue(client.putObjectACL(bucketName, objectKey, acl).get(10, TimeUnit.SECONDS));
// Confirm that the updated ACL has stuck.
acl = client.getObjectACL(bucketName, objectKey).get(10, TimeUnit.SECONDS);
checkGrants(acl);
// Confirm that the updated ACL has stuck.
acl = client.getObjectACL(bucketName, objectKey).get(10, TimeUnit.SECONDS);
checkGrants(acl);
/*
* Revoke all of owner's permissions!
*/
acl.revokeAllPermissions(new CanonicalUserGrantee(ownerId));
if (!ownerId.equals(TEST_ACL_ID))
acl.revokeAllPermissions(new CanonicalUserGrantee(TEST_ACL_ID));
assertEquals(acl.getGrants().size(), 1);
// Only public read permission should remain...
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ));
/*
* Revoke all of owner's permissions!
*/
acl.revokeAllPermissions(new CanonicalUserGrantee(ownerId));
if (!ownerId.equals(TEST_ACL_ID))
acl.revokeAllPermissions(new CanonicalUserGrantee(TEST_ACL_ID));
assertEquals(acl.getGrants().size(), 1);
// Only public read permission should remain...
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ));
// Update the object's ACL settings
assertTrue(client.putObjectACL(bucketName, objectKey, acl).get(10, TimeUnit.SECONDS));
// Update the object's ACL settings
assertTrue(client.putObjectACL(bucketName, objectKey, acl).get(10, TimeUnit.SECONDS));
// Confirm that the updated ACL has stuck
acl = client.getObjectACL(bucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(acl.getGrants().size(), 1);
assertEquals(acl.getPermissions(ownerId).size(), 0);
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ), acl.toString());
} finally {
returnBucket(bucketName);
}
// Confirm that the updated ACL has stuck
acl = client.getObjectACL(bucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(acl.getGrants().size(), 1);
assertEquals(acl.getPermissions(ownerId).size(), 0);
assertTrue(acl.hasPermission(GroupGranteeURI.ALL_USERS, Permission.READ), acl.toString());
}
private void checkGrants(AccessControlList acl) {

View File

@ -45,41 +45,57 @@ import java.util.concurrent.TimeUnit;
@Test(testName = "s3.PutBucketLiveTest")
public class PutBucketLiveTest extends S3IntegrationTest {
/**
* overriding bucketName as we are changing access permissions
*/
@Test(groups = { "live" })
void testPublicReadAccessPolicy() throws Exception {
String bucketName = bucketPrefix + "public";
client.putBucketIfNotExists(bucketName, withBucketAcl(CannedAccessPolicy.PUBLIC_READ)).get(
10, TimeUnit.SECONDS);
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com", bucketName));
S3Utils.toStringAndClose(url.openStream());
String bucketName = getScratchBucketName();
try {
deleteBucket(bucketName);
client.putBucketIfNotExists(bucketName, withBucketAcl(CannedAccessPolicy.PUBLIC_READ))
.get(10, TimeUnit.SECONDS);
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com", bucketName));
S3Utils.toStringAndClose(url.openStream());
} finally {
returnScratchBucket(bucketName);
}
}
@Test(groups = { "live" })
void testPutTwiceIsOk() throws Exception {
client.putBucketIfNotExists(bucketName).get(10, TimeUnit.SECONDS);
String bucketName = getBucketName();
try {
client.putBucketIfNotExists(bucketName).get(10, TimeUnit.SECONDS);
} finally {
returnBucket(bucketName);
}
}
@Test(expectedExceptions = IOException.class, groups = { "live" })
void testDefaultAccessPolicy() throws Exception {
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com", bucketName));
S3Utils.toStringAndClose(url.openStream());
String bucketName = getBucketName();
try {
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com", bucketName));
S3Utils.toStringAndClose(url.openStream());
} finally {
returnBucket(bucketName);
}
}
/**
* overriding bucketName as we are changing location
* using scratch bucketName as we are changing location
*/
@Test(groups = "live")
void testEu() throws Exception {
String bucketName = (bucketPrefix + "wow").toLowerCase();
client.putBucketIfNotExists(bucketName,
createIn(LocationConstraint.EU).withBucketAcl(CannedAccessPolicy.PUBLIC_READ)).get(
10, TimeUnit.SECONDS);
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com", bucketName));
S3Utils.toStringAndClose(url.openStream());
String bucketName = getScratchBucketName();
try {
deleteBucket(bucketName);
client.putBucketIfNotExists(bucketName,
createIn(LocationConstraint.EU).withBucketAcl(CannedAccessPolicy.PUBLIC_READ)).get(
10, TimeUnit.SECONDS);
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com", bucketName));
S3Utils.toStringAndClose(url.openStream());
} finally {
returnScratchBucket(bucketName);
}
}
}

View File

@ -61,27 +61,27 @@ public class PutObjectIntegrationTest extends S3IntegrationTest {
@Test(dataProvider = "putTests", groups = { "integration", "live" })
void testPutObject(String key, String type, Object content, Object realObject) throws Exception {
String bucketName = bucketPrefix + "tpo";
client.putBucketIfNotExists(bucketName).get(10, TimeUnit.SECONDS);
context.createS3ObjectMap(bucketName).clear();
assertEquals(client.listBucket(bucketName).get(10, TimeUnit.SECONDS).getContents().size(), 0);
S3Object object = new S3Object(key);
object.getMetadata().setContentType(type);
object.setData(content);
if (content instanceof InputStream) {
object.generateMd5();
}
assertNotNull(client.putObject(bucketName, object).get(10, TimeUnit.SECONDS));
object = client.getObject(bucketName, object.getKey()).get(10, TimeUnit.SECONDS);
String returnedString = S3Utils.getContentAsStringAndClose(object);
assertEquals(returnedString, realObject);
assertEquals(client.listBucket(bucketName).get(10, TimeUnit.SECONDS).getContents().size(), 1);
String bucketName = getBucketName();
try {
assertNotNull(client.putObject(bucketName, object).get(10, TimeUnit.SECONDS));
object = client.getObject(bucketName, object.getKey()).get(10, TimeUnit.SECONDS);
String returnedString = S3Utils.getContentAsStringAndClose(object);
assertEquals(returnedString, realObject);
assertEquals(client.listBucket(bucketName).get(10, TimeUnit.SECONDS).getContents().size(),
1);
} finally {
returnBucket(bucketName);
}
}
@Test(groups = { "integration", "live" })
void testMetadata() throws Exception {
String bucketName = bucketPrefix + "tmd";
createBucketAndEnsureEmpty(bucketName);
String key = "hello";
S3Object object = new S3Object(key, TEST_STRING);
@ -93,19 +93,23 @@ public class PutObjectIntegrationTest extends S3IntegrationTest {
object.getMetadata().getUserMetadata().put(S3Headers.USER_METADATA_PREFIX + "adrian",
"powderpuff");
object.getMetadata().setMd5(S3Utils.md5(TEST_STRING.getBytes()));
String bucketName = getBucketName();
try {
addObjectToBucket(bucketName, object);
S3Object newObject = validateContent(bucketName, key);
addObjectToBucket(bucketName, object);
S3Object newObject = validateContent(bucketName, key);
assertEquals(newObject.getMetadata().getContentType(), "text/plain");
assertEquals(newObject.getMetadata().getContentEncoding(), "x-compress");
assertEquals(newObject.getMetadata().getContentDisposition(),
"attachment; filename=hello.txt");
assertEquals(newObject.getMetadata().getCacheControl(), "no-cache");
assertEquals(newObject.getMetadata().getSize(), TEST_STRING.length());
assertEquals(newObject.getMetadata().getUserMetadata().values().iterator().next(),
"powderpuff");
assertEquals(newObject.getMetadata().getMd5(), S3Utils.md5(TEST_STRING.getBytes()));
assertEquals(newObject.getMetadata().getContentType(), "text/plain");
assertEquals(newObject.getMetadata().getContentEncoding(), "x-compress");
assertEquals(newObject.getMetadata().getContentDisposition(),
"attachment; filename=hello.txt");
assertEquals(newObject.getMetadata().getCacheControl(), "no-cache");
assertEquals(newObject.getMetadata().getSize(), TEST_STRING.length());
assertEquals(newObject.getMetadata().getUserMetadata().values().iterator().next(),
"powderpuff");
assertEquals(newObject.getMetadata().getMd5(), S3Utils.md5(TEST_STRING.getBytes()));
} finally {
returnBucket(bucketName);
}
}
}

View File

@ -23,47 +23,43 @@
*/
package org.jclouds.aws.s3.commands;
import org.jclouds.aws.s3.S3IntegrationTest;
import static org.jclouds.aws.s3.commands.options.PutBucketOptions.Builder.withBucketAcl;
import static org.jclouds.aws.s3.commands.options.PutObjectOptions.Builder.withAcl;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.jclouds.aws.s3.S3IntegrationTest;
import org.jclouds.aws.s3.domain.S3Object;
import org.jclouds.aws.s3.domain.acl.CannedAccessPolicy;
import org.jclouds.aws.s3.util.S3Utils;
import org.testng.annotations.Test;
import java.net.URL;
import java.util.concurrent.TimeUnit;
/**
* Tests integrated functionality of all PutObject commands.
* <p/>
* Each test uses a different bucket name, so it should be perfectly fine to run
* in parallel.
*
* Each test uses a different bucket name, so it should be perfectly fine to run in parallel.
*
* @author Adrian Cole
*/
@Test(testName = "s3.PutObjectLiveTest")
public class PutObjectLiveTest extends S3IntegrationTest {
@Test(groups = { "live" })
void testCannedAccessPolicyPublic() throws Exception {
String bucketName = getBucketName();
try {
String key = "hello";
@Test(groups = {"live"})
void testCannedAccessPolicyPublic() throws Exception {
String bucketName = bucketPrefix + "tcapp";
createBucketAndEnsureEmpty(bucketName);
String key = "hello";
client.putObject(bucketName, new S3Object(key, TEST_STRING),
client.putBucketIfNotExists(bucketName,
withBucketAcl(CannedAccessPolicy.PUBLIC_READ)).get(10,
TimeUnit.SECONDS);
client.putObject(bucketName, new S3Object(key, TEST_STRING),
withAcl(CannedAccessPolicy.PUBLIC_READ)).get(10, TimeUnit.SECONDS);
withAcl(CannedAccessPolicy.PUBLIC_READ)).get(10, TimeUnit.SECONDS);
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com/%2$s", bucketName, key));
S3Utils.toStringAndClose(url.openStream());
} finally {
returnBucket(bucketName);
}
URL url = new URL(String.format("http://%1$s.s3.amazonaws.com/%2$s",
bucketName, key));
S3Utils.toStringAndClose(url.openStream());
}
}
}

View File

@ -62,475 +62,450 @@ import com.google.common.collect.Iterators;
/**
* Tests to cover JCloudsS3Service
*
*
* @author James Murty
* @author Adrian Cole
*/
@Test(groups = {"integration", "live"}, testName = "s3.JCloudsS3ServiceIntegrationTest")
@Test(groups = { "integration", "live" }, testName = "s3.JCloudsS3ServiceIntegrationTest")
public class JCloudsS3ServiceIntegrationTest extends S3IntegrationTest {
AWSCredentials credentials;
S3Service service;
String commonTestingBucketName;
public JCloudsS3ServiceIntegrationTest() {
this.commonTestingBucketName = bucketPrefix + ".JCloudsS3ServiceIntegrationTest".toLowerCase();
}
AWSCredentials credentials;
S3Service service;
@Override
protected boolean debugEnabled() {
return true;
}
@Override
protected boolean debugEnabled() {
return true;
}
/**
* overridden only to get access to the amazon credentials used for jets3t
* initialization.
*/
@Override
protected void createLiveS3Context(String AWSAccessKeyId, String AWSSecretAccessKey) {
credentials = new AWSCredentials(AWSAccessKeyId, AWSSecretAccessKey);
super.createLiveS3Context(AWSAccessKeyId, AWSSecretAccessKey);
}
/**
* overridden only to get access to the amazon credentials used for jets3t initialization.
*/
@Override
protected void createLiveS3Context(String AWSAccessKeyId, String AWSSecretAccessKey) {
credentials = new AWSCredentials(AWSAccessKeyId, AWSSecretAccessKey);
super.createLiveS3Context(AWSAccessKeyId, AWSSecretAccessKey);
}
/**
* initialize a new JCloudsS3Service, but passing
* JavaUrlHttpFutureCommandClientModule(), as it is easier to debug in unit
* tests.
*
* @throws S3ServiceException
*/
@BeforeMethod
public void testJCloudsS3Service() throws S3ServiceException {
service = (credentials != null) ? new JCloudsS3Service(credentials) :
new JCloudsS3Service(new AWSCredentials("foo", "bar"), new StubS3ConnectionModule());
}
/**
* initialize a new JCloudsS3Service, but passing JavaUrlHttpFutureCommandClientModule(), as it
* is easier to debug in unit tests.
*
* @throws S3ServiceException
*/
@BeforeMethod
public void testJCloudsS3Service() throws S3ServiceException {
service = (credentials != null) ? new JCloudsS3Service(credentials) : new JCloudsS3Service(
new AWSCredentials("foo", "bar"), new StubS3ConnectionModule());
}
@Test
public void testCreateBucketImpl()
throws S3ServiceException, InterruptedException, ExecutionException
{
S3Bucket bucket = service.createBucket(new S3Bucket(commonTestingBucketName));
assertEquals(bucket.getName(), commonTestingBucketName);
assertTrue(client.bucketExists(commonTestingBucketName).get());
}
@Test
@AfterSuite
public void testDeleteBucketImpl() throws S3ServiceException,
InterruptedException, ExecutionException, TimeoutException
{
emptyBucket(commonTestingBucketName);
service.deleteBucket(commonTestingBucketName);
assertFalse(client.bucketExists(commonTestingBucketName).get(10, TimeUnit.SECONDS));
}
@Test
public void testCreateBucketImpl() throws S3ServiceException, InterruptedException,
ExecutionException {
S3Bucket bucket = service.createBucket(new S3Bucket(bucketName));
assertEquals(bucket.getName(), bucketName);
assertTrue(client.bucketExists(bucketName).get());
}
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testDeleteObjectImpl() throws InterruptedException,
ExecutionException, TimeoutException, S3ServiceException, IOException
{
String objectKey = "key-testDeleteObjectImpl";
String objectValue = "test";
@Test
@AfterSuite
public void testDeleteBucketImpl() throws S3ServiceException, InterruptedException,
ExecutionException, TimeoutException {
emptyBucket(bucketName);
org.jclouds.aws.s3.domain.S3Object s3Object =
new org.jclouds.aws.s3.domain.S3Object(objectKey, objectValue);
addObjectToBucket(commonTestingBucketName, s3Object);
service.deleteBucket(bucketName);
assertFalse(client.bucketExists(bucketName).get(10, TimeUnit.SECONDS));
}
service.deleteObject(commonTestingBucketName, objectKey);
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testDeleteObjectImpl() throws InterruptedException, ExecutionException,
TimeoutException, S3ServiceException, IOException {
String objectKey = "key-testDeleteObjectImpl";
String objectValue = "test";
assertEquals(
client.headObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS),
org.jclouds.aws.s3.domain.S3Object.Metadata.NOT_FOUND);
}
org.jclouds.aws.s3.domain.S3Object s3Object = new org.jclouds.aws.s3.domain.S3Object(
objectKey, objectValue);
addObjectToBucket(bucketName, s3Object);
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testGetObjectDetailsImpl()
throws InterruptedException, ExecutionException, TimeoutException,
S3ServiceException, IOException
{
String objectKey = "key-testGetObjectDetailsImpl";
String objectValue = "test";
String metadataName = "metadata-name-1";
String metadataValue = "metadata-value-1";
org.jclouds.aws.s3.domain.S3Object s3Object =
new org.jclouds.aws.s3.domain.S3Object(objectKey, objectValue);
s3Object.getMetadata().getUserMetadata().put(
S3Constants.USER_METADATA_PREFIX + metadataName, metadataValue);
addObjectToBucket(commonTestingBucketName, s3Object);
service.deleteObject(bucketName, objectKey);
S3Object objectDetails = service.getObjectDetails(
new S3Bucket(commonTestingBucketName), objectKey);
assertEquals(client.headObject(bucketName, objectKey).get(10, TimeUnit.SECONDS),
org.jclouds.aws.s3.domain.S3Object.Metadata.NOT_FOUND);
}
assertEquals(objectDetails.getKey(), objectKey);
assertEquals(objectDetails.getContentLength(), 4);
assertNull(objectDetails.getDataInputStream());
assertEquals(objectDetails.getMetadata(metadataName), metadataValue);
emptyBucket(commonTestingBucketName);
}
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testGetObjectDetailsImpl() throws InterruptedException, ExecutionException,
TimeoutException, S3ServiceException, IOException {
String objectKey = "key-testGetObjectDetailsImpl";
String objectValue = "test";
String metadataName = "metadata-name-1";
String metadataValue = "metadata-value-1";
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testGetObjectImpl()
throws InterruptedException, ExecutionException, TimeoutException, S3ServiceException, IOException
{
String objectKey = "key-testGetObjectImpl";
String objectValue = "test";
String metadataName = "metadata-name-2";
String metadataValue = "metadata-value-2";
org.jclouds.aws.s3.domain.S3Object s3Object = new org.jclouds.aws.s3.domain.S3Object(
objectKey, objectValue);
s3Object.getMetadata().getUserMetadata().put(S3Constants.USER_METADATA_PREFIX + metadataName,
metadataValue);
addObjectToBucket(bucketName, s3Object);
org.jclouds.aws.s3.domain.S3Object s3Object =
new org.jclouds.aws.s3.domain.S3Object(objectKey, objectValue);
s3Object.getMetadata().getUserMetadata().put(
S3Constants.USER_METADATA_PREFIX + metadataName, metadataValue);
addObjectToBucket(commonTestingBucketName, s3Object);
S3Object objectDetails = service.getObjectDetails(new S3Bucket(bucketName), objectKey);
S3Object object = service.getObject(new S3Bucket(commonTestingBucketName), objectKey);
assertEquals(object.getKey(), objectKey);
assertNotNull(object.getDataInputStream());
assertEquals(IOUtils.toString(object.getDataInputStream()), objectValue);
assertEquals(object.getContentLength(), objectValue.length());
assertEquals(object.getMetadata(metadataName), metadataValue);
// TODO: Test conditional gets
emptyBucket(commonTestingBucketName);
}
assertEquals(objectDetails.getKey(), objectKey);
assertEquals(objectDetails.getContentLength(), 4);
assertNull(objectDetails.getDataInputStream());
assertEquals(objectDetails.getMetadata(metadataName), metadataValue);
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testListAllBucketsImpl() throws InterruptedException,
ExecutionException, TimeoutException, S3ServiceException {
// Ensure there is at least 1 bucket in S3 account to list and compare.
S3Bucket[] jsBuckets = service.listAllBuckets();
emptyBucket(bucketName);
}
List<org.jclouds.aws.s3.domain.S3Bucket.Metadata> jcBuckets = client
.listOwnedBuckets().get(10, TimeUnit.SECONDS);
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testGetObjectImpl() throws InterruptedException, ExecutionException,
TimeoutException, S3ServiceException, IOException {
String objectKey = "key-testGetObjectImpl";
String objectValue = "test";
String metadataName = "metadata-name-2";
String metadataValue = "metadata-value-2";
assert jsBuckets.length == jcBuckets.size();
Iterator<org.jclouds.aws.s3.domain.S3Bucket.Metadata> jcBucketsIter = jcBuckets
.iterator();
for (S3Bucket jsBucket : jsBuckets) {
assert jcBucketsIter.hasNext();
org.jclouds.aws.s3.domain.S3Object s3Object = new org.jclouds.aws.s3.domain.S3Object(
objectKey, objectValue);
s3Object.getMetadata().getUserMetadata().put(S3Constants.USER_METADATA_PREFIX + metadataName,
metadataValue);
addObjectToBucket(bucketName, s3Object);
org.jclouds.aws.s3.domain.S3Bucket.Metadata jcBucket = jcBucketsIter
.next();
assert jsBucket.getName().equals(jcBucket.getName());
}
}
S3Object object = service.getObject(new S3Bucket(bucketName), objectKey);
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testListObjectsChunkedImpl() throws InterruptedException, ExecutionException,
TimeoutException, IOException, S3ServiceException
{
addObjectToBucket(commonTestingBucketName, "item1/subobject2");
addObjectToBucket(commonTestingBucketName, "item2");
addObjectToBucket(commonTestingBucketName, "object1");
addObjectToBucket(commonTestingBucketName, "object2/subobject1");
S3ObjectsChunk chunk;
// Normal complete listing
chunk = service.listObjectsChunked(commonTestingBucketName, null, null, 1000, null, true);
assertEquals(chunk.getObjects().length, 4);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
// Partial listing
chunk = service.listObjectsChunked(commonTestingBucketName, null, null, 2, null, false);
assertEquals(chunk.getObjects().length, 2);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertEquals(chunk.getPriorLastKey(), "item2");
assertEquals(object.getKey(), objectKey);
assertNotNull(object.getDataInputStream());
assertEquals(IOUtils.toString(object.getDataInputStream()), objectValue);
assertEquals(object.getContentLength(), objectValue.length());
assertEquals(object.getMetadata(metadataName), metadataValue);
// Complete listing, in two chunks
chunk = service.listObjectsChunked(commonTestingBucketName, null, null, 2, null, true);
assertEquals(chunk.getObjects().length, 4);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
// TODO: Test conditional gets
// Partial listing with marker
chunk = service.listObjectsChunked(commonTestingBucketName, null, null, 1000,
"item1/subobject2", true);
assertEquals(chunk.getObjects().length, 3);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
emptyBucket(bucketName);
}
// Partial listing with marker
chunk = service.listObjectsChunked(commonTestingBucketName, null, null, 1000,
"object1", true);
assertEquals(chunk.getObjects().length, 1);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testListAllBucketsImpl() throws InterruptedException, ExecutionException,
TimeoutException, S3ServiceException {
// Ensure there is at least 1 bucket in S3 account to list and compare.
S3Bucket[] jsBuckets = service.listAllBuckets();
// Prefix test
chunk = service.listObjectsChunked(commonTestingBucketName, "item", null, 1000, null, true);
assertEquals(chunk.getObjects().length, 2);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertEquals(chunk.getPrefix(), "item");
assertNull(chunk.getPriorLastKey());
List<org.jclouds.aws.s3.domain.S3Bucket.Metadata> jcBuckets = client.listOwnedBuckets().get(
10, TimeUnit.SECONDS);
// Delimiter test
chunk = service.listObjectsChunked(commonTestingBucketName, null, "/", 1000, null, true);
assertEquals(chunk.getObjects().length, 2);
assertEquals(chunk.getCommonPrefixes().length, 2);
assertEquals(chunk.getDelimiter(), "/");
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
assert jsBuckets.length == jcBuckets.size();
// Prefix & delimiter test
chunk = service.listObjectsChunked(commonTestingBucketName, "item", "/", 1000, null, true);
assertEquals(chunk.getObjects().length, 1);
assertEquals(chunk.getCommonPrefixes().length, 1);
assertEquals(chunk.getDelimiter(), "/");
assertEquals(chunk.getPrefix(), "item");
assertNull(chunk.getPriorLastKey());
Iterator<org.jclouds.aws.s3.domain.S3Bucket.Metadata> jcBucketsIter = jcBuckets.iterator();
for (S3Bucket jsBucket : jsBuckets) {
assert jcBucketsIter.hasNext();
emptyBucket(commonTestingBucketName);
}
org.jclouds.aws.s3.domain.S3Bucket.Metadata jcBucket = jcBucketsIter.next();
assert jsBucket.getName().equals(jcBucket.getName());
}
}
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testListObjectsImpl() throws InterruptedException, ExecutionException,
TimeoutException, IOException, S3ServiceException
{
addObjectToBucket(commonTestingBucketName, "item1/subobject2");
addObjectToBucket(commonTestingBucketName, "item2");
addObjectToBucket(commonTestingBucketName, "object1");
addObjectToBucket(commonTestingBucketName, "object2/subobject1");
S3Object[] objects;
// Normal complete listing
objects = service.listObjects(commonTestingBucketName, null, null, 1000);
assertEquals(objects.length, 4);
// Complete listing, in two chunks
objects = service.listObjects(commonTestingBucketName, null, null, 2);
assertEquals(objects.length, 4);
assertEquals(objects[0].getKey(), "item1/subobject2");
assertEquals(objects[3].getKey(), "object2/subobject1");
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testListObjectsChunkedImpl() throws InterruptedException, ExecutionException,
TimeoutException, IOException, S3ServiceException {
addObjectToBucket(bucketName, "item1/subobject2");
addObjectToBucket(bucketName, "item2");
addObjectToBucket(bucketName, "object1");
addObjectToBucket(bucketName, "object2/subobject1");
// Prefix test
objects = service.listObjects(commonTestingBucketName, "item", null, 1000);
assertEquals(objects.length, 2);
S3ObjectsChunk chunk;
// Delimiter test
objects = service.listObjects(commonTestingBucketName, null, "/", 1000);
assertEquals(objects.length, 2);
assertEquals(objects[0].getKey(), "item2");
assertEquals(objects[1].getKey(), "object1");
// Normal complete listing
chunk = service.listObjectsChunked(bucketName, null, null, 1000, null, true);
assertEquals(chunk.getObjects().length, 4);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
// Prefix & delimiter test
objects = service.listObjects(commonTestingBucketName, "item", "/", 1000);
assertEquals(objects.length, 1);
assertEquals(objects[0].getKey(), "item2");
// Partial listing
chunk = service.listObjectsChunked(bucketName, null, null, 2, null, false);
assertEquals(chunk.getObjects().length, 2);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertEquals(chunk.getPriorLastKey(), "item2");
emptyBucket(commonTestingBucketName);
}
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testPutObjectImpl() throws S3ServiceException, InterruptedException,
ExecutionException, TimeoutException, NoSuchAlgorithmException, IOException
{
String objectKey = "putObject";
S3Object requestObject, jsResultObject;
org.jclouds.aws.s3.domain.S3Object jcObject;
// Upload empty object
requestObject = new S3Object(objectKey);
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(jcObject.getKey(), objectKey);
assertEquals(jcObject.getMetadata().getSize(), 0);
assertEquals(jcObject.getMetadata().getContentType(), ContentTypes.BINARY);
assertEquals(jsResultObject.getKey(), requestObject.getKey());
assertEquals(jsResultObject.getContentLength(), 0);
assertEquals(jsResultObject.getContentType(), ContentTypes.BINARY);
// Complete listing, in two chunks
chunk = service.listObjectsChunked(bucketName, null, null, 2, null, true);
assertEquals(chunk.getObjects().length, 4);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
// Upload unicode-named object
requestObject = new S3Object("Ÿn<EFBFBD>˜dŽ-object");
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, requestObject.getKey())
.get(10, TimeUnit.SECONDS);
assertEquals(jcObject.getKey(), requestObject.getKey());
assertEquals(jcObject.getMetadata().getSize(), 0);
assertEquals(jcObject.getMetadata().getContentType(), ContentTypes.BINARY);
assertEquals(jsResultObject.getKey(), requestObject.getKey());
assertEquals(jsResultObject.getContentLength(), 0);
assertEquals(jsResultObject.getContentType(), ContentTypes.BINARY);
// Partial listing with marker
chunk = service.listObjectsChunked(bucketName, null, null, 1000, "item1/subobject2", true);
assertEquals(chunk.getObjects().length, 3);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
// Upload string object
String data = "This is my Ÿn<6E>˜dŽ data";
requestObject = new S3Object(objectKey, data);
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(jcObject.getMetadata().getSize(), data.getBytes("UTF-8").length);
assertTrue(jcObject.getMetadata().getContentType().startsWith("text/plain"));
assertEquals(jsResultObject.getContentLength(), data.getBytes("UTF-8").length);
assertTrue(jsResultObject.getContentType().startsWith("text/plain"));
// Partial listing with marker
chunk = service.listObjectsChunked(bucketName, null, null, 1000, "object1", true);
assertEquals(chunk.getObjects().length, 1);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
// Upload object with metadata
requestObject = new S3Object(objectKey);
requestObject.addMetadata(S3Constants.USER_METADATA_PREFIX + "my-metadata-1", "value-1");
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(Iterables.getLast(jcObject.getMetadata().getUserMetadata()
.get(S3Constants.USER_METADATA_PREFIX + "my-metadata-1")), "value-1");
assertEquals(
jsResultObject.getMetadata(S3Constants.USER_METADATA_PREFIX + "my-metadata-1"),
"value-1");
// Prefix test
chunk = service.listObjectsChunked(bucketName, "item", null, 1000, null, true);
assertEquals(chunk.getObjects().length, 2);
assertEquals(chunk.getCommonPrefixes().length, 0);
assertNull(chunk.getDelimiter());
assertEquals(chunk.getPrefix(), "item");
assertNull(chunk.getPriorLastKey());
// Upload object with public-read ACL
requestObject = new S3Object(objectKey);
requestObject.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS);
// TODO: No way yet to get/lookup ACL from jClouds object
// assertEquals(jcObject.getAcl(), CannedAccessPolicy.PUBLIC_READ);
assertEquals(jsResultObject.getAcl(), AccessControlList.REST_CANNED_PUBLIC_READ);
// Delimiter test
chunk = service.listObjectsChunked(bucketName, null, "/", 1000, null, true);
assertEquals(chunk.getObjects().length, 2);
assertEquals(chunk.getCommonPrefixes().length, 2);
assertEquals(chunk.getDelimiter(), "/");
assertNull(chunk.getPrefix());
assertNull(chunk.getPriorLastKey());
// TODO : Any way to test a URL lookup that works for live and stub testing?
// URL publicUrl = new URL(
// "http://" + commonTestingBucketName + ".s3.amazonaws.com:80/" + requestObject.getKey());
// assertEquals(((HttpURLConnection) publicUrl.openConnection()).getResponseCode(), 200);
// Prefix & delimiter test
chunk = service.listObjectsChunked(bucketName, "item", "/", 1000, null, true);
assertEquals(chunk.getObjects().length, 1);
assertEquals(chunk.getCommonPrefixes().length, 1);
assertEquals(chunk.getDelimiter(), "/");
assertEquals(chunk.getPrefix(), "item");
assertNull(chunk.getPriorLastKey());
// Upload object and check MD5
requestObject = new S3Object(objectKey);
data = "Here is some d‡tˆ for you";
requestObject.setDataInputStream(new ByteArrayInputStream(data.getBytes("UTF-8")));
jsResultObject = service.putObject(new S3Bucket(commonTestingBucketName), requestObject);
jcObject = client.getObject(commonTestingBucketName, objectKey).get(10, TimeUnit.SECONDS);
assertTrue(jsResultObject.verifyData(data.getBytes("UTF-8")));
assertEquals(jsResultObject.getMd5HashAsHex(),
S3Utils.toHexString(jcObject.getMetadata().getMd5()));
emptyBucket(bucketName);
}
emptyBucket(commonTestingBucketName);
}
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testListObjectsImpl() throws InterruptedException, ExecutionException,
TimeoutException, IOException, S3ServiceException {
addObjectToBucket(bucketName, "item1/subobject2");
addObjectToBucket(bucketName, "item2");
addObjectToBucket(bucketName, "object1");
addObjectToBucket(bucketName, "object2/subobject1");
@Test(dependsOnMethods = "testCreateBucketImpl")
@SuppressWarnings("unchecked")
public void testCopyObjectImpl() throws InterruptedException, ExecutionException,
TimeoutException, IOException, S3ServiceException
{
String data = "This is my data";
String sourceObjectKey = "šriginalObject"; // Notice the use of non-ASCII
String destinationObjectKey = "dŽstinationObject"; // characters here.
String metadataName = "metadata-name";
String sourceMetadataValue = "souce-metadata-value";
String destinationMetadataValue = "destination-metadata-value";
org.jclouds.aws.s3.domain.S3Object sourceObject =
new org.jclouds.aws.s3.domain.S3Object(sourceObjectKey, data);
sourceObject.getMetadata().getUserMetadata().put(
S3Constants.USER_METADATA_PREFIX + metadataName, sourceMetadataValue);
addObjectToBucket(commonTestingBucketName, sourceObject);
S3Object[] objects;
S3Object destinationObject;
Map copyResult;
org.jclouds.aws.s3.domain.S3Object jcDestinationObject;
// Copy with metadata and ACL retained
destinationObject = new S3Object(destinationObjectKey);
copyResult = service.copyObject(commonTestingBucketName, sourceObjectKey,
commonTestingBucketName, destinationObject, false);
jcDestinationObject = client.getObject(commonTestingBucketName, destinationObject.getKey())
.get(10, TimeUnit.SECONDS);
assertEquals(jcDestinationObject.getKey(), destinationObjectKey);
assertEquals(Iterators.getLast(jcDestinationObject.getMetadata().getUserMetadata().get(
S3Constants.USER_METADATA_PREFIX + metadataName).iterator()), sourceMetadataValue);
assertEquals(copyResult.get("ETag"),
S3Utils.toHexString(jcDestinationObject.getMetadata().getMd5()));
// TODO: Test destination ACL is unchanged (ie private)
// Copy with metadata replaced
destinationObject = new S3Object(destinationObjectKey);
destinationObject.addMetadata(S3Constants.USER_METADATA_PREFIX + metadataName,
destinationMetadataValue);
copyResult = service.copyObject(commonTestingBucketName, sourceObjectKey,
commonTestingBucketName, destinationObject, true);
jcDestinationObject = client.getObject(commonTestingBucketName, destinationObject.getKey())
.get(10, TimeUnit.SECONDS);
assertEquals(Iterators.getLast(jcDestinationObject.getMetadata().getUserMetadata().get(
S3Constants.USER_METADATA_PREFIX + metadataName).iterator()),
destinationMetadataValue);
// TODO: Test destination ACL is unchanged (ie private)
// Copy with ACL modified
destinationObject = new S3Object(destinationObjectKey);
destinationObject.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
copyResult = service.copyObject(commonTestingBucketName, sourceObjectKey,
commonTestingBucketName, destinationObject, false);
jcDestinationObject = client.getObject(commonTestingBucketName, destinationObject.getKey())
.get(10, TimeUnit.SECONDS);
// TODO: Test destination ACL is changed (ie public-read)
// Normal complete listing
objects = service.listObjects(bucketName, null, null, 1000);
assertEquals(objects.length, 4);
emptyBucket(commonTestingBucketName);
}
// Complete listing, in two chunks
objects = service.listObjects(bucketName, null, null, 2);
assertEquals(objects.length, 4);
assertEquals(objects[0].getKey(), "item1/subobject2");
assertEquals(objects[3].getKey(), "object2/subobject1");
@Test(enabled = false)
public void testCheckBucketStatus() {
fail("Not yet implemented");
}
// Prefix test
objects = service.listObjects(bucketName, "item", null, 1000);
assertEquals(objects.length, 2);
@Test(enabled = false)
public void testGetBucketAclImpl() {
fail("Not yet implemented");
}
// Delimiter test
objects = service.listObjects(bucketName, null, "/", 1000);
assertEquals(objects.length, 2);
assertEquals(objects[0].getKey(), "item2");
assertEquals(objects[1].getKey(), "object1");
@Test(enabled = false)
public void testGetBucketLocationImpl() {
fail("Not yet implemented");
}
// Prefix & delimiter test
objects = service.listObjects(bucketName, "item", "/", 1000);
assertEquals(objects.length, 1);
assertEquals(objects[0].getKey(), "item2");
@Test(enabled = false)
public void testGetBucketLoggingStatus() {
fail("Not yet implemented");
}
emptyBucket(bucketName);
}
@Test(enabled = false)
public void testGetObjectAclImpl() {
fail("Not yet implemented");
}
@Test(dependsOnMethods = "testCreateBucketImpl")
public void testPutObjectImpl() throws S3ServiceException, InterruptedException,
ExecutionException, TimeoutException, NoSuchAlgorithmException, IOException {
String objectKey = "putObject";
@Test(enabled = false)
public void testPutBucketAclImpl() {
fail("Not yet implemented");
}
S3Object requestObject, jsResultObject;
org.jclouds.aws.s3.domain.S3Object jcObject;
@Test(enabled = false)
public void testPutObjectAclImpl() {
fail("Not yet implemented");
}
// Upload empty object
requestObject = new S3Object(objectKey);
jsResultObject = service.putObject(new S3Bucket(bucketName), requestObject);
jcObject = client.getObject(bucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(jcObject.getKey(), objectKey);
assertEquals(jcObject.getMetadata().getSize(), 0);
assertEquals(jcObject.getMetadata().getContentType(), ContentTypes.BINARY);
assertEquals(jsResultObject.getKey(), requestObject.getKey());
assertEquals(jsResultObject.getContentLength(), 0);
assertEquals(jsResultObject.getContentType(), ContentTypes.BINARY);
@Test(enabled = false)
public void testSetBucketLoggingStatusImpl() {
fail("Not yet implemented");
}
// Upload unicode-named object
requestObject = new S3Object("Ÿn<EFBFBD>˜dŽ-object");
jsResultObject = service.putObject(new S3Bucket(bucketName), requestObject);
jcObject = client.getObject(bucketName, requestObject.getKey()).get(10, TimeUnit.SECONDS);
assertEquals(jcObject.getKey(), requestObject.getKey());
assertEquals(jcObject.getMetadata().getSize(), 0);
assertEquals(jcObject.getMetadata().getContentType(), ContentTypes.BINARY);
assertEquals(jsResultObject.getKey(), requestObject.getKey());
assertEquals(jsResultObject.getContentLength(), 0);
assertEquals(jsResultObject.getContentType(), ContentTypes.BINARY);
@Test(enabled = false)
public void testSetRequesterPaysBucketImpl() {
fail("Not yet implemented");
}
// Upload string object
String data = "This is my Ÿn<6E>˜dŽ data";
requestObject = new S3Object(objectKey, data);
jsResultObject = service.putObject(new S3Bucket(bucketName), requestObject);
jcObject = client.getObject(bucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(jcObject.getMetadata().getSize(), data.getBytes("UTF-8").length);
assertTrue(jcObject.getMetadata().getContentType().startsWith("text/plain"));
assertEquals(jsResultObject.getContentLength(), data.getBytes("UTF-8").length);
assertTrue(jsResultObject.getContentType().startsWith("text/plain"));
@Test(enabled = false)
public void testIsBucketAccessible() {
fail("Not yet implemented");
}
// Upload object with metadata
requestObject = new S3Object(objectKey);
requestObject.addMetadata(S3Constants.USER_METADATA_PREFIX + "my-metadata-1", "value-1");
jsResultObject = service.putObject(new S3Bucket(bucketName), requestObject);
jcObject = client.getObject(bucketName, objectKey).get(10, TimeUnit.SECONDS);
assertEquals(Iterables.getLast(jcObject.getMetadata().getUserMetadata().get(
S3Constants.USER_METADATA_PREFIX + "my-metadata-1")), "value-1");
assertEquals(jsResultObject.getMetadata(S3Constants.USER_METADATA_PREFIX + "my-metadata-1"),
"value-1");
@Test(enabled = false)
public void testIsRequesterPaysBucketImpl() {
fail("Not yet implemented");
}
// Upload object with public-read ACL
requestObject = new S3Object(objectKey);
requestObject.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
jsResultObject = service.putObject(new S3Bucket(bucketName), requestObject);
jcObject = client.getObject(bucketName, objectKey).get(10, TimeUnit.SECONDS);
// TODO: No way yet to get/lookup ACL from jClouds object
// assertEquals(jcObject.getAcl(), CannedAccessPolicy.PUBLIC_READ);
assertEquals(jsResultObject.getAcl(), AccessControlList.REST_CANNED_PUBLIC_READ);
// TODO : Any way to test a URL lookup that works for live and stub testing?
// URL publicUrl = new URL(
// "http://" + bucketName + ".s3.amazonaws.com:80/" + requestObject.getKey());
// assertEquals(((HttpURLConnection) publicUrl.openConnection()).getResponseCode(), 200);
// Upload object and check MD5
requestObject = new S3Object(objectKey);
data = "Here is some d‡tˆ for you";
requestObject.setDataInputStream(new ByteArrayInputStream(data.getBytes("UTF-8")));
jsResultObject = service.putObject(new S3Bucket(bucketName), requestObject);
jcObject = client.getObject(bucketName, objectKey).get(10, TimeUnit.SECONDS);
assertTrue(jsResultObject.verifyData(data.getBytes("UTF-8")));
assertEquals(jsResultObject.getMd5HashAsHex(), S3Utils.toHexString(jcObject.getMetadata()
.getMd5()));
emptyBucket(bucketName);
}
@Test(dependsOnMethods = "testCreateBucketImpl")
@SuppressWarnings("unchecked")
public void testCopyObjectImpl() throws InterruptedException, ExecutionException,
TimeoutException, IOException, S3ServiceException {
String data = "This is my data";
String sourceObjectKey = "šriginalObject"; // Notice the use of non-ASCII
String destinationObjectKey = "dŽstinationObject"; // characters here.
String metadataName = "metadata-name";
String sourceMetadataValue = "souce-metadata-value";
String destinationMetadataValue = "destination-metadata-value";
org.jclouds.aws.s3.domain.S3Object sourceObject = new org.jclouds.aws.s3.domain.S3Object(
sourceObjectKey, data);
sourceObject.getMetadata().getUserMetadata().put(
S3Constants.USER_METADATA_PREFIX + metadataName, sourceMetadataValue);
addObjectToBucket(bucketName, sourceObject);
S3Object destinationObject;
Map copyResult;
org.jclouds.aws.s3.domain.S3Object jcDestinationObject;
// Copy with metadata and ACL retained
destinationObject = new S3Object(destinationObjectKey);
copyResult = service.copyObject(bucketName, sourceObjectKey, bucketName, destinationObject,
false);
jcDestinationObject = client.getObject(bucketName, destinationObject.getKey()).get(10,
TimeUnit.SECONDS);
assertEquals(jcDestinationObject.getKey(), destinationObjectKey);
assertEquals(Iterators.getLast(jcDestinationObject.getMetadata().getUserMetadata().get(
S3Constants.USER_METADATA_PREFIX + metadataName).iterator()), sourceMetadataValue);
assertEquals(copyResult.get("ETag"), S3Utils.toHexString(jcDestinationObject.getMetadata()
.getMd5()));
// TODO: Test destination ACL is unchanged (ie private)
// Copy with metadata replaced
destinationObject = new S3Object(destinationObjectKey);
destinationObject.addMetadata(S3Constants.USER_METADATA_PREFIX + metadataName,
destinationMetadataValue);
copyResult = service.copyObject(bucketName, sourceObjectKey, bucketName, destinationObject,
true);
jcDestinationObject = client.getObject(bucketName, destinationObject.getKey()).get(10,
TimeUnit.SECONDS);
assertEquals(Iterators.getLast(jcDestinationObject.getMetadata().getUserMetadata().get(
S3Constants.USER_METADATA_PREFIX + metadataName).iterator()),
destinationMetadataValue);
// TODO: Test destination ACL is unchanged (ie private)
// Copy with ACL modified
destinationObject = new S3Object(destinationObjectKey);
destinationObject.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
copyResult = service.copyObject(bucketName, sourceObjectKey, bucketName, destinationObject,
false);
jcDestinationObject = client.getObject(bucketName, destinationObject.getKey()).get(10,
TimeUnit.SECONDS);
// TODO: Test destination ACL is changed (ie public-read)
emptyBucket(bucketName);
}
@Test(enabled = false)
public void testCheckBucketStatus() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testGetBucketAclImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testGetBucketLocationImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testGetBucketLoggingStatus() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testGetObjectAclImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testPutBucketAclImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testPutObjectAclImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testSetBucketLoggingStatusImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testSetRequesterPaysBucketImpl() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testIsBucketAccessible() {
fail("Not yet implemented");
}
@Test(enabled = false)
public void testIsRequesterPaysBucketImpl() {
fail("Not yet implemented");
}
}

View File

@ -23,8 +23,6 @@
*/
package org.jclouds.aws.s3;
import static org.jclouds.aws.s3.commands.options.PutBucketOptions.Builder.createIn;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
@ -38,7 +36,6 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import org.jclouds.aws.s3.domain.S3Bucket.Metadata.LocationConstraint;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
@ -63,81 +60,119 @@ public abstract class BasePerformance extends S3IntegrationTest {
protected ExecutorService exec;
protected CompletionService<Boolean> completer;
protected String bucketNameEU;
@BeforeTest
protected void setUpCallables() throws InterruptedException, ExecutionException,
TimeoutException {
exec = Executors.newCachedThreadPool();
completer = new ExecutorCompletionService<Boolean>(exec);
bucketNameEU = (bucketPrefix).toLowerCase() + ".eu";
}
@AfterTest
protected void tearDownExecutor() throws Exception {
if (bucketNameEU != null)
deleteBucket(bucketNameEU);
exec.shutdownNow();
exec = null;
}
@Test(enabled = true)
public void testPutBytesSerialEU() throws Exception {
client.putBucketIfNotExists(bucketNameEU, createIn(LocationConstraint.EU)).get(10,
TimeUnit.SECONDS);
doSerial(new PutBytesCallable(this.bucketNameEU), loopCount / 10);
String euBucketName = createScratchBucketInEU();
doSerial(new PutBytesCallable(euBucketName), loopCount / 10);
}
@Test(enabled = true)
public void testPutBytesParallelEU() throws InterruptedException, ExecutionException,
TimeoutException {
client.putBucketIfNotExists(bucketNameEU, createIn(LocationConstraint.EU)).get(10,
TimeUnit.SECONDS);
doParallel(new PutBytesCallable(this.bucketNameEU), loopCount);
String euBucketName = createScratchBucketInEU();
try {
doParallel(new PutBytesCallable(euBucketName), loopCount);
} finally {
returnBucket(euBucketName);
}
}
@Test(enabled = true)
public void testPutBytesSerial() throws Exception {
doSerial(new PutBytesCallable(this.bucketName), loopCount / 10);
String bucketName = getBucketName();
try {
doSerial(new PutBytesCallable(bucketName), loopCount / 10);
} finally {
returnBucket(bucketName);
}
}
@Test(enabled = true)
public void testPutBytesParallel() throws InterruptedException, ExecutionException,
TimeoutException {
doParallel(new PutBytesCallable(this.bucketName), loopCount);
String bucketName = getBucketName();
try {
doParallel(new PutBytesCallable(bucketName), loopCount);
} finally {
returnBucket(bucketName);
}
}
@Test(enabled = true)
public void testPutFileSerial() throws Exception {
doSerial(new PutFileCallable(this.bucketName), loopCount / 10);
String bucketName = getBucketName();
try {
doSerial(new PutFileCallable(bucketName), loopCount / 10);
} finally {
returnBucket(bucketName);
}
}
@Test(enabled = true)
public void testPutFileParallel() throws InterruptedException, ExecutionException,
TimeoutException {
doParallel(new PutFileCallable(this.bucketName), loopCount);
String bucketName = getBucketName();
try {
doParallel(new PutFileCallable(bucketName), loopCount);
} finally {
returnBucket(bucketName);
}
}
@Test(enabled = true)
public void testPutInputStreamSerial() throws Exception {
doSerial(new PutInputStreamCallable(this.bucketName), loopCount / 10);
String bucketName = getBucketName();
try {
doSerial(new PutInputStreamCallable(bucketName), loopCount / 10);
} finally {
returnBucket(bucketName);
}
}
@Test(enabled = true)
public void testPutInputStreamParallel() throws InterruptedException, ExecutionException,
TimeoutException {
doParallel(new PutInputStreamCallable(this.bucketName), loopCount);
String bucketName = getBucketName();
try {
doParallel(new PutInputStreamCallable(bucketName), loopCount);
} finally {
returnBucket(bucketName);
}
}
@Test(enabled = true)
public void testPutStringSerial() throws Exception {
doSerial(new PutStringCallable(this.bucketName), loopCount / 10);
String bucketName = getBucketName();
try {
doSerial(new PutStringCallable(bucketName), loopCount / 10);
} finally {
returnBucket(bucketName);
}
}
@Test(enabled = true)
public void testPutStringParallel() throws InterruptedException, ExecutionException,
TimeoutException {
doParallel(new PutStringCallable(this.bucketName), loopCount);
String bucketName = getBucketName();
try {
doParallel(new PutStringCallable(bucketName), loopCount);
} finally {
returnBucket(bucketName);
}
}
private void doSerial(Provider<Callable<Boolean>> provider, int loopCount) throws Exception,