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
This commit is contained in:
adrian.f.cole 2009-09-28 01:53:44 +00:00
parent 2de58bb5b9
commit be48d352f9
13 changed files with 284 additions and 226 deletions

View File

@ -56,6 +56,9 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Guice; 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} * Tests behavior of {@code AzureBlobStore}
@ -64,7 +67,6 @@ import com.google.inject.Guice;
*/ */
@Test(groups = "unit", testName = "cloudservers.AzureBlobStoreTest") @Test(groups = "unit", testName = "cloudservers.AzureBlobStoreTest")
public class AzureBlobStoreTest { public class AzureBlobStoreTest {
JaxrsAnnotationProcessor.Factory factory;
public void testListContainers() throws SecurityException, NoSuchMethodException { public void testListContainers() throws SecurityException, NoSuchMethodException {
Method method = AzureBlobStore.class.getMethod("listContainers"); Method method = AzureBlobStore.class.getMethod("listContainers");
@ -259,26 +261,26 @@ public class AzureBlobStoreTest {
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
} }
JaxrsAnnotationProcessor processor;
@BeforeClass @BeforeClass
void setupFactory() { void setupFactory() {
factory = Guice.createInjector( Injector injector = Guice.createInjector(new AbstractModule() {
new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
bind(URI.class).annotatedWith(AzureBlob.class).toInstance( bind(URI.class).annotatedWith(AzureBlob.class).toInstance(
URI.create("http://myaccount.blob.core.windows.net")); URI.create("http://myaccount.blob.core.windows.net"));
bindConstant().annotatedWith( bindConstant().annotatedWith(
Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT)) Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT)).to(
.to("myaccount"); "myaccount");
bindConstant().annotatedWith( bindConstant().annotatedWith(
Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY)).to( Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY)).to(
HttpUtils.toBase64String("key".getBytes())); HttpUtils.toBase64String("key".getBytes()));
} }
}, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()),
new JavaUrlHttpCommandExecutorServiceModule()).getInstance( new JavaUrlHttpCommandExecutorServiceModule());
JaxrsAnnotationProcessor.Factory.class); processor = injector.getInstance(Key
processor = factory.create(AzureBlobStore.class); .get(new TypeLiteral<JaxrsAnnotationProcessor<AzureBlobStore>>() {
}));
} }
JaxrsAnnotationProcessor<AzureBlobStore> processor;
} }

View File

@ -41,5 +41,4 @@ public class AzureQueueContextModule extends AbstractModule {
this.requireBinding(AzureQueueConnection.class); this.requireBinding(AzureQueueConnection.class);
bind(AzureQueueContext.class).to(GuiceAzureQueueContext.class); bind(AzureQueueContext.class).to(GuiceAzureQueueContext.class);
} }
} }

View File

@ -53,6 +53,9 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Guice; 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} * Tests behavior of {@code AzureQueueConnection}
@ -61,7 +64,6 @@ import com.google.inject.Guice;
*/ */
@Test(groups = "unit", testName = "cloudservers.AzureQueueConnectionTest") @Test(groups = "unit", testName = "cloudservers.AzureQueueConnectionTest")
public class AzureQueueConnectionTest { public class AzureQueueConnectionTest {
JaxrsAnnotationProcessor.Factory factory;
private static final Class<? extends ListOptions[]> listOptionsVarargsClass = new ListOptions[] {} private static final Class<? extends ListOptions[]> listOptionsVarargsClass = new ListOptions[] {}
.getClass(); .getClass();
private static final Class<? extends CreateOptions[]> createOptionsVarargsClass = new CreateOptions[] {} private static final Class<? extends CreateOptions[]> createOptionsVarargsClass = new CreateOptions[] {}
@ -162,26 +164,25 @@ public class AzureQueueConnectionTest {
assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null); assertEquals(processor.createExceptionParserOrNullIfNotFound(method), null);
} }
JaxrsAnnotationProcessor processor;
@BeforeClass @BeforeClass
void setupFactory() { void setupFactory() {
factory = Guice.createInjector( Injector injector = Guice.createInjector(new AbstractModule() {
new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
bind(URI.class).annotatedWith(AzureQueue.class).toInstance( bind(URI.class).annotatedWith(AzureQueue.class).toInstance(
URI.create("http://localhost:8080")); URI.create("http://localhost:8080"));
bindConstant().annotatedWith( bindConstant().annotatedWith(
Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT)) Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_ACCOUNT)).to("user");
.to("user");
bindConstant().annotatedWith( bindConstant().annotatedWith(
Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY)).to( Jsr330.named(AzureStorageConstants.PROPERTY_AZURESTORAGE_KEY)).to(
HttpUtils.toBase64String("key".getBytes())); HttpUtils.toBase64String("key".getBytes()));
} }
}, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()),
new JavaUrlHttpCommandExecutorServiceModule()).getInstance( new JavaUrlHttpCommandExecutorServiceModule());
JaxrsAnnotationProcessor.Factory.class); processor = injector.getInstance(Key
processor = factory.create(AzureQueueConnection.class); .get(new TypeLiteral<JaxrsAnnotationProcessor<AzureQueueConnection>>() {
}));
} }
JaxrsAnnotationProcessor<AzureQueueConnection> processor;
} }

View File

@ -29,7 +29,9 @@ import javax.ws.rs.core.HttpHeaders;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobMetadata; import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.http.HttpException; import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.rest.RestContext;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function; import com.google.common.base.Function;
@ -41,7 +43,7 @@ import com.google.common.base.Function;
* @author Adrian Cole * @author Adrian Cole
*/ */
public class ParseBlobFromHeadersAndHttpContent<M extends BlobMetadata, B extends Blob<M>> public class ParseBlobFromHeadersAndHttpContent<M extends BlobMetadata, B extends Blob<M>>
implements Function<HttpResponse, B> { implements Function<HttpResponse, B>, RestContext {
private final ParseContentTypeFromHeaders<M> metadataParser; private final ParseContentTypeFromHeaders<M> metadataParser;
private final BlobFactory<M, B> blobFactory; private final BlobFactory<M, B> blobFactory;
@ -91,4 +93,16 @@ public class ParseBlobFromHeadersAndHttpContent<M extends BlobMetadata, B extend
} }
} }
public Object[] getArgs() {
return metadataParser.getArgs();
}
public HttpRequest getRequest() {
return metadataParser.getRequest();
}
public void setContext(HttpRequest request, Object[] args) {
metadataParser.setContext(request, args);
}
} }

View File

@ -24,6 +24,7 @@
package org.jclouds.rest; package org.jclouds.rest;
import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
@ -36,8 +37,11 @@ import java.lang.annotation.Target;
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Target( { TYPE, METHOD }) @Target( { TYPE, METHOD, PARAMETER })
@Retention(RUNTIME) @Retention(RUNTIME)
public @interface Endpoint { public @interface Endpoint {
Class<? extends Annotation> value(); public static @interface NONE {
}
Class<? extends Annotation> value() default NONE.class;
} }

