Search in sources :

Example 6 with CacheSavingException

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

the class RequestProcessorTest method testAddRequest_when_fail_on_error_and_request_has_retry_polic_true_saving_to_cache_throws_exception.

public void testAddRequest_when_fail_on_error_and_request_has_retry_polic_true_saving_to_cache_throws_exception() throws InterruptedException, SpiceException {
    // 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))).andReturn(null);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andThrow(new CacheSavingException(""));
    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);
    assertTrue(stubRequest.isLoadDataFromNetworkCalled());
    assertTrue(mockRequestListener.isExecutedInUIThread());
    assertFalse(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener.isComplete());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) DefaultRetryPolicy(com.octo.android.robospice.retry.DefaultRetryPolicy) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 7 with CacheSavingException

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

the class InFileBigInputStreamObjectPersister method saveDataToCacheAndReturnData.

@Override
public InputStream saveDataToCacheAndReturnData(InputStream data, Object cacheKey) throws CacheSavingException {
    FileOutputStream output = null;
    // 2) we load and return it from the file
    try {
        output = new FileOutputStream(getCacheFile(cacheKey));
        IOUtils.copy(data, output);
        return new FileInputStream(getCacheFile(cacheKey));
    } catch (IOException e) {
        throw new CacheSavingException(e);
    } finally {
        IOUtils.closeQuietly(output);
    }
}
Also used : CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 8 with CacheSavingException

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

the class InFileBitmapObjectPersister method saveDataToCacheAndReturnData.

@Override
public Bitmap saveDataToCacheAndReturnData(Bitmap data, Object cacheKey) throws CacheSavingException {
    BufferedOutputStream out = null;
    try {
        File cacheFile = getCacheFile(cacheKey);
        out = new BufferedOutputStream(new FileOutputStream(cacheFile));
        boolean didCompress = data.compress(compressFormat, quality, out);
        if (!didCompress) {
            throw new CacheSavingException(String.format("Could not compress bitmap for path: %s", getCacheFile(cacheKey).getAbsolutePath()));
        }
        return data;
    } catch (IOException e) {
        throw new CacheSavingException(e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}
Also used : CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) File(java.io.File)

Example 9 with CacheSavingException

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

the class InFileInputStreamObjectPersister method saveDataToCacheAndReturnData.

@Override
public InputStream saveDataToCacheAndReturnData(InputStream data, final Object cacheKey) throws CacheSavingException {
    // special case for inputstream object : as it can be read only
    // once,
    // 0) we extract the content of the input stream as a byte[]
    // 1) we save it in file asynchronously if enabled
    // 2) the result will be a new InputStream on the byte[]
    final byte[] byteArray;
    try {
        byteArray = IOUtils.toByteArray(data);
        if (isAsyncSaveEnabled()) {
            Thread t = new Thread() {

                @Override
                public void run() {
                    try {
                        FileUtils.writeByteArrayToFile(getCacheFile(cacheKey), byteArray);
                    } catch (IOException e) {
                        Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously");
                    }
                }

                ;
            };
            t.start();
        } else {
            FileUtils.writeByteArrayToFile(getCacheFile(cacheKey), byteArray);
        }
        return new ByteArrayInputStream(byteArray);
    } catch (IOException e) {
        throw new CacheSavingException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) IOException(java.io.IOException)

Example 10 with CacheSavingException

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

the class InFileStringObjectPersister method saveDataToCacheAndReturnData.

@Override
public String saveDataToCacheAndReturnData(final String data, final Object cacheKey) throws CacheSavingException {
    Ln.v("Saving String " + data + " into cacheKey = " + cacheKey);
    try {
        if (isAsyncSaveEnabled()) {
            Thread t = new Thread() {

                @Override
                public void run() {
                    try {
                        FileUtils.writeStringToFile(getCacheFile(cacheKey), data, CharEncoding.UTF_8);
                    } catch (IOException e) {
                        Ln.e(e, "An error occured on saving request " + cacheKey + " data asynchronously");
                    }
                }

                ;
            };
            t.start();
        } else {
            FileUtils.writeStringToFile(getCacheFile(cacheKey), data, CharEncoding.UTF_8);
        }
    } catch (Exception e) {
        throw new CacheSavingException(e);
    }
    return data;
}
Also used : CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) IOException(java.io.IOException) 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)

Aggregations

CacheSavingException (com.octo.android.robospice.persistence.exception.CacheSavingException)10 IOException (java.io.IOException)7 CacheCreationException (com.octo.android.robospice.persistence.exception.CacheCreationException)4 CacheLoadingException (com.octo.android.robospice.persistence.exception.CacheLoadingException)4 FileNotFoundException (java.io.FileNotFoundException)4 RequestListener (com.octo.android.robospice.request.listener.RequestListener)3 PendingRequestListenerWithProgressStub (com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub)3 RequestListenerWithProgressStub (com.octo.android.robospice.stub.RequestListenerWithProgressStub)3 HashSet (java.util.HashSet)3 FileOutputStream (java.io.FileOutputStream)2 DefaultRetryPolicy (com.octo.android.robospice.retry.DefaultRetryPolicy)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1