use of com.octo.android.robospice.stub.RequestListenerWithProgressStub in project robospice by stephanenicolas.
the class RequestProcessorTest method test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor.
// ============================================================================================
// TDD
// ============================================================================================
public void test_2_spiceservicelisteners_should_be_notified_of_all_events_from_request_processor() throws Exception {
// TDD for issue 182
// given
PausableThreadPoolExecutor executorService = PriorityThreadPoolExecutor.getPriorityExecutor(1);
networkStateChecker = new MockNetworkStateChecker();
mockRequestRunner = new DefaultRequestRunner(getContext(), mockCacheManager, executorService, mockRequestProgressManager, networkStateChecker);
requestProcessorUnderTest = new RequestProcessor(mockCacheManager, mockRequestProgressManager, mockRequestRunner);
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))).andReturn(TEST_RETURNED_DATA);
EasyMock.expect(mockCacheManager.loadDataFromCache(EasyMock.eq(TEST_CLASS), EasyMock.eq(TEST_CACHE_KEY2), EasyMock.eq(TEST_DURATION))).andReturn(null);
EasyMock.expect(mockCacheManager.saveDataToCacheAndReturnData(EasyMock.eq(TEST_RETURNED_DATA2), EasyMock.eq(TEST_CACHE_KEY2))).andReturn(TEST_RETURNED_DATA2);
EasyMock.replay(mockCacheManager);
CachedSpiceRequestStub<String> spiceRequestStub = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY, TEST_DURATION, TEST_RETURNED_DATA);
spiceRequestStub.setPriority(SpiceRequest.PRIORITY_LOW);
spiceRequestStub.setRetryPolicy(null);
CachedSpiceRequestStub<String> spiceRequestStub2 = createSuccessfulRequest(TEST_CLASS, TEST_CACHE_KEY2, TEST_DURATION, TEST_RETURNED_DATA2);
spiceRequestStub2.setPriority(SpiceRequest.PRIORITY_HIGH);
spiceRequestStub2.setRetryPolicy(null);
RequestListenerWithProgressStub<String> mockRequestListener = new RequestListenerWithProgressStub<String>();
Set<RequestListener<?>> requestListenerSet = new HashSet<RequestListener<?>>();
requestListenerSet.add(mockRequestListener);
RequestListenerWithProgressStub<String> mockRequestListener2 = new RequestListenerWithProgressStub<String>();
Set<RequestListener<?>> requestListenerSet2 = new HashSet<RequestListener<?>>();
requestListenerSet2.add(mockRequestListener2);
SpiceServiceListener listener1 = EasyMock.createMock(SpiceServiceListener.class);
listener1.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().times(2);
listener1.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener1.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener1.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().times(2);
EasyMock.replay(listener1);
SpiceServiceListener listener2 = EasyMock.createMock(SpiceServiceListener.class);
listener2.onRequestAdded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().times(2);
listener2.onRequestProgressUpdated((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener2.onRequestProcessed((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().anyTimes();
listener2.onRequestSucceeded((CachedSpiceRequest<?>) EasyMock.anyObject(), (RequestProcessingContext) EasyMock.anyObject());
EasyMock.expectLastCall().times(2);
EasyMock.replay(listener2);
requestProcessorUnderTest.addSpiceServiceListener(listener1);
requestProcessorUnderTest.addSpiceServiceListener(listener2);
// when
requestProcessorUnderTest.addRequest(spiceRequestStub, requestListenerSet);
requestProcessorUnderTest.addRequest(spiceRequestStub2, requestListenerSet2);
mockRequestListener.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener.await(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener2.awaitComplete(REQUEST_COMPLETION_TIME_OUT);
mockRequestListener2.await(REQUEST_COMPLETION_TIME_OUT);
// test
assertTrue(mockRequestListener.isComplete());
assertTrue(mockRequestListener2.isComplete());
assertTrue(mockRequestListener.isSuccessful());
assertTrue(mockRequestListener2.isSuccessful());
EasyMock.verify(listener1);
EasyMock.verify(listener2);
}
use of com.octo.android.robospice.stub.RequestListenerWithProgressStub 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());
}
use of com.octo.android.robospice.stub.RequestListenerWithProgressStub 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());
}
use of com.octo.android.robospice.stub.RequestListenerWithProgressStub 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());
}
use of com.octo.android.robospice.stub.RequestListenerWithProgressStub 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());
}
Aggregations