HTTPCLIENT-1244: convert unit tests for cache module to use Mockito
Contributed by Joseph Walton <joe at kafsemo dot org> git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1661658 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d6b8a525b9
commit
95754d776c
|
@ -74,6 +74,11 @@
|
|||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
*/
|
||||
package org.apache.http.client.cache;
|
||||
|
||||
import static org.easymock.classextension.EasyMock.createNiceMock;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
@ -34,6 +33,7 @@ import static org.junit.Assert.assertNull;
|
|||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
@ -65,7 +65,7 @@ public class TestHttpCacheEntry {
|
|||
nineSecondsAgo = new Date(now.getTime() - 9 * 1000L);
|
||||
statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1,
|
||||
HttpStatus.SC_OK, "OK");
|
||||
mockResource = createNiceMock(Resource.class);
|
||||
mockResource = mock(Resource.class);
|
||||
}
|
||||
|
||||
private HttpCacheEntry makeEntry(final Header[] headers) {
|
||||
|
|
|
@ -26,6 +26,10 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.Header;
|
||||
|
@ -41,7 +45,6 @@ import org.apache.http.client.methods.HttpRequestWrapper;
|
|||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.easymock.classextension.EasyMock;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -60,15 +63,15 @@ public class TestAsynchronousValidationRequest {
|
|||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mockParent = EasyMock.createNiceMock(AsynchronousValidator.class);
|
||||
mockClient = EasyMock.createNiceMock(CachingExec.class);
|
||||
mockParent = mock(AsynchronousValidator.class);
|
||||
mockClient = mock(CachingExec.class);
|
||||
route = new HttpRoute(new HttpHost("foo.example.com", 80));
|
||||
request = HttpRequestWrapper.wrap(new HttpGet("/"));
|
||||
context = HttpClientContext.create();
|
||||
mockExecAware = EasyMock.createNiceMock(HttpExecutionAware.class);
|
||||
mockCacheEntry = EasyMock.createNiceMock(HttpCacheEntry.class);
|
||||
mockResponse = EasyMock.createNiceMock(CloseableHttpResponse.class);
|
||||
mockStatusLine = EasyMock.createNiceMock(StatusLine.class);
|
||||
mockExecAware = mock(HttpExecutionAware.class);
|
||||
mockCacheEntry = mock(HttpCacheEntry.class);
|
||||
mockResponse = mock(CloseableHttpResponse.class);
|
||||
mockStatusLine = mock(StatusLine.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -79,17 +82,19 @@ public class TestAsynchronousValidationRequest {
|
|||
mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
|
||||
identifier, 0);
|
||||
|
||||
EasyMock.expect(
|
||||
when(
|
||||
mockClient.revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).andReturn(mockResponse);
|
||||
EasyMock.expect(mockResponse.getStatusLine()).andReturn(mockStatusLine);
|
||||
EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(200);
|
||||
mockParent.markComplete(identifier);
|
||||
mockParent.jobSuccessful(identifier);
|
||||
route, request, context, mockExecAware, mockCacheEntry)).thenReturn(mockResponse);
|
||||
when(mockResponse.getStatusLine()).thenReturn(mockStatusLine);
|
||||
when(mockStatusLine.getStatusCode()).thenReturn(200);
|
||||
|
||||
replayMocks();
|
||||
impl.run();
|
||||
verifyMocks();
|
||||
|
||||
verify(mockClient).revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry);
|
||||
verify(mockResponse).getStatusLine();
|
||||
verify(mockParent).markComplete(identifier);
|
||||
verify(mockParent).jobSuccessful(identifier);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -100,17 +105,20 @@ public class TestAsynchronousValidationRequest {
|
|||
mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
|
||||
identifier, 0);
|
||||
|
||||
EasyMock.expect(
|
||||
when(
|
||||
mockClient.revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).andReturn(mockResponse);
|
||||
EasyMock.expect(mockResponse.getStatusLine()).andReturn(mockStatusLine);
|
||||
EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(200);
|
||||
mockParent.markComplete(identifier);
|
||||
mockParent.jobSuccessful(identifier);
|
||||
route, request, context, mockExecAware, mockCacheEntry)).thenReturn(mockResponse);
|
||||
when(mockResponse.getStatusLine()).thenReturn(mockStatusLine);
|
||||
when(mockStatusLine.getStatusCode()).thenReturn(200);
|
||||
|
||||
replayMocks();
|
||||
impl.run();
|
||||
verifyMocks();
|
||||
|
||||
verify(mockClient).revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry);
|
||||
verify(mockResponse).getStatusLine();
|
||||
verify(mockStatusLine).getStatusCode();
|
||||
verify(mockParent).markComplete(identifier);
|
||||
verify(mockParent).jobSuccessful(identifier);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -122,18 +130,22 @@ public class TestAsynchronousValidationRequest {
|
|||
mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
|
||||
identifier, 0);
|
||||
|
||||
EasyMock.expect(
|
||||
when(
|
||||
mockClient.revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).andReturn(mockResponse);
|
||||
EasyMock.expect(mockResponse.getStatusLine()).andReturn(mockStatusLine);
|
||||
EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(200);
|
||||
EasyMock.expect(mockResponse.getHeaders(HeaderConstants.WARNING)).andReturn(warning);
|
||||
mockParent.markComplete(identifier);
|
||||
mockParent.jobFailed(identifier);
|
||||
route, request, context, mockExecAware, mockCacheEntry)).thenReturn(mockResponse);
|
||||
when(mockResponse.getStatusLine()).thenReturn(mockStatusLine);
|
||||
when(mockStatusLine.getStatusCode()).thenReturn(200);
|
||||
when(mockResponse.getHeaders(HeaderConstants.WARNING)).thenReturn(warning);
|
||||
|
||||
replayMocks();
|
||||
impl.run();
|
||||
verifyMocks();
|
||||
|
||||
verify(mockClient).revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry);
|
||||
verify(mockResponse).getStatusLine();
|
||||
verify(mockStatusLine).getStatusCode();
|
||||
verify(mockResponse).getHeaders(HeaderConstants.WARNING);
|
||||
verify(mockParent).markComplete(identifier);
|
||||
verify(mockParent).jobFailed(identifier);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -144,16 +156,17 @@ public class TestAsynchronousValidationRequest {
|
|||
mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
|
||||
identifier, 0);
|
||||
|
||||
EasyMock.expect(
|
||||
when(
|
||||
mockClient.revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).andThrow(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).thenThrow(
|
||||
new ProtocolException());
|
||||
mockParent.markComplete(identifier);
|
||||
mockParent.jobFailed(identifier);
|
||||
|
||||
replayMocks();
|
||||
impl.run();
|
||||
verifyMocks();
|
||||
|
||||
verify(mockClient).revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry);
|
||||
verify(mockParent).markComplete(identifier);
|
||||
verify(mockParent).jobFailed(identifier);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -164,16 +177,17 @@ public class TestAsynchronousValidationRequest {
|
|||
mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
|
||||
identifier, 0);
|
||||
|
||||
EasyMock.expect(
|
||||
when(
|
||||
mockClient.revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).andThrow(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).thenThrow(
|
||||
new IOException());
|
||||
mockParent.markComplete(identifier);
|
||||
mockParent.jobFailed(identifier);
|
||||
|
||||
replayMocks();
|
||||
impl.run();
|
||||
verifyMocks();
|
||||
|
||||
verify(mockClient).revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry);
|
||||
verify(mockParent).markComplete(identifier);
|
||||
verify(mockParent).jobFailed(identifier);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -184,33 +198,16 @@ public class TestAsynchronousValidationRequest {
|
|||
mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
|
||||
identifier, 0);
|
||||
|
||||
EasyMock.expect(
|
||||
when(
|
||||
mockClient.revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).andThrow(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).thenThrow(
|
||||
new RuntimeException());
|
||||
mockParent.markComplete(identifier);
|
||||
mockParent.jobFailed(identifier);
|
||||
|
||||
replayMocks();
|
||||
impl.run();
|
||||
verifyMocks();
|
||||
}
|
||||
|
||||
public void replayMocks() {
|
||||
EasyMock.replay(mockClient);
|
||||
EasyMock.replay(mockExecAware);
|
||||
EasyMock.replay(mockCacheEntry);
|
||||
EasyMock.replay(mockResponse);
|
||||
EasyMock.replay(mockStatusLine);
|
||||
EasyMock.replay(mockParent);
|
||||
}
|
||||
|
||||
public void verifyMocks() {
|
||||
EasyMock.verify(mockClient);
|
||||
EasyMock.verify(mockExecAware);
|
||||
EasyMock.verify(mockCacheEntry);
|
||||
EasyMock.verify(mockResponse);
|
||||
EasyMock.verify(mockStatusLine);
|
||||
EasyMock.verify(mockParent);
|
||||
verify(mockClient).revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry);
|
||||
verify(mockParent).markComplete(identifier);
|
||||
verify(mockParent).jobFailed(identifier);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,13 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -41,11 +48,10 @@ import org.apache.http.client.methods.HttpRequestWrapper;
|
|||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.easymock.Capture;
|
||||
import org.easymock.classextension.EasyMock;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
@SuppressWarnings({"boxing","static-access"}) // test code
|
||||
public class TestAsynchronousValidator {
|
||||
|
@ -63,26 +69,26 @@ public class TestAsynchronousValidator {
|
|||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mockClient = EasyMock.createNiceMock(CachingExec.class);
|
||||
mockClient = mock(CachingExec.class);
|
||||
route = new HttpRoute(new HttpHost("foo.example.com", 80));
|
||||
request = HttpRequestWrapper.wrap(new HttpGet("/"));
|
||||
context = HttpClientContext.create();
|
||||
context.setTargetHost(new HttpHost("foo.example.com"));
|
||||
mockExecAware = EasyMock.createNiceMock(HttpExecutionAware.class);
|
||||
mockCacheEntry = EasyMock.createNiceMock(HttpCacheEntry.class);
|
||||
mockSchedulingStrategy = EasyMock.createNiceMock(SchedulingStrategy.class);
|
||||
mockExecAware = mock(HttpExecutionAware.class);
|
||||
mockCacheEntry = mock(HttpCacheEntry.class);
|
||||
mockSchedulingStrategy = mock(SchedulingStrategy.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRevalidateCacheEntrySchedulesExecutionAndPopulatesIdentifier() {
|
||||
impl = new AsynchronousValidator(mockSchedulingStrategy);
|
||||
|
||||
EasyMock.expect(mockCacheEntry.hasVariants()).andReturn(false);
|
||||
mockSchedulingStrategy.schedule(EasyMock.isA(AsynchronousValidationRequest.class));
|
||||
when(mockCacheEntry.hasVariants()).thenReturn(false);
|
||||
|
||||
replayMocks();
|
||||
impl.revalidateCacheEntry(mockClient, route, request, context, mockExecAware, mockCacheEntry);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCacheEntry).hasVariants();
|
||||
verify(mockSchedulingStrategy).schedule(isA(AsynchronousValidationRequest.class));
|
||||
|
||||
Assert.assertEquals(1, impl.getScheduledIdentifiers().size());
|
||||
}
|
||||
|
@ -91,13 +97,13 @@ public class TestAsynchronousValidator {
|
|||
public void testMarkCompleteRemovesIdentifier() {
|
||||
impl = new AsynchronousValidator(mockSchedulingStrategy);
|
||||
|
||||
EasyMock.expect(mockCacheEntry.hasVariants()).andReturn(false);
|
||||
final Capture<AsynchronousValidationRequest> cap = new Capture<AsynchronousValidationRequest>();
|
||||
mockSchedulingStrategy.schedule(EasyMock.capture(cap));
|
||||
when(mockCacheEntry.hasVariants()).thenReturn(false);
|
||||
|
||||
replayMocks();
|
||||
impl.revalidateCacheEntry(mockClient, route, request, context, mockExecAware, mockCacheEntry);
|
||||
verifyMocks();
|
||||
|
||||
final ArgumentCaptor<AsynchronousValidationRequest> cap = ArgumentCaptor.forClass(AsynchronousValidationRequest.class);
|
||||
verify(mockCacheEntry).hasVariants();
|
||||
verify(mockSchedulingStrategy).schedule(cap.capture());
|
||||
|
||||
Assert.assertEquals(1, impl.getScheduledIdentifiers().size());
|
||||
|
||||
|
@ -110,30 +116,28 @@ public class TestAsynchronousValidator {
|
|||
public void testRevalidateCacheEntryDoesNotPopulateIdentifierOnRejectedExecutionException() {
|
||||
impl = new AsynchronousValidator(mockSchedulingStrategy);
|
||||
|
||||
EasyMock.expect(mockCacheEntry.hasVariants()).andReturn(false);
|
||||
mockSchedulingStrategy.schedule(EasyMock.isA(AsynchronousValidationRequest.class));
|
||||
EasyMock.expectLastCall().andThrow(new RejectedExecutionException());
|
||||
when(mockCacheEntry.hasVariants()).thenReturn(false);
|
||||
doThrow(new RejectedExecutionException()).when(mockSchedulingStrategy).schedule(isA(AsynchronousValidationRequest.class));
|
||||
|
||||
replayMocks();
|
||||
impl.revalidateCacheEntry(mockClient, route, request, context, mockExecAware, mockCacheEntry);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCacheEntry).hasVariants();
|
||||
|
||||
Assert.assertEquals(0, impl.getScheduledIdentifiers().size());
|
||||
verify(mockSchedulingStrategy).schedule(isA(AsynchronousValidationRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRevalidateCacheEntryProperlyCollapsesRequest() {
|
||||
impl = new AsynchronousValidator(mockSchedulingStrategy);
|
||||
|
||||
EasyMock.expect(mockCacheEntry.hasVariants()).andReturn(false);
|
||||
mockSchedulingStrategy.schedule(EasyMock.isA(AsynchronousValidationRequest.class));
|
||||
when(mockCacheEntry.hasVariants()).thenReturn(false);
|
||||
|
||||
EasyMock.expect(mockCacheEntry.hasVariants()).andReturn(false);
|
||||
|
||||
replayMocks();
|
||||
impl.revalidateCacheEntry(mockClient, route, request, context, mockExecAware, mockCacheEntry);
|
||||
impl.revalidateCacheEntry(mockClient, route, request, context, mockExecAware, mockCacheEntry);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCacheEntry, times(2)).hasVariants();
|
||||
verify(mockSchedulingStrategy).schedule(isA(AsynchronousValidationRequest.class));
|
||||
|
||||
Assert.assertEquals(1, impl.getScheduledIdentifiers().size());
|
||||
}
|
||||
|
@ -152,18 +156,18 @@ public class TestAsynchronousValidator {
|
|||
new BasicHeader(HeaderConstants.VARY, "Accept-Encoding")
|
||||
};
|
||||
|
||||
EasyMock.expect(mockCacheEntry.hasVariants()).andReturn(true).times(2);
|
||||
EasyMock.expect(mockCacheEntry.getHeaders(HeaderConstants.VARY)).andReturn(variantHeaders).times(2);
|
||||
mockSchedulingStrategy.schedule(EasyMock.isA(AsynchronousValidationRequest.class));
|
||||
EasyMock.expectLastCall().times(2);
|
||||
when(mockCacheEntry.hasVariants()).thenReturn(true);
|
||||
when(mockCacheEntry.getHeaders(HeaderConstants.VARY)).thenReturn(variantHeaders);
|
||||
mockSchedulingStrategy.schedule(isA(AsynchronousValidationRequest.class));
|
||||
|
||||
replayMocks();
|
||||
impl.revalidateCacheEntry(mockClient, route, HttpRequestWrapper.wrap(req1), context, mockExecAware, mockCacheEntry);
|
||||
impl.revalidateCacheEntry(mockClient, route, HttpRequestWrapper.wrap(req2), context, mockExecAware, mockCacheEntry);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCacheEntry, times(2)).hasVariants();
|
||||
verify(mockCacheEntry, times(2)).getHeaders(HeaderConstants.VARY);
|
||||
verify(mockSchedulingStrategy, times(2)).schedule(isA(AsynchronousValidationRequest.class));
|
||||
|
||||
Assert.assertEquals(2, impl.getScheduledIdentifiers().size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -175,11 +179,10 @@ public class TestAsynchronousValidator {
|
|||
final ImmediateSchedulingStrategy schedulingStrategy = new ImmediateSchedulingStrategy(config);
|
||||
impl = new AsynchronousValidator(schedulingStrategy);
|
||||
|
||||
EasyMock.expect(mockCacheEntry.hasVariants()).andReturn(false);
|
||||
EasyMock.expect(mockClient.revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).andReturn(null);
|
||||
when(mockCacheEntry.hasVariants()).thenReturn(false);
|
||||
when(mockClient.revalidateCacheEntry(
|
||||
route, request, context, mockExecAware, mockCacheEntry)).thenReturn(null);
|
||||
|
||||
replayMocks();
|
||||
impl.revalidateCacheEntry(mockClient, route, request, context, mockExecAware, mockCacheEntry);
|
||||
|
||||
try {
|
||||
|
@ -189,7 +192,8 @@ public class TestAsynchronousValidator {
|
|||
} catch (final InterruptedException ie) {
|
||||
|
||||
} finally {
|
||||
verifyMocks();
|
||||
verify(mockCacheEntry).hasVariants();
|
||||
verify(mockClient).revalidateCacheEntry(route, request, context, mockExecAware, mockCacheEntry);
|
||||
|
||||
Assert.assertEquals(0, impl.getScheduledIdentifiers().size());
|
||||
}
|
||||
|
@ -199,24 +203,8 @@ public class TestAsynchronousValidator {
|
|||
public void testSchedulingStrategyShutdownOnClose() throws IOException {
|
||||
impl = new AsynchronousValidator(mockSchedulingStrategy);
|
||||
|
||||
mockSchedulingStrategy.close();
|
||||
|
||||
replayMocks();
|
||||
impl.close();
|
||||
verifyMocks();
|
||||
}
|
||||
|
||||
public void replayMocks() {
|
||||
EasyMock.replay(mockSchedulingStrategy);
|
||||
EasyMock.replay(mockClient);
|
||||
EasyMock.replay(mockExecAware);
|
||||
EasyMock.replay(mockCacheEntry);
|
||||
}
|
||||
|
||||
public void verifyMocks() {
|
||||
EasyMock.verify(mockSchedulingStrategy);
|
||||
EasyMock.verify(mockClient);
|
||||
EasyMock.verify(mockExecAware);
|
||||
EasyMock.verify(mockCacheEntry);
|
||||
verify(mockSchedulingStrategy).close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,10 +26,10 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.classextension.EasyMock.createNiceMock;
|
||||
import static org.easymock.classextension.EasyMock.replay;
|
||||
import static org.easymock.classextension.EasyMock.verify;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
@ -51,9 +51,6 @@ import org.apache.http.message.BasicHeader;
|
|||
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
|
||||
import org.apache.http.message.BasicHttpRequest;
|
||||
import org.apache.http.message.BasicHttpResponse;
|
||||
import org.easymock.EasyMock;
|
||||
import org.easymock.IAnswer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -78,25 +75,15 @@ public class TestCacheInvalidator {
|
|||
tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
|
||||
|
||||
host = new HttpHost("foo.example.com");
|
||||
mockStorage = createNiceMock(HttpCacheStorage.class);
|
||||
mockStorage = mock(HttpCacheStorage.class);
|
||||
cacheKeyGenerator = new CacheKeyGenerator();
|
||||
mockEntry = createNiceMock(HttpCacheEntry.class);
|
||||
mockEntry = mock(HttpCacheEntry.class);
|
||||
request = HttpTestUtils.makeDefaultRequest();
|
||||
response = HttpTestUtils.make200Response();
|
||||
|
||||
impl = new CacheInvalidator(cacheKeyGenerator, mockStorage);
|
||||
}
|
||||
|
||||
private void replayMocks() {
|
||||
replay(mockStorage);
|
||||
replay(mockEntry);
|
||||
}
|
||||
|
||||
private void verifyMocks() {
|
||||
verify(mockStorage);
|
||||
verify(mockEntry);
|
||||
}
|
||||
|
||||
// Tests
|
||||
@Test
|
||||
public void testInvalidatesRequestsThatArentGETorHEAD() throws Exception {
|
||||
|
@ -106,12 +93,12 @@ public class TestCacheInvalidator {
|
|||
cacheEntryHasVariantMap(variantMap);
|
||||
|
||||
cacheReturnsEntryForUri(theUri);
|
||||
entryIsRemoved(theUri);
|
||||
replayMocks();
|
||||
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).getVariantMap();
|
||||
verify(mockStorage).getEntry(theUri);
|
||||
verify(mockStorage).removeEntry(theUri);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -127,14 +114,13 @@ public class TestCacheInvalidator {
|
|||
cacheEntryHasVariantMap(new HashMap<String,String>());
|
||||
|
||||
cacheReturnsEntryForUri(theUri);
|
||||
entryIsRemoved(theUri);
|
||||
entryIsRemoved("http://foo.example.com:80/content");
|
||||
|
||||
replayMocks();
|
||||
|
||||
impl.flushInvalidatedCacheEntries(host, putRequest);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).getVariantMap();
|
||||
verify(mockStorage).getEntry(theUri);
|
||||
verify(mockStorage).removeEntry(theUri);
|
||||
verify(mockStorage).removeEntry("http://foo.example.com:80/content");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -150,14 +136,13 @@ public class TestCacheInvalidator {
|
|||
cacheEntryHasVariantMap(new HashMap<String,String>());
|
||||
|
||||
cacheReturnsEntryForUri(theUri);
|
||||
entryIsRemoved(theUri);
|
||||
entryIsRemoved(cacheKeyGenerator.canonicalizeUri(contentLocation));
|
||||
|
||||
replayMocks();
|
||||
|
||||
impl.flushInvalidatedCacheEntries(host, putRequest);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).getVariantMap();
|
||||
verify(mockStorage).getEntry(theUri);
|
||||
verify(mockStorage).removeEntry(theUri);
|
||||
verify(mockStorage).removeEntry(cacheKeyGenerator.canonicalizeUri(contentLocation));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -173,14 +158,13 @@ public class TestCacheInvalidator {
|
|||
cacheEntryHasVariantMap(new HashMap<String,String>());
|
||||
|
||||
cacheReturnsEntryForUri(theUri);
|
||||
entryIsRemoved(theUri);
|
||||
entryIsRemoved("http://foo.example.com:80/content");
|
||||
|
||||
replayMocks();
|
||||
|
||||
impl.flushInvalidatedCacheEntries(host, putRequest);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).getVariantMap();
|
||||
verify(mockStorage).getEntry(theUri);
|
||||
verify(mockStorage).removeEntry(theUri);
|
||||
verify(mockStorage).removeEntry("http://foo.example.com:80/content");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -196,29 +180,30 @@ public class TestCacheInvalidator {
|
|||
cacheEntryHasVariantMap(new HashMap<String,String>());
|
||||
|
||||
cacheReturnsEntryForUri(theUri);
|
||||
entryIsRemoved(theUri);
|
||||
|
||||
replayMocks();
|
||||
|
||||
impl.flushInvalidatedCacheEntries(host, putRequest);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).getVariantMap();
|
||||
verify(mockStorage).getEntry(theUri);
|
||||
verify(mockStorage).removeEntry(theUri);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesNotInvalidateGETRequest() throws Exception {
|
||||
request = new BasicHttpRequest("GET","/",HTTP_1_1);
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry("http://foo.example.com:80/");
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesNotInvalidateHEADRequest() throws Exception {
|
||||
request = new BasicHttpRequest("HEAD","/",HTTP_1_1);
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry("http://foo.example.com:80/");
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -230,11 +215,13 @@ public class TestCacheInvalidator {
|
|||
cacheEntryisForMethod("HEAD");
|
||||
cacheEntryHasVariantMap(new HashMap<String, String>());
|
||||
cacheReturnsEntryForUri(theURI);
|
||||
entryIsRemoved(theURI);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockEntry).getRequestMethod();
|
||||
verify(mockEntry).getVariantMap();
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verify(mockStorage).removeEntry(theURI);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -249,12 +236,14 @@ public class TestCacheInvalidator {
|
|||
cacheEntryisForMethod("HEAD");
|
||||
cacheEntryHasVariantMap(variants);
|
||||
cacheReturnsEntryForUri(theURI);
|
||||
entryIsRemoved(theURI);
|
||||
entryIsRemoved(theVariantURI);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockEntry).getRequestMethod();
|
||||
verify(mockEntry).getVariantMap();
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verify(mockStorage).removeEntry(theURI);
|
||||
verify(mockStorage).removeEntry(theVariantURI);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -264,9 +253,10 @@ public class TestCacheInvalidator {
|
|||
|
||||
cacheReturnsEntryForUri(theURI);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -277,9 +267,10 @@ public class TestCacheInvalidator {
|
|||
|
||||
cacheReturnsEntryForUri(theURI);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -291,27 +282,33 @@ public class TestCacheInvalidator {
|
|||
cacheEntryisForMethod("GET");
|
||||
cacheReturnsEntryForUri(theURI);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockEntry).getRequestMethod();
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesNotInvalidateRequestsWithClientCacheControlHeaders() throws Exception {
|
||||
request = new BasicHttpRequest("GET","/",HTTP_1_1);
|
||||
request.setHeader("Cache-Control","no-cache");
|
||||
replayMocks();
|
||||
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry("http://foo.example.com:80/");
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesNotInvalidateRequestsWithClientPragmaHeaders() throws Exception {
|
||||
request = new BasicHttpRequest("GET","/",HTTP_1_1);
|
||||
request.setHeader("Pragma","no-cache");
|
||||
replayMocks();
|
||||
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry("http://foo.example.com:80/");
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -324,12 +321,12 @@ public class TestCacheInvalidator {
|
|||
cacheReturnsEntryForUri(theUri);
|
||||
cacheEntryHasVariantMap(mapOfURIs);
|
||||
|
||||
entryIsRemoved(variantUri);
|
||||
entryIsRemoved(theUri);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theUri);
|
||||
verify(mockEntry).getVariantMap();
|
||||
verify(mockStorage).removeEntry(variantUri);
|
||||
verify(mockStorage).removeEntry(theUri);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -339,17 +336,18 @@ public class TestCacheInvalidator {
|
|||
|
||||
cacheReturnsExceptionForUri(theURI);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotFlushForResponsesWithoutContentLocation()
|
||||
throws Exception {
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -365,12 +363,12 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("ETag", "\"old-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verify(mockStorage).removeEntry(theURI);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -387,12 +385,12 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("ETag", "\"old-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verify(mockStorage).removeEntry(theURI);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -404,24 +402,9 @@ public class TestCacheInvalidator {
|
|||
final String theURI = "http://foo.example.com:80/bar";
|
||||
response.setHeader("Content-Location", theURI);
|
||||
|
||||
final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] {
|
||||
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
|
||||
new BasicHeader("ETag", "\"old-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {
|
||||
@Override
|
||||
public Void answer() {
|
||||
Assert.fail();
|
||||
return null;
|
||||
}
|
||||
}).anyTimes();
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -437,12 +420,12 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("ETag", "\"old-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(cacheKey)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(cacheKey);
|
||||
when(mockStorage.getEntry(cacheKey)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(cacheKey);
|
||||
verify(mockStorage).removeEntry(cacheKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -458,12 +441,12 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("ETag", "\"old-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(cacheKey)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(cacheKey);
|
||||
when(mockStorage.getEntry(cacheKey)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(cacheKey);
|
||||
verify(mockStorage).removeEntry(cacheKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -479,19 +462,12 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("ETag", "\"old-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(cacheKey)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(cacheKey);
|
||||
EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {
|
||||
@Override
|
||||
public Void answer() {
|
||||
Assert.fail();
|
||||
return null;
|
||||
}
|
||||
}).anyTimes();
|
||||
when(mockStorage.getEntry(cacheKey)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(cacheKey);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
|
||||
|
@ -509,19 +485,11 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("ETag", "\"same-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {
|
||||
@Override
|
||||
public Void answer() {
|
||||
Assert.fail();
|
||||
return null;
|
||||
}
|
||||
}).anyTimes();
|
||||
|
||||
replayMocks();
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -537,19 +505,12 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("ETag", "\"old-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {
|
||||
@Override
|
||||
public Void answer() {
|
||||
Assert.fail();
|
||||
return null;
|
||||
}
|
||||
}).anyTimes();
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -560,19 +521,12 @@ public class TestCacheInvalidator {
|
|||
final String theURI = "http://foo.example.com:80/bar";
|
||||
response.setHeader("Content-Location", theURI);
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(null).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {
|
||||
@Override
|
||||
public Void answer() {
|
||||
Assert.fail();
|
||||
return null;
|
||||
}
|
||||
}).anyTimes();
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(null);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -588,19 +542,12 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("ETag", "\"old-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
EasyMock.expectLastCall().andAnswer(new IAnswer<Void>() {
|
||||
@Override
|
||||
public Void answer() {
|
||||
Assert.fail();
|
||||
return null;
|
||||
}
|
||||
}).anyTimes();
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -615,11 +562,12 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -635,12 +583,13 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verify(mockStorage).removeEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -655,12 +604,13 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("ETag", "\"old-etag\"")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verify(mockStorage).removeEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -676,12 +626,13 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo))
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verify(mockStorage).removeEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -697,35 +648,31 @@ public class TestCacheInvalidator {
|
|||
new BasicHeader("Date", "foo")
|
||||
});
|
||||
|
||||
expect(mockStorage.getEntry(theURI)).andReturn(entry).anyTimes();
|
||||
mockStorage.removeEntry(theURI);
|
||||
when(mockStorage.getEntry(theURI)).thenReturn(entry);
|
||||
|
||||
replayMocks();
|
||||
impl.flushInvalidatedCacheEntries(host, request, response);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockStorage).getEntry(theURI);
|
||||
verify(mockStorage).removeEntry(theURI);
|
||||
verifyNoMoreInteractions(mockStorage);
|
||||
}
|
||||
|
||||
|
||||
// Expectations
|
||||
private void cacheEntryHasVariantMap(final Map<String,String> variantMap) {
|
||||
expect(mockEntry.getVariantMap()).andReturn(variantMap);
|
||||
when(mockEntry.getVariantMap()).thenReturn(variantMap);
|
||||
}
|
||||
|
||||
private void cacheReturnsEntryForUri(final String theUri) throws IOException {
|
||||
expect(mockStorage.getEntry(theUri)).andReturn(mockEntry);
|
||||
when(mockStorage.getEntry(theUri)).thenReturn(mockEntry);
|
||||
}
|
||||
|
||||
private void cacheReturnsExceptionForUri(final String theUri) throws IOException {
|
||||
expect(mockStorage.getEntry(theUri)).andThrow(
|
||||
when(mockStorage.getEntry(theUri)).thenThrow(
|
||||
new IOException("TOTAL FAIL"));
|
||||
}
|
||||
|
||||
private void entryIsRemoved(final String theUri) throws IOException {
|
||||
mockStorage.removeEntry(theUri);
|
||||
}
|
||||
|
||||
private void cacheEntryisForMethod(final String httpMethod) {
|
||||
expect(mockEntry.getRequestMethod()).andReturn(httpMethod);
|
||||
when(mockEntry.getRequestMethod()).thenReturn(httpMethod);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,6 +26,10 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
|
@ -34,7 +38,6 @@ import org.apache.http.client.cache.HttpCacheEntry;
|
|||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicHttpRequest;
|
||||
import org.easymock.classextension.EasyMock;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -54,21 +57,11 @@ public class TestCacheKeyGenerator {
|
|||
@Before
|
||||
public void setUp() throws Exception {
|
||||
defaultHost = new HttpHost("foo.example.com");
|
||||
mockEntry = EasyMock.createNiceMock(HttpCacheEntry.class);
|
||||
mockRequest = EasyMock.createNiceMock(HttpRequest.class);
|
||||
mockEntry = mock(HttpCacheEntry.class);
|
||||
mockRequest = mock(HttpRequest.class);
|
||||
extractor = new CacheKeyGenerator();
|
||||
}
|
||||
|
||||
private void replayMocks() {
|
||||
EasyMock.replay(mockEntry);
|
||||
EasyMock.replay(mockRequest);
|
||||
}
|
||||
|
||||
private void verifyMocks() {
|
||||
EasyMock.verify(mockEntry);
|
||||
EasyMock.verify(mockRequest);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtractsUriFromAbsoluteUriInRequest() {
|
||||
final HttpHost host = new HttpHost("bar.example.com");
|
||||
|
@ -124,7 +117,7 @@ public class TestCacheKeyGenerator {
|
|||
@Test
|
||||
public void testGetVariantURIWithNoVaryHeaderReturnsNormalURI() {
|
||||
final String theURI = "theURI";
|
||||
org.easymock.EasyMock.expect(mockEntry.hasVariants()).andReturn(false);
|
||||
when(mockEntry.hasVariants()).thenReturn(false);
|
||||
extractor = new CacheKeyGenerator() {
|
||||
@Override
|
||||
public String getURI(final HttpHost h, final HttpRequest req) {
|
||||
|
@ -134,9 +127,8 @@ public class TestCacheKeyGenerator {
|
|||
}
|
||||
};
|
||||
|
||||
replayMocks();
|
||||
final String result = extractor.getVariantURI(defaultHost, mockRequest, mockEntry);
|
||||
verifyMocks();
|
||||
verify(mockEntry).hasVariants();
|
||||
Assert.assertSame(theURI, result);
|
||||
}
|
||||
|
||||
|
@ -154,15 +146,16 @@ public class TestCacheKeyGenerator {
|
|||
return theURI;
|
||||
}
|
||||
};
|
||||
EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
|
||||
EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
|
||||
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(
|
||||
when(mockEntry.hasVariants()).thenReturn(true);
|
||||
when(mockEntry.getHeaders("Vary")).thenReturn(varyHeaders);
|
||||
when(mockRequest.getHeaders("Accept-Encoding")).thenReturn(
|
||||
encHeaders);
|
||||
replayMocks();
|
||||
|
||||
final String result = extractor.getVariantURI(defaultHost, mockRequest, mockEntry);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).hasVariants();
|
||||
verify(mockEntry).getHeaders("Vary");
|
||||
verify(mockRequest).getHeaders("Accept-Encoding");
|
||||
Assert.assertEquals("{Accept-Encoding=gzip}" + theURI, result);
|
||||
}
|
||||
|
||||
|
@ -179,15 +172,16 @@ public class TestCacheKeyGenerator {
|
|||
return theURI;
|
||||
}
|
||||
};
|
||||
EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
|
||||
EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
|
||||
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding"))
|
||||
.andReturn(noHeaders);
|
||||
replayMocks();
|
||||
when(mockEntry.hasVariants()).thenReturn(true);
|
||||
when(mockEntry.getHeaders("Vary")).thenReturn(varyHeaders);
|
||||
when(mockRequest.getHeaders("Accept-Encoding"))
|
||||
.thenReturn(noHeaders);
|
||||
|
||||
final String result = extractor.getVariantURI(defaultHost, mockRequest, mockEntry);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).hasVariants();
|
||||
verify(mockEntry).getHeaders("Vary");
|
||||
verify(mockRequest).getHeaders("Accept-Encoding");
|
||||
Assert.assertEquals("{Accept-Encoding=}" + theURI, result);
|
||||
}
|
||||
|
||||
|
@ -205,16 +199,18 @@ public class TestCacheKeyGenerator {
|
|||
return theURI;
|
||||
}
|
||||
};
|
||||
EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
|
||||
EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
|
||||
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(
|
||||
when(mockEntry.hasVariants()).thenReturn(true);
|
||||
when(mockEntry.getHeaders("Vary")).thenReturn(varyHeaders);
|
||||
when(mockRequest.getHeaders("Accept-Encoding")).thenReturn(
|
||||
encHeaders);
|
||||
EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders);
|
||||
replayMocks();
|
||||
when(mockRequest.getHeaders("User-Agent")).thenReturn(uaHeaders);
|
||||
|
||||
final String result = extractor.getVariantURI(defaultHost, mockRequest, mockEntry);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).hasVariants();
|
||||
verify(mockEntry).getHeaders("Vary");
|
||||
verify(mockRequest).getHeaders("Accept-Encoding");
|
||||
verify(mockRequest).getHeaders("User-Agent");
|
||||
Assert.assertEquals("{Accept-Encoding=gzip&User-Agent=browser}" + theURI, result);
|
||||
}
|
||||
|
||||
|
@ -233,15 +229,17 @@ public class TestCacheKeyGenerator {
|
|||
return theURI;
|
||||
}
|
||||
};
|
||||
EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
|
||||
EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
|
||||
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(encHeaders);
|
||||
EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders);
|
||||
replayMocks();
|
||||
when(mockEntry.hasVariants()).thenReturn(true);
|
||||
when(mockEntry.getHeaders("Vary")).thenReturn(varyHeaders);
|
||||
when(mockRequest.getHeaders("Accept-Encoding")).thenReturn(encHeaders);
|
||||
when(mockRequest.getHeaders("User-Agent")).thenReturn(uaHeaders);
|
||||
|
||||
final String result = extractor.getVariantURI(defaultHost, mockRequest, mockEntry);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).hasVariants();
|
||||
verify(mockEntry).getHeaders("Vary");
|
||||
verify(mockRequest).getHeaders("Accept-Encoding");
|
||||
verify(mockRequest).getHeaders("User-Agent");
|
||||
Assert.assertEquals("{Accept-Encoding=gzip&User-Agent=browser}" + theURI, result);
|
||||
}
|
||||
|
||||
|
@ -260,15 +258,17 @@ public class TestCacheKeyGenerator {
|
|||
return theURI;
|
||||
}
|
||||
};
|
||||
EasyMock.expect(mockEntry.hasVariants()).andReturn(true).anyTimes();
|
||||
EasyMock.expect(mockEntry.getHeaders("Vary")).andReturn(varyHeaders);
|
||||
EasyMock.expect(mockRequest.getHeaders("Accept-Encoding")).andReturn(encHeaders);
|
||||
EasyMock.expect(mockRequest.getHeaders("User-Agent")).andReturn(uaHeaders);
|
||||
replayMocks();
|
||||
when(mockEntry.hasVariants()).thenReturn(true);
|
||||
when(mockEntry.getHeaders("Vary")).thenReturn(varyHeaders);
|
||||
when(mockRequest.getHeaders("Accept-Encoding")).thenReturn(encHeaders);
|
||||
when(mockRequest.getHeaders("User-Agent")).thenReturn(uaHeaders);
|
||||
|
||||
final String result = extractor.getVariantURI(defaultHost, mockRequest, mockEntry);
|
||||
|
||||
verifyMocks();
|
||||
verify(mockEntry).hasVariants();
|
||||
verify(mockEntry).getHeaders("Vary");
|
||||
verify(mockRequest).getHeaders("Accept-Encoding");
|
||||
verify(mockRequest).getHeaders("User-Agent");
|
||||
Assert
|
||||
.assertEquals("{Accept-Encoding=gzip%2C+deflate&User-Agent=browser}" + theURI,
|
||||
result);
|
||||
|
|
|
@ -26,6 +26,12 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Matchers.same;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
@ -33,9 +39,7 @@ import org.apache.http.Header;
|
|||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.cache.HttpCacheEntry;
|
||||
import org.apache.http.client.methods.HttpRequestWrapper;
|
||||
import org.apache.http.client.utils.DateUtils;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.easymock.classextension.EasyMock;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -47,27 +51,15 @@ public class TestCachedHttpResponseGenerator {
|
|||
private HttpRequestWrapper request;
|
||||
private CacheValidityPolicy mockValidityPolicy;
|
||||
private CachedHttpResponseGenerator impl;
|
||||
private Date now;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
now = new Date();
|
||||
final Date eightSecondsAgo = new Date(now.getTime() - 8 * 1000L);
|
||||
final Date tenSecondsFromNow = new Date(now.getTime() + 10 * 1000L);
|
||||
final Header[] hdrs = { new BasicHeader("Date", DateUtils.formatDate(eightSecondsAgo)),
|
||||
new BasicHeader("Expires", DateUtils.formatDate(tenSecondsFromNow)),
|
||||
new BasicHeader("Content-Length", "150") };
|
||||
|
||||
entry = HttpTestUtils.makeCacheEntry(new HashMap<String, String>());
|
||||
request = HttpRequestWrapper.wrap(HttpTestUtils.makeDefaultRequest());
|
||||
mockValidityPolicy = EasyMock.createNiceMock(CacheValidityPolicy.class);
|
||||
mockValidityPolicy = mock(CacheValidityPolicy.class);
|
||||
impl = new CachedHttpResponseGenerator(mockValidityPolicy);
|
||||
}
|
||||
|
||||
public void replayMocks() {
|
||||
EasyMock.replay(mockValidityPolicy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResponseHasContentLength() {
|
||||
final byte[] buf = new byte[] { 1, 2, 3, 4, 5 };
|
||||
|
@ -117,10 +109,11 @@ public class TestCachedHttpResponseGenerator {
|
|||
@Test
|
||||
public void testAgeHeaderIsPopulatedWithCurrentAgeOfCacheEntryIfNonZero() {
|
||||
currentAge(10L);
|
||||
replayMocks();
|
||||
|
||||
final HttpResponse response = impl.generateResponse(request, entry);
|
||||
|
||||
verify(mockValidityPolicy).getCurrentAgeSecs(same(entry), isA(Date.class));
|
||||
|
||||
final Header ageHdr = response.getFirstHeader("Age");
|
||||
Assert.assertNotNull(ageHdr);
|
||||
Assert.assertEquals(10L, Long.parseLong(ageHdr.getValue()));
|
||||
|
@ -129,10 +122,11 @@ public class TestCachedHttpResponseGenerator {
|
|||
@Test
|
||||
public void testAgeHeaderIsNotPopulatedIfCurrentAgeOfCacheEntryIsZero() {
|
||||
currentAge(0L);
|
||||
replayMocks();
|
||||
|
||||
final HttpResponse response = impl.generateResponse(request, entry);
|
||||
|
||||
verify(mockValidityPolicy).getCurrentAgeSecs(same(entry), isA(Date.class));
|
||||
|
||||
final Header ageHdr = response.getFirstHeader("Age");
|
||||
Assert.assertNull(ageHdr);
|
||||
}
|
||||
|
@ -140,19 +134,20 @@ public class TestCachedHttpResponseGenerator {
|
|||
@Test
|
||||
public void testAgeHeaderIsPopulatedWithMaxAgeIfCurrentAgeTooBig() {
|
||||
currentAge(CacheValidityPolicy.MAX_AGE + 1L);
|
||||
replayMocks();
|
||||
|
||||
final HttpResponse response = impl.generateResponse(request, entry);
|
||||
|
||||
verify(mockValidityPolicy).getCurrentAgeSecs(same(entry), isA(Date.class));
|
||||
|
||||
final Header ageHdr = response.getFirstHeader("Age");
|
||||
Assert.assertNotNull(ageHdr);
|
||||
Assert.assertEquals(CacheValidityPolicy.MAX_AGE, Long.parseLong(ageHdr.getValue()));
|
||||
}
|
||||
|
||||
private void currentAge(final long sec) {
|
||||
EasyMock.expect(
|
||||
mockValidityPolicy.getCurrentAgeSecs(EasyMock.same(entry),
|
||||
EasyMock.isA(Date.class))).andReturn(sec);
|
||||
when(
|
||||
mockValidityPolicy.getCurrentAgeSecs(same(entry),
|
||||
isA(Date.class))).thenReturn(sec);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -36,7 +36,6 @@ import org.apache.http.client.cache.HttpCacheEntry;
|
|||
import org.apache.http.client.utils.DateUtils;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.message.BasicHttpRequest;
|
||||
import org.easymock.classextension.EasyMock;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -51,7 +50,6 @@ public class TestCachedResponseSuitabilityChecker {
|
|||
private HttpHost host;
|
||||
private HttpRequest request;
|
||||
private HttpCacheEntry entry;
|
||||
private CacheValidityPolicy mockValidityPolicy;
|
||||
private CachedResponseSuitabilityChecker impl;
|
||||
|
||||
@Before
|
||||
|
@ -63,7 +61,6 @@ public class TestCachedResponseSuitabilityChecker {
|
|||
|
||||
host = new HttpHost("foo.example.com");
|
||||
request = new BasicHttpRequest("GET", "/foo", HttpVersion.HTTP_1_1);
|
||||
mockValidityPolicy = EasyMock.createNiceMock(CacheValidityPolicy.class);
|
||||
entry = HttpTestUtils.makeCacheEntry();
|
||||
|
||||
impl = new CachedResponseSuitabilityChecker(CacheConfig.DEFAULT);
|
||||
|
@ -73,14 +70,6 @@ public class TestCachedResponseSuitabilityChecker {
|
|||
return HttpTestUtils.makeCacheEntry(elevenSecondsAgo, nineSecondsAgo, headers);
|
||||
}
|
||||
|
||||
public void replayMocks() {
|
||||
EasyMock.replay(mockValidityPolicy);
|
||||
}
|
||||
|
||||
public void verifyMocks() {
|
||||
EasyMock.verify(mockValidityPolicy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotSuitableIfContentLengthHeaderIsWrong() {
|
||||
final Header[] headers = {
|
||||
|
|
|
@ -26,11 +26,14 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import org.apache.http.client.cache.Resource;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -38,11 +41,9 @@ public class TestCombinedEntity {
|
|||
|
||||
@Test
|
||||
public void testCombinedEntityBasics() throws Exception {
|
||||
final Resource resource = EasyMock.createNiceMock(Resource.class);
|
||||
EasyMock.expect(resource.getInputStream()).andReturn(
|
||||
final Resource resource = mock(Resource.class);
|
||||
when(resource.getInputStream()).thenReturn(
|
||||
new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, 5 }));
|
||||
resource.dispose();
|
||||
EasyMock.replay(resource);
|
||||
|
||||
final ByteArrayInputStream instream = new ByteArrayInputStream(new byte[] { 6, 7, 8, 9, 10 });
|
||||
final CombinedEntity entity = new CombinedEntity(resource, instream);
|
||||
|
@ -53,7 +54,8 @@ public class TestCombinedEntity {
|
|||
final byte[] result = EntityUtils.toByteArray(entity);
|
||||
Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, result);
|
||||
|
||||
EasyMock.verify(resource);
|
||||
verify(resource).getInputStream();
|
||||
verify(resource).dispose();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,13 +26,16 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.methods.HttpRequestWrapper;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.impl.execchain.ClientExecChain;
|
||||
import org.apache.http.message.BasicHttpRequest;
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -46,7 +49,7 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mockExecutor = EasyMock.createMock(ScheduledExecutorService.class);
|
||||
mockExecutor = mock(ScheduledExecutorService.class);
|
||||
|
||||
impl = new ExponentialBackOffSchedulingStrategy(
|
||||
mockExecutor,
|
||||
|
@ -62,9 +65,9 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
|
||||
expectRequestScheduledWithoutDelay(request);
|
||||
|
||||
replayMocks();
|
||||
impl.schedule(request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockExecutor).schedule(request, 0, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -73,9 +76,9 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
|
||||
expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(6));
|
||||
|
||||
replayMocks();
|
||||
impl.schedule(request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockExecutor).schedule(request, 6000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -84,9 +87,9 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
|
||||
expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(60));
|
||||
|
||||
replayMocks();
|
||||
impl.schedule(request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockExecutor).schedule(request, 60000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -95,9 +98,9 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
|
||||
expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(600));
|
||||
|
||||
replayMocks();
|
||||
impl.schedule(request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockExecutor).schedule(request, 600000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -106,9 +109,9 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
|
||||
expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(6000));
|
||||
|
||||
replayMocks();
|
||||
impl.schedule(request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockExecutor).schedule(request, 6000000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -117,9 +120,9 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
|
||||
expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(60000));
|
||||
|
||||
replayMocks();
|
||||
impl.schedule(request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockExecutor).schedule(request, 60000000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -128,9 +131,9 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
|
||||
expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(86400));
|
||||
|
||||
replayMocks();
|
||||
impl.schedule(request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockExecutor).schedule(request, 86400000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -139,9 +142,9 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
|
||||
expectRequestScheduledWithDelay(request, TimeUnit.SECONDS.toMillis(86400));
|
||||
|
||||
replayMocks();
|
||||
impl.schedule(request);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockExecutor).schedule(request, 86400000, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
private void expectRequestScheduledWithoutDelay(final AsynchronousValidationRequest request) {
|
||||
|
@ -149,19 +152,11 @@ public class TestExponentialBackingOffSchedulingStrategy {
|
|||
}
|
||||
|
||||
private void expectRequestScheduledWithDelay(final AsynchronousValidationRequest request, final long delayInMillis) {
|
||||
EasyMock.expect(mockExecutor.schedule(request, delayInMillis, TimeUnit.MILLISECONDS)).andReturn(null);
|
||||
}
|
||||
|
||||
private void replayMocks() {
|
||||
EasyMock.replay(mockExecutor);
|
||||
}
|
||||
|
||||
private void verifyMocks() {
|
||||
EasyMock.verify(mockExecutor);
|
||||
when(mockExecutor.schedule(request, delayInMillis, TimeUnit.MILLISECONDS)).thenReturn(null);
|
||||
}
|
||||
|
||||
private AsynchronousValidationRequest createAsynchronousValidationRequest(final int errorCount) {
|
||||
final ClientExecChain clientExecChain = EasyMock.createNiceMock(ClientExecChain.class);
|
||||
final ClientExecChain clientExecChain = mock(ClientExecChain.class);
|
||||
final CachingExec cachingHttpClient = new CachingExec(clientExecChain);
|
||||
final AsynchronousValidator mockValidator = new AsynchronousValidator(impl);
|
||||
final HttpRoute httpRoute = new HttpRoute(new HttpHost("foo.example.com", 80));
|
||||
|
|
|
@ -26,6 +26,13 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
|
||||
|
@ -42,11 +49,11 @@ import org.apache.http.client.utils.DateUtils;
|
|||
import org.apache.http.conn.routing.HttpRoute;
|
||||
import org.apache.http.impl.execchain.ClientExecChain;
|
||||
import org.apache.http.message.BasicHttpResponse;
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Matchers;
|
||||
|
||||
public class TestHttpCacheJiraNumber1147 {
|
||||
|
||||
|
@ -87,7 +94,7 @@ public class TestHttpCacheJiraNumber1147 {
|
|||
final ResourceFactory resourceFactory = new FileResourceFactory(cacheDir);
|
||||
final HttpCacheStorage httpCacheStorage = new ManagedHttpCacheStorage(cacheConfig);
|
||||
|
||||
final ClientExecChain backend = EasyMock.createNiceMock(ClientExecChain.class);
|
||||
final ClientExecChain backend = mock(ClientExecChain.class);
|
||||
final HttpRequestWrapper get = HttpRequestWrapper.wrap(new HttpGet("http://somehost/"));
|
||||
final HttpClientContext context = HttpClientContext.create();
|
||||
final HttpHost target = new HttpHost("somehost", 80);
|
||||
|
@ -105,12 +112,11 @@ public class TestHttpCacheJiraNumber1147 {
|
|||
response.setHeader("Cache-Control", "public, max-age=3600");
|
||||
response.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo));
|
||||
|
||||
EasyMock.expect(backend.execute(
|
||||
EasyMock.eq(route),
|
||||
EasyMock.isA(HttpRequestWrapper.class),
|
||||
EasyMock.isA(HttpClientContext.class),
|
||||
EasyMock.<HttpExecutionAware>isNull())).andReturn(Proxies.enhanceResponse(response));
|
||||
EasyMock.replay(backend);
|
||||
when(backend.execute(
|
||||
eq(route),
|
||||
isA(HttpRequestWrapper.class),
|
||||
isA(HttpClientContext.class),
|
||||
(HttpExecutionAware) Matchers.isNull())).thenReturn(Proxies.enhanceResponse(response));
|
||||
|
||||
final BasicHttpCache cache = new BasicHttpCache(resourceFactory, httpCacheStorage, cacheConfig);
|
||||
final ClientExecChain t = createCachingExecChain(backend, cache, cacheConfig);
|
||||
|
@ -119,23 +125,30 @@ public class TestHttpCacheJiraNumber1147 {
|
|||
Assert.assertEquals(200, response1.getStatusLine().getStatusCode());
|
||||
IOUtils.consume(response1.getEntity());
|
||||
|
||||
EasyMock.verify(backend);
|
||||
verify(backend).execute(
|
||||
eq(route),
|
||||
isA(HttpRequestWrapper.class),
|
||||
isA(HttpClientContext.class),
|
||||
(HttpExecutionAware) Matchers.isNull());
|
||||
|
||||
removeCache();
|
||||
|
||||
EasyMock.reset(backend);
|
||||
EasyMock.expect(backend.execute(
|
||||
EasyMock.eq(route),
|
||||
EasyMock.isA(HttpRequestWrapper.class),
|
||||
EasyMock.isA(HttpClientContext.class),
|
||||
EasyMock.<HttpExecutionAware>isNull())).andReturn(Proxies.enhanceResponse(response));
|
||||
EasyMock.replay(backend);
|
||||
reset(backend);
|
||||
when(backend.execute(
|
||||
eq(route),
|
||||
isA(HttpRequestWrapper.class),
|
||||
isA(HttpClientContext.class),
|
||||
(HttpExecutionAware) Matchers.isNull())).thenReturn(Proxies.enhanceResponse(response));
|
||||
|
||||
final HttpResponse response2 = t.execute(route, get, context, null);
|
||||
Assert.assertEquals(200, response2.getStatusLine().getStatusCode());
|
||||
IOUtils.consume(response2.getEntity());
|
||||
|
||||
EasyMock.verify(backend);
|
||||
verify(backend).execute(
|
||||
eq(route),
|
||||
isA(HttpRequestWrapper.class),
|
||||
isA(HttpClientContext.class),
|
||||
(HttpExecutionAware) Matchers.isNull());
|
||||
}
|
||||
|
||||
protected ClientExecChain createCachingExecChain(final ClientExecChain backend,
|
||||
|
|
|
@ -26,7 +26,8 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import org.easymock.classextension.EasyMock;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -40,8 +41,8 @@ public class TestImmediateSchedulingStrategy {
|
|||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mockExecutor = EasyMock.createMock(ExecutorService.class);
|
||||
mockRevalidationRequest = EasyMock.createNiceMock(AsynchronousValidationRequest.class);
|
||||
mockExecutor = mock(ExecutorService.class);
|
||||
mockRevalidationRequest = mock(AsynchronousValidationRequest.class);
|
||||
schedulingStrategy = new ImmediateSchedulingStrategy(mockExecutor);
|
||||
}
|
||||
|
||||
|
@ -49,10 +50,6 @@ public class TestImmediateSchedulingStrategy {
|
|||
public void testRequestScheduledImmediately() {
|
||||
mockExecutor.execute(mockRevalidationRequest);
|
||||
|
||||
EasyMock.replay(mockExecutor);
|
||||
EasyMock.replay(mockRevalidationRequest);
|
||||
schedulingStrategy.schedule(mockRevalidationRequest);
|
||||
EasyMock.verify(mockExecutor);
|
||||
EasyMock.verify(mockRevalidationRequest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,13 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache.ehcache;
|
||||
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Matchers.same;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
@ -40,7 +47,6 @@ import org.apache.http.client.cache.HttpCacheUpdateCallback;
|
|||
import org.apache.http.client.cache.HttpCacheUpdateException;
|
||||
import org.apache.http.impl.client.cache.CacheConfig;
|
||||
import org.apache.http.impl.client.cache.HttpTestUtils;
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.Test;
|
||||
|
||||
@SuppressWarnings("boxing") // test code
|
||||
|
@ -52,22 +58,12 @@ public class TestEhcacheHttpCacheStorage extends TestCase {
|
|||
|
||||
@Override
|
||||
public void setUp() {
|
||||
mockCache = EasyMock.createNiceMock(Ehcache.class);
|
||||
mockCache = mock(Ehcache.class);
|
||||
final CacheConfig config = CacheConfig.custom().setMaxUpdateRetries(1).build();
|
||||
mockSerializer = EasyMock.createNiceMock(HttpCacheEntrySerializer.class);
|
||||
mockSerializer = mock(HttpCacheEntrySerializer.class);
|
||||
impl = new EhcacheHttpCacheStorage(mockCache, config, mockSerializer);
|
||||
}
|
||||
|
||||
private void replayMocks(){
|
||||
EasyMock.replay(mockCache);
|
||||
EasyMock.replay(mockSerializer);
|
||||
}
|
||||
|
||||
private void verifyMocks(){
|
||||
EasyMock.verify(mockCache);
|
||||
EasyMock.verify(mockSerializer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCachePut() throws IOException {
|
||||
final String key = "foo";
|
||||
|
@ -75,23 +71,21 @@ public class TestEhcacheHttpCacheStorage extends TestCase {
|
|||
|
||||
final Element e = new Element(key, new byte[]{});
|
||||
|
||||
mockSerializer.writeTo(EasyMock.same(value), EasyMock.isA(OutputStream.class));
|
||||
mockCache.put(e);
|
||||
|
||||
replayMocks();
|
||||
impl.putEntry(key, value);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockSerializer).writeTo(same(value), isA(OutputStream.class));
|
||||
verify(mockCache).put(e);;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheGetNullEntry() throws IOException {
|
||||
final String key = "foo";
|
||||
|
||||
EasyMock.expect(mockCache.get(key)).andReturn(null);
|
||||
when(mockCache.get(key)).thenReturn(null);
|
||||
|
||||
replayMocks();
|
||||
final HttpCacheEntry resultingEntry = impl.getEntry(key);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCache).get(key);
|
||||
|
||||
assertNull(resultingEntry);
|
||||
}
|
||||
|
@ -103,14 +97,15 @@ public class TestEhcacheHttpCacheStorage extends TestCase {
|
|||
|
||||
final Element element = new Element(key, new byte[]{});
|
||||
|
||||
EasyMock.expect(mockCache.get(key))
|
||||
.andReturn(element);
|
||||
EasyMock.expect(mockSerializer.readFrom(EasyMock.isA(InputStream.class)))
|
||||
.andReturn(cachedValue);
|
||||
when(mockCache.get(key))
|
||||
.thenReturn(element);
|
||||
when(mockSerializer.readFrom(isA(InputStream.class)))
|
||||
.thenReturn(cachedValue);
|
||||
|
||||
replayMocks();
|
||||
final HttpCacheEntry resultingEntry = impl.getEntry(key);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCache).get(key);
|
||||
verify(mockSerializer).readFrom(isA(InputStream.class));
|
||||
|
||||
assertSame(cachedValue, resultingEntry);
|
||||
}
|
||||
|
@ -119,11 +114,10 @@ public class TestEhcacheHttpCacheStorage extends TestCase {
|
|||
public void testCacheRemove() {
|
||||
final String key = "foo";
|
||||
|
||||
EasyMock.expect(mockCache.remove(key)).andReturn(true);
|
||||
when(mockCache.remove(key)).thenReturn(true);
|
||||
|
||||
replayMocks();
|
||||
impl.removeEntry(key);
|
||||
verifyMocks();
|
||||
verify(mockCache).remove(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -142,15 +136,16 @@ public class TestEhcacheHttpCacheStorage extends TestCase {
|
|||
};
|
||||
|
||||
// get empty old entry
|
||||
EasyMock.expect(mockCache.get(key)).andReturn(null);
|
||||
when(mockCache.get(key)).thenReturn(null);
|
||||
|
||||
// put new entry
|
||||
mockSerializer.writeTo(EasyMock.same(updatedValue), EasyMock.isA(OutputStream.class));
|
||||
mockCache.put(element);
|
||||
mockSerializer.writeTo(same(updatedValue), isA(OutputStream.class));
|
||||
|
||||
replayMocks();
|
||||
impl.updateEntry(key, callback);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCache).get(key);
|
||||
verify(mockSerializer).writeTo(same(updatedValue), isA(OutputStream.class));
|
||||
verify(mockCache).put(element);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -170,16 +165,19 @@ public class TestEhcacheHttpCacheStorage extends TestCase {
|
|||
};
|
||||
|
||||
// get existing old entry
|
||||
EasyMock.expect(mockCache.get(key)).andReturn(existingElement);
|
||||
EasyMock.expect(mockSerializer.readFrom(EasyMock.isA(InputStream.class))).andReturn(existingValue);
|
||||
when(mockCache.get(key)).thenReturn(existingElement);
|
||||
when(mockSerializer.readFrom(isA(InputStream.class))).thenReturn(existingValue);
|
||||
|
||||
// update
|
||||
mockSerializer.writeTo(EasyMock.same(updatedValue), EasyMock.isA(OutputStream.class));
|
||||
EasyMock.expect(mockCache.replace(EasyMock.same(existingElement), EasyMock.isA(Element.class))).andReturn(true);
|
||||
mockSerializer.writeTo(same(updatedValue), isA(OutputStream.class));
|
||||
when(mockCache.replace(same(existingElement), isA(Element.class))).thenReturn(true);
|
||||
|
||||
replayMocks();
|
||||
impl.updateEntry(key, callback);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCache).get(key);
|
||||
verify(mockSerializer).readFrom(isA(InputStream.class));
|
||||
verify(mockSerializer).writeTo(same(updatedValue), isA(OutputStream.class));
|
||||
verify(mockCache).replace(same(existingElement), isA(Element.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -199,20 +197,18 @@ public class TestEhcacheHttpCacheStorage extends TestCase {
|
|||
};
|
||||
|
||||
// get existing old entry, will happen twice
|
||||
EasyMock.expect(mockCache.get(key)).andReturn(existingElement).times(2);
|
||||
EasyMock.expect(mockSerializer.readFrom(EasyMock.isA(InputStream.class))).andReturn(existingValue).times(2);
|
||||
when(mockCache.get(key)).thenReturn(existingElement);
|
||||
when(mockSerializer.readFrom(isA(InputStream.class))).thenReturn(existingValue);
|
||||
|
||||
// update but fail
|
||||
mockSerializer.writeTo(EasyMock.same(updatedValue), EasyMock.isA(OutputStream.class));
|
||||
EasyMock.expectLastCall().times(2);
|
||||
EasyMock.expect(mockCache.replace(EasyMock.same(existingElement), EasyMock.isA(Element.class))).andReturn(false);
|
||||
// Fail first and then succeed
|
||||
when(mockCache.replace(same(existingElement), isA(Element.class))).thenReturn(false).thenReturn(true);
|
||||
|
||||
// update again and succeed
|
||||
EasyMock.expect(mockCache.replace(EasyMock.same(existingElement), EasyMock.isA(Element.class))).andReturn(true);
|
||||
|
||||
replayMocks();
|
||||
impl.updateEntry(key, callback);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCache, times(2)).get(key);
|
||||
verify(mockSerializer, times(2)).readFrom(isA(InputStream.class));
|
||||
verify(mockSerializer, times(2)).writeTo(same(updatedValue), isA(OutputStream.class));
|
||||
verify(mockCache, times(2)).replace(same(existingElement), isA(Element.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -232,19 +228,20 @@ public class TestEhcacheHttpCacheStorage extends TestCase {
|
|||
};
|
||||
|
||||
// get existing old entry
|
||||
EasyMock.expect(mockCache.get(key)).andReturn(existingElement).times(2);
|
||||
EasyMock.expect(mockSerializer.readFrom(EasyMock.isA(InputStream.class))).andReturn(existingValue).times(2);
|
||||
when(mockCache.get(key)).thenReturn(existingElement);
|
||||
when(mockSerializer.readFrom(isA(InputStream.class))).thenReturn(existingValue);
|
||||
|
||||
// update but fail
|
||||
mockSerializer.writeTo(EasyMock.same(updatedValue), EasyMock.isA(OutputStream.class));
|
||||
EasyMock.expectLastCall().times(2);
|
||||
EasyMock.expect(mockCache.replace(EasyMock.same(existingElement), EasyMock.isA(Element.class))).andReturn(false).times(2);
|
||||
when(mockCache.replace(same(existingElement), isA(Element.class))).thenReturn(false);
|
||||
|
||||
replayMocks();
|
||||
try{
|
||||
impl.updateEntry(key, callback);
|
||||
fail("Expected HttpCacheUpdateException");
|
||||
} catch (final HttpCacheUpdateException e) { }
|
||||
verifyMocks();
|
||||
|
||||
verify(mockCache, times(2)).get(key);
|
||||
verify(mockSerializer, times(2)).readFrom(isA(InputStream.class));
|
||||
verify(mockSerializer, times(2)).writeTo(same(updatedValue), isA(OutputStream.class));
|
||||
verify(mockCache, times(2)).replace(same(existingElement), isA(Element.class));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,13 @@
|
|||
*/
|
||||
package org.apache.http.impl.client.cache.memcached;
|
||||
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
|
@ -40,7 +47,6 @@ import org.apache.http.client.cache.HttpCacheUpdateCallback;
|
|||
import org.apache.http.client.cache.HttpCacheUpdateException;
|
||||
import org.apache.http.impl.client.cache.CacheConfig;
|
||||
import org.apache.http.impl.client.cache.HttpTestUtils;
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -57,38 +63,18 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
@Override
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mockMemcachedClient = EasyMock.createNiceMock(MemcachedClientIF.class);
|
||||
mockKeyHashingScheme = EasyMock.createNiceMock(KeyHashingScheme.class);
|
||||
mockMemcachedCacheEntryFactory = EasyMock.createNiceMock(MemcachedCacheEntryFactory.class);
|
||||
mockMemcachedCacheEntry = EasyMock.createNiceMock(MemcachedCacheEntry.class);
|
||||
mockMemcachedCacheEntry2 = EasyMock.createNiceMock(MemcachedCacheEntry.class);
|
||||
mockMemcachedCacheEntry3 = EasyMock.createNiceMock(MemcachedCacheEntry.class);
|
||||
mockMemcachedCacheEntry4 = EasyMock.createNiceMock(MemcachedCacheEntry.class);
|
||||
mockMemcachedClient = mock(MemcachedClientIF.class);
|
||||
mockKeyHashingScheme = mock(KeyHashingScheme.class);
|
||||
mockMemcachedCacheEntryFactory = mock(MemcachedCacheEntryFactory.class);
|
||||
mockMemcachedCacheEntry = mock(MemcachedCacheEntry.class);
|
||||
mockMemcachedCacheEntry2 = mock(MemcachedCacheEntry.class);
|
||||
mockMemcachedCacheEntry3 = mock(MemcachedCacheEntry.class);
|
||||
mockMemcachedCacheEntry4 = mock(MemcachedCacheEntry.class);
|
||||
final CacheConfig config = CacheConfig.custom().setMaxUpdateRetries(1).build();
|
||||
impl = new MemcachedHttpCacheStorage(mockMemcachedClient, config,
|
||||
mockMemcachedCacheEntryFactory, mockKeyHashingScheme);
|
||||
}
|
||||
|
||||
private void replayMocks() {
|
||||
EasyMock.replay(mockMemcachedClient);
|
||||
EasyMock.replay(mockKeyHashingScheme);
|
||||
EasyMock.replay(mockMemcachedCacheEntry);
|
||||
EasyMock.replay(mockMemcachedCacheEntry2);
|
||||
EasyMock.replay(mockMemcachedCacheEntry3);
|
||||
EasyMock.replay(mockMemcachedCacheEntry4);
|
||||
EasyMock.replay(mockMemcachedCacheEntryFactory);
|
||||
}
|
||||
|
||||
private void verifyMocks() {
|
||||
EasyMock.verify(mockMemcachedClient);
|
||||
EasyMock.verify(mockKeyHashingScheme);
|
||||
EasyMock.verify(mockMemcachedCacheEntry);
|
||||
EasyMock.verify(mockMemcachedCacheEntry2);
|
||||
EasyMock.verify(mockMemcachedCacheEntry3);
|
||||
EasyMock.verify(mockMemcachedCacheEntry4);
|
||||
EasyMock.verify(mockMemcachedCacheEntryFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccessfulCachePut() throws IOException {
|
||||
final String url = "foo";
|
||||
|
@ -96,18 +82,20 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
final HttpCacheEntry value = HttpTestUtils.makeCacheEntry();
|
||||
final byte[] serialized = HttpTestUtils.getRandomBytes(128);
|
||||
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, value))
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.toByteArray())
|
||||
.andReturn(serialized);
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url))
|
||||
.andReturn(key);
|
||||
EasyMock.expect(mockMemcachedClient.set(key, 0, serialized))
|
||||
.andReturn(null);
|
||||
when(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, value))
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
when(mockMemcachedCacheEntry.toByteArray())
|
||||
.thenReturn(serialized);
|
||||
when(mockKeyHashingScheme.hash(url))
|
||||
.thenReturn(key);
|
||||
when(mockMemcachedClient.set(key, 0, serialized))
|
||||
.thenReturn(null);
|
||||
|
||||
replayMocks();
|
||||
impl.putEntry(url, value);
|
||||
verifyMocks();
|
||||
verify(mockMemcachedCacheEntryFactory).getMemcachedCacheEntry(url, value);
|
||||
verify(mockMemcachedCacheEntry).toByteArray();
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).set(key, 0, serialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -116,16 +104,18 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
final HttpCacheEntry value = HttpTestUtils.makeCacheEntry();
|
||||
final byte[] serialized = HttpTestUtils.getRandomBytes(128);
|
||||
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, value))
|
||||
.andReturn(mockMemcachedCacheEntry).times(0,1);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.toByteArray())
|
||||
.andReturn(serialized).times(0,1);
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url))
|
||||
.andThrow(new MemcachedKeyHashingException(new Exception()));
|
||||
when(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, value))
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
when(mockMemcachedCacheEntry.toByteArray())
|
||||
.thenReturn(serialized);
|
||||
when(mockKeyHashingScheme.hash(url))
|
||||
.thenThrow(new MemcachedKeyHashingException(new Exception()));
|
||||
|
||||
replayMocks();
|
||||
impl.putEntry(url, value);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockMemcachedCacheEntryFactory).getMemcachedCacheEntry(url, value);
|
||||
verify(mockMemcachedCacheEntry).toByteArray();
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
}
|
||||
|
||||
public void testThrowsIOExceptionWhenMemcachedPutTimesOut() {
|
||||
|
@ -134,22 +124,25 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
final HttpCacheEntry value = HttpTestUtils.makeCacheEntry();
|
||||
final byte[] serialized = HttpTestUtils.getRandomBytes(128);
|
||||
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, value))
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.toByteArray())
|
||||
.andReturn(serialized);
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url))
|
||||
.andReturn(key);
|
||||
EasyMock.expect(mockMemcachedClient.set(key, 0, serialized))
|
||||
.andThrow(new OperationTimeoutException("timed out"));
|
||||
when(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, value))
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
when(mockMemcachedCacheEntry.toByteArray())
|
||||
.thenReturn(serialized);
|
||||
when(mockKeyHashingScheme.hash(url))
|
||||
.thenReturn(key);
|
||||
when(mockMemcachedClient.set(key, 0, serialized))
|
||||
.thenThrow(new OperationTimeoutException("timed out"));
|
||||
|
||||
replayMocks();
|
||||
try {
|
||||
impl.putEntry(url, value);
|
||||
fail("should have thrown exception");
|
||||
} catch (final IOException expected) {
|
||||
}
|
||||
verifyMocks();
|
||||
|
||||
verify(mockMemcachedCacheEntryFactory).getMemcachedCacheEntry(url, value);
|
||||
verify(mockMemcachedCacheEntry).toByteArray();
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).set(key, 0, serialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -158,21 +151,20 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
final String key = "key";
|
||||
final HttpCacheEntry value = HttpTestUtils.makeCacheEntry();
|
||||
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, value))
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.toByteArray())
|
||||
.andThrow(new MemcachedSerializationException(new Exception()));
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url))
|
||||
.andReturn(key).times(0,1);
|
||||
when(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, value))
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
when(mockMemcachedCacheEntry.toByteArray())
|
||||
.thenThrow(new MemcachedSerializationException(new Exception()));
|
||||
|
||||
replayMocks();
|
||||
try {
|
||||
impl.putEntry(url, value);
|
||||
fail("should have thrown exception");
|
||||
} catch (final IOException expected) {
|
||||
|
||||
}
|
||||
verifyMocks();
|
||||
|
||||
verify(mockMemcachedCacheEntryFactory).getMemcachedCacheEntry(url, value);
|
||||
verify(mockMemcachedCacheEntry).toByteArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -183,17 +175,22 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
final byte[] serialized = HttpTestUtils.getRandomBytes(128);
|
||||
final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry();
|
||||
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key);
|
||||
EasyMock.expect(mockMemcachedClient.get(key)).andReturn(serialized);
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
mockMemcachedCacheEntry.set(serialized);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.getStorageKey()).andReturn(url);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.getHttpCacheEntry()).andReturn(cacheEntry);
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.get(key)).thenReturn(serialized);
|
||||
when(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
when(mockMemcachedCacheEntry.getStorageKey()).thenReturn(url);
|
||||
when(mockMemcachedCacheEntry.getHttpCacheEntry()).thenReturn(cacheEntry);
|
||||
|
||||
replayMocks();
|
||||
final HttpCacheEntry resultingEntry = impl.getEntry(url);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).get(key);
|
||||
verify(mockMemcachedCacheEntryFactory).getUnsetCacheEntry();
|
||||
verify(mockMemcachedCacheEntry).set(serialized);
|
||||
verify(mockMemcachedCacheEntry).getStorageKey();
|
||||
verify(mockMemcachedCacheEntry).getHttpCacheEntry();
|
||||
|
||||
assertSame(cacheEntry, resultingEntry);
|
||||
}
|
||||
|
||||
|
@ -203,12 +200,14 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
final String url = "foo";
|
||||
final String key = "key";
|
||||
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key);
|
||||
EasyMock.expect(mockMemcachedClient.get(key)).andReturn(new Object());
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.get(key)).thenReturn(new Object());
|
||||
|
||||
replayMocks();
|
||||
final HttpCacheEntry resultingEntry = impl.getEntry(url);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).get(key);
|
||||
|
||||
assertNull(resultingEntry);
|
||||
}
|
||||
|
||||
|
@ -218,12 +217,14 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
final String url = "foo";
|
||||
final String key = "key";
|
||||
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key);
|
||||
EasyMock.expect(mockMemcachedClient.get(key)).andReturn(null);
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.get(key)).thenReturn(null);
|
||||
|
||||
replayMocks();
|
||||
final HttpCacheEntry resultingEntry = impl.getEntry(url);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).get(key);
|
||||
|
||||
assertNull(resultingEntry);
|
||||
}
|
||||
|
||||
|
@ -234,16 +235,18 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
final String key = "key";
|
||||
final byte[] serialized = HttpTestUtils.getRandomBytes(128);
|
||||
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key);
|
||||
EasyMock.expect(mockMemcachedClient.get(key)).andReturn(serialized);
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
mockMemcachedCacheEntry.set(serialized);
|
||||
EasyMock.expectLastCall().andThrow(new MemcachedSerializationException(new Exception()));
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.get(key)).thenReturn(serialized);
|
||||
when(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
doThrow(new MemcachedSerializationException(new Exception())).when(mockMemcachedCacheEntry).set(serialized);
|
||||
|
||||
replayMocks();
|
||||
assertNull(impl.getEntry(url));
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).get(key);
|
||||
verify(mockMemcachedCacheEntryFactory).getUnsetCacheEntry();
|
||||
verify(mockMemcachedCacheEntry).set(serialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -251,65 +254,66 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
IOException {
|
||||
final String url = "foo";
|
||||
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andThrow(new MemcachedKeyHashingException(new Exception()));
|
||||
when(mockKeyHashingScheme.hash(url)).thenThrow(new MemcachedKeyHashingException(new Exception()));
|
||||
|
||||
replayMocks();
|
||||
assertNull(impl.getEntry(url));
|
||||
verifyMocks();
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThrowsIOExceptionIfMemcachedTimesOutOnGet() {
|
||||
final String url = "foo";
|
||||
final String key = "key";
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key);
|
||||
EasyMock.expect(mockMemcachedClient.get(key))
|
||||
.andThrow(new OperationTimeoutException(""));
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.get(key))
|
||||
.thenThrow(new OperationTimeoutException(""));
|
||||
|
||||
replayMocks();
|
||||
try {
|
||||
impl.getEntry(url);
|
||||
fail("should have thrown exception");
|
||||
} catch (final IOException expected) {
|
||||
}
|
||||
verifyMocks();
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).get(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheRemove() throws IOException {
|
||||
final String url = "foo";
|
||||
final String key = "key";
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key);
|
||||
EasyMock.expect(mockMemcachedClient.delete(key)).andReturn(null);
|
||||
replayMocks();
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.delete(key)).thenReturn(null);
|
||||
|
||||
impl.removeEntry(url);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).delete(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheRemoveHandlesKeyHashingFailure() throws IOException {
|
||||
final String url = "foo";
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(null);
|
||||
replayMocks();
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(null);
|
||||
impl.removeEntry(url);
|
||||
verifyMocks();
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheRemoveThrowsIOExceptionOnMemcachedTimeout() {
|
||||
final String url = "foo";
|
||||
final String key = "key";
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key);
|
||||
EasyMock.expect(mockMemcachedClient.delete(key))
|
||||
.andThrow(new OperationTimeoutException(""));
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.delete(key))
|
||||
.thenThrow(new OperationTimeoutException(""));
|
||||
|
||||
replayMocks();
|
||||
try {
|
||||
impl.removeEntry(url);
|
||||
fail("should have thrown exception");
|
||||
} catch (final IOException expected) {
|
||||
}
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).delete(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -329,18 +333,22 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
};
|
||||
|
||||
// get empty old entry
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key).anyTimes();
|
||||
EasyMock.expect(mockMemcachedClient.gets(key)).andReturn(null);
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue))
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.toByteArray()).andReturn(serialized);
|
||||
EasyMock.expect(
|
||||
mockMemcachedClient.set(EasyMock.eq(key), EasyMock.eq(0),
|
||||
EasyMock.aryEq(serialized))).andReturn(null);
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.gets(key)).thenReturn(null);
|
||||
when(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue))
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
when(mockMemcachedCacheEntry.toByteArray()).thenReturn(serialized);
|
||||
when(
|
||||
mockMemcachedClient.set(key, 0,
|
||||
serialized)).thenReturn(null);
|
||||
|
||||
replayMocks();
|
||||
impl.updateEntry(url, callback);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme, times(2)).hash(url);
|
||||
verify(mockMemcachedClient).gets(key);
|
||||
verify(mockMemcachedCacheEntryFactory).getMemcachedCacheEntry(url, updatedValue);
|
||||
verify(mockMemcachedCacheEntry).toByteArray();
|
||||
verify(mockMemcachedClient).set(key, 0, serialized);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -362,23 +370,28 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
};
|
||||
|
||||
// get empty old entry
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key).anyTimes();
|
||||
EasyMock.expect(mockMemcachedClient.gets(key)).andReturn(casValue);
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
mockMemcachedCacheEntry.set(oldBytes);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.getStorageKey()).andReturn("not" + url).anyTimes();
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.gets(key)).thenReturn(casValue);
|
||||
when(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
when(mockMemcachedCacheEntry.getStorageKey()).thenReturn("not" + url);
|
||||
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue))
|
||||
.andReturn(mockMemcachedCacheEntry2);
|
||||
EasyMock.expect(mockMemcachedCacheEntry2.toByteArray()).andReturn(newBytes);
|
||||
EasyMock.expect(
|
||||
mockMemcachedClient.set(EasyMock.eq(key), EasyMock.eq(0),
|
||||
EasyMock.aryEq(newBytes))).andReturn(null);
|
||||
when(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue))
|
||||
.thenReturn(mockMemcachedCacheEntry2);
|
||||
when(mockMemcachedCacheEntry2.toByteArray()).thenReturn(newBytes);
|
||||
when(
|
||||
mockMemcachedClient.set(key, 0,
|
||||
newBytes)).thenReturn(null);
|
||||
|
||||
replayMocks();
|
||||
impl.updateEntry(url, callback);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme, times(2)).hash(url);
|
||||
verify(mockMemcachedClient).gets(key);
|
||||
verify(mockMemcachedCacheEntryFactory).getUnsetCacheEntry();
|
||||
verify(mockMemcachedCacheEntry).getStorageKey();
|
||||
verify(mockMemcachedCacheEntryFactory).getMemcachedCacheEntry(url, updatedValue);
|
||||
verify(mockMemcachedCacheEntry2).toByteArray();
|
||||
verify(mockMemcachedClient).set(key, 0, newBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -402,25 +415,31 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
};
|
||||
|
||||
// get empty old entry
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key).anyTimes();
|
||||
EasyMock.expect(mockMemcachedClient.gets(key)).andReturn(casValue);
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
mockMemcachedCacheEntry.set(oldBytes);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.getStorageKey()).andReturn(url);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.getHttpCacheEntry()).andReturn(existingValue);
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.gets(key)).thenReturn(casValue);
|
||||
when(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
when(mockMemcachedCacheEntry.getStorageKey()).thenReturn(url);
|
||||
when(mockMemcachedCacheEntry.getHttpCacheEntry()).thenReturn(existingValue);
|
||||
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue))
|
||||
.andReturn(mockMemcachedCacheEntry2);
|
||||
EasyMock.expect(mockMemcachedCacheEntry2.toByteArray()).andReturn(newBytes);
|
||||
when(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue))
|
||||
.thenReturn(mockMemcachedCacheEntry2);
|
||||
when(mockMemcachedCacheEntry2.toByteArray()).thenReturn(newBytes);
|
||||
|
||||
EasyMock.expect(
|
||||
mockMemcachedClient.cas(EasyMock.eq(key), EasyMock.eq(casValue.getCas()),
|
||||
EasyMock.aryEq(newBytes))).andReturn(CASResponse.OK);
|
||||
when(
|
||||
mockMemcachedClient.cas(key, casValue.getCas(),
|
||||
newBytes)).thenReturn(CASResponse.OK);
|
||||
|
||||
replayMocks();
|
||||
impl.updateEntry(url, callback);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).gets(key);
|
||||
verify(mockMemcachedCacheEntryFactory).getUnsetCacheEntry();
|
||||
verify(mockMemcachedCacheEntry).getStorageKey();
|
||||
verify(mockMemcachedCacheEntry).getHttpCacheEntry();
|
||||
verify(mockMemcachedCacheEntryFactory).getMemcachedCacheEntry(url, updatedValue);
|
||||
verify(mockMemcachedCacheEntry2).toByteArray();
|
||||
verify(mockMemcachedClient).cas(key, casValue.getCas(), newBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -446,29 +465,35 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
};
|
||||
|
||||
// get empty old entry
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key).anyTimes();
|
||||
EasyMock.expect(mockMemcachedClient.gets(key)).andReturn(casValue);
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
mockMemcachedCacheEntry.set(oldBytes);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.getStorageKey()).andReturn(url);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.getHttpCacheEntry()).andReturn(existingValue);
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.gets(key)).thenReturn(casValue);
|
||||
when(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.thenReturn(mockMemcachedCacheEntry);
|
||||
when(mockMemcachedCacheEntry.getStorageKey()).thenReturn(url);
|
||||
when(mockMemcachedCacheEntry.getHttpCacheEntry()).thenReturn(existingValue);
|
||||
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue))
|
||||
.andReturn(mockMemcachedCacheEntry2);
|
||||
EasyMock.expect(mockMemcachedCacheEntry2.toByteArray()).andReturn(newBytes);
|
||||
when(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue))
|
||||
.thenReturn(mockMemcachedCacheEntry2);
|
||||
when(mockMemcachedCacheEntry2.toByteArray()).thenReturn(newBytes);
|
||||
|
||||
EasyMock.expect(
|
||||
mockMemcachedClient.cas(EasyMock.eq(key), EasyMock.eq(casValue.getCas()),
|
||||
EasyMock.aryEq(newBytes))).andReturn(CASResponse.EXISTS);
|
||||
when(
|
||||
mockMemcachedClient.cas(key, casValue.getCas(),
|
||||
newBytes)).thenReturn(CASResponse.EXISTS);
|
||||
|
||||
replayMocks();
|
||||
try {
|
||||
impl.updateEntry(url, callback);
|
||||
fail("should have thrown exception");
|
||||
} catch (final HttpCacheUpdateException expected) {
|
||||
}
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).gets(key);
|
||||
verify(mockMemcachedCacheEntryFactory).getUnsetCacheEntry();
|
||||
verify(mockMemcachedCacheEntry).getStorageKey();
|
||||
verify(mockMemcachedCacheEntry).getHttpCacheEntry();
|
||||
verify(mockMemcachedCacheEntryFactory).getMemcachedCacheEntry(url, updatedValue);
|
||||
verify(mockMemcachedCacheEntry2).toByteArray();
|
||||
verify(mockMemcachedClient).cas(key, casValue.getCas(), newBytes);
|
||||
}
|
||||
|
||||
|
||||
|
@ -481,11 +506,8 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
final HttpCacheEntry existingValue2 = HttpTestUtils.makeCacheEntry();
|
||||
final HttpCacheEntry updatedValue = HttpTestUtils.makeCacheEntry();
|
||||
final HttpCacheEntry updatedValue2 = HttpTestUtils.makeCacheEntry();
|
||||
final byte[] oldBytes = HttpTestUtils.getRandomBytes(128);
|
||||
final byte[] oldBytes2 = HttpTestUtils.getRandomBytes(128);
|
||||
final CASValue<Object> casValue = new CASValue<Object>(1, oldBytes);
|
||||
final CASValue<Object> casValue2 = new CASValue<Object>(2, oldBytes2);
|
||||
final byte[] newBytes = HttpTestUtils.getRandomBytes(128);
|
||||
final byte[] newBytes2 = HttpTestUtils.getRandomBytes(128);
|
||||
|
||||
final HttpCacheUpdateCallback callback = new HttpCacheUpdateCallback() {
|
||||
|
@ -499,41 +521,43 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
}
|
||||
};
|
||||
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key).anyTimes();
|
||||
EasyMock.expect(mockMemcachedClient.gets(key)).andReturn(casValue);
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.andReturn(mockMemcachedCacheEntry);
|
||||
mockMemcachedCacheEntry.set(oldBytes);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.getStorageKey()).andReturn(url);
|
||||
EasyMock.expect(mockMemcachedCacheEntry.getHttpCacheEntry()).andReturn(existingValue);
|
||||
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue))
|
||||
.andReturn(mockMemcachedCacheEntry2);
|
||||
EasyMock.expect(mockMemcachedCacheEntry2.toByteArray()).andReturn(newBytes);
|
||||
|
||||
EasyMock.expect(
|
||||
mockMemcachedClient.cas(EasyMock.eq(key), EasyMock.eq(casValue.getCas()),
|
||||
EasyMock.aryEq(newBytes))).andReturn(CASResponse.EXISTS);
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
|
||||
// take two
|
||||
EasyMock.expect(mockMemcachedClient.gets(key)).andReturn(casValue2);
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.andReturn(mockMemcachedCacheEntry3);
|
||||
mockMemcachedCacheEntry3.set(oldBytes2);
|
||||
EasyMock.expect(mockMemcachedCacheEntry3.getStorageKey()).andReturn(url);
|
||||
EasyMock.expect(mockMemcachedCacheEntry3.getHttpCacheEntry()).andReturn(existingValue2);
|
||||
when(mockMemcachedClient.gets(key)).thenReturn(casValue2);
|
||||
when(mockMemcachedCacheEntryFactory.getUnsetCacheEntry())
|
||||
.thenReturn(mockMemcachedCacheEntry3);
|
||||
when(mockMemcachedCacheEntry3.getStorageKey()).thenReturn(url);
|
||||
when(mockMemcachedCacheEntry3.getHttpCacheEntry()).thenReturn(existingValue2);
|
||||
|
||||
EasyMock.expect(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue2))
|
||||
.andReturn(mockMemcachedCacheEntry4);
|
||||
EasyMock.expect(mockMemcachedCacheEntry4.toByteArray()).andReturn(newBytes2);
|
||||
when(mockMemcachedCacheEntryFactory.getMemcachedCacheEntry(url, updatedValue2))
|
||||
.thenReturn(mockMemcachedCacheEntry4);
|
||||
when(mockMemcachedCacheEntry4.toByteArray()).thenReturn(newBytes2);
|
||||
|
||||
EasyMock.expect(
|
||||
mockMemcachedClient.cas(EasyMock.eq(key), EasyMock.eq(casValue2.getCas()),
|
||||
EasyMock.aryEq(newBytes2))).andReturn(CASResponse.OK);
|
||||
when(
|
||||
mockMemcachedClient.cas(key, casValue2.getCas(),
|
||||
newBytes2)).thenReturn(CASResponse.OK);
|
||||
|
||||
replayMocks();
|
||||
impl.updateEntry(url, callback);
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).gets(key);
|
||||
verify(mockMemcachedCacheEntryFactory).getUnsetCacheEntry();
|
||||
|
||||
verify(mockMemcachedCacheEntry3).set(oldBytes2);
|
||||
verify(mockMemcachedCacheEntry3).getStorageKey();
|
||||
verify(mockMemcachedCacheEntry3).getHttpCacheEntry();
|
||||
verify(mockMemcachedCacheEntryFactory).getMemcachedCacheEntry(url, updatedValue2);
|
||||
verify(mockMemcachedCacheEntry4).toByteArray();
|
||||
verify(mockMemcachedClient).cas(key, casValue2.getCas(), newBytes2);
|
||||
|
||||
verifyNoMoreInteractions(mockMemcachedClient);
|
||||
verifyNoMoreInteractions(mockKeyHashingScheme);
|
||||
verifyNoMoreInteractions(mockMemcachedCacheEntry);
|
||||
verifyNoMoreInteractions(mockMemcachedCacheEntry2);
|
||||
verifyNoMoreInteractions(mockMemcachedCacheEntry3);
|
||||
verifyNoMoreInteractions(mockMemcachedCacheEntry4);
|
||||
verifyNoMoreInteractions(mockMemcachedCacheEntryFactory);
|
||||
}
|
||||
|
||||
|
||||
|
@ -552,17 +576,18 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
};
|
||||
|
||||
// get empty old entry
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url)).andReturn(key).anyTimes();
|
||||
EasyMock.expect(mockMemcachedClient.gets(key))
|
||||
.andThrow(new OperationTimeoutException(""));
|
||||
when(mockKeyHashingScheme.hash(url)).thenReturn(key);
|
||||
when(mockMemcachedClient.gets(key))
|
||||
.thenThrow(new OperationTimeoutException(""));
|
||||
|
||||
replayMocks();
|
||||
try {
|
||||
impl.updateEntry(url, callback);
|
||||
fail("should have thrown exception");
|
||||
} catch (final IOException expected) {
|
||||
}
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
verify(mockMemcachedClient).gets(key);
|
||||
}
|
||||
|
||||
|
||||
|
@ -570,15 +595,15 @@ public class TestMemcachedHttpCacheStorage extends TestCase {
|
|||
public void testThrowsExceptionOnUpdateIfCannotHashStorageKey() throws Exception {
|
||||
final String url = "foo";
|
||||
|
||||
EasyMock.expect(mockKeyHashingScheme.hash(url))
|
||||
.andThrow(new MemcachedKeyHashingException(new Exception()));
|
||||
when(mockKeyHashingScheme.hash(url))
|
||||
.thenThrow(new MemcachedKeyHashingException(new Exception()));
|
||||
|
||||
replayMocks();
|
||||
try {
|
||||
impl.updateEntry(url, null);
|
||||
fail("should have thrown exception");
|
||||
} catch (final HttpCacheUpdateException expected) {
|
||||
}
|
||||
verifyMocks();
|
||||
|
||||
verify(mockKeyHashingScheme).hash(url);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue