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());
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations