Search in sources :

Example 1 with CacheSavingException

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

the class JsonObjectPersister method saveDataToCacheAndReturnData.

@Override
public T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException {
    try {
        if (isAsyncSaveEnabled()) {
            Thread t = new Thread() {

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

                ;
            };
            t.start();
        } else {
            saveData(data, cacheKey);
        }
    } catch (CacheSavingException e) {
        throw e;
    } 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)

Example 2 with CacheSavingException

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

the class RetrofitObjectPersister method saveDataToCacheAndReturnData.

// ============================================================================================
// METHODS
// ============================================================================================
@Override
public T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException {
    try {
        if (isAsyncSaveEnabled()) {
            Thread t = new Thread() {

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

                ;
            };
            t.start();
        } else {
            saveData(data, cacheKey);
        }
    } catch (CacheSavingException e) {
        throw e;
    } 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)

Example 3 with CacheSavingException

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

the class SpringAndroidObjectPersister method saveDataToCacheAndReturnData.

@Override
public T saveDataToCacheAndReturnData(final T data, final Object cacheKey) throws CacheSavingException {
    try {
        if (isAsyncSaveEnabled()) {
            Thread t = new Thread() {

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

                ;
            };
            t.start();
        } else {
            saveData(data, cacheKey);
        }
    } catch (CacheSavingException e) {
        throw e;
    } 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)

Example 4 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_true_saving_to_cache_throws_exception.

public void testAddRequest_when_fail_on_error_true_saving_to_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))).andReturn(null);
    EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andThrow(new CacheSavingException(""));
    EasyMock.replay(mockCacheManager);
    // when
    requestProcessorUnderTest.setFailOnCacheError(true);
    requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    // then
    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) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

Example 5 with CacheSavingException

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

the class RequestProcessorTest method testAddRequest_when_saving_to_cache_throws_exception.

public void testAddRequest_when_saving_to_cache_throws_exception() throws CacheLoadingException, CacheSavingException, InterruptedException, CacheCreationException {
    // given
    CachedSpiceRequestStub<String> stubRequest = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
    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.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA), EasyMock.eq(TEST_CACHE_KEY))).andThrow(new CacheSavingException(""));
    EasyMock.replay(mockCacheManager);
    // when
    requestProcessorUnderTest.addRequest(stubRequest, requestListenerSet);
    mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
    // then
    EasyMock.verify(mockCacheManager);
    assertTrue(stubRequest.isLoadDataFromNetworkCalled());
    assertTrue(mockRequestListener.isExecutedInUIThread());
    assertTrue(mockRequestListener.isSuccessful());
    assertTrue(mockRequestListener.isComplete());
}
Also used : RequestListener(com.octo.android.robospice.request.listener.RequestListener) CacheSavingException(com.octo.android.robospice.persistence.exception.CacheSavingException) RequestListenerWithProgressStub(com.octo.android.robospice.stub.RequestListenerWithProgressStub) PendingRequestListenerWithProgressStub(com.octo.android.robospice.stub.PendingRequestListenerWithProgressStub) HashSet(java.util.HashSet)

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