View File

@ -25,6 +25,7 @@ package org.jclouds.rest;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@ -76,7 +77,6 @@ import com.google.common.collect.Sets;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Key; import com.google.inject.Key;
import com.google.inject.TypeLiteral; import com.google.inject.TypeLiteral;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.internal.Lists; import com.google.inject.internal.Lists;
/** /**
@ -85,16 +85,17 @@ import com.google.inject.internal.Lists;
* @author Adrian Cole * @author Adrian Cole
*/ */
@Singleton @Singleton
public class JaxrsAnnotationProcessor { public class JaxrsAnnotationProcessor<T> {
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private final Class<?> declaring; private final Class<T> declaring;
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToEntityAnnotation = createMethodToIndexOfParamToAnnotation(EntityParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToEntityAnnotation = createMethodToIndexOfParamToAnnotation(EntityParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHeaderParamAnnotations = createMethodToIndexOfParamToAnnotation(HeaderParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHeaderParamAnnotations = createMethodToIndexOfParamToAnnotation(HeaderParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHostPrefixParamAnnotations = createMethodToIndexOfParamToAnnotation(HostPrefixParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToIndexOfParamToHostPrefixParamAnnotations = createMethodToIndexOfParamToAnnotation(HostPrefixParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToEndpointAnnotations = createMethodToIndexOfParamToAnnotation(Endpoint.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToQueryParamAnnotations = createMethodToIndexOfParamToAnnotation(QueryParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToQueryParamAnnotations = createMethodToIndexOfParamToAnnotation(QueryParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPathParamAnnotations = createMethodToIndexOfParamToAnnotation(PathParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPathParamAnnotations = createMethodToIndexOfParamToAnnotation(PathParam.class);
private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPostParamAnnotations = createMethodToIndexOfParamToAnnotation(MapEntityParam.class); private final Map<Method, Map<Integer, Set<Annotation>>> methodToindexOfParamToPostParamAnnotations = createMethodToIndexOfParamToAnnotation(MapEntityParam.class);
@ -155,10 +156,6 @@ public class JaxrsAnnotationProcessor {
} }
}); });
public static interface Factory {
JaxrsAnnotationProcessor create(Class<?> declaring);
}
private final ParseSax.Factory parserFactory; private final ParseSax.Factory parserFactory;
@VisibleForTesting @VisibleForTesting
@ -186,10 +183,11 @@ public class JaxrsAnnotationProcessor {
return null; return null;
} }
@SuppressWarnings("unchecked")
@Inject @Inject
public JaxrsAnnotationProcessor(Injector injector, ParseSax.Factory parserFactory, public JaxrsAnnotationProcessor(Injector injector, ParseSax.Factory parserFactory,
@Assisted Class<?> declaring) { TypeLiteral<T> typeLiteral) {
this.declaring = declaring; this.declaring = (Class<T>) typeLiteral.getRawType();
this.injector = injector; this.injector = injector;
this.parserFactory = parserFactory; this.parserFactory = parserFactory;
this.optionsBinder = injector.getInstance(HttpRequestOptionsBinder.class); this.optionsBinder = injector.getInstance(HttpRequestOptionsBinder.class);
@ -210,6 +208,7 @@ public class JaxrsAnnotationProcessor {
methodToIndexOfParamToHeaderParamAnnotations.get(method).get(index); methodToIndexOfParamToHeaderParamAnnotations.get(method).get(index);
methodToIndexOfParamToHostPrefixParamAnnotations.get(method).get(index); methodToIndexOfParamToHostPrefixParamAnnotations.get(method).get(index);
methodToindexOfParamToQueryParamAnnotations.get(method).get(index); methodToindexOfParamToQueryParamAnnotations.get(method).get(index);
methodToindexOfParamToEndpointAnnotations.get(method).get(index);
methodToindexOfParamToPathParamAnnotations.get(method).get(index); methodToindexOfParamToPathParamAnnotations.get(method).get(index);
methodToindexOfParamToPostParamAnnotations.get(method).get(index); methodToindexOfParamToPostParamAnnotations.get(method).get(index);
methodToindexOfParamToParamParserAnnotations.get(method).get(index); methodToindexOfParamToParamParserAnnotations.get(method).get(index);
@ -272,7 +271,7 @@ public class JaxrsAnnotationProcessor {
private HttpRequestOptionsBinder optionsBinder; private HttpRequestOptionsBinder optionsBinder;
public HttpRequest createRequest(Method method, Object[] args) { public HttpRequest createRequest(Method method, Object[] args) {
URI endpoint = getEndpointFor(method); URI endpoint = getEndpointFor(method, args);
String httpMethod = getHttpMethodOrConstantOrThrowException(method); String httpMethod = getHttpMethodOrConstantOrThrowException(method);
@ -367,8 +366,31 @@ public class JaxrsAnnotationProcessor {
} }
} }
@VisibleForTesting
URI getEndpointInParametersOrNull(Method method, Object[] args) {
Map<Integer, Set<Annotation>> 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) { private UriBuilder addHostPrefixIfPresent(URI endpoint, Method method, Object[] args) {
Map<Integer, Set<Annotation>> map = getIndexToHostPrefixAnnotation(method); Map<Integer, Set<Annotation>> map = indexWithOnlyOneAnnotation(method, "@HostPrefixParam",
methodToIndexOfParamToHostPrefixParamAnnotations);
UriBuilder builder = UriBuilder.fromUri(endpoint); UriBuilder builder = UriBuilder.fromUri(endpoint);
if (map.size() == 1) { if (map.size() == 1) {
HostPrefixParam param = (HostPrefixParam) map.values().iterator().next().iterator().next(); HostPrefixParam param = (HostPrefixParam) map.values().iterator().next().iterator().next();
@ -504,7 +526,8 @@ public class JaxrsAnnotationProcessor {
} }
if (request.getEntity() == null) { if (request.getEntity() == null) {
Map<Integer, Set<Annotation>> indexToEntityAnnotation = getIndexToEntityAnnotation(method); Map<Integer, Set<Annotation>> indexToEntityAnnotation = indexWithOnlyOneAnnotation(
method, "@Entity", methodToIndexOfParamToEntityAnnotation);
if (indexToEntityAnnotation.size() == 1) { if (indexToEntityAnnotation.size() == 1) {
Entry<Integer, Set<Annotation>> entry = indexToEntityAnnotation.entrySet() Entry<Integer, Set<Annotation>> entry = indexToEntityAnnotation.entrySet()
@ -527,10 +550,10 @@ public class JaxrsAnnotationProcessor {
return request; return request;
} }
private Map<Integer, Set<Annotation>> getIndexToEntityAnnotation(Method method) { protected Map<Integer, Set<Annotation>> indexWithOnlyOneAnnotation(Method method,
Map<Integer, Set<Annotation>> indexToEntityAnnotation = Maps.filterValues( String description, Map<Method, Map<Integer, Set<Annotation>>> toRefine) {
methodToIndexOfParamToEntityAnnotation.get(method), Map<Integer, Set<Annotation>> indexToEntityAnnotation = Maps.filterValues(toRefine
new Predicate<Set<Annotation>>() { .get(method), new Predicate<Set<Annotation>>() {
public boolean apply(Set<Annotation> input) { public boolean apply(Set<Annotation> input) {
return input.size() == 1; return input.size() == 1;
} }
@ -538,31 +561,12 @@ public class JaxrsAnnotationProcessor {
if (indexToEntityAnnotation.size() > 1) { if (indexToEntityAnnotation.size() > 1) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"You must not specify more than one @Entity annotation on: %s; found %s", method "You must not specify more than one %s annotation on: %s; found %s", description,
.toString(), indexToEntityAnnotation)); method.toString(), indexToEntityAnnotation));
} }
return indexToEntityAnnotation; return indexToEntityAnnotation;
} }
private Map<Integer, Set<Annotation>> getIndexToHostPrefixAnnotation(Method method) {
Map<Integer, Set<Annotation>> indexToHostPrefixAnnotation = Maps.filterValues(
methodToIndexOfParamToHostPrefixParamAnnotations.get(method),
new Predicate<Set<Annotation>>() {
public boolean apply(Set<Annotation> 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) { private HttpRequestOptions findOptionsIn(Method method, Object[] args) {
for (int index : methodToIndexesOfOptions.get(method)) { for (int index : methodToIndexesOfOptions.get(method)) {
if (args.length >= index + 1) {// accomodate varargs if (args.length >= index + 1) {// accomodate varargs
@ -718,16 +722,27 @@ public class JaxrsAnnotationProcessor {
return postParams; return postParams;
} }
public URI getEndpointFor(Method method) { public URI getEndpointFor(Method method, Object[] args) {
Endpoint endpoint; URI endpoint = getEndpointInParametersOrNull(method, args);
if (endpoint == null) {
Endpoint annotation;
if (method.isAnnotationPresent(Endpoint.class)) { if (method.isAnnotationPresent(Endpoint.class)) {
endpoint = method.getAnnotation(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)) { } else if (declaring.isAnnotationPresent(Endpoint.class)) {
endpoint = declaring.getAnnotation(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 { } else {
throw new IllegalStateException( throw new IllegalStateException(
"There must be an @Endpoint annotation on type or method: " + method); "There must be an @Endpoint annotation on parameter, method or type: "
+ method);
} }
return injector.getInstance(Key.get(URI.class, endpoint.value())); return injector.getInstance(Key.get(URI.class, annotation.value()));
}
return endpoint;
} }
} }

View File

@ -23,24 +23,29 @@
*/ */
package org.jclouds.rest; package org.jclouds.rest;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import javax.inject.Inject; 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 { public class RestClientFactory {
private final RestClientProxyFactory proxyFactory; private final Injector injector;
@Inject @Inject
public RestClientFactory(RestClientProxyFactory proxyFactory) { public RestClientFactory(Injector injector) {
this.proxyFactory = proxyFactory; this.injector = injector;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T create(Class<T> clazz) { public <T> T create(Class<T> clazz) {
return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new 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)))));
} }
} }

View File

@ -38,6 +38,7 @@ import java.util.concurrent.TimeoutException;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.concurrent.FutureExceptionParser; import org.jclouds.concurrent.FutureExceptionParser;
import org.jclouds.http.HttpConstants; import org.jclouds.http.HttpConstants;
@ -47,11 +48,12 @@ import org.jclouds.http.TransformingHttpCommand;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.inject.assistedinject.Assisted; import com.google.inject.TypeLiteral;
public class RestClientProxy implements InvocationHandler { @Singleton
private final JaxrsAnnotationProcessor util; public class RestClientProxy<T> implements InvocationHandler {
private final Class<?> declaring; private final JaxrsAnnotationProcessor<T> util;
private final Class<T> declaring;
private final TransformingHttpCommand.Factory commandFactory; private final TransformingHttpCommand.Factory commandFactory;
/** /**
@ -64,15 +66,12 @@ public class RestClientProxy implements InvocationHandler {
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
public static interface RestClientProxyFactory { @SuppressWarnings("unchecked")
RestClientProxy create(Class<?> clazz);
}
@Inject @Inject
public RestClientProxy(JaxrsAnnotationProcessor.Factory utilFactory, public RestClientProxy(TransformingHttpCommand.Factory factory,
TransformingHttpCommand.Factory factory, @Assisted Class<?> declaring) { JaxrsAnnotationProcessor<T> util, TypeLiteral<T> typeLiteral) {
this.util = utilFactory.create(declaring); this.util = util;
this.declaring = declaring; this.declaring = (Class<T>) typeLiteral.getRawType();
this.commandFactory = factory; this.commandFactory = factory;
} }
@ -156,9 +155,9 @@ public class RestClientProxy implements InvocationHandler {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == null || !(obj instanceof RestClientProxy)) if (obj == null || !(obj instanceof RestClientProxy<?>))
return false; return false;
RestClientProxy other = (RestClientProxy) obj; RestClientProxy<?> other = (RestClientProxy<?>) obj;
if (other == this) if (other == this)
return true; return true;
if (other.declaring != this.declaring) if (other.declaring != this.declaring)

View File

@ -28,8 +28,6 @@ import javax.ws.rs.ext.RuntimeDelegate;
import org.jclouds.http.TransformingHttpCommand; import org.jclouds.http.TransformingHttpCommand;
import org.jclouds.http.TransformingHttpCommandImpl; import org.jclouds.http.TransformingHttpCommandImpl;
import org.jclouds.http.functions.config.ParserModule; import org.jclouds.http.functions.config.ParserModule;
import org.jclouds.rest.JaxrsAnnotationProcessor;
import org.jclouds.rest.RestClientProxy;
import org.jclouds.rest.RuntimeDelegateImpl; import org.jclouds.rest.RuntimeDelegateImpl;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
@ -48,11 +46,6 @@ public class JaxrsModule extends AbstractModule {
protected void configure() { protected void configure() {
install(new ParserModule()); install(new ParserModule());
RuntimeDelegate.setInstance(new RuntimeDelegateImpl()); 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( bind(httpCommandFactoryLiteral).toProvider(
FactoryProvider.newFactory(httpCommandFactoryLiteral, FactoryProvider.newFactory(httpCommandFactoryLiteral,
new TypeLiteral<TransformingHttpCommandImpl<?>>() { new TypeLiteral<TransformingHttpCommandImpl<?>>() {

View File

@ -149,6 +149,7 @@ public abstract class BaseJettyTest {
bind(URI.class).annotatedWith(Localhost.class).toInstance( bind(URI.class).annotatedWith(Localhost.class).toInstance(
URI.create("http://localhost:" + testPort)); URI.create("http://localhost:" + testPort));
} }
}, new JDKLoggingModule(), new JaxrsModule(), createClientModule()); }, new JDKLoggingModule(), new JaxrsModule(), createClientModule());
CloudContextBuilder.addExecutorServiceIfNotPresent(modules); CloudContextBuilder.addExecutorServiceIfNotPresent(modules);
injector = Guice.createInjector(modules); injector = Guice.createInjector(modules);

View File

@ -78,6 +78,10 @@ import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Guice; 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} * Tests behavior of {@code JaxrsAnnotationProcessor}
@ -125,8 +129,7 @@ public class JaxrsAnnotationProcessorTest {
public void testQuery() throws SecurityException, NoSuchMethodException { public void testQuery() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("foo"); Method method = TestQuery.class.getMethod("foo");
HttpRequest httpMethod = factory.create(TestQuery.class).createRequest(method, HttpRequest httpMethod = factory(TestQuery.class).createRequest(method, new Object[] {});
new Object[] {});
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getEndpoint().getPath(), "");
assertEquals(httpMethod.getEndpoint().getQuery(), "x-ms-version=2009-07-17&x-ms-rubbish=bin"); 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 { public void testQuery2() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("foo2"); Method method = TestQuery.class.getMethod("foo2");
HttpRequest httpMethod = factory.create(TestQuery.class).createRequest(method, HttpRequest httpMethod = factory(TestQuery.class).createRequest(method, new Object[] {});
new Object[] {});
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getEndpoint().getPath(), "");
assertEquals(httpMethod.getEndpoint().getQuery(), assertEquals(httpMethod.getEndpoint().getQuery(),
@ -146,7 +148,7 @@ public class JaxrsAnnotationProcessorTest {
public void testQuery3() throws SecurityException, NoSuchMethodException { public void testQuery3() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("foo3", String.class); 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" }); new Object[] { "wonder" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getEndpoint().getPath(), "");
@ -164,7 +166,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCustomMethod() throws SecurityException, NoSuchMethodException { public void testCustomMethod() throws SecurityException, NoSuchMethodException {
Method method = TestCustomMethod.class.getMethod("foo"); Method method = TestCustomMethod.class.getMethod("foo");
HttpRequest httpMethod = factory.create(TestCustomMethod.class).createRequest(method, HttpRequest httpMethod = factory(TestCustomMethod.class).createRequest(method,
new Object[] {}); new Object[] {});
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getEndpoint().getPath(), "");
@ -184,8 +186,7 @@ public class JaxrsAnnotationProcessorTest {
public void testOverriddenMethod() throws SecurityException, NoSuchMethodException { public void testOverriddenMethod() throws SecurityException, NoSuchMethodException {
Method method = TestOverridden.class.getMethod("foo"); Method method = TestOverridden.class.getMethod("foo");
HttpRequest httpMethod = factory.create(TestOverridden.class).createRequest(method, HttpRequest httpMethod = factory(TestOverridden.class).createRequest(method, new Object[] {});
new Object[] {});
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getEndpoint().getPath(), "");
assertEquals(httpMethod.getMethod(), "POST"); assertEquals(httpMethod.getMethod(), "POST");
@ -198,11 +199,15 @@ public class JaxrsAnnotationProcessorTest {
@Endpoint(Localhost2.class) @Endpoint(Localhost2.class)
public void foo() { public void foo() {
} }
@POST
public void foo(@Endpoint URI endpoint) {
}
} }
public void testOverriddenEndpointMethod() throws SecurityException, NoSuchMethodException { public void testOverriddenEndpointMethod() throws SecurityException, NoSuchMethodException {
Method method = TestOverriddenEndpoint.class.getMethod("foo"); Method method = TestOverriddenEndpoint.class.getMethod("foo");
HttpRequest httpMethod = factory.create(TestOverriddenEndpoint.class).createRequest(method, HttpRequest httpMethod = factory(TestOverriddenEndpoint.class).createRequest(method,
new Object[] {}); new Object[] {});
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPort(), 8081); assertEquals(httpMethod.getEndpoint().getPort(), 8081);
@ -210,6 +215,16 @@ public class JaxrsAnnotationProcessorTest {
assertEquals(httpMethod.getMethod(), "POST"); 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) @Endpoint(Localhost.class)
public class TestPost { public class TestPost {
@POST @POST
@ -235,7 +250,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreatePostRequest() throws SecurityException, NoSuchMethodException { public void testCreatePostRequest() throws SecurityException, NoSuchMethodException {
Method method = TestPost.class.getMethod("post", String.class); 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" }); new Object[] { "data" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getEndpoint().getPath(), "");
@ -250,7 +265,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreatePostJsonRequest() throws SecurityException, NoSuchMethodException { public void testCreatePostJsonRequest() throws SecurityException, NoSuchMethodException {
Method method = TestPost.class.getMethod("postAsJson", String.class); 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" }); new Object[] { "data" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), ""); assertEquals(httpMethod.getEndpoint().getPath(), "");
@ -265,7 +280,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreatePostWithPathRequest() throws SecurityException, NoSuchMethodException { public void testCreatePostWithPathRequest() throws SecurityException, NoSuchMethodException {
Method method = TestPost.class.getMethod("postWithPath", String.class, MapEntityBinder.class); 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() { new Object[] { "data", new MapEntityBinder() {
public void addEntityToRequest(Map<String, String> postParams, HttpRequest request) { public void addEntityToRequest(Map<String, String> postParams, HttpRequest request) {
request.setEntity(postParams.get("fooble")); request.setEntity(postParams.get("fooble"));
@ -284,7 +299,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreatePostWithMethodBinder() throws SecurityException, NoSuchMethodException { public void testCreatePostWithMethodBinder() throws SecurityException, NoSuchMethodException {
Method method = TestPost.class.getMethod("postWithMethodBinder", String.class); 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", }); new Object[] { "data", });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/data"); assertEquals(httpMethod.getEndpoint().getPath(), "/data");
@ -309,7 +324,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreatePutWithMethodBinder() throws SecurityException, NoSuchMethodException { public void testCreatePutWithMethodBinder() throws SecurityException, NoSuchMethodException {
Method method = TestPut.class.getMethod("putWithMethodBinder", String.class); 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", }); new Object[] { "data", });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/data"); assertEquals(httpMethod.getEndpoint().getPath(), "/data");
@ -347,7 +362,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testRequestFilter() throws SecurityException, NoSuchMethodException { public void testRequestFilter() throws SecurityException, NoSuchMethodException {
Method method = TestRequestFilter.class.getMethod("get"); Method method = TestRequestFilter.class.getMethod("get");
HttpRequest httpMethod = factory.create(TestRequestFilter.class).createRequest(method, HttpRequest httpMethod = factory(TestRequestFilter.class).createRequest(method,
new Object[] {}); new Object[] {});
assertEquals(httpMethod.getFilters().size(), 2); assertEquals(httpMethod.getFilters().size(), 2);
assertEquals(httpMethod.getFilters().get(0).getClass(), TestRequestFilter1.class); assertEquals(httpMethod.getFilters().get(0).getClass(), TestRequestFilter1.class);
@ -366,7 +381,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testSkipEncoding() throws SecurityException, NoSuchMethodException { public void testSkipEncoding() throws SecurityException, NoSuchMethodException {
Method method = TestEncoding.class.getMethod("twoPaths", String.class, String.class); 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" }); new Object[] { "1", "localhost" });
assertEquals(httpMethod.getEndpoint().getPath(), "/1/localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/1/localhost");
assertEquals(httpMethod.getMethod(), HttpMethod.GET); assertEquals(httpMethod.getMethod(), HttpMethod.GET);
@ -376,7 +391,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testEncodingPath() throws SecurityException, NoSuchMethodException { public void testEncodingPath() throws SecurityException, NoSuchMethodException {
Method method = TestEncoding.class.getMethod("twoPaths", String.class, String.class); 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" }); new Object[] { "/", "localhost" });
assertEquals(httpMethod.getEndpoint().getPath(), "///localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "///localhost");
assertEquals(httpMethod.getMethod(), HttpMethod.GET); assertEquals(httpMethod.getMethod(), HttpMethod.GET);
@ -399,7 +414,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testConstantPathParam() throws SecurityException, NoSuchMethodException { public void testConstantPathParam() throws SecurityException, NoSuchMethodException {
Method method = TestConstantPathParam.class.getMethod("twoPaths", String.class, String.class); 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" }); new Object[] { "1", "localhost" });
assertEquals(httpMethod.getEndpoint().getPath(), "/v1/ralphie/1/localhost"); assertEquals(httpMethod.getEndpoint().getPath(), "/v1/ralphie/1/localhost");
assertEquals(httpMethod.getMethod(), HttpMethod.GET); assertEquals(httpMethod.getMethod(), HttpMethod.GET);
@ -441,7 +456,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testParamExtractor() throws SecurityException, NoSuchMethodException { public void testParamExtractor() throws SecurityException, NoSuchMethodException {
Method method = TestPath.class.getMethod("onePathParamExtractor", String.class); 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" }); new Object[] { "localhost" });
assertEquals(httpMethod.getEndpoint().getPath(), "/l"); assertEquals(httpMethod.getEndpoint().getPath(), "/l");
assertEquals(httpMethod.getMethod(), HttpMethod.GET); assertEquals(httpMethod.getMethod(), HttpMethod.GET);
@ -451,7 +466,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testParamExtractorMethod() throws SecurityException, NoSuchMethodException { public void testParamExtractorMethod() throws SecurityException, NoSuchMethodException {
Method method = TestPath.class.getMethod("onePathParamExtractorMethod", String.class); 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" }); new Object[] { "localhost" });
assertEquals(httpMethod.getEndpoint().getPath(), "/l"); assertEquals(httpMethod.getEndpoint().getPath(), "/l");
assertEquals(httpMethod.getMethod(), HttpMethod.GET); assertEquals(httpMethod.getMethod(), HttpMethod.GET);
@ -499,7 +514,7 @@ public class JaxrsAnnotationProcessorTest {
UnsupportedEncodingException { UnsupportedEncodingException {
Method oneHeader = TestHeader.class.getMethod("twoHeader", String.class); Method oneHeader = TestHeader.class.getMethod("twoHeader", String.class);
Multimap<String, String> headers = HashMultimap.create(); Multimap<String, String> headers = HashMultimap.create();
factory.create(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader, factory(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader,
new Object[] { "robot" }); new Object[] { "robot" });
assertEquals(headers.size(), 2); assertEquals(headers.size(), 2);
assertEquals(headers.get("slash"), Collections.singletonList("/robot")); assertEquals(headers.get("slash"), Collections.singletonList("/robot"));
@ -519,8 +534,8 @@ public class JaxrsAnnotationProcessorTest {
UnsupportedEncodingException { UnsupportedEncodingException {
Method oneHeader = TestClassHeader.class.getMethod("oneHeader", String.class); Method oneHeader = TestClassHeader.class.getMethod("oneHeader", String.class);
Multimap<String, String> headers = HashMultimap.create(); Multimap<String, String> headers = HashMultimap.create();
factory.create(TestClassHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, factory(TestClassHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader,
oneHeader, new Object[] { "robot" }); new Object[] { "robot" });
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot")); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot"));
} }
@ -530,7 +545,7 @@ public class JaxrsAnnotationProcessorTest {
UnsupportedEncodingException { UnsupportedEncodingException {
Method oneHeader = TestHeader.class.getMethod("oneHeader", String.class); Method oneHeader = TestHeader.class.getMethod("oneHeader", String.class);
Multimap<String, String> headers = HashMultimap.create(); Multimap<String, String> headers = HashMultimap.create();
factory.create(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader, factory(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, oneHeader,
new Object[] { "robot" }); new Object[] { "robot" });
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot")); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot"));
@ -541,7 +556,7 @@ public class JaxrsAnnotationProcessorTest {
UnsupportedEncodingException { UnsupportedEncodingException {
Method twoHeaders = TestHeader.class.getMethod("twoHeaders", String.class, String.class); Method twoHeaders = TestHeader.class.getMethod("twoHeaders", String.class, String.class);
Multimap<String, String> headers = HashMultimap.create(); Multimap<String, String> headers = HashMultimap.create();
factory.create(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, twoHeaders, factory(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, twoHeaders,
new Object[] { "robot", "eggs" }); new Object[] { "robot", "eggs" });
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot/eggs")); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/robot/eggs"));
@ -553,8 +568,8 @@ public class JaxrsAnnotationProcessorTest {
Method twoHeadersOutOfOrder = TestHeader.class.getMethod("twoHeadersOutOfOrder", Method twoHeadersOutOfOrder = TestHeader.class.getMethod("twoHeadersOutOfOrder",
String.class, String.class); String.class, String.class);
Multimap<String, String> headers = HashMultimap.create(); Multimap<String, String> headers = HashMultimap.create();
factory.create(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, factory(TestHeader.class).addHeaderIfAnnotationPresentOnMethod(headers, twoHeadersOutOfOrder,
twoHeadersOutOfOrder, new Object[] { "robot", "eggs" }); new Object[] { "robot", "eggs" });
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/eggs/robot")); assertEquals(headers.get("x-amz-copy-source"), Collections.singletonList("/eggs/robot"));
} }
@ -600,8 +615,8 @@ public class JaxrsAnnotationProcessorTest {
@Test(expectedExceptions = { RuntimeException.class }) @Test(expectedExceptions = { RuntimeException.class })
public void testNoTransformer() throws SecurityException, NoSuchMethodException { public void testNoTransformer() throws SecurityException, NoSuchMethodException {
Method method = TestTransformers.class.getMethod("noTransformer"); Method method = TestTransformers.class.getMethod("noTransformer");
Class<? extends Function<HttpResponse, ?>> transformer = factory.create( Class<? extends Function<HttpResponse, ?>> transformer = factory(TestTransformers.class)
TestTransformers.class).getParserOrThrowException(method); .getParserOrThrowException(method);
assertEquals(transformer, ReturnStringIf200.class); assertEquals(transformer, ReturnStringIf200.class);
} }
@ -609,8 +624,8 @@ public class JaxrsAnnotationProcessorTest {
Method method = TestTransformers.class.getMethod("oneTransformerWithContext"); Method method = TestTransformers.class.getMethod("oneTransformerWithContext");
HttpRequest request = new HttpRequest("GET", URI.create("http://localhost")); HttpRequest request = new HttpRequest("GET", URI.create("http://localhost"));
Object[] args = new Object[] {}; Object[] args = new Object[] {};
Function<HttpResponse, ?> transformer = factory.create(TestTransformers.class) Function<HttpResponse, ?> transformer = factory(TestTransformers.class).createResponseParser(
.createResponseParser(method, request, args); method, request, args);
assertEquals(transformer.getClass(), ReturnStringIf200Context.class); assertEquals(transformer.getClass(), ReturnStringIf200Context.class);
assertEquals(((ReturnStringIf200Context) transformer).getArgs(), args); assertEquals(((ReturnStringIf200Context) transformer).getArgs(), args);
assertEquals(((ReturnStringIf200Context) transformer).getRequest(), request); assertEquals(((ReturnStringIf200Context) transformer).getRequest(), request);
@ -619,8 +634,8 @@ public class JaxrsAnnotationProcessorTest {
@SuppressWarnings("static-access") @SuppressWarnings("static-access")
public void testOneTransformer() throws SecurityException, NoSuchMethodException { public void testOneTransformer() throws SecurityException, NoSuchMethodException {
Method method = TestTransformers.class.getMethod("oneTransformer"); Method method = TestTransformers.class.getMethod("oneTransformer");
Class<? extends Function<HttpResponse, ?>> transformer = factory.create( Class<? extends Function<HttpResponse, ?>> transformer = factory(TestTransformers.class)
TestTransformers.class).getParserOrThrowException(method); .getParserOrThrowException(method);
assertEquals(transformer, ReturnStringIf200.class); assertEquals(transformer, ReturnStringIf200.class);
} }
@ -691,7 +706,7 @@ public class JaxrsAnnotationProcessorTest {
GetOptions options = GetOptions.Builder.ifModifiedSince(date); GetOptions options = GetOptions.Builder.ifModifiedSince(date);
HttpRequestOptions[] optionsHolder = new HttpRequestOptions[] {}; HttpRequestOptions[] optionsHolder = new HttpRequestOptions[] {};
Method method = TestRequest.class.getMethod("get", String.class, optionsHolder.getClass()); 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 }); new Object[] { "1", options });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -708,7 +723,7 @@ public class JaxrsAnnotationProcessorTest {
DateTime date = new DateTime(); DateTime date = new DateTime();
GetOptions options = GetOptions.Builder.ifModifiedSince(date); GetOptions options = GetOptions.Builder.ifModifiedSince(date);
Method method = TestRequest.class.getMethod("get", String.class, HttpRequestOptions.class); 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 }); new Object[] { "1", options });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -732,7 +747,7 @@ public class JaxrsAnnotationProcessorTest {
NoSuchMethodException { NoSuchMethodException {
PrefixOptions options = new PrefixOptions().withPrefix("1"); PrefixOptions options = new PrefixOptions().withPrefix("1");
Method method = TestRequest.class.getMethod("get", String.class, HttpRequestOptions.class); 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 }); new Object[] { "1", options });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -745,7 +760,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreateGetQuery() throws SecurityException, NoSuchMethodException { public void testCreateGetQuery() throws SecurityException, NoSuchMethodException {
Method method = TestRequest.class.getMethod("getQuery", String.class); 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" }); new Object[] { "1" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -756,7 +771,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreateGetQueryNull() throws SecurityException, NoSuchMethodException { public void testCreateGetQueryNull() throws SecurityException, NoSuchMethodException {
Method method = TestRequest.class.getMethod("getQueryNull", String.class); 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" }); new Object[] { "1" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -778,7 +793,7 @@ public class JaxrsAnnotationProcessorTest {
EntityOptions options = new EntityOptions(); EntityOptions options = new EntityOptions();
Method method = TestRequest.class.getMethod("putOptions", String.class, Method method = TestRequest.class.getMethod("putOptions", String.class,
HttpRequestOptions.class); HttpRequestOptions.class);
HttpRequest httpMethod = factory.create(TestRequest.class).createRequest(method, HttpRequest httpMethod = factory(TestRequest.class).createRequest(method,
new Object[] { "1", options }); new Object[] { "1", options });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -802,7 +817,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreateGetRequest(String key) throws SecurityException, NoSuchMethodException, public void testCreateGetRequest(String key) throws SecurityException, NoSuchMethodException,
UnsupportedEncodingException { UnsupportedEncodingException {
Method method = TestRequest.class.getMethod("get", String.class, String.class); 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" }); new Object[] { key, "localhost" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
String expectedPath = "/" + URLEncoder.encode(key, "UTF-8").replaceAll("\\+", "%20"); String expectedPath = "/" + URLEncoder.encode(key, "UTF-8").replaceAll("\\+", "%20");
@ -816,7 +831,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreatePutRequest() throws SecurityException, NoSuchMethodException { public void testCreatePutRequest() throws SecurityException, NoSuchMethodException {
Method method = TestRequest.class.getMethod("put", String.class, String.class); 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" }); new Object[] { "111", "data" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -831,7 +846,7 @@ public class JaxrsAnnotationProcessorTest {
public void testCreatePutHeader() throws SecurityException, NoSuchMethodException { public void testCreatePutHeader() throws SecurityException, NoSuchMethodException {
Method method = TestRequest.class.getMethod("putHeader", String.class, String.class); 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" }); new Object[] { "1", "data" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -858,7 +873,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testVirtualHostMethod() throws SecurityException, NoSuchMethodException { public void testVirtualHostMethod() throws SecurityException, NoSuchMethodException {
Method method = TestVirtualHostMethod.class.getMethod("get", String.class, String.class); 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" }); new Object[] { "1", "localhost" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -893,7 +908,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testVirtualHost() throws SecurityException, NoSuchMethodException { public void testVirtualHost() throws SecurityException, NoSuchMethodException {
Method method = TestVirtualHost.class.getMethod("get", String.class, String.class); 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" }); new Object[] { "1", "localhost" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -906,7 +921,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testHostPrefix() throws SecurityException, NoSuchMethodException { public void testHostPrefix() throws SecurityException, NoSuchMethodException {
Method method = TestVirtualHost.class.getMethod("getPrefix", String.class, String.class); 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" }); new Object[] { "1", "holy" });
assertEquals(httpMethod.getEndpoint().getHost(), "holylocalhost"); assertEquals(httpMethod.getEndpoint().getHost(), "holylocalhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -917,7 +932,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testHostPrefixDot() throws SecurityException, NoSuchMethodException { public void testHostPrefixDot() throws SecurityException, NoSuchMethodException {
Method method = TestVirtualHost.class.getMethod("getPrefixDot", String.class, String.class); 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" }); new Object[] { "1", "holy" });
assertEquals(httpMethod.getEndpoint().getHost(), "holy.localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "holy.localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/1"); assertEquals(httpMethod.getEndpoint().getPath(), "/1");
@ -928,13 +943,13 @@ public class JaxrsAnnotationProcessorTest {
@Test(expectedExceptions = IllegalArgumentException.class) @Test(expectedExceptions = IllegalArgumentException.class)
public void testHostPrefixDotEmpty() throws SecurityException, NoSuchMethodException { public void testHostPrefixDotEmpty() throws SecurityException, NoSuchMethodException {
Method method = TestVirtualHost.class.getMethod("getPrefixDot", String.class, String.class); 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) @Test(expectedExceptions = NullPointerException.class)
public void testHostPrefixDotNull() throws SecurityException, NoSuchMethodException { public void testHostPrefixDotNull() throws SecurityException, NoSuchMethodException {
Method method = TestVirtualHost.class.getMethod("getPrefixDot", String.class, String.class); 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) @Endpoint(Localhost.class)
@ -961,7 +976,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testOneHeader() throws SecurityException, NoSuchMethodException { public void testOneHeader() throws SecurityException, NoSuchMethodException {
Method method = TestHeaders.class.getMethod("oneHeader", String.class); Method method = TestHeaders.class.getMethod("oneHeader", String.class);
Multimap<String, String> headers = factory.create(TestHeaders.class).buildHeaders(method, Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders(method,
new Object[] { "robot" }); new Object[] { "robot" });
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("header"), Collections.singletonList("robot")); assertEquals(headers.get("header"), Collections.singletonList("robot"));
@ -970,7 +985,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testOneIntHeader() throws SecurityException, NoSuchMethodException { public void testOneIntHeader() throws SecurityException, NoSuchMethodException {
Method method = TestHeaders.class.getMethod("oneIntHeader", int.class); Method method = TestHeaders.class.getMethod("oneIntHeader", int.class);
Multimap<String, String> headers = factory.create(TestHeaders.class).buildHeaders(method, Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders(method,
new Object[] { 1 }); new Object[] { 1 });
assertEquals(headers.size(), 1); assertEquals(headers.size(), 1);
assertEquals(headers.get("header"), Collections.singletonList("1")); assertEquals(headers.get("header"), Collections.singletonList("1"));
@ -980,7 +995,7 @@ public class JaxrsAnnotationProcessorTest {
public void testTwoDifferentHeaders() throws SecurityException, NoSuchMethodException { public void testTwoDifferentHeaders() throws SecurityException, NoSuchMethodException {
Method method = TestHeaders.class Method method = TestHeaders.class
.getMethod("twoDifferentHeaders", String.class, String.class); .getMethod("twoDifferentHeaders", String.class, String.class);
Multimap<String, String> headers = factory.create(TestHeaders.class).buildHeaders(method, Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders(method,
new Object[] { "robot", "egg" }); new Object[] { "robot", "egg" });
assertEquals(headers.size(), 2); assertEquals(headers.size(), 2);
assertEquals(headers.get("header1"), Collections.singletonList("robot")); assertEquals(headers.get("header1"), Collections.singletonList("robot"));
@ -990,7 +1005,7 @@ public class JaxrsAnnotationProcessorTest {
@Test @Test
public void testTwoSameHeaders() throws SecurityException, NoSuchMethodException { public void testTwoSameHeaders() throws SecurityException, NoSuchMethodException {
Method method = TestHeaders.class.getMethod("twoSameHeaders", String.class, String.class); Method method = TestHeaders.class.getMethod("twoSameHeaders", String.class, String.class);
Multimap<String, String> headers = factory.create(TestHeaders.class).buildHeaders(method, Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders(method,
new Object[] { "robot", "egg" }); new Object[] { "robot", "egg" });
assertEquals(headers.size(), 2); assertEquals(headers.size(), 2);
Collection<String> values = headers.get("header"); Collection<String> values = headers.get("header");
@ -1018,8 +1033,8 @@ public class JaxrsAnnotationProcessorTest {
public void testPut() throws SecurityException, NoSuchMethodException { public void testPut() throws SecurityException, NoSuchMethodException {
Method method = TestEntity.class.getMethod("put", String.class); Method method = TestEntity.class.getMethod("put", String.class);
HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost:8080")); 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" },
new Object[] { "test" }, request); request);
assertEquals(request.getEntity(), "test"); assertEquals(request.getEntity(), "test");
assertEquals(request.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections assertEquals(request.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
.singletonList("application/unknown")); .singletonList("application/unknown"));
@ -1031,7 +1046,7 @@ public class JaxrsAnnotationProcessorTest {
public void putWithPath() throws SecurityException, NoSuchMethodException { public void putWithPath() throws SecurityException, NoSuchMethodException {
Method method = TestEntity.class.getMethod("putWithPath", String.class, String.class); Method method = TestEntity.class.getMethod("putWithPath", String.class, String.class);
HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost:8080")); 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); new Object[] { "rabble", "test" }, request);
assertEquals(request.getEntity(), "test"); assertEquals(request.getEntity(), "test");
assertEquals(request.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections assertEquals(request.getHeaders().get(HttpHeaders.CONTENT_TYPE), Collections
@ -1044,17 +1059,22 @@ public class JaxrsAnnotationProcessorTest {
public void testPutTwoEntities() throws SecurityException, NoSuchMethodException { public void testPutTwoEntities() throws SecurityException, NoSuchMethodException {
Method method = TestEntity.class.getMethod("twoEntities", String.class, String.class); Method method = TestEntity.class.getMethod("twoEntities", String.class, String.class);
HttpRequest request = new HttpRequest(HttpMethod.PUT, URI.create("http://localhost:8080")); 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); new Object[] { "test", "ralphie" }, request);
} }
JaxrsAnnotationProcessor.Factory factory; @SuppressWarnings("unchecked")
private <T> JaxrsAnnotationProcessor<T> factory(Class<T> clazz) {
return ((JaxrsAnnotationProcessor<T>) injector.getInstance(Key.get(TypeLiteral.get(Types
.newParameterizedType(JaxrsAnnotationProcessor.class, clazz)))));
}
Injector injector;
DateService dateService = new DateService(); DateService dateService = new DateService();
@BeforeClass @BeforeClass
void setupFactory() { void setupFactory() {
factory = Guice.createInjector( injector = Guice.createInjector(new AbstractModule() {
new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
bindConstant().annotatedWith(Jsr330.named("testaccount")).to("ralphie"); bindConstant().annotatedWith(Jsr330.named("testaccount")).to("ralphie");
@ -1064,7 +1084,7 @@ public class JaxrsAnnotationProcessorTest {
URI.create("http://localhost:8081")); URI.create("http://localhost:8081"));
} }
}, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()),
new JavaUrlHttpCommandExecutorServiceModule()).getInstance( new JavaUrlHttpCommandExecutorServiceModule());
JaxrsAnnotationProcessor.Factory.class);
} }
} }

View File

@ -83,7 +83,10 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
/** /**
* Tests behavior of {@code CloudServersConnection} * Tests behavior of {@code CloudServersConnection}
@ -92,7 +95,6 @@ import com.google.inject.Provides;
*/ */
@Test(groups = "unit", testName = "cloudservers.CloudServersConnectionTest") @Test(groups = "unit", testName = "cloudservers.CloudServersConnectionTest")
public class CloudServersConnectionTest { public class CloudServersConnectionTest {
JaxrsAnnotationProcessor.Factory factory;
private static final Class<? extends ListOptions[]> listOptionsVarargsClass = new ListOptions[] {} private static final Class<? extends ListOptions[]> listOptionsVarargsClass = new ListOptions[] {}
.getClass(); .getClass();
private static final Class<? extends CreateServerOptions[]> createServerOptionsVarargsClass = new CreateServerOptions[] {} private static final Class<? extends CreateServerOptions[]> createServerOptionsVarargsClass = new CreateServerOptions[] {}
@ -871,12 +873,9 @@ public class CloudServersConnectionTest {
ReturnTrueIf2xx.class); ReturnTrueIf2xx.class);
} }
JaxrsAnnotationProcessor processor;
@BeforeClass @BeforeClass
void setupFactory() { void setupFactory() {
factory = Guice.createInjector( Injector injector = Guice.createInjector(new AbstractModule() {
new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
bind(URI.class).annotatedWith(CloudServers.class).toInstance( bind(URI.class).annotatedWith(CloudServers.class).toInstance(
@ -890,8 +889,12 @@ public class CloudServersConnectionTest {
return "testtoken"; return "testtoken";
} }
}, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()),
new JavaUrlHttpCommandExecutorServiceModule()).getInstance( new JavaUrlHttpCommandExecutorServiceModule());
JaxrsAnnotationProcessor.Factory.class); processor = injector.getInstance(Key
processor = factory.create(CloudServersConnection.class); .get(new TypeLiteral<JaxrsAnnotationProcessor<CloudServersConnection>>() {
}));
} }
JaxrsAnnotationProcessor<CloudServersConnection> processor;
} }

View File

@ -44,6 +44,9 @@ import org.testng.annotations.Test;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Guice; 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} * Tests behavior of {@code JaxrsAnnotationProcessor}
@ -53,13 +56,12 @@ import com.google.inject.Guice;
@Test(groups = "unit", testName = "rackspace.RackspaceAuthentication") @Test(groups = "unit", testName = "rackspace.RackspaceAuthentication")
public class RackspaceAuthenticationTest { public class RackspaceAuthenticationTest {
JaxrsAnnotationProcessor.Factory factory; private JaxrsAnnotationProcessor<RackspaceAuthentication> processor;
public void testAuthenticate() throws SecurityException, NoSuchMethodException { public void testAuthenticate() throws SecurityException, NoSuchMethodException {
Method method = RackspaceAuthentication.class.getMethod("authenticate", String.class, Method method = RackspaceAuthentication.class.getMethod("authenticate", String.class,
String.class); String.class);
HttpRequest httpMethod = factory.create(RackspaceAuthentication.class).createRequest(method, HttpRequest httpMethod = processor.createRequest(method, new Object[] { "foo", "bar" });
new Object[] { "foo", "bar" });
assertEquals(httpMethod.getEndpoint().getHost(), "localhost"); assertEquals(httpMethod.getEndpoint().getHost(), "localhost");
assertEquals(httpMethod.getEndpoint().getPath(), "/auth"); assertEquals(httpMethod.getEndpoint().getPath(), "/auth");
assertEquals(httpMethod.getMethod(), HttpMethod.GET); assertEquals(httpMethod.getMethod(), HttpMethod.GET);
@ -68,7 +70,6 @@ public class RackspaceAuthenticationTest {
.singletonList("foo")); .singletonList("foo"));
assertEquals(httpMethod.getHeaders().get(RackspaceHeaders.AUTH_KEY), Collections assertEquals(httpMethod.getHeaders().get(RackspaceHeaders.AUTH_KEY), Collections
.singletonList("bar")); .singletonList("bar"));
factory.create(RackspaceAuthentication.class);
assertEquals(JaxrsAnnotationProcessor.getParserOrThrowException(method), assertEquals(JaxrsAnnotationProcessor.getParserOrThrowException(method),
ParseAuthenticationResponseFromHeaders.class); ParseAuthenticationResponseFromHeaders.class);
@ -76,16 +77,17 @@ public class RackspaceAuthenticationTest {
@BeforeClass @BeforeClass
void setupFactory() { void setupFactory() {
factory = Guice.createInjector( Injector injector = Guice.createInjector(new AbstractModule() {
new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
bind(URI.class).annotatedWith(Authentication.class).toInstance( bind(URI.class).annotatedWith(Authentication.class).toInstance(
URI.create("http://localhost:8080")); URI.create("http://localhost:8080"));
} }
}, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()), }, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()),
new JavaUrlHttpCommandExecutorServiceModule()).getInstance( new JavaUrlHttpCommandExecutorServiceModule());
JaxrsAnnotationProcessor.Factory.class); processor = injector.getInstance(Key
.get(new TypeLiteral<JaxrsAnnotationProcessor<RackspaceAuthentication>>() {
}));
} }
} }