Add a test for the PageCacheHandler

This commit is contained in:
Fuzzlemann 2017-08-15 00:11:15 +02:00
parent 521950804c
commit 8eff9720ba
3 changed files with 70 additions and 0 deletions

View File

@ -67,6 +67,16 @@ public class PageCacheHandler {
pageCache.put(identifier, response);
}
/**
* Checks if the page is cached.
*
* @param identifier The identifier of the page
* @return true if the page is cached
*/
public static boolean isCached(String identifier) {
return pageCache.asMap().containsKey(identifier);
}
/**
* Clears the cache from all its contents.
*/

View File

@ -38,4 +38,22 @@ public abstract class Response {
public int getCode() {
return header == null ? 500 : Integer.parseInt(header.split(" ")[1]);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response response = (Response) o;
return (header != null ? header.equals(response.header) : response.header == null)
&& (content != null ? content.equals(response.content) : response.content == null);
}
@Override
public int hashCode() {
int result = header != null ? header.hashCode() : 0;
result = 31 * result + (content != null ? content.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,42 @@
package test.java.main.java.com.djrapitops.plan.data.cache;
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
import main.java.com.djrapitops.plan.data.cache.PageLoader;
import main.java.com.djrapitops.plan.ui.webserver.response.Response;
import org.junit.Test;
import static junit.framework.TestCase.*;
/**
* @author Fuzzlemann
*/
public class PageCacheHandlerTest {
private final String IDENTIFIER = "test";
private final PageLoader LOADER = () -> new Response() {
@Override
public String getResponse() {
return "Test";
}
};
@Test
public void testCache() {
Response expResponse = LOADER.createResponse();
assertFalse(PageCacheHandler.isCached(IDENTIFIER));
Response response = PageCacheHandler.loadPage(IDENTIFIER, LOADER);
assertTrue(PageCacheHandler.isCached(IDENTIFIER));
assertEquals(expResponse, response);
}
@Test
public void testClearCache() {
PageCacheHandler.cachePage(IDENTIFIER, LOADER);
assertTrue(PageCacheHandler.isCached(IDENTIFIER));
PageCacheHandler.clearCache();
assertFalse(PageCacheHandler.isCached(IDENTIFIER));
}
}