From be48d352f9a548270805fdd95086e870b765ac4f Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Mon, 28 Sep 2009 01:53:44 +0000 Subject: [PATCH] Issue 103: changed class processors to use TypeLiteral to obtain class object instead of factories git-svn-id: http://jclouds.googlecode.com/svn/trunk@1936 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../storage/blob/AzureBlobStoreTest.java | 42 ++--- .../queue/config/AzureQueueContextModule.java | 1 - .../queue/AzureQueueConnectionTest.java | 41 ++--- .../ParseBlobFromHeadersAndHttpContent.java | 18 +- .../main/java/org/jclouds/rest/Endpoint.java | 8 +- .../rest/JaxrsAnnotationProcessor.java | 117 +++++++------ .../org/jclouds/rest/RestClientFactory.java | 15 +- .../org/jclouds/rest/RestClientProxy.java | 27 ++- .../org/jclouds/rest/config/JaxrsModule.java | 7 - .../java/org/jclouds/http/BaseJettyTest.java | 1 + .../rest/JaxrsAnnotationProcessorTest.java | 160 ++++++++++-------- .../CloudServersConnectionTest.java | 43 ++--- .../RackspaceAuthenticationTest.java | 30 ++-- 13 files changed, 284 insertions(+), 226 deletions(-) diff --git a/azure/storage/blob/core/src/test/java/org/jclouds/azure/storage/blob/AzureBlobStoreTest.java b/azure/storage/blob/core/src/test/java/org/jclouds/azure/storage/blob/AzureBlobStoreTest.java index 83e1f52d7f..fd417f51da 100644 --- a/azure/storage/blob/core/src/test/java/org/jclouds/azure/storage/blob/AzureBlobStoreTest.java +++ b/azure/storage/blob/core/src/test/java/org/jclouds/azure/storage/blob/AzureBlobStoreTest.java @@ -56,6 +56,9 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableMultimap; import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code AzureBlobStore} @@ -64,7 +67,6 @@ import com.google.inject.Guice; */ @Test(groups = "unit", testName = "cloudservers.AzureBlobStoreTest") public class AzureBlobStoreTest { - JaxrsAnnotationProcessor.Factory factory; public void testListContainers() throws SecurityException, NoSuchMethodException { Method method = AzureBlobStore.class.getMethod("listContainers"); @@ -259,26 +261,26 @@ public class AzureBlobStoreTest { assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); } - JaxrsAnnotationProcessor processor; - @BeforeClass void setupFactory() { - factory = Guice.createInjector( - new AbstractModule() { - @Override - protected void configure() { - bind(URI.class).annotatedWith(AzureBlob.class).toInstance( - URI.create("http://myaccount.blob.core.windows.net")); - bindConstant().annotatedWith( - Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT)) - .to("myaccount"); - bindConstant().annotatedWith( - Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY)).to( - HttpUtils.toBase64String("key".getBytes())); - } - }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), - new JavaUrlHttpCommandExecutorServiceModule()).getInstance( - JaxrsAnnotationProcessor.Factory.class); - processor = factory.create(AzureBlobStore.class); + Injector injector = Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + bind(URI.class).annotatedWith(AzureBlob.class).toInstance( + URI.create("http://myaccount.blob.core.windows.net")); + bindConstant().annotatedWith( + Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT)).to( + "myaccount"); + bindConstant().annotatedWith( + Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY)).to( + HttpUtils.toBase64String("key".getBytes())); + } + }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), + new JavaUrlHttpCommandExecutorServiceModule()); + processor = injector.getInstance(Key + .get(new TypeLiteral>() { + })); } + + JaxrsAnnotationProcessor processor; } diff --git a/azure/storage/queue/core/src/main/java/org/jclouds/azure/storage/queue/config/AzureQueueContextModule.java b/azure/storage/queue/core/src/main/java/org/jclouds/azure/storage/queue/config/AzureQueueContextModule.java index 3b1843ecf3..bc978574d0 100644 --- a/azure/storage/queue/core/src/main/java/org/jclouds/azure/storage/queue/config/AzureQueueContextModule.java +++ b/azure/storage/queue/core/src/main/java/org/jclouds/azure/storage/queue/config/AzureQueueContextModule.java @@ -41,5 +41,4 @@ public class AzureQueueContextModule extends AbstractModule { this.requireBinding(AzureQueueConnection.class); bind(AzureQueueContext.class).to(GuiceAzureQueueContext.class); } - } diff --git a/azure/storage/queue/core/src/test/java/org/jclouds/azure/storage/queue/AzureQueueConnectionTest.java b/azure/storage/queue/core/src/test/java/org/jclouds/azure/storage/queue/AzureQueueConnectionTest.java index a7938fb522..53dedcbbe3 100644 --- a/azure/storage/queue/core/src/test/java/org/jclouds/azure/storage/queue/AzureQueueConnectionTest.java +++ b/azure/storage/queue/core/src/test/java/org/jclouds/azure/storage/queue/AzureQueueConnectionTest.java @@ -53,6 +53,9 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableMultimap; import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code AzureQueueConnection} @@ -61,7 +64,6 @@ import com.google.inject.Guice; */ @Test(groups = "unit", testName = "cloudservers.AzureQueueConnectionTest") public class AzureQueueConnectionTest { - JaxrsAnnotationProcessor.Factory factory; private static final Class listOptionsVarargsClass = new ListOptions[] {} .getClass(); private static final Class createOptionsVarargsClass = new CreateOptions[] {} @@ -162,26 +164,25 @@ public class AzureQueueConnectionTest { assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); } - JaxrsAnnotationProcessor processor; - @BeforeClass void setupFactory() { - factory = Guice.createInjector( - new AbstractModule() { - @Override - protected void configure() { - bind(URI.class).annotatedWith(AzureQueue.class).toInstance( - URI.create("http://localhost:8080")); - bindConstant().annotatedWith( - Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT)) - .to("user"); - bindConstant().annotatedWith( - Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY)).to( - HttpUtils.toBase64String("key".getBytes())); - } - }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), - new JavaUrlHttpCommandExecutorServiceModule()).getInstance( - JaxrsAnnotationProcessor.Factory.class); - processor = factory.create(AzureQueueConnection.class); + Injector injector = Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + bind(URI.class).annotatedWith(AzureQueue.class).toInstance( + URI.create("http://localhost:8080")); + bindConstant().annotatedWith( + Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT)).to("user"); + bindConstant().annotatedWith( + Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY)).to( + HttpUtils.toBase64String("key".getBytes())); + } + }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), + new JavaUrlHttpCommandExecutorServiceModule()); + processor = injector.getInstance(Key + .get(new TypeLiteral>() { + })); } + + JaxrsAnnotationProcessor processor; } diff --git a/blobstore/core/src/main/java/org/jclouds/blobstore/functions/ParseBlobFromHeadersAndHttpContent.java b/blobstore/core/src/main/java/org/jclouds/blobstore/functions/ParseBlobFromHeadersAndHttpContent.java index fd53123a30..828dd9a6bb 100644 --- a/blobstore/core/src/main/java/org/jclouds/blobstore/functions/ParseBlobFromHeadersAndHttpContent.java +++ b/blobstore/core/src/main/java/org/jclouds/blobstore/functions/ParseBlobFromHeadersAndHttpContent.java @@ -29,7 +29,9 @@ import javax.ws.rs.core.HttpHeaders; import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.BlobMetadata; import org.jclouds.http.HttpException; +import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; +import org.jclouds.rest.RestContext; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; @@ -41,7 +43,7 @@ import com.google.common.base.Function; * @author Adrian Cole */ public class ParseBlobFromHeadersAndHttpContent> - implements Function { + implements Function, RestContext { private final ParseContentTypeFromHeaders metadataParser; private final BlobFactory blobFactory; @@ -84,11 +86,23 @@ public class ParseBlobFromHeadersAndHttpContent value(); + public static @interface NONE { + } + + Class value() default NONE.class; } diff --git a/core/src/main/java/org/jclouds/rest/JaxrsAnnotationProcessor.java b/core/src/main/java/org/jclouds/rest/JaxrsAnnotationProcessor.java index c9edcd497e..e6ceb1eaa1 100644 --- a/core/src/main/java/org/jclouds/rest/JaxrsAnnotationProcessor.java +++ b/core/src/main/java/org/jclouds/rest/JaxrsAnnotationProcessor.java @@ -25,6 +25,7 @@ package org.jclouds.rest; 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.UnsupportedEncodingException; import java.lang.annotation.Annotation; @@ -76,7 +77,6 @@ import com.google.common.collect.Sets; import com.google.inject.Injector; import com.google.inject.Key; import com.google.inject.TypeLiteral; -import com.google.inject.assistedinject.Assisted; import com.google.inject.internal.Lists; /** @@ -85,16 +85,17 @@ import com.google.inject.internal.Lists; * @author Adrian Cole */ @Singleton -public class JaxrsAnnotationProcessor { +public class JaxrsAnnotationProcessor { @Resource protected Logger logger = Logger.NULL; - private final Class declaring; + private final Class declaring; private final Map>> methodToIndexOfParamToEntityAnnotation = createMethodToIndexOfParamToAnnotation(EntityParam.class); private final Map>> methodToIndexOfParamToHeaderParamAnnotations = createMethodToIndexOfParamToAnnotation(HeaderParam.class); private final Map>> methodToIndexOfParamToHostPrefixParamAnnotations = createMethodToIndexOfParamToAnnotation(HostPrefixParam.class); + private final Map>> methodToindexOfParamToEndpointAnnotations = createMethodToIndexOfParamToAnnotation(Endpoint.class); private final Map>> methodToindexOfParamToQueryParamAnnotations = createMethodToIndexOfParamToAnnotation(QueryParam.class); private final Map>> methodToindexOfParamToPathParamAnnotations = createMethodToIndexOfParamToAnnotation(PathParam.class); private final Map>> methodToindexOfParamToPostParamAnnotations = createMethodToIndexOfParamToAnnotation(MapEntityParam.class); @@ -155,10 +156,6 @@ public class JaxrsAnnotationProcessor { } }); - public static interface Factory { - JaxrsAnnotationProcessor create(Class declaring); - } - private final ParseSax.Factory parserFactory; @VisibleForTesting @@ -186,10 +183,11 @@ public class JaxrsAnnotationProcessor { return null; } + @SuppressWarnings("unchecked") @Inject public JaxrsAnnotationProcessor(Injector injector, ParseSax.Factory parserFactory, - @Assisted Class declaring) { - this.declaring = declaring; + TypeLiteral typeLiteral) { + this.declaring = (Class) typeLiteral.getRawType(); this.injector = injector; this.parserFactory = parserFactory; this.optionsBinder = injector.getInstance(HttpRequestOptionsBinder.class); @@ -210,6 +208,7 @@ public class JaxrsAnnotationProcessor { methodToIndexOfParamToHeaderParamAnnotations.get(method).get(index); methodToIndexOfParamToHostPrefixParamAnnotations.get(method).get(index); methodToindexOfParamToQueryParamAnnotations.get(method).get(index); + methodToindexOfParamToEndpointAnnotations.get(method).get(index); methodToindexOfParamToPathParamAnnotations.get(method).get(index); methodToindexOfParamToPostParamAnnotations.get(method).get(index); methodToindexOfParamToParamParserAnnotations.get(method).get(index); @@ -272,7 +271,7 @@ public class JaxrsAnnotationProcessor { private HttpRequestOptionsBinder optionsBinder; public HttpRequest createRequest(Method method, Object[] args) { - URI endpoint = getEndpointFor(method); + URI endpoint = getEndpointFor(method, args); String httpMethod = getHttpMethodOrConstantOrThrowException(method); @@ -367,8 +366,31 @@ public class JaxrsAnnotationProcessor { } } + @VisibleForTesting + URI getEndpointInParametersOrNull(Method method, Object[] args) { + Map> map = indexWithOnlyOneAnnotation(method, "@Endpoint", + methodToindexOfParamToEndpointAnnotations); + if (map.size() == 1) { + Endpoint annotation = (Endpoint) map.values().iterator().next().iterator().next(); + int index = map.keySet().iterator().next(); + checkState( + annotation.value() == Endpoint.NONE.class, + String + .format( + "@Endpoint annotation at index %d on method %s should not have a value() except Endpoint.NONE ", + index, method)); + Object arg = checkNotNull(args[index], String.format("argument at index %d on method %s", + index, method)); + checkArgument(arg instanceof URI, String.format( + "argument at index %d must be a URI for method %s", index, method)); + return (URI) arg; + } + return null; + } + private UriBuilder addHostPrefixIfPresent(URI endpoint, Method method, Object[] args) { - Map> map = getIndexToHostPrefixAnnotation(method); + Map> map = indexWithOnlyOneAnnotation(method, "@HostPrefixParam", + methodToIndexOfParamToHostPrefixParamAnnotations); UriBuilder builder = UriBuilder.fromUri(endpoint); if (map.size() == 1) { HostPrefixParam param = (HostPrefixParam) map.values().iterator().next().iterator().next(); @@ -504,7 +526,8 @@ public class JaxrsAnnotationProcessor { } if (request.getEntity() == null) { - Map> indexToEntityAnnotation = getIndexToEntityAnnotation(method); + Map> indexToEntityAnnotation = indexWithOnlyOneAnnotation( + method, "@Entity", methodToIndexOfParamToEntityAnnotation); if (indexToEntityAnnotation.size() == 1) { Entry> entry = indexToEntityAnnotation.entrySet() @@ -527,42 +550,23 @@ public class JaxrsAnnotationProcessor { return request; } - private Map> getIndexToEntityAnnotation(Method method) { - Map> indexToEntityAnnotation = Maps.filterValues( - methodToIndexOfParamToEntityAnnotation.get(method), - new Predicate>() { - public boolean apply(Set input) { - return input.size() == 1; - } - }); + protected Map> indexWithOnlyOneAnnotation(Method method, + String description, Map>> toRefine) { + Map> indexToEntityAnnotation = Maps.filterValues(toRefine + .get(method), new Predicate>() { + public boolean apply(Set input) { + return input.size() == 1; + } + }); if (indexToEntityAnnotation.size() > 1) { throw new IllegalStateException(String.format( - "You must not specify more than one @Entity annotation on: %s; found %s", method - .toString(), indexToEntityAnnotation)); + "You must not specify more than one %s annotation on: %s; found %s", description, + method.toString(), indexToEntityAnnotation)); } return indexToEntityAnnotation; } - private Map> getIndexToHostPrefixAnnotation(Method method) { - Map> indexToHostPrefixAnnotation = Maps.filterValues( - methodToIndexOfParamToHostPrefixParamAnnotations.get(method), - new Predicate>() { - public boolean apply(Set input) { - return input.size() == 1; - } - }); - - if (indexToHostPrefixAnnotation.size() > 1) { - throw new IllegalStateException( - String - .format( - "You must not specify more than one @HostPrefixParam annotation on: %s; found %s", - method.toString(), indexToHostPrefixAnnotation)); - } - return indexToHostPrefixAnnotation; - } - private HttpRequestOptions findOptionsIn(Method method, Object[] args) { for (int index : methodToIndexesOfOptions.get(method)) { if (args.length >= index + 1) {// accomodate varargs @@ -718,16 +722,27 @@ public class JaxrsAnnotationProcessor { return postParams; } - public URI getEndpointFor(Method method) { - Endpoint endpoint; - if (method.isAnnotationPresent(Endpoint.class)) { - endpoint = method.getAnnotation(Endpoint.class); - } else if (declaring.isAnnotationPresent(Endpoint.class)) { - endpoint = declaring.getAnnotation(Endpoint.class); - } else { - throw new IllegalStateException( - "There must be an @Endpoint annotation on type or method: " + method); + public URI getEndpointFor(Method method, Object[] args) { + URI endpoint = getEndpointInParametersOrNull(method, args); + if (endpoint == null) { + Endpoint annotation; + if (method.isAnnotationPresent(Endpoint.class)) { + annotation = method.getAnnotation(Endpoint.class); + checkState(annotation.value() != Endpoint.NONE.class, String.format( + "@Endpoint annotation at method %s must have a value() of valid Qualifier", + method)); + } else if (declaring.isAnnotationPresent(Endpoint.class)) { + annotation = declaring.getAnnotation(Endpoint.class); + checkState(annotation.value() != Endpoint.NONE.class, String.format( + "@Endpoint annotation at type %s must have a value() of valid Qualifier", + declaring)); + } else { + throw new IllegalStateException( + "There must be an @Endpoint annotation on parameter, method or type: " + + method); + } + return injector.getInstance(Key.get(URI.class, annotation.value())); } - return injector.getInstance(Key.get(URI.class, endpoint.value())); + return endpoint; } } diff --git a/core/src/main/java/org/jclouds/rest/RestClientFactory.java b/core/src/main/java/org/jclouds/rest/RestClientFactory.java index 550e249ee6..16c6b017ea 100644 --- a/core/src/main/java/org/jclouds/rest/RestClientFactory.java +++ b/core/src/main/java/org/jclouds/rest/RestClientFactory.java @@ -23,24 +23,29 @@ */ package org.jclouds.rest; +import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import javax.inject.Inject; -import org.jclouds.rest.RestClientProxy.RestClientProxyFactory; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; +import com.google.inject.util.Types; public class RestClientFactory { - private final RestClientProxyFactory proxyFactory; + private final Injector injector; @Inject - public RestClientFactory(RestClientProxyFactory proxyFactory) { - this.proxyFactory = proxyFactory; + public RestClientFactory(Injector injector) { + this.injector = injector; } @SuppressWarnings("unchecked") public T create(Class clazz) { return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, - proxyFactory.create(clazz)); + (InvocationHandler) injector.getInstance(Key.get(TypeLiteral.get(Types + .newParameterizedType(RestClientProxy.class, clazz))))); } } diff --git a/core/src/main/java/org/jclouds/rest/RestClientProxy.java b/core/src/main/java/org/jclouds/rest/RestClientProxy.java index e57fd8938f..ea20b8d013 100644 --- a/core/src/main/java/org/jclouds/rest/RestClientProxy.java +++ b/core/src/main/java/org/jclouds/rest/RestClientProxy.java @@ -38,6 +38,7 @@ import java.util.concurrent.TimeoutException; import javax.annotation.Resource; import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Singleton; import org.jclouds.concurrent.FutureExceptionParser; import org.jclouds.http.HttpConstants; @@ -47,11 +48,12 @@ import org.jclouds.http.TransformingHttpCommand; import org.jclouds.logging.Logger; import com.google.common.base.Function; -import com.google.inject.assistedinject.Assisted; +import com.google.inject.TypeLiteral; -public class RestClientProxy implements InvocationHandler { - private final JaxrsAnnotationProcessor util; - private final Class declaring; +@Singleton +public class RestClientProxy implements InvocationHandler { + private final JaxrsAnnotationProcessor util; + private final Class declaring; private final TransformingHttpCommand.Factory commandFactory; /** @@ -64,15 +66,12 @@ public class RestClientProxy implements InvocationHandler { @Resource protected Logger logger = Logger.NULL; - public static interface RestClientProxyFactory { - RestClientProxy create(Class clazz); - } - + @SuppressWarnings("unchecked") @Inject - public RestClientProxy(JaxrsAnnotationProcessor.Factory utilFactory, - TransformingHttpCommand.Factory factory, @Assisted Class declaring) { - this.util = utilFactory.create(declaring); - this.declaring = declaring; + public RestClientProxy(TransformingHttpCommand.Factory factory, + JaxrsAnnotationProcessor util, TypeLiteral typeLiteral) { + this.util = util; + this.declaring = (Class) typeLiteral.getRawType(); this.commandFactory = factory; } @@ -156,9 +155,9 @@ public class RestClientProxy implements InvocationHandler { @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof RestClientProxy)) + if (obj == null || !(obj instanceof RestClientProxy)) return false; - RestClientProxy other = (RestClientProxy) obj; + RestClientProxy other = (RestClientProxy) obj; if (other == this) return true; if (other.declaring != this.declaring) diff --git a/core/src/main/java/org/jclouds/rest/config/JaxrsModule.java b/core/src/main/java/org/jclouds/rest/config/JaxrsModule.java index 7dd9a00735..e042dc8590 100644 --- a/core/src/main/java/org/jclouds/rest/config/JaxrsModule.java +++ b/core/src/main/java/org/jclouds/rest/config/JaxrsModule.java @@ -28,8 +28,6 @@ import javax.ws.rs.ext.RuntimeDelegate; import org.jclouds.http.TransformingHttpCommand; import org.jclouds.http.TransformingHttpCommandImpl; import org.jclouds.http.functions.config.ParserModule; -import org.jclouds.rest.JaxrsAnnotationProcessor; -import org.jclouds.rest.RestClientProxy; import org.jclouds.rest.RuntimeDelegateImpl; import com.google.inject.AbstractModule; @@ -48,11 +46,6 @@ public class JaxrsModule extends AbstractModule { protected void configure() { install(new ParserModule()); RuntimeDelegate.setInstance(new RuntimeDelegateImpl()); - bind(RestClientProxy.RestClientProxyFactory.class).toProvider( - FactoryProvider.newFactory(RestClientProxy.RestClientProxyFactory.class, RestClientProxy.class)); - bind(JaxrsAnnotationProcessor.Factory.class).toProvider( - FactoryProvider.newFactory(JaxrsAnnotationProcessor.Factory.class, - JaxrsAnnotationProcessor.class)); bind(httpCommandFactoryLiteral).toProvider( FactoryProvider.newFactory(httpCommandFactoryLiteral, new TypeLiteral>() { diff --git a/core/src/test/java/org/jclouds/http/BaseJettyTest.java b/core/src/test/java/org/jclouds/http/BaseJettyTest.java index b9a7fce23e..7fafb0a4fa 100644 --- a/core/src/test/java/org/jclouds/http/BaseJettyTest.java +++ b/core/src/test/java/org/jclouds/http/BaseJettyTest.java @@ -149,6 +149,7 @@ public abstract class BaseJettyTest { bind(URI.class).annotatedWith(Localhost.class).toInstance( URI.create("http://localhost:" + testPort)); } + }, new JDKLoggingModule(), new JaxrsModule(), createClientModule()); CloudContextBuilder.addExecutorServiceIfNotPresent(modules); injector = Guice.createInjector(modules); diff --git a/core/src/test/java/org/jclouds/rest/JaxrsAnnotationProcessorTest.java b/core/src/test/java/org/jclouds/rest/JaxrsAnnotationProcessorTest.java index 7392a85b6c..7d4eb0b662 100644 --- a/core/src/test/java/org/jclouds/rest/JaxrsAnnotationProcessorTest.java +++ b/core/src/test/java/org/jclouds/rest/JaxrsAnnotationProcessorTest.java @@ -78,6 +78,10 @@ import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; +import com.google.inject.util.Types; /** * Tests behavior of {@code JaxrsAnnotationProcessor} @@ -125,8 +129,7 @@ public class JaxrsAnnotationProcessorTest { public void testQuery() throws SecurityException, NoSuchMethodException { Method method = TestQuery.class.getMethod("foo"); - HttpRequest httpMethod = factory.create(TestQuery.class).createRequest(method, - new Object[] {}); + HttpRequest httpMethod = factory(TestQuery.class).createRequest(method, new Object[] {}); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getEndpoint().getQuery(), "x-ms-version=2009-07-17&x-ms-rubbish=bin"); @@ -135,8 +138,7 @@ public class JaxrsAnnotationProcessorTest { public void testQuery2() throws SecurityException, NoSuchMethodException { Method method = TestQuery.class.getMethod("foo2"); - HttpRequest httpMethod = factory.create(TestQuery.class).createRequest(method, - new Object[] {}); + HttpRequest httpMethod = factory(TestQuery.class).createRequest(method, new Object[] {}); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getEndpoint().getQuery(), @@ -146,7 +148,7 @@ public class JaxrsAnnotationProcessorTest { public void testQuery3() throws SecurityException, NoSuchMethodException { Method method = TestQuery.class.getMethod("foo3", String.class); - HttpRequest httpMethod = factory.create(TestQuery.class).createRequest(method, + HttpRequest httpMethod = factory(TestQuery.class).createRequest(method, new Object[] { "wonder" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), ""); @@ -164,7 +166,7 @@ public class JaxrsAnnotationProcessorTest { public void testCustomMethod() throws SecurityException, NoSuchMethodException { Method method = TestCustomMethod.class.getMethod("foo"); - HttpRequest httpMethod = factory.create(TestCustomMethod.class).createRequest(method, + HttpRequest httpMethod = factory(TestCustomMethod.class).createRequest(method, new Object[] {}); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), ""); @@ -184,8 +186,7 @@ public class JaxrsAnnotationProcessorTest { public void testOverriddenMethod() throws SecurityException, NoSuchMethodException { Method method = TestOverridden.class.getMethod("foo"); - HttpRequest httpMethod = factory.create(TestOverridden.class).createRequest(method, - new Object[] {}); + HttpRequest httpMethod = factory(TestOverridden.class).createRequest(method, new Object[] {}); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getMethod(), "POST"); @@ -198,11 +199,15 @@ public class JaxrsAnnotationProcessorTest { @Endpoint(Localhost2.class) public void foo() { } + + @POST + public void foo(@Endpoint URI endpoint) { + } } public void testOverriddenEndpointMethod() throws SecurityException, NoSuchMethodException { Method method = TestOverriddenEndpoint.class.getMethod("foo"); - HttpRequest httpMethod = factory.create(TestOverriddenEndpoint.class).createRequest(method, + HttpRequest httpMethod = factory(TestOverriddenEndpoint.class).createRequest(method, new Object[] {}); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPort(), 8081); @@ -210,6 +215,16 @@ public class JaxrsAnnotationProcessorTest { assertEquals(httpMethod.getMethod(), "POST"); } + public void testOverriddenEndpointParameter() throws SecurityException, NoSuchMethodException { + Method method = TestOverriddenEndpoint.class.getMethod("foo", URI.class); + HttpRequest httpMethod = factory(TestOverriddenEndpoint.class).createRequest(method, + new Object[] { URI.create("http://wowsa:8001") }); + assertEquals(httpMethod.getEndpoint().getHost(), "wowsa"); + assertEquals(httpMethod.getEndpoint().getPort(), 8001); + assertEquals(httpMethod.getEndpoint().getPath(), ""); + assertEquals(httpMethod.getMethod(), "POST"); + } + @Endpoint(Localhost.class) public class TestPost { @POST @@ -235,7 +250,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreatePostRequest() throws SecurityException, NoSuchMethodException { Method method = TestPost.class.getMethod("post", String.class); - HttpRequest httpMethod = factory.create(TestPost.class).createRequest(method, + HttpRequest httpMethod = factory(TestPost.class).createRequest(method, new Object[] { "data" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), ""); @@ -250,7 +265,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreatePostJsonRequest() throws SecurityException, NoSuchMethodException { Method method = TestPost.class.getMethod("postAsJson", String.class); - HttpRequest httpMethod = factory.create(TestPost.class).createRequest(method, + HttpRequest httpMethod = factory(TestPost.class).createRequest(method, new Object[] { "data" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), ""); @@ -265,7 +280,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreatePostWithPathRequest() throws SecurityException, NoSuchMethodException { Method method = TestPost.class.getMethod("postWithPath", String.class, MapEntityBinder.class); - HttpRequest httpMethod = factory.create(TestPost.class).createRequest(method, + HttpRequest httpMethod = factory(TestPost.class).createRequest(method, new Object[] { "data", new MapEntityBinder() { public void addEntityToRequest(Map postParams, HttpRequest request) { request.setEntity(postParams.get("fooble")); @@ -284,7 +299,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreatePostWithMethodBinder() throws SecurityException, NoSuchMethodException { Method method = TestPost.class.getMethod("postWithMethodBinder", String.class); - HttpRequest httpMethod = factory.create(TestPost.class).createRequest(method, + HttpRequest httpMethod = factory(TestPost.class).createRequest(method, new Object[] { "data", }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/data"); @@ -309,7 +324,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreatePutWithMethodBinder() throws SecurityException, NoSuchMethodException { Method method = TestPut.class.getMethod("putWithMethodBinder", String.class); - HttpRequest httpMethod = factory.create(TestPut.class).createRequest(method, + HttpRequest httpMethod = factory(TestPut.class).createRequest(method, new Object[] { "data", }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/data"); @@ -347,7 +362,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testRequestFilter() throws SecurityException, NoSuchMethodException { Method method = TestRequestFilter.class.getMethod("get"); - HttpRequest httpMethod = factory.create(TestRequestFilter.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequestFilter.class).createRequest(method, new Object[] {}); assertEquals(httpMethod.getFilters().size(), 2); assertEquals(httpMethod.getFilters().get(0).getClass(), TestRequestFilter1.class); @@ -366,7 +381,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testSkipEncoding() throws SecurityException, NoSuchMethodException { Method method = TestEncoding.class.getMethod("twoPaths", String.class, String.class); - HttpRequest httpMethod = factory.create(TestEncoding.class).createRequest(method, + HttpRequest httpMethod = factory(TestEncoding.class).createRequest(method, new Object[] { "1", "localhost" }); assertEquals(httpMethod.getEndpoint().getPath(), "/1/localhost"); assertEquals(httpMethod.getMethod(), HttpMethod.GET); @@ -376,7 +391,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testEncodingPath() throws SecurityException, NoSuchMethodException { Method method = TestEncoding.class.getMethod("twoPaths", String.class, String.class); - HttpRequest httpMethod = factory.create(TestEncoding.class).createRequest(method, + HttpRequest httpMethod = factory(TestEncoding.class).createRequest(method, new Object[] { "/", "localhost" }); assertEquals(httpMethod.getEndpoint().getPath(), "///localhost"); assertEquals(httpMethod.getMethod(), HttpMethod.GET); @@ -399,7 +414,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testConstantPathParam() throws SecurityException, NoSuchMethodException { Method method = TestConstantPathParam.class.getMethod("twoPaths", String.class, String.class); - HttpRequest httpMethod = factory.create(TestConstantPathParam.class).createRequest(method, + HttpRequest httpMethod = factory(TestConstantPathParam.class).createRequest(method, new Object[] { "1", "localhost" }); assertEquals(httpMethod.getEndpoint().getPath(), "/v1/ralphie/1/localhost"); assertEquals(httpMethod.getMethod(), HttpMethod.GET); @@ -441,7 +456,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testParamExtractor() throws SecurityException, NoSuchMethodException { Method method = TestPath.class.getMethod("onePathParamExtractor", String.class); - HttpRequest httpMethod = factory.create(TestPath.class).createRequest(method, + HttpRequest httpMethod = factory(TestPath.class).createRequest(method, new Object[] { "localhost" }); assertEquals(httpMethod.getEndpoint().getPath(), "/l"); assertEquals(httpMethod.getMethod(), HttpMethod.GET); @@ -451,7 +466,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testParamExtractorMethod() throws SecurityException, NoSuchMethodException { Method method = TestPath.class.getMethod("onePathParamExtractorMethod", String.class); - HttpRequest httpMethod = factory.create(TestPath.class).createRequest(method, + HttpRequest httpMethod = factory(TestPath.class).createRequest(method, new Object[] { "localhost" }); assertEquals(httpMethod.getEndpoint().getPath(), "/l"); assertEquals(httpMethod.getMethod(), HttpMethod.GET); @@ -499,7 +514,7 @@ public class JaxrsAnnotationProcessorTest { UnsupportedEncodingException { Method oneHeader = TestHeader.class.getMethod("twoHeader", String.class); Multimap headers = HashMultimap.create(); - factory.create(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader, + factory(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader, new Object[] { "robot" }); assertEquals(headers.size(), 2); assertEquals(headers.get("slash"), Collections.singletonList("/robot")); @@ -519,8 +534,8 @@ public class JaxrsAnnotationProcessorTest { UnsupportedEncodingException { Method oneHeader = TestClassHeader.class.getMethod("oneHeader", String.class); Multimap headers = HashMultimap.create(); - factory.create(TestClassHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, - oneHeader, new Object[] { "robot" }); + factory(TestClassHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader, + new Object[] { "robot" }); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot")); } @@ -530,7 +545,7 @@ public class JaxrsAnnotationProcessorTest { UnsupportedEncodingException { Method oneHeader = TestHeader.class.getMethod("oneHeader", String.class); Multimap headers = HashMultimap.create(); - factory.create(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader, + factory(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader, new Object[] { "robot" }); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot")); @@ -541,7 +556,7 @@ public class JaxrsAnnotationProcessorTest { UnsupportedEncodingException { Method twoHeaders = TestHeader.class.getMethod("twoHeaders", String.class, String.class); Multimap headers = HashMultimap.create(); - factory.create(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, twoHeaders, + factory(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, twoHeaders, new Object[] { "robot", "eggs" }); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot/eggs")); @@ -553,8 +568,8 @@ public class JaxrsAnnotationProcessorTest { Method twoHeadersOutOfOrder = TestHeader.class.getMethod("twoHeadersOutOfOrder", String.class, String.class); Multimap headers = HashMultimap.create(); - factory.create(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, - twoHeadersOutOfOrder, new Object[] { "robot", "eggs" }); + factory(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, twoHeadersOutOfOrder, + new Object[] { "robot", "eggs" }); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/eggs/robot")); } @@ -600,8 +615,8 @@ public class JaxrsAnnotationProcessorTest { @Test(expectedExceptions = { RuntimeException.class }) public void testNoTransformer() throws SecurityException, NoSuchMethodException { Method method = TestTransformers.class.getMethod("noTransformer"); - Class> transformer = factory.create( - TestTransformers.class).getParserOrThrowException(method); + Class> transformer = factory(TestTransformers.class) + .getParserOrThrowException(method); assertEquals(transformer, ReturnStringIf200.class); } @@ -609,8 +624,8 @@ public class JaxrsAnnotationProcessorTest { Method method = TestTransformers.class.getMethod("oneTransformerWithContext"); HttpRequest request = new HttpRequest("GET", URI.create("http://localhost")); Object[] args = new Object[] {}; - Function transformer = factory.create(TestTransformers.class) - .createResponseParser(method, request, args); + Function transformer = factory(TestTransformers.class).createResponseParser( + method, request, args); assertEquals(transformer.getClass(), ReturnStringIf200Context.class); assertEquals(((ReturnStringIf200Context) transformer).getArgs(), args); assertEquals(((ReturnStringIf200Context) transformer).getRequest(), request); @@ -619,8 +634,8 @@ public class JaxrsAnnotationProcessorTest { @SuppressWarnings("static-access") public void testOneTransformer() throws SecurityException, NoSuchMethodException { Method method = TestTransformers.class.getMethod("oneTransformer"); - Class> transformer = factory.create( - TestTransformers.class).getParserOrThrowException(method); + Class> transformer = factory(TestTransformers.class) + .getParserOrThrowException(method); assertEquals(transformer, ReturnStringIf200.class); } @@ -691,7 +706,7 @@ public class JaxrsAnnotationProcessorTest { GetOptions options = GetOptions.Builder.ifModifiedSince(date); HttpRequestOptions[] optionsHolder = new HttpRequestOptions[] {}; Method method = TestRequest.class.getMethod("get", String.class, optionsHolder.getClass()); - HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequest.class).createRequest(method, new Object[] { "1", options }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -708,7 +723,7 @@ public class JaxrsAnnotationProcessorTest { DateTime date = new DateTime(); GetOptions options = GetOptions.Builder.ifModifiedSince(date); Method method = TestRequest.class.getMethod("get", String.class, HttpRequestOptions.class); - HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequest.class).createRequest(method, new Object[] { "1", options }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -732,7 +747,7 @@ public class JaxrsAnnotationProcessorTest { NoSuchMethodException { PrefixOptions options = new PrefixOptions().withPrefix("1"); Method method = TestRequest.class.getMethod("get", String.class, HttpRequestOptions.class); - HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequest.class).createRequest(method, new Object[] { "1", options }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -745,7 +760,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreateGetQuery() throws SecurityException, NoSuchMethodException { Method method = TestRequest.class.getMethod("getQuery", String.class); - HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequest.class).createRequest(method, new Object[] { "1" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -756,7 +771,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreateGetQueryNull() throws SecurityException, NoSuchMethodException { Method method = TestRequest.class.getMethod("getQueryNull", String.class); - HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequest.class).createRequest(method, new Object[] { "1" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -778,7 +793,7 @@ public class JaxrsAnnotationProcessorTest { EntityOptions options = new EntityOptions(); Method method = TestRequest.class.getMethod("putOptions", String.class, HttpRequestOptions.class); - HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequest.class).createRequest(method, new Object[] { "1", options }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -802,7 +817,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreateGetRequest(String key) throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { Method method = TestRequest.class.getMethod("get", String.class, String.class); - HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequest.class).createRequest(method, new Object[] { key, "localhost" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); String expectedPath = "/" + URLEncoder.encode(key, "UTF-8").replaceAll("\\+", "%20"); @@ -816,7 +831,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreatePutRequest() throws SecurityException, NoSuchMethodException { Method method = TestRequest.class.getMethod("put", String.class, String.class); - HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequest.class).createRequest(method, new Object[] { "111", "data" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -831,7 +846,7 @@ public class JaxrsAnnotationProcessorTest { public void testCreatePutHeader() throws SecurityException, NoSuchMethodException { Method method = TestRequest.class.getMethod("putHeader", String.class, String.class); - HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, + HttpRequest httpMethod = factory(TestRequest.class).createRequest(method, new Object[] { "1", "data" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -858,7 +873,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testVirtualHostMethod() throws SecurityException, NoSuchMethodException { Method method = TestVirtualHostMethod.class.getMethod("get", String.class, String.class); - HttpRequest httpMethod = factory.create(TestVirtualHostMethod.class).createRequest(method, + HttpRequest httpMethod = factory(TestVirtualHostMethod.class).createRequest(method, new Object[] { "1", "localhost" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -893,7 +908,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testVirtualHost() throws SecurityException, NoSuchMethodException { Method method = TestVirtualHost.class.getMethod("get", String.class, String.class); - HttpRequest httpMethod = factory.create(TestVirtualHost.class).createRequest(method, + HttpRequest httpMethod = factory(TestVirtualHost.class).createRequest(method, new Object[] { "1", "localhost" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -906,7 +921,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testHostPrefix() throws SecurityException, NoSuchMethodException { Method method = TestVirtualHost.class.getMethod("getPrefix", String.class, String.class); - HttpRequest httpMethod = factory.create(TestVirtualHost.class).createRequest(method, + HttpRequest httpMethod = factory(TestVirtualHost.class).createRequest(method, new Object[] { "1", "holy" }); assertEquals(httpMethod.getEndpoint().getHost(), "holylocalhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -917,7 +932,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testHostPrefixDot() throws SecurityException, NoSuchMethodException { Method method = TestVirtualHost.class.getMethod("getPrefixDot", String.class, String.class); - HttpRequest httpMethod = factory.create(TestVirtualHost.class).createRequest(method, + HttpRequest httpMethod = factory(TestVirtualHost.class).createRequest(method, new Object[] { "1", "holy" }); assertEquals(httpMethod.getEndpoint().getHost(), "holy.localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1"); @@ -928,13 +943,13 @@ public class JaxrsAnnotationProcessorTest { @Test(expectedExceptions = IllegalArgumentException.class) public void testHostPrefixDotEmpty() throws SecurityException, NoSuchMethodException { Method method = TestVirtualHost.class.getMethod("getPrefixDot", String.class, String.class); - factory.create(TestVirtualHost.class).createRequest(method, new Object[] { "1", "" }); + factory(TestVirtualHost.class).createRequest(method, new Object[] { "1", "" }); } @Test(expectedExceptions = NullPointerException.class) public void testHostPrefixDotNull() throws SecurityException, NoSuchMethodException { Method method = TestVirtualHost.class.getMethod("getPrefixDot", String.class, String.class); - factory.create(TestVirtualHost.class).createRequest(method, new Object[] { "1", null }); + factory(TestVirtualHost.class).createRequest(method, new Object[] { "1", null }); } @Endpoint(Localhost.class) @@ -961,7 +976,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testOneHeader() throws SecurityException, NoSuchMethodException { Method method = TestHeaders.class.getMethod("oneHeader", String.class); - Multimap headers = factory.create(TestHeaders.class).buildHeaders(method, + Multimap headers = factory(TestHeaders.class).buildHeaders(method, new Object[] { "robot" }); assertEquals(headers.size(), 1); assertEquals(headers.get("header"), Collections.singletonList("robot")); @@ -970,7 +985,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testOneIntHeader() throws SecurityException, NoSuchMethodException { Method method = TestHeaders.class.getMethod("oneIntHeader", int.class); - Multimap headers = factory.create(TestHeaders.class).buildHeaders(method, + Multimap headers = factory(TestHeaders.class).buildHeaders(method, new Object[] { 1 }); assertEquals(headers.size(), 1); assertEquals(headers.get("header"), Collections.singletonList("1")); @@ -980,7 +995,7 @@ public class JaxrsAnnotationProcessorTest { public void testTwoDifferentHeaders() throws SecurityException, NoSuchMethodException { Method method = TestHeaders.class .getMethod("twoDifferentHeaders", String.class, String.class); - Multimap headers = factory.create(TestHeaders.class).buildHeaders(method, + Multimap headers = factory(TestHeaders.class).buildHeaders(method, new Object[] { "robot", "egg" }); assertEquals(headers.size(), 2); assertEquals(headers.get("header1"), Collections.singletonList("robot")); @@ -990,7 +1005,7 @@ public class JaxrsAnnotationProcessorTest { @Test public void testTwoSameHeaders() throws SecurityException, NoSuchMethodException { Method method = TestHeaders.class.getMethod("twoSameHeaders", String.class, String.class); - Multimap headers = factory.create(TestHeaders.class).buildHeaders(method, + Multimap headers = factory(TestHeaders.class).buildHeaders(method, new Object[] { "robot", "egg" }); assertEquals(headers.size(), 2); Collection values = headers.get("header"); @@ -1018,8 +1033,8 @@ public class JaxrsAnnotationProcessorTest { public void testPut() throws SecurityException, NoSuchMethodException { Method method = TestEntity.class.getMethod("put", String.class); HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost:8080")); - factory.create(TestEntity.class).buildEntityIfPostOrPutRequest(method, - new Object[] { "test" }, request); + factory(TestEntity.class).buildEntityIfPostOrPutRequest(method, new Object[] { "test" }, + request); assertEquals(request.getEntity(), "test"); assertEquals(request.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections .singletonList("application/unknown")); @@ -1031,7 +1046,7 @@ public class JaxrsAnnotationProcessorTest { public void putWithPath() throws SecurityException, NoSuchMethodException { Method method = TestEntity.class.getMethod("putWithPath", String.class, String.class); HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost:8080")); - factory.create(TestEntity.class).buildEntityIfPostOrPutRequest(method, + factory(TestEntity.class).buildEntityIfPostOrPutRequest(method, new Object[] { "rabble", "test" }, request); assertEquals(request.getEntity(), "test"); assertEquals(request.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections @@ -1044,27 +1059,32 @@ public class JaxrsAnnotationProcessorTest { public void testPutTwoEntities() throws SecurityException, NoSuchMethodException { Method method = TestEntity.class.getMethod("twoEntities", String.class, String.class); HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost:8080")); - factory.create(TestEntity.class).buildEntityIfPostOrPutRequest(method, + factory(TestEntity.class).buildEntityIfPostOrPutRequest(method, new Object[] { "test", "ralphie" }, request); } - JaxrsAnnotationProcessor.Factory factory; + @SuppressWarnings("unchecked") + private JaxrsAnnotationProcessor factory(Class clazz) { + return ((JaxrsAnnotationProcessor) injector.getInstance(Key.get(TypeLiteral.get(Types + .newParameterizedType(JaxrsAnnotationProcessor.class, clazz))))); + } + + Injector injector; DateService dateService = new DateService(); @BeforeClass void setupFactory() { - factory = Guice.createInjector( - new AbstractModule() { - @Override - protected void configure() { - bindConstant().annotatedWith(Jsr330.named("testaccount")).to("ralphie"); - bind(URI.class).annotatedWith(Localhost.class).toInstance( - URI.create("http://localhost:8080")); - bind(URI.class).annotatedWith(Localhost2.class).toInstance( - URI.create("http://localhost:8081")); - } - }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), - new JavaUrlHttpCommandExecutorServiceModule()).getInstance( - JaxrsAnnotationProcessor.Factory.class); + injector = Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + bindConstant().annotatedWith(Jsr330.named("testaccount")).to("ralphie"); + bind(URI.class).annotatedWith(Localhost.class).toInstance( + URI.create("http://localhost:8080")); + bind(URI.class).annotatedWith(Localhost2.class).toInstance( + URI.create("http://localhost:8081")); + } + }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), + new JavaUrlHttpCommandExecutorServiceModule()); + } } diff --git a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java index 1ca757c534..13050ec82b 100755 --- a/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java +++ b/rackspace/cloudservers/core/src/test/java/org/jclouds/rackspace/cloudservers/CloudServersConnectionTest.java @@ -83,7 +83,10 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableMap; import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; import com.google.inject.Provides; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code CloudServersConnection} @@ -92,7 +95,6 @@ import com.google.inject.Provides; */ @Test(groups = "unit", testName = "cloudservers.CloudServersConnectionTest") public class CloudServersConnectionTest { - JaxrsAnnotationProcessor.Factory factory; private static final Class listOptionsVarargsClass = new ListOptions[] {} .getClass(); private static final Class createServerOptionsVarargsClass = new CreateServerOptions[] {} @@ -871,27 +873,28 @@ public class CloudServersConnectionTest { ReturnTrueIf2xx.class); } - JaxrsAnnotationProcessor processor; - @BeforeClass void setupFactory() { - factory = Guice.createInjector( - new AbstractModule() { - @Override - protected void configure() { - bind(URI.class).annotatedWith(CloudServers.class).toInstance( - URI.create("http://localhost:8080")); - } + Injector injector = Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + bind(URI.class).annotatedWith(CloudServers.class).toInstance( + URI.create("http://localhost:8080")); + } - @SuppressWarnings("unused") - @Provides - @Authentication - public String getAuthToken() { - return "testtoken"; - } - }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), - new JavaUrlHttpCommandExecutorServiceModule()).getInstance( - JaxrsAnnotationProcessor.Factory.class); - processor = factory.create(CloudServersConnection.class); + @SuppressWarnings("unused") + @Provides + @Authentication + public String getAuthToken() { + return "testtoken"; + } + }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), + new JavaUrlHttpCommandExecutorServiceModule()); + processor = injector.getInstance(Key + .get(new TypeLiteral>() { + })); } + + JaxrsAnnotationProcessor processor; + } diff --git a/rackspace/core/src/test/java/org/jclouds/rackspace/RackspaceAuthenticationTest.java b/rackspace/core/src/test/java/org/jclouds/rackspace/RackspaceAuthenticationTest.java index 6feffd1514..3d93cd469a 100644 --- a/rackspace/core/src/test/java/org/jclouds/rackspace/RackspaceAuthenticationTest.java +++ b/rackspace/core/src/test/java/org/jclouds/rackspace/RackspaceAuthenticationTest.java @@ -44,6 +44,9 @@ import org.testng.annotations.Test; import com.google.inject.AbstractModule; import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code JaxrsAnnotationProcessor} @@ -53,13 +56,12 @@ import com.google.inject.Guice; @Test(groups = "unit", testName = "rackspace.RackspaceAuthentication") public class RackspaceAuthenticationTest { - JaxrsAnnotationProcessor.Factory factory; + private JaxrsAnnotationProcessor processor; public void testAuthenticate() throws SecurityException, NoSuchMethodException { Method method = RackspaceAuthentication.class.getMethod("authenticate", String.class, String.class); - HttpRequest httpMethod = factory.create(RackspaceAuthentication.class).createRequest(method, - new Object[] { "foo", "bar" }); + HttpRequest httpMethod = processor.createRequest(method, new Object[] { "foo", "bar" }); assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/auth"); assertEquals(httpMethod.getMethod(), HttpMethod.GET); @@ -68,7 +70,6 @@ public class RackspaceAuthenticationTest { .singletonList("foo")); assertEquals(httpMethod.getHeaders().get(RackspaceHeaders.AUTH_KEY), Collections .singletonList("bar")); - factory.create(RackspaceAuthentication.class); assertEquals(JaxrsAnnotationProcessor.getParserOrThrowException(method), ParseAuthenticationResponseFromHeaders.class); @@ -76,16 +77,17 @@ public class RackspaceAuthenticationTest { @BeforeClass void setupFactory() { - factory = Guice.createInjector( - new AbstractModule() { - @Override - protected void configure() { - bind(URI.class).annotatedWith(Authentication.class).toInstance( - URI.create("http://localhost:8080")); - } - }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), - new JavaUrlHttpCommandExecutorServiceModule()).getInstance( - JaxrsAnnotationProcessor.Factory.class); + Injector injector = Guice.createInjector(new AbstractModule() { + @Override + protected void configure() { + bind(URI.class).annotatedWith(Authentication.class).toInstance( + URI.create("http://localhost:8080")); + } + }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), + new JavaUrlHttpCommandExecutorServiceModule()); + processor = injector.getInstance(Key + .get(new TypeLiteral>() { + })); } }