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.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() {
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");
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);
new JavaUrlHttpCommandExecutorServiceModule());
processor = injector.getInstance(Key
.get(new TypeLiteral<JaxrsAnnotationProcessor<AzureBlobStore>>() {
}));
}
JaxrsAnnotationProcessor<AzureBlobStore> processor;
}

View File

@ -41,5 +41,4 @@ public class AzureQueueContextModule extends AbstractModule {
this.requireBinding(AzureQueueConnection.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.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<? extends ListOptions[]> listOptionsVarargsClass = new ListOptions[] {}
.getClass();
private static final Class<? extends CreateOptions[]> 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() {
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");
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);
new JavaUrlHttpCommandExecutorServiceModule());
processor = injector.getInstance(Key
.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.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<M extends BlobMetadata, B extends Blob<M>>
implements Function<HttpResponse, B> {
implements Function<HttpResponse, B>, RestContext {
private final ParseContentTypeFromHeaders<M> metadataParser;
private final BlobFactory<M, B> blobFactory;
@ -84,11 +86,23 @@ public class ParseBlobFromHeadersAndHttpContent<M extends BlobMetadata, B extend
if (contentRange == null && contentLength != null) {
object.getMetadata().setSize(object.getContentLength());
} else if (contentRange != null ) {
} else if (contentRange != null) {
object.setContentRange(contentRange);
object.getMetadata().setSize(
Long.parseLong(contentRange.substring(contentRange.lastIndexOf('/') + 1)));
}
}
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;
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.RetentionPolicy.RUNTIME;
@ -36,8 +37,11 @@ import java.lang.annotation.Target;
*
* @author Adrian Cole
*/
@Target( { TYPE, METHOD })
@Target( { TYPE, METHOD, PARAMETER })
@Retention(RUNTIME)
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.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<T> {
@Resource
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>>> methodToIndexOfParamToHeaderParamAnnotations = createMethodToIndexOfParamToAnnotation(HeaderParam.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>>> methodToindexOfParamToPathParamAnnotations = createMethodToIndexOfParamToAnnotation(PathParam.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;
@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<T> typeLiteral) {
this.declaring = (Class<T>) 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<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) {
Map<Integer, Set<Annotation>> map = getIndexToHostPrefixAnnotation(method);
Map<Integer, Set<Annotation>> 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<Integer, Set<Annotation>> indexToEntityAnnotation = getIndexToEntityAnnotation(method);
Map<Integer, Set<Annotation>> indexToEntityAnnotation = indexWithOnlyOneAnnotation(
method, "@Entity", methodToIndexOfParamToEntityAnnotation);
if (indexToEntityAnnotation.size() == 1) {
Entry<Integer, Set<Annotation>> entry = indexToEntityAnnotation.entrySet()
@ -527,10 +550,10 @@ public class JaxrsAnnotationProcessor {
return request;
}
private Map<Integer, Set<Annotation>> getIndexToEntityAnnotation(Method method) {
Map<Integer, Set<Annotation>> indexToEntityAnnotation = Maps.filterValues(
methodToIndexOfParamToEntityAnnotation.get(method),
new Predicate<Set<Annotation>>() {
protected Map<Integer, Set<Annotation>> indexWithOnlyOneAnnotation(Method method,
String description, Map<Method, Map<Integer, Set<Annotation>>> toRefine) {
Map<Integer, Set<Annotation>> indexToEntityAnnotation = Maps.filterValues(toRefine
.get(method), new Predicate<Set<Annotation>>() {
public boolean apply(Set<Annotation> input) {
return input.size() == 1;
}
@ -538,31 +561,12 @@ public class JaxrsAnnotationProcessor {
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<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) {
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;
public URI getEndpointFor(Method method, Object[] args) {
URI endpoint = getEndpointInParametersOrNull(method, args);
if (endpoint == null) {
Endpoint annotation;
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)) {
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 {
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;
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> T create(Class<T> 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.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<T> implements InvocationHandler {
private final JaxrsAnnotationProcessor<T> util;
private final Class<T> 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<T> util, TypeLiteral<T> typeLiteral) {
this.util = util;
this.declaring = (Class<T>) 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)

View File

@ -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<TransformingHttpCommandImpl<?>>() {

View File

@ -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);

View File

@ -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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<? extends Function<HttpResponse, ?>> transformer = factory.create(
TestTransformers.class).getParserOrThrowException(method);
Class<? extends Function<HttpResponse, ?>> 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<HttpResponse, ?> transformer = factory.create(TestTransformers.class)
.createResponseParser(method, request, args);
Function<HttpResponse, ?> 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<? extends Function<HttpResponse, ?>> transformer = factory.create(
TestTransformers.class).getParserOrThrowException(method);
Class<? extends Function<HttpResponse, ?>> 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<String, String> headers = factory.create(TestHeaders.class).buildHeaders(method,
Multimap<String, String> 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<String, String> headers = factory.create(TestHeaders.class).buildHeaders(method,
Multimap<String, String> 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<String, String> headers = factory.create(TestHeaders.class).buildHeaders(method,
Multimap<String, String> 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<String, String> headers = factory.create(TestHeaders.class).buildHeaders(method,
Multimap<String, String> headers = factory(TestHeaders.class).buildHeaders(method,
new Object[] { "robot", "egg" });
assertEquals(headers.size(), 2);
Collection<String> 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,17 +1059,22 @@ 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 <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();
@BeforeClass
void setupFactory() {
factory = Guice.createInjector(
new AbstractModule() {
injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindConstant().annotatedWith(Jsr330.named("testaccount")).to("ralphie");
@ -1064,7 +1084,7 @@ public class JaxrsAnnotationProcessorTest {
URI.create("http://localhost:8081"));
}
}, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()),
new JavaUrlHttpCommandExecutorServiceModule()).getInstance(
JaxrsAnnotationProcessor.Factory.class);
new JavaUrlHttpCommandExecutorServiceModule());
}
}

View File

@ -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<? extends ListOptions[]> listOptionsVarargsClass = new ListOptions[] {}
.getClass();
private static final Class<? extends CreateServerOptions[]> createServerOptionsVarargsClass = new CreateServerOptions[] {}
@ -871,12 +873,9 @@ public class CloudServersConnectionTest {
ReturnTrueIf2xx.class);
}
JaxrsAnnotationProcessor processor;
@BeforeClass
void setupFactory() {
factory = Guice.createInjector(
new AbstractModule() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(URI.class).annotatedWith(CloudServers.class).toInstance(
@ -890,8 +889,12 @@ public class CloudServersConnectionTest {
return "testtoken";
}
}, new JaxrsModule(), new ExecutorServiceModule(new WithinThreadExecutorService()),
new JavaUrlHttpCommandExecutorServiceModule()).getInstance(
JaxrsAnnotationProcessor.Factory.class);
processor = factory.create(CloudServersConnection.class);
new JavaUrlHttpCommandExecutorServiceModule());
processor = injector.getInstance(Key
.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.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<RackspaceAuthentication> 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() {
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()).getInstance(
JaxrsAnnotationProcessor.Factory.class);
new JavaUrlHttpCommandExecutorServiceModule());
processor = injector.getInstance(Key
.get(new TypeLiteral<JaxrsAnnotationProcessor<RackspaceAuthentication>>() {
}));
}
}