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