mirror of https://github.com/apache/jclouds.git
Improve use of ByteSource
This commit is contained in:
parent
22c5fbbd78
commit
86de7923a1
|
@ -20,10 +20,9 @@ import static org.testng.Assert.assertEquals;
|
||||||
import static org.testng.Assert.assertNotEquals;
|
import static org.testng.Assert.assertNotEquals;
|
||||||
import static org.testng.Assert.assertTrue;
|
import static org.testng.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
@ -32,6 +31,7 @@ import org.jclouds.blobstore.domain.Blob;
|
||||||
import org.jclouds.blobstore.integration.internal.BaseBlobIntegrationTest;
|
import org.jclouds.blobstore.integration.internal.BaseBlobIntegrationTest;
|
||||||
import org.jclouds.blobstore.options.PutOptions;
|
import org.jclouds.blobstore.options.PutOptions;
|
||||||
import org.jclouds.io.ByteSources;
|
import org.jclouds.io.ByteSources;
|
||||||
|
import org.jclouds.io.ByteStreams2;
|
||||||
import org.jclouds.io.Payload;
|
import org.jclouds.io.Payload;
|
||||||
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
|
import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
|
||||||
import org.jclouds.openstack.swift.blobstore.strategy.MultipartUpload;
|
import org.jclouds.openstack.swift.blobstore.strategy.MultipartUpload;
|
||||||
|
@ -43,8 +43,6 @@ import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.google.common.hash.Hashing;
|
import com.google.common.hash.Hashing;
|
||||||
import com.google.common.io.ByteSource;
|
import com.google.common.io.ByteSource;
|
||||||
import com.google.common.io.Closeables;
|
|
||||||
import com.google.common.io.Files;
|
|
||||||
|
|
||||||
@Test(groups = "live")
|
@Test(groups = "live")
|
||||||
public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
|
public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
|
||||||
|
@ -171,23 +169,17 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
|
||||||
|
|
||||||
blobStore.createContainerInLocation(null, container);
|
blobStore.createContainerInLocation(null, container);
|
||||||
|
|
||||||
File inFile = createFileBiggerThan(PART_SIZE);
|
ByteSource input = createByteSourceBiggerThan(PART_SIZE);
|
||||||
File outFile = new File("target/lots-of-const-readback.txt");
|
|
||||||
|
|
||||||
InputStream contentToUpload = new FileInputStream(inFile);
|
Blob write = blobStore.blobBuilder("const.txt")
|
||||||
Blob write = blobStore.blobBuilder("const.txt").payload(contentToUpload).contentLength(inFile.length()).build();
|
.payload(input.openStream())
|
||||||
|
.contentLength(input.size())
|
||||||
|
.build();
|
||||||
blobStore.putBlob(container, write, PutOptions.Builder.multipart());
|
blobStore.putBlob(container, write, PutOptions.Builder.multipart());
|
||||||
|
|
||||||
Blob read = blobStore.getBlob(container, "const.txt");
|
Blob read = blobStore.getBlob(container, "const.txt");
|
||||||
InputStream is = read.getPayload().openStream();
|
InputStream is = read.getPayload().openStream();
|
||||||
try {
|
assertEquals(ByteStreams2.hashAndClose(is, Hashing.md5()), input.hash(Hashing.md5()));
|
||||||
Files.asByteSink(outFile).writeFrom(is);
|
|
||||||
} finally {
|
|
||||||
Closeables.closeQuietly(is);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals(Files.hash(outFile, Hashing.md5()), Files.hash(inFile, Hashing.md5()));
|
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
returnContainer(container);
|
returnContainer(container);
|
||||||
}
|
}
|
||||||
|
@ -199,8 +191,8 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addMultipartBlobToContainer(String containerName, String key) throws IOException {
|
protected void addMultipartBlobToContainer(String containerName, String key) throws IOException {
|
||||||
File fileToUpload = createFileBiggerThan(PART_SIZE);
|
ByteSource byteSource = createByteSourceBiggerThan(PART_SIZE);
|
||||||
addMultipartBlobToContainer(containerName, key, Files.asByteSource(fileToUpload));
|
addMultipartBlobToContainer(containerName, key, byteSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addMultipartBlobToContainer(String containerName, String key, ByteSource byteSource) throws IOException {
|
protected void addMultipartBlobToContainer(String containerName, String key, ByteSource byteSource) throws IOException {
|
||||||
|
@ -222,20 +214,8 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
|
||||||
return byteSource;
|
return byteSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
private ByteSource createByteSourceBiggerThan(long partSize) throws IOException {
|
||||||
private File createFileBiggerThan(long partSize) throws IOException {
|
int nCopies = (int) (partSize / getOneHundredOneConstitutionsLength()) + 1;
|
||||||
long copiesNeeded = (partSize / getOneHundredOneConstitutionsLength()) + 1;
|
return ByteSource.concat(Collections.nCopies(nCopies, oneHundredOneConstitutions));
|
||||||
|
|
||||||
ByteSource temp = ByteSource.concat(oneHundredOneConstitutions);
|
|
||||||
|
|
||||||
for (int i = 0; i < copiesNeeded; i++) {
|
|
||||||
temp = ByteSource.concat(temp, oneHundredOneConstitutions);
|
|
||||||
}
|
|
||||||
|
|
||||||
File fileToUpload = new File("target/lots-of-const.txt");
|
|
||||||
temp.copyTo(Files.asByteSink(fileToUpload));
|
|
||||||
|
|
||||||
assertTrue(fileToUpload.length() > partSize);
|
|
||||||
return fileToUpload;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue