Search in sources :

Example 16 with Converter

use of retrofit2.Converter in project retrofit by square.

the class FallbackParserFinderTest method converterFactoryFallsBackToParserField.

@Test
public void converterFactoryFallsBackToParserField() {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://localhost/").build();
    ProtoConverterFactory factory = ProtoConverterFactory.create();
    Converter<ResponseBody, ?> converter = factory.responseBodyConverter(FakePhone.class, new Annotation[0], retrofit);
    assertThat(converter).isNotNull();
}
Also used : Retrofit(retrofit2.Retrofit) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 17 with Converter

use of retrofit2.Converter in project retrofit by square.

the class RetrofitTest method responseConverterFactoryNoMatchThrows.

@Test
public void responseConverterFactoryNoMatchThrows() {
    Type type = String.class;
    Annotation[] annotations = new Annotation[0];
    NonMatchingConverterFactory nonMatchingFactory = new NonMatchingConverterFactory();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory).build();
    try {
        retrofit.responseBodyConverter(type, annotations);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Could not locate ResponseBody converter for class java.lang.String.\n" + "  Tried:\n" + "   * retrofit2.BuiltInConverters\n" + "   * retrofit2.helpers.NonMatchingConverterFactory");
    }
    assertThat(nonMatchingFactory.called).isTrue();
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) Test(org.junit.Test)

Example 18 with Converter

use of retrofit2.Converter in project retrofit by square.

the class RetrofitTest method methodAndParameterAnnotationsPassedToRequestBodyConverter.

@Test
public void methodAndParameterAnnotationsPassedToRequestBodyConverter() {
    final AtomicReference<Annotation[]> parameterAnnotationsRef = new AtomicReference<>();
    final AtomicReference<Annotation[]> methodAnnotationsRef = new AtomicReference<>();
    class MyConverterFactory extends Converter.Factory {

        @Override
        public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
            parameterAnnotationsRef.set(parameterAnnotations);
            methodAnnotationsRef.set(methodAnnotations);
            return new ToStringConverterFactory().requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
        }
    }
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new MyConverterFactory()).build();
    Annotated annotated = retrofit.create(Annotated.class);
    // Trigger internal setup.
    annotated.bodyParameter(null);
    assertThat(parameterAnnotationsRef.get()).hasAtLeastOneElementOfType(Annotated.Foo.class);
    assertThat(methodAnnotationsRef.get()).hasAtLeastOneElementOfType(POST.class);
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 19 with Converter

use of retrofit2.Converter in project retrofit by square.

the class RetrofitTest method stringConverterCalledForString.

@Test
public void stringConverterCalledForString() {
    final AtomicBoolean factoryCalled = new AtomicBoolean();
    class MyConverterFactory extends Converter.Factory {

        @Override
        public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            factoryCalled.set(true);
            return null;
        }
    }
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new MyConverterFactory()).build();
    CallMethod service = retrofit.create(CallMethod.class);
    Call<ResponseBody> call = service.queryString(null);
    assertThat(call).isNotNull();
    assertThat(factoryCalled.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ToStringConverterFactory(retrofit2.helpers.ToStringConverterFactory) NonMatchingCallAdapterFactory(retrofit2.helpers.NonMatchingCallAdapterFactory) DelegatingCallAdapterFactory(retrofit2.helpers.DelegatingCallAdapterFactory) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 20 with Converter

use of retrofit2.Converter in project retrofit by square.

the class RetrofitTest method responseConverterFactorySkippedNoMatchThrows.

@Test
public void responseConverterFactorySkippedNoMatchThrows() {
    Type type = String.class;
    Annotation[] annotations = new Annotation[0];
    NonMatchingConverterFactory nonMatchingFactory1 = new NonMatchingConverterFactory();
    NonMatchingConverterFactory nonMatchingFactory2 = new NonMatchingConverterFactory();
    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com/").addConverterFactory(nonMatchingFactory1).addConverterFactory(nonMatchingFactory2).build();
    try {
        retrofit.nextResponseBodyConverter(nonMatchingFactory1, type, annotations);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("" + "Could not locate ResponseBody converter for class java.lang.String.\n" + "  Skipped:\n" + "   * retrofit2.BuiltInConverters\n" + "   * retrofit2.helpers.NonMatchingConverterFactory\n" + "  Tried:\n" + "   * retrofit2.helpers.NonMatchingConverterFactory");
    }
    assertThat(nonMatchingFactory1.called).isFalse();
    assertThat(nonMatchingFactory2.called).isTrue();
}
Also used : MediaType(okhttp3.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) NonMatchingConverterFactory(retrofit2.helpers.NonMatchingConverterFactory) Test(org.junit.Test)

Aggregations

Type (java.lang.reflect.Type)19 ResponseBody (okhttp3.ResponseBody)18 Test (org.junit.Test)12 Converter (retrofit2.Converter)10 JsonObject (com.google.gson.JsonObject)8 ParameterizedType (java.lang.reflect.ParameterizedType)8 MediaType (okhttp3.MediaType)8 NonMatchingConverterFactory (retrofit2.helpers.NonMatchingConverterFactory)8 JsonElement (com.google.gson.JsonElement)7 IOException (java.io.IOException)7 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)7 Gson (com.google.gson.Gson)6 JsonParser (com.google.gson.JsonParser)6 InputStreamReader (java.io.InputStreamReader)6 JsonArray (com.google.gson.JsonArray)5 Annotation (java.lang.annotation.Annotation)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 DelegatingCallAdapterFactory (retrofit2.helpers.DelegatingCallAdapterFactory)4