mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-30 16:19:56 +08:00
Add GeolocationCache Tests
Changes some debug messages of the GeolocationCache
This commit is contained in:
parent
28a9ea870f
commit
bdb4b4ffbe
@ -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<String, String> 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 <a href="http://freegeoip.net">http://freegeoip.net</a>
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
70
Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java
vendored
Normal file
70
Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/GeolocationCacheTest.java
vendored
Normal file
@ -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<String, String> 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<String, String> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -31,9 +31,6 @@ public class RandomData {
|
||||
return test;
|
||||
}
|
||||
|
||||
/**
|
||||
* Random enough.
|
||||
*/
|
||||
public static String randomString(int size) {
|
||||
return RandomStringUtils.random(size);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user