Issue 214: leaned test a bit

This commit is contained in:
Adrian Cole 2010-03-24 17:00:57 -07:00
parent 53bac3fa65
commit de359a52cc
7 changed files with 89 additions and 53 deletions

View File

@ -124,7 +124,7 @@ public class JCEEncryptionService extends BaseEncryptionService {
return new MD5InputStreamResult(out.toByteArray(), eTag.digest(), length); return new MD5InputStreamResult(out.toByteArray(), eTag.digest(), length);
} }
private static MessageDigest getDigest() { public static MessageDigest getDigest() {
MessageDigest eTag; MessageDigest eTag;
try { try {
eTag = MessageDigest.getInstance("MD5"); eTag = MessageDigest.getInstance("MD5");

View File

@ -21,21 +21,23 @@ package org.jclouds.http.functions.config;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.*; import java.util.Comparator;
import java.util.Date;
import java.util.Map;
import java.util.SortedSet;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import com.google.common.collect.Maps;
import com.google.inject.name.Named;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.date.DateService; import org.jclouds.date.DateService;
import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.ParseSax.HandlerWithResult; import org.jclouds.http.functions.ParseSax.HandlerWithResult;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@ -52,6 +54,7 @@ import com.google.inject.AbstractModule;
import com.google.inject.ImplementedBy; import com.google.inject.ImplementedBy;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.Scopes; import com.google.inject.Scopes;
import com.google.inject.name.Named;
/** /**
* Contains logic for parsing objects from Strings. * Contains logic for parsing objects from Strings.
@ -126,17 +129,18 @@ public class ParserModule extends AbstractModule {
return factory; return factory;
} }
@SuppressWarnings("unchecked")
@Provides @Provides
@Singleton @Singleton
Gson provideGson(DateAdapter adapter, SortedSetOfInetAddressCreator addressSetCreator, Gson provideGson(DateAdapter adapter, SortedSetOfInetAddressCreator addressSetCreator,
GsonAdapterBindings bindings) { GsonAdapterBindings bindings) {
GsonBuilder gson = new GsonBuilder(); GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(InetAddress.class, new InetAddressAdapter()); gson.registerTypeAdapter(InetAddress.class, new InetAddressAdapter());
gson.registerTypeAdapter(Date.class, adapter); gson.registerTypeAdapter(Date.class, adapter);
gson.registerTypeAdapter(new TypeToken<SortedSet<InetAddress>>() { gson.registerTypeAdapter(new TypeToken<SortedSet<InetAddress>>() {
}.getType(), addressSetCreator); }.getType(), addressSetCreator);
for(Map.Entry<Class, Object> binding : bindings.getBindings().entrySet()) { for (Map.Entry<Class, Object> binding : bindings.getBindings().entrySet()) {
gson.registerTypeAdapter(binding.getKey(), binding.getValue()); gson.registerTypeAdapter(binding.getKey(), binding.getValue());
} }
return gson.create(); return gson.create();
} }
@ -195,15 +199,18 @@ public class ParserModule extends AbstractModule {
@Singleton @Singleton
public static class GsonAdapterBindings { public static class GsonAdapterBindings {
private final Map<Class, Object> bindings = Maps.newHashMap(); @SuppressWarnings("unchecked")
private final Map<Class, Object> bindings = Maps.newHashMap();
@com.google.inject.Inject(optional=true) @SuppressWarnings("unchecked")
public void setBindings(@Named(Constants.PROPERTY_GSON_ADAPTERS) Map<Class, Object> bindings) { @com.google.inject.Inject(optional = true)
this.bindings.putAll(bindings); public void setBindings(@Named(Constants.PROPERTY_GSON_ADAPTERS) Map<Class, Object> bindings) {
} this.bindings.putAll(bindings);
}
public Map<Class, Object> getBindings() { @SuppressWarnings("unchecked")
return bindings; public Map<Class, Object> getBindings() {
} return bindings;
}
} }
} }

View File

@ -20,21 +20,26 @@ package org.jclouds.http;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.security.MessageDigest;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.jclouds.encryption.internal.JCEEncryptionService;
import org.jclouds.http.options.GetOptions; import org.jclouds.http.options.GetOptions;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.io.Closeables;
/** /**
* Tests for functionality all HttpCommandExecutorServices must express. These tests will operate * Tests for functionality all HttpCommandExecutorServices must express. These tests will operate
@ -130,33 +135,48 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
} }
/** /**
* Tests sending a big file to the server. * Tests sending a big file to the server. Note: this is a heavy test, takes several minutes to
* Note: this is a heavy test, takes several minutes to finish. * finish.
* @throws java.io.IOException *
*/ * @throws java.io.IOException
@Test */
public void testUploadBigFile() throws IOException { @Test(invocationCount = 1)
String filename = "jclouds"; public void testUploadBigFile() throws IOException {
OutputStream os = null; String filename = "jclouds";
File f = null; OutputStream os = null;
try { File f = null;
//create a file, twice big as free heap memory try {
f = File.createTempFile(filename, "tmp"); // create a file, twice big as free heap memory
f.deleteOnExit(); f = File.createTempFile(filename, "tmp");
long length = Runtime.getRuntime().freeMemory() * 2; f.deleteOnExit();
os = new FileOutputStream(f.getAbsolutePath()); long length = (long) (Runtime.getRuntime().freeMemory() * 1.1);
for(long i = 0; i < length; i++) os.write('a'); os = new BufferedOutputStream(new FileOutputStream(f.getAbsolutePath()));
MessageDigest eTag = JCEEncryptionService.getDigest();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
for (long i = 0; i < length; i++) {
eTag.update((byte) 'a');
os.write((byte) 'a');
}
os.flush(); os.flush();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
Closeables.closeQuietly(out);
}
// upload and verify the response
assertEquals(client.postWithMd5("fileso",
this.encryptionService.toBase64String(eTag.digest()), f).trim(), "created");
} finally {
if (os != null)
os.close(); os.close();
if (f != null && f.exists())
//upload and verify the response f.delete();
assertEquals(client.postWithMd5("fileso", f).trim(), "created"); }
}
} finally {
if(os != null) os.close();
if(f != null && f.exists()) f.delete();
}
}
protected AtomicInteger postFailures = new AtomicInteger(); protected AtomicInteger postFailures = new AtomicInteger();

View File

@ -19,8 +19,6 @@
package org.jclouds.http; package org.jclouds.http;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
@ -34,11 +32,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables;
import com.google.common.io.ByteStreams;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.encryption.internal.JCEEncryptionService;
import org.jclouds.http.functions.ParseSax; import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.options.HttpRequestOptions; import org.jclouds.http.options.HttpRequestOptions;
import org.jclouds.rest.Binder; import org.jclouds.rest.Binder;
@ -60,7 +53,7 @@ import com.google.common.util.concurrent.ListenableFuture;
/** /**
* Sample test for the behaviour of our Integration Test jetty server. * Sample test for the behaviour of our Integration Test jetty server.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Endpoint(Localhost.class) @Endpoint(Localhost.class)
@ -127,6 +120,7 @@ public interface IntegrationTestAsyncClient {
@POST @POST
@Path("objects/{id}") @Path("objects/{id}")
ListenableFuture<String> postWithMd5(@PathParam("id") String id, ListenableFuture<String> postWithMd5(@PathParam("id") String id,
@HeaderParam("Content-MD5") String base64MD5,
@BinderParam(BindToFilePayload.class) File file); @BinderParam(BindToFilePayload.class) File file);
static class BindToFilePayload implements Binder { static class BindToFilePayload implements Binder {
@ -134,9 +128,9 @@ public interface IntegrationTestAsyncClient {
public void bindToRequest(HttpRequest request, Object payload) { public void bindToRequest(HttpRequest request, Object payload) {
File f = (File) payload; File f = (File) payload;
if (request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE) == null) if (request.getFirstHeaderOrNull(HttpHeaders.CONTENT_TYPE) == null)
request.getHeaders().put(HttpHeaders.CONTENT_TYPE, "application/unknown"); request.getHeaders().put(HttpHeaders.CONTENT_TYPE, "application/unknown");
request.getHeaders().replaceValues(HttpHeaders.CONTENT_LENGTH, Collections.singletonList(f.length() + "")); request.getHeaders().replaceValues(HttpHeaders.CONTENT_LENGTH,
request.getHeaders().replaceValues("Content-MD5", Collections.singletonList(Base64.encodeBytes(new JCEEncryptionService().md5(f)))); Collections.singletonList(f.length() + ""));
request.setPayload(f); request.setPayload(f);
} }
} }

View File

@ -52,7 +52,7 @@ public interface IntegrationTestClient {
String postAsInputStream(String id, String toPut); String postAsInputStream(String id, String toPut);
String postWithMd5(String id, File file); String postWithMd5(String id, String base64MD5, File file);
String postJson(String id, String toPut); String postJson(String id, String toPut);

View File

@ -21,6 +21,7 @@ package org.jclouds.gae;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -249,10 +250,16 @@ public class GaeHttpCommandExecutorServiceIntegrationTest extends
} }
@Override @Override
@Test(invocationCount = 50, timeOut = 3000) @Test(enabled = false)
public void testGetBigFile() throws MalformedURLException, ExecutionException, public void testGetBigFile() throws MalformedURLException, ExecutionException,
InterruptedException, TimeoutException { InterruptedException, TimeoutException {
// disabled since test data is too big // disabled since test data is too big
} }
@Override
@Test(enabled = false)
public void testUploadBigFile() throws IOException {
// disabled since test data is too big
}
} }

View File

@ -22,6 +22,7 @@ import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS;
import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST; import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST;
import static org.jclouds.Constants.PROPERTY_USER_THREADS; import static org.jclouds.Constants.PROPERTY_USER_THREADS;
import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -72,4 +73,11 @@ public class NioTransformingHttpCommandExecutorServiceTest extends
InterruptedException, TimeoutException { InterruptedException, TimeoutException {
// disabled since test data is too big // disabled since test data is too big
} }
@Override
@Test(enabled = false)
public void testUploadBigFile() throws IOException {
// disabled since test data is too big
}
} }