Search in sources :

Example 1 with UrlConnectionBuilderFactory

use of com.stanfy.enroscar.net.UrlConnectionBuilderFactory in project enroscar by stanfy.

the class BaseRequestDescriptionConverter method prepareUrlConnectionBuilder.

/**
   * @return URL connection builder with set cache,
   */
protected UrlConnectionBuilder prepareUrlConnectionBuilder() {
    UrlConnectionBuilderFactory factory = BeansManager.get(context).getContainer().getBean(CONNECTION_BUILDER_FACTORY_NAME, UrlConnectionBuilderFactory.class);
    if (factory == null) {
        throw new IllegalStateException("UrlConnectionBuilderFactory bean is not defined.");
    }
    final String contentHandlerName = requestDescription.getContentHandler();
    return factory.newUrlConnectionBuilder().setCacheManagerName(requestDescription.getCacheName()).setContentHandler(BeansManager.get(context).getContainer().getBean(contentHandlerName, ContentHandler.class)).setEntityTypeToken(requestDescription.getModelType());
}
Also used : UrlConnectionBuilderFactory(com.stanfy.enroscar.net.UrlConnectionBuilderFactory)

Example 2 with UrlConnectionBuilderFactory

use of com.stanfy.enroscar.net.UrlConnectionBuilderFactory in project enroscar by stanfy.

the class RetrofitClientTest method init.

@Before
public void init() throws Exception {
    EnroscarConnectionsEngine.config().treatFileScheme(false).setup(Robolectric.application);
    testThing = new Thing();
    testThing.name = "foo!";
    mockResponse = new Gson().toJson(testThing);
    File f = new File(Robolectric.application.getFilesDir(), "resp");
    FileOutputStream out = new FileOutputStream(f);
    out.write(mockResponse.getBytes());
    out.close();
    testResponseInput = new FileInputStream(f);
    mockResolver = mock(ContentResolver.class);
    doReturn("application/json").when(mockResolver).getType(any(Uri.class));
    AssetFileDescriptor mockDescriptor = mock(AssetFileDescriptor.class);
    doReturn(testResponseInput).when(mockDescriptor).createInputStream();
    doReturn(mockDescriptor).when(mockResolver).openAssetFileDescriptor(any(Uri.class), any(String.class));
    client = new RetrofitClient(new UrlConnectionBuilderFactory() {

        @Override
        public UrlConnectionBuilder newUrlConnectionBuilder() {
            return new UrlConnectionBuilder() {

                @Override
                public URLConnection create() throws IOException {
                    URL url = getUrl();
                    if ("content".equals(url.getProtocol())) {
                        return new ContentUriConnection(url, mockResolver);
                    }
                    return super.create();
                }
            };
        }
    });
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) UrlConnectionBuilderFactory(com.stanfy.enroscar.net.UrlConnectionBuilderFactory) ContentUriConnection(com.stanfy.enroscar.net.ContentUriConnection) Gson(com.google.gson.Gson) Uri(android.net.Uri) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ContentResolver(android.content.ContentResolver) UrlConnectionBuilder(com.stanfy.enroscar.net.UrlConnectionBuilder) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Before(org.junit.Before)

Aggregations

UrlConnectionBuilderFactory (com.stanfy.enroscar.net.UrlConnectionBuilderFactory)2 ContentResolver (android.content.ContentResolver)1 AssetFileDescriptor (android.content.res.AssetFileDescriptor)1 Uri (android.net.Uri)1 Gson (com.google.gson.Gson)1 ContentUriConnection (com.stanfy.enroscar.net.ContentUriConnection)1 UrlConnectionBuilder (com.stanfy.enroscar.net.UrlConnectionBuilder)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 URL (java.net.URL)1 Before (org.junit.Before)1