From dc181287f6b04104f4ee6db3cd115736a6ea204d Mon Sep 17 00:00:00 2001 From: Aled Sage Date: Fri, 9 Dec 2011 21:34:17 +0000 Subject: [PATCH] Extract TrackingJavaUrlHttpCommandExecutorService, so usable by other tests --- ...kingJavaUrlHttpCommandExecutorService.java | 71 +++++++++++++++++++ .../AWSEC2TemplateBuilderLiveTest.java | 58 ++------------- 2 files changed, 76 insertions(+), 53 deletions(-) create mode 100644 core/src/test/java/org/jclouds/http/internal/TrackingJavaUrlHttpCommandExecutorService.java diff --git a/core/src/test/java/org/jclouds/http/internal/TrackingJavaUrlHttpCommandExecutorService.java b/core/src/test/java/org/jclouds/http/internal/TrackingJavaUrlHttpCommandExecutorService.java new file mode 100644 index 0000000000..40908abbb9 --- /dev/null +++ b/core/src/test/java/org/jclouds/http/internal/TrackingJavaUrlHttpCommandExecutorService.java @@ -0,0 +1,71 @@ +package org.jclouds.http.internal; + +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; + +import org.jclouds.Constants; +import org.jclouds.http.HttpCommand; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.HttpUtils; +import org.jclouds.http.IOExceptionRetryHandler; +import org.jclouds.http.handlers.DelegatingErrorHandler; +import org.jclouds.http.handlers.DelegatingRetryHandler; + +import com.google.common.base.Supplier; +import com.google.inject.AbstractModule; +import com.google.inject.Module; +import com.google.inject.TypeLiteral; + +/** + * Records all http commands submitted, storing them in the given List. + * + * @author Adrian Cole + */ +@Singleton +public class TrackingJavaUrlHttpCommandExecutorService extends JavaUrlHttpCommandExecutorService { + + private final List commandsInvoked; + + /** + * Creates a guice module, which will bind in TrackingJavaUrlHttpCommandExecutorService and also bind + * the given list so that it is used by the tracker. + */ + public static Module newTrackingModule(final List commandsInvoked) { + return new AbstractModule() { + + @Override + protected void configure() { + bind(JavaUrlHttpCommandExecutorService.class).to( + TrackingJavaUrlHttpCommandExecutorService.class); + bind(new TypeLiteral>() { + }).toInstance(commandsInvoked); + } + }; + } + + @Inject + public TrackingJavaUrlHttpCommandExecutorService(HttpUtils utils, + @Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor, + DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler, + DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier, + @Named("untrusted") Supplier untrustedSSLContextProvider, List commandsInvoked) + throws SecurityException, NoSuchFieldException { + super(utils, ioWorkerExecutor, retryHandler, ioRetryHandler, errorHandler, wire, verifier, + untrustedSSLContextProvider); + this.commandsInvoked = commandsInvoked; + } + + @Override + public Future submit(HttpCommand command) { + commandsInvoked.add(command); + return super.submit(command); + } + +} \ No newline at end of file diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderLiveTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderLiveTest.java index 15b3df8514..69462ea315 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderLiveTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/AWSEC2TemplateBuilderLiveTest.java @@ -27,16 +27,8 @@ import java.lang.reflect.Method; import java.util.List; import java.util.Properties; import java.util.Set; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLContext; -import org.jclouds.Constants; import org.jclouds.aws.domain.Region; import org.jclouds.aws.ec2.reference.AWSEC2Constants; import org.jclouds.compute.BaseTemplateBuilderLiveTest; @@ -53,20 +45,14 @@ import org.jclouds.ec2.options.DescribeRegionsOptions; import org.jclouds.ec2.reference.EC2Constants; import org.jclouds.ec2.services.AvailabilityZoneAndRegionAsyncClient; import org.jclouds.http.HttpCommand; -import org.jclouds.http.HttpResponse; -import org.jclouds.http.HttpUtils; -import org.jclouds.http.IOExceptionRetryHandler; -import org.jclouds.http.handlers.DelegatingErrorHandler; -import org.jclouds.http.handlers.DelegatingRetryHandler; -import org.jclouds.http.internal.HttpWire; import org.jclouds.http.internal.JavaUrlHttpCommandExecutorService; +import org.jclouds.http.internal.TrackingJavaUrlHttpCommandExecutorService; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.Test; import com.google.common.base.Predicate; import com.google.common.base.Predicates; -import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.inject.AbstractModule; @@ -327,31 +313,6 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest { } } - @Singleton - public static class TrackingJavaUrlHttpCommandExecutorService extends JavaUrlHttpCommandExecutorService { - - private final List commandsInvoked; - - @Inject - public TrackingJavaUrlHttpCommandExecutorService(HttpUtils utils, - @Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor, - DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler, - DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier, - @Named("untrusted") Supplier untrustedSSLContextProvider, List commandsInvoked) - throws SecurityException, NoSuchFieldException { - super(utils, ioWorkerExecutor, retryHandler, ioRetryHandler, errorHandler, wire, verifier, - untrustedSSLContextProvider); - this.commandsInvoked = commandsInvoked; - } - - @Override - public Future submit(HttpCommand command) { - commandsInvoked.add(command); - return super.submit(command); - } - - } - @Test public void testTemplateBuilderWithLessRegions() throws IOException, SecurityException, NoSuchMethodException { ComputeServiceContext context = null; @@ -362,19 +323,10 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest { final List commandsInvoked = Lists.newArrayList(); context = new ComputeServiceContextFactory().createContext(provider, ImmutableSet. of( - new Log4JLoggingModule(), new AbstractModule() { + new Log4JLoggingModule(), TrackingJavaUrlHttpCommandExecutorService.newTrackingModule(commandsInvoked)), + overrides); - @Override - protected void configure() { - bind(JavaUrlHttpCommandExecutorService.class).to( - TrackingJavaUrlHttpCommandExecutorService.class); - bind(new TypeLiteral>() { - }).toInstance(commandsInvoked); - } - - }), overrides); - - assertOnlyOnRegionQueriedForAvailabilityZone(commandsInvoked); + assertOnlyOneRegionQueriedForAvailabilityZone(commandsInvoked); assert context.getComputeService().listImages().size() < this.context.getComputeService().listImages().size(); @@ -396,7 +348,7 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest { } } - private static void assertOnlyOnRegionQueriedForAvailabilityZone(List commandsInvoked) + private static void assertOnlyOneRegionQueriedForAvailabilityZone(List commandsInvoked) throws NoSuchMethodException { assert commandsInvoked.size() == 2 : commandsInvoked; assertEquals(getJavaMethodForRequestAtIndex(commandsInvoked, 0), AvailabilityZoneAndRegionAsyncClient.class