mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-28 03:57:33 +01:00
Add a test for the PageCacheHandler
This commit is contained in:
parent
521950804c
commit
8eff9720ba
@ -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.
|
||||
*/
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
42
Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheHandlerTest.java
vendored
Normal file
42
Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheHandlerTest.java
vendored
Normal 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user