Rewrote test case for HTTPCLIENT-1147: removed dependency on an external resource; removed hard-coded cache directory location
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1222843 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a6db7c5185
commit
cb6254fea6
|
@ -1,87 +0,0 @@
|
|||
package org.apache.http.client.cache;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.HttpResponseException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.impl.client.cache.CacheConfig;
|
||||
import org.apache.http.impl.client.cache.CachingHttpClient;
|
||||
import org.apache.http.impl.client.cache.FileResourceFactory;
|
||||
import org.apache.http.impl.client.cache.ManagedHttpCacheStorage;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class TestHttpCacheJiraNumber1147 {
|
||||
final String cacheDir = "/tmp/cachedir";
|
||||
HttpClient cachingHttpClient;
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
|
||||
@Test
|
||||
public void testIssue1147() throws Exception {
|
||||
final CacheConfig cacheConfig = new CacheConfig();
|
||||
cacheConfig.setSharedCache(true);
|
||||
cacheConfig.setMaxObjectSize(262144); //256kb
|
||||
|
||||
new File(cacheDir).mkdir();
|
||||
|
||||
if(! new File(cacheDir, "httpclient-cache").exists()){
|
||||
if(!new File(cacheDir, "httpclient-cache").mkdir()){
|
||||
throw new RuntimeException("failed to create httpclient cache directory: " +
|
||||
new File(cacheDir, "httpclient-cache").getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
final ResourceFactory resourceFactory = new FileResourceFactory(new File(cacheDir, "httpclient-cache"));
|
||||
|
||||
final HttpCacheStorage httpCacheStorage = new ManagedHttpCacheStorage(cacheConfig);
|
||||
|
||||
cachingHttpClient = new CachingHttpClient(client, resourceFactory, httpCacheStorage, cacheConfig);
|
||||
|
||||
final HttpGet get = new HttpGet("http://www.apache.org/js/jquery.js");
|
||||
|
||||
System.out.println("Calling URL First time.");
|
||||
executeCall(get);
|
||||
|
||||
removeDirectory(cacheDir);
|
||||
|
||||
System.out.println("Calling URL Second time.");
|
||||
executeCall(get);
|
||||
}
|
||||
|
||||
private void removeDirectory(String cacheDir) {
|
||||
File theDirectory = new File(cacheDir, "httpclient-cache");
|
||||
File[] files = theDirectory.listFiles();
|
||||
|
||||
for (File cacheFile : files) {
|
||||
cacheFile.delete();
|
||||
}
|
||||
|
||||
theDirectory.delete();
|
||||
|
||||
new File(cacheDir).delete();
|
||||
}
|
||||
|
||||
private void executeCall(HttpGet get) throws Exception {
|
||||
final HttpResponse response = cachingHttpClient.execute(get);
|
||||
final StatusLine statusLine = response.getStatusLine();
|
||||
System.out.println("Status Code: " + statusLine.getStatusCode());
|
||||
|
||||
if (statusLine.getStatusCode() >= 300) {
|
||||
if(statusLine.getStatusCode() == 404)
|
||||
throw new NoResultException();
|
||||
|
||||
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
|
||||
}
|
||||
response.getEntity().getContent();
|
||||
}
|
||||
|
||||
private class NoResultException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1277878788978491946L;
|
||||
|
||||
}
|
||||
}
|
113
httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheJiraNumber1147.java
vendored
Normal file
113
httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestHttpCacheJiraNumber1147.java
vendored
Normal file
|
@ -0,0 +1,113 @@
|
|||
package org.apache.http.impl.client.cache;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpVersion;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.cache.HttpCacheStorage;
|
||||
import org.apache.http.client.cache.ResourceFactory;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.cache.CacheConfig;
|
||||
import org.apache.http.impl.client.cache.CachingHttpClient;
|
||||
import org.apache.http.impl.client.cache.FileResourceFactory;
|
||||
import org.apache.http.impl.client.cache.ManagedHttpCacheStorage;
|
||||
import org.apache.http.impl.cookie.DateUtils;
|
||||
import org.apache.http.message.BasicHttpResponse;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestHttpCacheJiraNumber1147 {
|
||||
|
||||
private File cacheDir;
|
||||
|
||||
private void removeCache() {
|
||||
if (this.cacheDir != null) {
|
||||
File[] files = this.cacheDir.listFiles();
|
||||
for (File cacheFile : files) {
|
||||
cacheFile.delete();
|
||||
}
|
||||
this.cacheDir.delete();
|
||||
this.cacheDir = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
cacheDir = File.createTempFile("cachedir", "");
|
||||
if (cacheDir.exists()) {
|
||||
cacheDir.delete();
|
||||
}
|
||||
cacheDir.mkdir();
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
removeCache();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIssue1147() throws Exception {
|
||||
CacheConfig cacheConfig = new CacheConfig();
|
||||
cacheConfig.setSharedCache(true);
|
||||
cacheConfig.setMaxObjectSize(262144); //256kb
|
||||
|
||||
ResourceFactory resourceFactory = new FileResourceFactory(cacheDir);
|
||||
HttpCacheStorage httpCacheStorage = new ManagedHttpCacheStorage(cacheConfig);
|
||||
|
||||
HttpClient client = EasyMock.createMock(HttpClient.class);
|
||||
HttpGet get = new HttpGet("http://somehost/");
|
||||
HttpContext context = new BasicHttpContext();
|
||||
HttpHost target = new HttpHost("somehost");
|
||||
|
||||
Date now = new Date();
|
||||
Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
|
||||
|
||||
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
|
||||
response.setEntity(HttpTestUtils.makeBody(128));
|
||||
response.setHeader("Content-Length", "128");
|
||||
response.setHeader("ETag", "\"etag\"");
|
||||
response.setHeader("Cache-Control", "public, max-age=3600");
|
||||
response.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo));
|
||||
|
||||
EasyMock.expect(client.execute(
|
||||
EasyMock.eq(target),
|
||||
EasyMock.isA(HttpRequest.class),
|
||||
EasyMock.same(context))).andReturn(response);
|
||||
EasyMock.replay(client);
|
||||
|
||||
CachingHttpClient t = new CachingHttpClient(client, resourceFactory, httpCacheStorage, cacheConfig);
|
||||
|
||||
HttpResponse response1 = t.execute(get, context);
|
||||
Assert.assertEquals(200, response1.getStatusLine().getStatusCode());
|
||||
EntityUtils.consume(response1.getEntity());
|
||||
|
||||
EasyMock.verify(client);
|
||||
|
||||
removeCache();
|
||||
|
||||
EasyMock.reset(client);
|
||||
EasyMock.expect(client.execute(
|
||||
EasyMock.eq(target),
|
||||
EasyMock.isA(HttpRequest.class),
|
||||
EasyMock.same(context))).andReturn(response);
|
||||
EasyMock.replay(client);
|
||||
|
||||
HttpResponse response2 = t.execute(get, context);
|
||||
Assert.assertEquals(200, response2.getStatusLine().getStatusCode());
|
||||
EntityUtils.consume(response2.getEntity());
|
||||
|
||||
EasyMock.verify(client);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue