fixed broken unit test

git-svn-id: http://jclouds.googlecode.com/svn/trunk@484 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-05-08 21:36:07 +00:00
parent 8c1861f73d
commit 9ef49c6e5e
5 changed files with 75 additions and 22 deletions

View File

@ -25,9 +25,16 @@ package org.jclouds.aws.s3.domain;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.commons.io.IOUtils;
import org.jclouds.aws.s3.S3Utils;
import org.jclouds.http.ContentTypes;
import org.joda.time.DateTime;
@ -74,10 +81,10 @@ public class S3Object {
// only parsed during head or get
private Multimap<String, String> userMetadata = HashMultimap.create();
private DateTime lastModified;
private String contentType = ContentTypes.UNKNOWN_MIME_TYPE;
private String dataType = ContentTypes.UNKNOWN_MIME_TYPE;
private String cacheControl;
private String contentDisposition;
private String contentEncoding;
private String dataDisposition;
private String dataEncoding;
// only parsed on list
private S3Owner owner = null;
@ -99,7 +106,7 @@ public class S3Object {
getMd5() == null ? "null" : Arrays.asList(getMd5())
.toString());
sb.append(", size=").append(size);
sb.append(", contentType='").append(contentType).append('\'');
sb.append(", dataType='").append(dataType).append('\'');
sb.append('}');
return sb.toString();
}
@ -115,8 +122,8 @@ public class S3Object {
if (size != metaData.size)
return false;
if (contentType != null ? !contentType.equals(metaData.contentType)
: metaData.contentType != null)
if (dataType != null ? !dataType.equals(metaData.dataType)
: metaData.dataType != null)
return false;
if (!key.equals(metaData.key))
return false;
@ -137,8 +144,7 @@ public class S3Object {
result = 31 * result
+ (getMd5() != null ? Arrays.hashCode(getMd5()) : 0);
result = 31 * result + (int) (size ^ (size >>> 32));
result = 31 * result
+ (contentType != null ? contentType.hashCode() : 0);
result = 31 * result + (dataType != null ? dataType.hashCode() : 0);
return result;
}
@ -163,11 +169,11 @@ public class S3Object {
}
public String getContentType() {
return contentType;
return dataType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
public void setContentType(String dataType) {
this.dataType = dataType;
}
public void setMd5(byte[] md5) {
@ -210,20 +216,20 @@ public class S3Object {
return cacheControl;
}
public void setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
public void setContentDisposition(String dataDisposition) {
this.dataDisposition = dataDisposition;
}
public String getContentDisposition() {
return contentDisposition;
return dataDisposition;
}
public void setContentEncoding(String contentEncoding) {
this.contentEncoding = contentEncoding;
public void setContentEncoding(String dataEncoding) {
this.dataEncoding = dataEncoding;
}
public String getContentEncoding() {
return contentEncoding;
return dataEncoding;
}
}
@ -235,6 +241,43 @@ public class S3Object {
this.data = data;
}
/**
* converts String or InputStreams to byte [] type before generating an md5;
*
* @throws IOException
*/
public void generateMd5() throws IOException {
checkState(data != null,
"data must be set before calling generateMd5()");
byte[] md5;
if (data == null || data instanceof byte[]) {
md5 = S3Utils.md5((byte[]) data);
} else if (data instanceof String) {
this.data = ((String) data).getBytes();
md5 = S3Utils.md5((byte[]) data);
} else if (data instanceof File) {
InputStream i = new FileInputStream((File) data);
try {
md5 = S3Utils.md5(i);
} finally {
IOUtils.closeQuietly(i);
}
} else if (data instanceof InputStream) {
InputStream i = (InputStream) data;
try {
this.data = (IOUtils.toByteArray(i));
md5 = S3Utils.md5(i);
} finally {
IOUtils.closeQuietly(i);
}
} else {
throw new UnsupportedOperationException("Content not supported "
+ data.getClass());
}
getMetaData().setMd5(md5);
}
public Object getData() {
return data;
}

View File

@ -204,7 +204,7 @@ public class S3IntegrationTest {
@AfterTest
protected void tearDownClient() throws Exception {
// deleteEverything();
deleteEverything();
context.close();
context = null;
}

View File

@ -91,6 +91,7 @@ public class S3ObjectMapTest extends BaseS3MapTest<S3Object> {
fiveStrings.get(entry.getKey()));
S3Object value = entry.getValue();
value.setData("");
value.generateMd5();
entry.setValue(value);
}
assertEquals(map.size(), 5);

View File

@ -39,6 +39,8 @@ import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.name.Names;
@ -120,6 +122,13 @@ public class S3CommandFactoryTest {
expect(metaData.getKey()).andReturn("rawr");
expect(metaData.getContentType()).andReturn("text/xml").atLeastOnce();
expect(metaData.getSize()).andReturn(4L);
expect(metaData.getCacheControl()).andReturn("no-cache").atLeastOnce();
expect(metaData.getContentDisposition()).andReturn("disposition").atLeastOnce();
expect(metaData.getContentEncoding()).andReturn("encoding").atLeastOnce();
expect(metaData.getMd5()).andReturn("encoding".getBytes()).atLeastOnce();
Multimap<String,String> userMdata = HashMultimap.create();
expect(metaData.getUserMetadata()).andReturn(userMdata).atLeastOnce();
replay(metaData);