final int[] getObjectHeadsEventCount = new int[] {0};
final List<StorageObject> getObjectsList = new ArrayList<StorageObject>();
final int[] deleteObjectsEventCount = new int[] {0};
// Multi-threaded service with adaptor to count event occurrences.
ThreadedStorageService threadedService = new ThreadedStorageService(
service,
new StorageServiceEventAdaptor() {
@Override
public void event(CreateObjectsEvent event) {
if (CreateObjectsEvent.EVENT_IN_PROGRESS == event.getEventCode()) {
createObjectsEventCount[0] += event.getCreatedObjects().length;
}
}
@Override
public void event(GetObjectHeadsEvent event) {
if (GetObjectHeadsEvent.EVENT_IN_PROGRESS == event.getEventCode()) {
getObjectHeadsEventCount[0] += event.getCompletedObjects().length;
}
}
@Override
public void event(GetObjectsEvent event) {
if (GetObjectsEvent.EVENT_IN_PROGRESS == event.getEventCode()) {
for (StorageObject object: event.getCompletedObjects()) {
getObjectsList.add(object);
}
}
}
@Override
public void event(DeleteObjectsEvent event) {
if (DeleteObjectsEvent.EVENT_IN_PROGRESS == event.getEventCode()) {
deleteObjectsEventCount[0] += event.getDeletedObjects().length;
}
}
});
assertEquals(0, createObjectsEventCount[0]);
assertEquals(0, getObjectHeadsEventCount[0]);
assertEquals(0, getObjectsList.size());
assertEquals(0, deleteObjectsEventCount[0]);
StorageObject[] objects = new StorageObject[] {
new StorageObject("one.txt", "Some data"),
new StorageObject("twö.txt", "Some data"),
new StorageObject("thréè.txt", "Some data"),
new StorageObject("fôür.txt", "Some data"),
new StorageObject("fîvæ∫.txt", "Some data")
};
// Upload multiple objects
boolean success = threadedService.putObjects(bucketName, objects);
assertTrue(success);
assertEquals(objects.length, createObjectsEventCount[0]);
assertEquals(0, getObjectHeadsEventCount[0]);
assertEquals(0, getObjectsList.size());
assertEquals(0, deleteObjectsEventCount[0]);
// Retrieve details for multiple objects
success = threadedService.getObjectsHeads(bucketName, objects);
assertTrue(success);
assertEquals(objects.length, createObjectsEventCount[0]);
assertEquals(objects.length, getObjectHeadsEventCount[0]);
assertEquals(0, getObjectsList.size());
assertEquals(0, deleteObjectsEventCount[0]);
// Retrieve data for multiple objects
success = threadedService.getObjects(bucketName, objects);
assertTrue(success);
assertEquals(objects.length, createObjectsEventCount[0]);
assertEquals(objects.length, getObjectHeadsEventCount[0]);
assertEquals(objects.length, getObjectsList.size());
assertEquals(0, deleteObjectsEventCount[0]);
// Check all objects retrieved have expected data content.
for (StorageObject getObject: getObjectsList) {
// TODO: Google Storage doesn't reliably return Content-Length in a GET
if (!TARGET_SERVICE_GS.equals(getTargetService())) {
assertEquals("Some data".length(), getObject.getContentLength());
}
String objectData = ServiceUtils.readInputStreamToString(
getObject.getDataInputStream(), Constants.DEFAULT_ENCODING);
assertEquals("Some data", objectData);
}
// Delete multiple objects
success = threadedService.deleteObjects(bucketName, objects);
assertTrue(success);
assertEquals(objects.length, createObjectsEventCount[0]);
assertEquals(objects.length, getObjectHeadsEventCount[0]);
assertEquals(objects.length, getObjectsList.size());
assertEquals(objects.length, deleteObjectsEventCount[0]);