Issue 75: cleaned up context bulding and added logging

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1638 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-07-17 15:26:09 +00:00
parent 2d68a4e748
commit cf8fa7aa33
1 changed files with 20 additions and 22 deletions

View File

@ -36,11 +36,13 @@ import java.util.concurrent.TimeUnit;
import org.jclouds.http.HttpResponseException; import org.jclouds.http.HttpResponseException;
import org.jclouds.http.HttpUtils; import org.jclouds.http.HttpUtils;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.rackspace.cloudfiles.domain.AccountMetadata; import org.jclouds.rackspace.cloudfiles.domain.AccountMetadata;
import org.jclouds.rackspace.cloudfiles.domain.CFObject; import org.jclouds.rackspace.cloudfiles.domain.CFObject;
import org.jclouds.rackspace.cloudfiles.domain.ContainerMetadata; import org.jclouds.rackspace.cloudfiles.domain.ContainerMetadata;
import org.jclouds.rackspace.cloudfiles.options.ListContainerOptions; import org.jclouds.rackspace.cloudfiles.options.ListContainerOptions;
import org.jclouds.rackspace.cloudfiles.reference.CloudFilesHeaders; import org.jclouds.rackspace.cloudfiles.reference.CloudFilesHeaders;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
@ -55,11 +57,16 @@ public class CloudFilesConnectionLiveTest {
protected static final String sysRackspaceKey = System.getProperty(PROPERTY_RACKSPACE_KEY); protected static final String sysRackspaceKey = System.getProperty(PROPERTY_RACKSPACE_KEY);
private String bucketPrefix = System.getProperty("user.name") + ".cfint"; private String bucketPrefix = System.getProperty("user.name") + ".cfint";
CloudFilesConnection connection;
@BeforeGroups(groups = { "live" })
public void setupConnection() {
connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser, sysRackspaceKey)
.withModule(new Log4JLoggingModule()).withJsonDebug().buildContext().getConnection();
}
@Test @Test
public void testListOwnedContainers() throws Exception { public void testListOwnedContainers() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
List<ContainerMetadata> response = connection.listOwnedContainers(); List<ContainerMetadata> response = connection.listOwnedContainers();
assertNotNull(response); assertNotNull(response);
long initialContainerCount = response.size(); long initialContainerCount = response.size();
@ -80,8 +87,6 @@ public class CloudFilesConnectionLiveTest {
@Test @Test
public void testHeadAccountMetadata() throws Exception { public void testHeadAccountMetadata() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
AccountMetadata metadata = connection.getAccountMetadata(); AccountMetadata metadata = connection.getAccountMetadata();
assertNotNull(metadata); assertNotNull(metadata);
long initialContainerCount = metadata.getContainerCount(); long initialContainerCount = metadata.getContainerCount();
@ -98,9 +103,6 @@ public class CloudFilesConnectionLiveTest {
@Test @Test
public void testDeleteContainer() throws Exception { public void testDeleteContainer() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
assertTrue(connection.deleteContainerIfEmpty("does-not-exist")); assertTrue(connection.deleteContainerIfEmpty("does-not-exist"));
String containerName = bucketPrefix + ".testDeleteContainer"; String containerName = bucketPrefix + ".testDeleteContainer";
@ -110,8 +112,6 @@ public class CloudFilesConnectionLiveTest {
@Test @Test
public void testPutContainers() throws Exception { public void testPutContainers() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
String containerName1 = bucketPrefix + ".hello"; String containerName1 = bucketPrefix + ".hello";
assertTrue(connection.putContainer(containerName1)); assertTrue(connection.putContainer(containerName1));
// List only the container just created, using a marker with the container name less 1 char // List only the container just created, using a marker with the container name less 1 char
@ -140,16 +140,14 @@ public class CloudFilesConnectionLiveTest {
assertTrue(connection.deleteContainerIfEmpty(containerName1)); assertTrue(connection.deleteContainerIfEmpty(containerName1));
assertTrue(connection.deleteContainerIfEmpty(containerName2)); assertTrue(connection.deleteContainerIfEmpty(containerName2));
} }
@Test @Test
public void testPutAndDeleteObjects() throws Exception { public void testPutAndDeleteObjects() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
String containerName = bucketPrefix + ".testPutAndDeleteObjects"; String containerName = bucketPrefix + ".testPutAndDeleteObjects";
String data = "Here is my data"; String data = "Here is my data";
assertTrue(connection.putContainer(containerName)); assertTrue(connection.putContainer(containerName));
// Test with string data, ETag hash, and a piece of metadata // Test with string data, ETag hash, and a piece of metadata
CFObject object = new CFObject("object", data); CFObject object = new CFObject("object", data);
object.setContentLength(data.length()); object.setContentLength(data.length());
@ -157,12 +155,12 @@ public class CloudFilesConnectionLiveTest {
object.getMetadata().setContentType("text/plain"); object.getMetadata().setContentType("text/plain");
// TODO: Metadata values aren't being stored by CF, but the names are. Odd... // TODO: Metadata values aren't being stored by CF, but the names are. Odd...
object.getMetadata().getUserMetadata().put( object.getMetadata().getUserMetadata().put(
CloudFilesHeaders.USER_METADATA_PREFIX + "metadata", "metadata-value"); CloudFilesHeaders.USER_METADATA_PREFIX + "metadata", "metadata-value");
byte[] md5 = connection.putObject(containerName, object).get(10, TimeUnit.SECONDS); byte[] md5 = connection.putObject(containerName, object).get(10, TimeUnit.SECONDS);
assertEquals(HttpUtils.toHexString(md5), assertEquals(HttpUtils.toHexString(md5), HttpUtils
HttpUtils.toHexString(object.getMetadata().getETag())); .toHexString(object.getMetadata().getETag()));
// TODO: Get and confirm data // TODO: Get and confirm data
// Test with invalid ETag (as if object's data was corrupted in transit) // Test with invalid ETag (as if object's data was corrupted in transit)
String correctEtag = HttpUtils.toHexString(object.getMetadata().getETag()); String correctEtag = HttpUtils.toHexString(object.getMetadata().getETag());
String incorrectEtag = "0" + correctEtag.substring(1); String incorrectEtag = "0" + correctEtag.substring(1);
@ -171,19 +169,19 @@ public class CloudFilesConnectionLiveTest {
connection.putObject(containerName, object).get(10, TimeUnit.SECONDS); connection.putObject(containerName, object).get(10, TimeUnit.SECONDS);
} catch (Throwable e) { } catch (Throwable e) {
assertEquals(e.getCause().getClass(), HttpResponseException.class); assertEquals(e.getCause().getClass(), HttpResponseException.class);
assertEquals(((HttpResponseException)e.getCause()).getResponse().getStatusCode(), 422); assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422);
} }
// Test chunked/streamed upload with data of "unknown" length // Test chunked/streamed upload with data of "unknown" length
ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes("UTF-8")); ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes("UTF-8"));
object = new CFObject("chunked-object", bais); object = new CFObject("chunked-object", bais);
md5 = connection.putObject(containerName, object).get(10, TimeUnit.SECONDS); md5 = connection.putObject(containerName, object).get(10, TimeUnit.SECONDS);
assertEquals(HttpUtils.toHexString(md5), correctEtag); assertEquals(HttpUtils.toHexString(md5), correctEtag);
// TODO: Get and confirm data // TODO: Get and confirm data
assertTrue(connection.deleteObject(containerName, "object")); assertTrue(connection.deleteObject(containerName, "object"));
assertTrue(connection.deleteObject(containerName, "chunked-object")); assertTrue(connection.deleteObject(containerName, "chunked-object"));
assertTrue(connection.deleteContainerIfEmpty(containerName)); assertTrue(connection.deleteContainerIfEmpty(containerName));
} }
} }