diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/GeolocationCacheHandler.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/GeolocationCacheHandler.java index e26e44fcc..efa4701d2 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/GeolocationCacheHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/GeolocationCacheHandler.java @@ -9,7 +9,6 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; -import java.util.Map; /** * This class contains the geolocation cache. @@ -48,12 +47,10 @@ public class GeolocationCacheHandler { public static String getCountry(String ipAddress) { Log.debug("Started country retrieval from IP Address " + ipAddress); - Map geolocationMap = geolocationCache.asMap(); - String country = geolocationMap.get(ipAddress); - - Log.debug("Got country from " + ipAddress + " out of cache: " + country + " (if null, country wasn't cached)"); + String country = getCachedCountry(ipAddress); if (country != null) { + Log.debug("Got cached country from IP Address " + ipAddress + ": " + country); return country; } else { country = getUncachedCountry(ipAddress); @@ -77,7 +74,7 @@ public class GeolocationCacheHandler { * @see http://freegeoip.net * @see #getCountry(String) */ - private static String getUncachedCountry(String ipAddress) { + public static String getUncachedCountry(String ipAddress) { Benchmark.start("getUncachedCountry"); URL url; @@ -104,4 +101,24 @@ public class GeolocationCacheHandler { Benchmark.stop("getUncachedCountry"); } } + + /** + * Returns the cached country + * + * @param ipAddress The IP Address which is retrieved out of the cache + * @return The cached country, {@code null} if the country is not cached + */ + public static String getCachedCountry(String ipAddress) { + return geolocationCache.getIfPresent(ipAddress); + } + + /** + * Checks if the IP Address is cached + * + * @param ipAddress The IP Address which is checked + * @return true if the IP Address is cached + */ + public static boolean isCached(String ipAddress) { + return geolocationCache.asMap().containsKey(ipAddress); + } } diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java new file mode 100644 index 000000000..83d337744 --- /dev/null +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java @@ -0,0 +1,70 @@ +package test.java.main.java.com.djrapitops.plan.data.cache; + +import main.java.com.djrapitops.plan.data.cache.GeolocationCacheHandler; +import org.bukkit.plugin.java.JavaPlugin; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import test.java.utils.TestInit; + +import java.util.HashMap; +import java.util.Map; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; + +/** + * @author Fuzzlemann + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest(JavaPlugin.class) +public class GeolocationCacheTest { + + private Map ipsToCountries = new HashMap<>(); + + @Before + public void setUp() { + ipsToCountries.put("8.8.8.8", "United States"); + ipsToCountries.put("8.8.4.4", "United States"); + ipsToCountries.put("4.4.2.2", "United States"); + ipsToCountries.put("208.67.222.222", "United States"); + ipsToCountries.put("208.67.220.220", "United States"); + ipsToCountries.put("205.210.42.205", "Canada"); + ipsToCountries.put("64.68.200.200", "Canada"); + ipsToCountries.put("0.0.0.0", "Not Known"); + ipsToCountries.put("127.0.0.1", "Not Known"); + } + + @Test + public void testCountryGetting() throws Exception { + TestInit.init(); + + for (Map.Entry entry : ipsToCountries.entrySet()) { + String ip = entry.getKey(); + String expCountry = entry.getValue(); + + String country = GeolocationCacheHandler.getUncachedCountry(ip); + + assertEquals(country, expCountry); + } + } + + @Test + public void testCaching() throws Exception { + TestInit.init(); + + for (String ip : ipsToCountries.keySet()) { + String countryFirstCall = GeolocationCacheHandler.getUncachedCountry(ip); + assertFalse(GeolocationCacheHandler.isCached(ip)); + + String countrySecondCall = GeolocationCacheHandler.getCountry(ip); + assertTrue(GeolocationCacheHandler.isCached(ip)); + + String countryThirdCall = GeolocationCacheHandler.getCachedCountry(ip); + assertEquals(countryFirstCall, countrySecondCall, countryThirdCall); + } + } +} diff --git a/Plan/src/test/java/utils/RandomData.java b/Plan/src/test/java/utils/RandomData.java index 5646c7256..35b3b42fc 100644 --- a/Plan/src/test/java/utils/RandomData.java +++ b/Plan/src/test/java/utils/RandomData.java @@ -31,9 +31,6 @@ public class RandomData { return test; } - /** - * Random enough. - */ public static String randomString(int size) { return RandomStringUtils.random(size); }