Search in sources :

Example 1 with CacheLoadingException

use of com.octo.android.robospice.persistence.exception.CacheLoadingException in project robospice by stephanenicolas.

the class RequestProcessorTest method testAddRequest_when_fail_on_error_true_loading_from_cache_throws_exception.

public void testAddRequest_when_fail_on_error_true_loading_from_cache_throws_exception() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
    // given
    CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
    stubRequest.setRetryPolicy(null);
    RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
    requestListenerSet.add(mockRequestListener);
    EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andThrow(new CacheLoadingException(""));
    EasyMock.replay(mockCacheManager);
    // when
    requestProcessorUnderTest.setFailOnCacheError(true);
    requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    // then
    EasyMock.verify(mockCacheManager);
    assertFalse(stubRequest.isLoadDataFromNetworkCalled());
    assertFalse(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener.isComplete());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 2 with CacheLoadingException

use of com.octo.android.robospice.persistence.exception.CacheLoadingException in project robospice by stephanenicolas.

the class JsonObjectPersister method readCacheDataFromFile.

// ============================================================================================
// METHODS
// ============================================================================================
@Override
protected T readCacheDataFromFile(File file) throws CacheLoadingException {
    try {
        JsonParser jsonParser = jsonFactory.createJsonParser(new FileReader(file));
        T result = jsonParser.parse(getHandledClass(), null);
        jsonParser.close();
        return result;
    } catch (FileNotFoundException e) {
        // Should not occur (we test before if file exists)
        // Do not throw, file is not cached
        Ln.w("file " + file.getAbsolutePath() + " does not exists", e);
        return null;
    } catch (Exception e) {
        throw new CacheLoadingException(e);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException) CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) CacheCreationException(com.octo.android.robospice.persistence.exception.CacheCreationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException) JsonParser(com.google.api.client.json.JsonParser)

Example 3 with CacheLoadingException

use of com.octo.android.robospice.persistence.exception.CacheLoadingException in project robospice by stephanenicolas.

the class RetrofitObjectPersister method readCacheDataFromFile.

@SuppressWarnings("unchecked")
@Override
protected T readCacheDataFromFile(File file) throws CacheLoadingException {
    InputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(file);
        final byte[] body = IOUtils.toByteArray(fileInputStream);
        TypedInput typedInput = new TypedInput() {

            @Override
            public String mimeType() {
                return "application/json";
            }

            @Override
            public long length() {
                return body.length;
            }

            @Override
            public InputStream in() throws IOException {
                return new ByteArrayInputStream(body);
            }
        };
        return (T) converter.fromBody(typedInput, getHandledClass());
    } catch (FileNotFoundException e) {
        // Should not occur (we test before if file exists)
        // Do not throw, file is not cached
        Ln.w("file " + file.getAbsolutePath() + " does not exists", e);
        return null;
    } catch (Exception e) {
        throw new CacheLoadingException(e);
    } finally {
        IOUtils.closeQuietly(fileInputStream);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TypedInput(retrofit.mime.TypedInput) FileNotFoundException(java.io.FileNotFoundException) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException) FileInputStream(java.io.FileInputStream) CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) CacheCreationException(com.octo.android.robospice.persistence.exception.CacheCreationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException)

Example 4 with CacheLoadingException

use of com.octo.android.robospice.persistence.exception.CacheLoadingException in project robospice by stephanenicolas.

the class InFileBitmapObjectPersister method readCacheDataFromFile.

@Override
protected Bitmap readCacheDataFromFile(File file) throws CacheLoadingException {
    Bitmap data;
    FileInputStream is = null;
    try {
        is = new FileInputStream(file.getAbsolutePath());
        data = BitmapFactory.decodeStream(is, null, decodingOptions);
    } catch (Throwable ex) {
        throw new CacheLoadingException(String.format("Found the file %s but could not decode bitmap.", file.getAbsolutePath()), ex);
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
    if (data == null) {
        throw new CacheLoadingException(String.format("Found the file %s but could not decode bitmap.", file.getAbsolutePath()));
    }
    return data;
}
Also used : Bitmap(android.graphics.Bitmap) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException) FileInputStream(java.io.FileInputStream)

Example 5 with CacheLoadingException

use of com.octo.android.robospice.persistence.exception.CacheLoadingException in project robospice by stephanenicolas.

the class RequestProcessorTest method testAddRequest_when_fail_on_error_true_and_request_has_retry_policy_loading_from_cache_throws_exception.

public void testAddRequest_when_fail_on_error_true_and_request_has_retry_policy_loading_from_cache_throws_exception() throws SpiceException, InterruptedException {
    // given
    CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
    DefaultRetryPolicy retryPolicy = new DefaultRetryPolicy(TEST_RETRY_COUNT, TEST_DELAY_BEFORE_RETRY, TEST_RETRY_BACKOFF_MULTIPLIER);
    stubRequest.setRetryPolicy(retryPolicy);
    RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
    Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
    requestListenerSet.add(mockRequestListener);
    EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY), EasyMock.eq(TEST_DURATION))).andThrow(new CacheLoadingException(""));
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mockCacheManager);
    // when
    requestProcessorUnderTest.setFailOnCacheError(true);
    requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    // then
    assertNotNull(stubRequest.getRetryPolicy());
    assertEquals(0, stubRequest.getRetryPolicy().getRetryCount());
    EasyMock.verify(mockCacheManager);
    assertFalse(stubRequest.isLoadDataFromNetworkCalled());
    assertFalse(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener.isComplete());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) DefaultRetryPolicy(com.octo.android.robospice.retry.DefaultRetryPolicy) CacheLoadingException(com.octo.android.robospice.persistence.exception.CacheLoadingException) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Aggregations

CacheLoadingException (com.octo.android.robospice.persistence.exception.CacheLoadingException)6 Bitmap (android.graphics.Bitmap)2 CacheCreationException (com.octo.android.robospice.persistence.exception.CacheCreationException)2 CacheSavingException (com.octo.android.robospice.persistence.exception.CacheSavingException)2 RequestListener (com.octo.android.robospice.request.listener.RequestListener)2 PendingRequestListenerWithProgressStub (com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub)2 RequestListenerWithProgressStub (com.octo.android.robospice.stub.RequestListenerWithProgressStub)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 JsonParser (com.google.api.client.json.JsonParser)1 DefaultRetryPolicy (com.octo.android.robospice.retry.DefaultRetryPolicy)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 TypedInput (retrofit.mime.TypedInput)1