diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java index e913c34c9..b4b283dfe 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java @@ -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. */ diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/response/Response.java b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/response/Response.java index cb22979d5..2c32c3cd4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/response/Response.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/response/Response.java @@ -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; + } } diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheHandlerTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheHandlerTest.java new file mode 100644 index 000000000..1306d473c --- /dev/null +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheHandlerTest.java @@ -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)); + } +}