mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-18 01:31:01 +02:00
[Debt] Turned GeolocationCache methods non-static
getInstance is not necessary as GeolocationCache can be injected everywhere where it is required (Mostly at gathering layer) Discovered old GeolocationCacheTest that was removed.
This commit is contained in:
parent
654d7ca03c
commit
f26294d9dd
@ -9,7 +9,6 @@ import com.djrapitops.plan.system.settings.Settings;
|
|||||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||||
import com.djrapitops.plugin.logging.L;
|
import com.djrapitops.plugin.logging.L;
|
||||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||||
import com.djrapitops.plugin.utilities.Verify;
|
|
||||||
import com.maxmind.geoip2.DatabaseReader;
|
import com.maxmind.geoip2.DatabaseReader;
|
||||||
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
||||||
import com.maxmind.geoip2.model.CountryResponse;
|
import com.maxmind.geoip2.model.CountryResponse;
|
||||||
@ -59,7 +58,8 @@ public class GeolocationCache implements SubSystem {
|
|||||||
this.fileSystem = fileSystem;
|
this.fileSystem = fileSystem;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
cached = new HashMap<>();
|
|
||||||
|
this.cached = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -67,7 +67,7 @@ public class GeolocationCache implements SubSystem {
|
|||||||
geolocationDB = fileSystem.getFileFromPluginFolder("GeoIP.dat");
|
geolocationDB = fileSystem.getFileFromPluginFolder("GeoIP.dat");
|
||||||
if (config.isTrue(Settings.DATA_GEOLOCATIONS)) {
|
if (config.isTrue(Settings.DATA_GEOLOCATIONS)) {
|
||||||
try {
|
try {
|
||||||
GeolocationCache.checkDB();
|
checkDB();
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
logger.error(locale.getString(PluginLang.ENABLE_NOTIFY_GEOLOCATIONS_INTERNET_REQUIRED));
|
logger.error(locale.getString(PluginLang.ENABLE_NOTIFY_GEOLOCATIONS_INTERNET_REQUIRED));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -90,33 +90,27 @@ public class GeolocationCache implements SubSystem {
|
|||||||
* if that happens, "Not Known" will be returned.
|
* if that happens, "Not Known" will be returned.
|
||||||
* @see #getUnCachedCountry(String)
|
* @see #getUnCachedCountry(String)
|
||||||
*/
|
*/
|
||||||
public static String getCountry(String ipAddress) {
|
public String getCountry(String ipAddress) {
|
||||||
String country = getCachedCountry(ipAddress);
|
String country = getCachedCountry(ipAddress);
|
||||||
|
|
||||||
if (country != null) {
|
if (country != null) {
|
||||||
return country;
|
return country;
|
||||||
} else {
|
} else {
|
||||||
country = getUnCachedCountry(ipAddress);
|
country = getUnCachedCountry(ipAddress);
|
||||||
getInstance().cached.put(ipAddress, country);
|
cached.put(ipAddress, country);
|
||||||
|
|
||||||
return country;
|
return country;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static GeolocationCache getInstance() {
|
|
||||||
GeolocationCache geolocationCache = CacheSystem.getInstance().getGeolocationCache();
|
|
||||||
Verify.nullCheck(geolocationCache, () -> new IllegalStateException("GeolocationCache was not initialized."));
|
|
||||||
return geolocationCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the cached country
|
* Returns the cached country
|
||||||
*
|
*
|
||||||
* @param ipAddress The IP Address which is retrieved out of the cache
|
* @param ipAddress The IP Address which is retrieved out of the cache
|
||||||
* @return The cached country, {@code null} if the country is not cached
|
* @return The cached country, {@code null} if the country is not cached
|
||||||
*/
|
*/
|
||||||
private static String getCachedCountry(String ipAddress) {
|
private String getCachedCountry(String ipAddress) {
|
||||||
return getInstance().cached.get(ipAddress);
|
return cached.get(ipAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,14 +127,14 @@ public class GeolocationCache implements SubSystem {
|
|||||||
* @see <a href="http://maxmind.com">http://maxmind.com</a>
|
* @see <a href="http://maxmind.com">http://maxmind.com</a>
|
||||||
* @see #getCountry(String)
|
* @see #getCountry(String)
|
||||||
*/
|
*/
|
||||||
private static String getUnCachedCountry(String ipAddress) {
|
private String getUnCachedCountry(String ipAddress) {
|
||||||
if ("127.0.0.1".equals(ipAddress)) {
|
if ("127.0.0.1".equals(ipAddress)) {
|
||||||
return "Local Machine";
|
return "Local Machine";
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
checkDB();
|
checkDB();
|
||||||
|
|
||||||
try (DatabaseReader reader = new DatabaseReader.Builder(getInstance().geolocationDB).build()) {
|
try (DatabaseReader reader = new DatabaseReader.Builder(geolocationDB).build()) {
|
||||||
InetAddress inetAddress = InetAddress.getByName(ipAddress);
|
InetAddress inetAddress = InetAddress.getByName(ipAddress);
|
||||||
|
|
||||||
CountryResponse response = reader.country(inetAddress);
|
CountryResponse response = reader.country(inetAddress);
|
||||||
@ -159,13 +153,13 @@ public class GeolocationCache implements SubSystem {
|
|||||||
*
|
*
|
||||||
* @throws IOException when an error at download or saving the DB happens
|
* @throws IOException when an error at download or saving the DB happens
|
||||||
*/
|
*/
|
||||||
public static void checkDB() throws IOException {
|
private void checkDB() throws IOException {
|
||||||
if (getInstance().geolocationDB.exists()) {
|
if (geolocationDB.exists()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
URL downloadSite = new URL("http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz");
|
URL downloadSite = new URL("http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz");
|
||||||
try (ReadableByteChannel rbc = Channels.newChannel(new GZIPInputStream(downloadSite.openStream()));
|
try (ReadableByteChannel rbc = Channels.newChannel(new GZIPInputStream(downloadSite.openStream()));
|
||||||
FileOutputStream fos = new FileOutputStream(getInstance().geolocationDB.getAbsoluteFile())) {
|
FileOutputStream fos = new FileOutputStream(geolocationDB.getAbsoluteFile())) {
|
||||||
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -176,8 +170,8 @@ public class GeolocationCache implements SubSystem {
|
|||||||
* @param ipAddress The IP Address which is checked
|
* @param ipAddress The IP Address which is checked
|
||||||
* @return true if the IP Address is cached
|
* @return true if the IP Address is cached
|
||||||
*/
|
*/
|
||||||
public static boolean isCached(String ipAddress) {
|
boolean isCached(String ipAddress) {
|
||||||
return getInstance().cached.containsKey(ipAddress);
|
return cached.containsKey(ipAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.djrapitops.plan.system.importing;
|
package com.djrapitops.plan.system.importing;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.system.cache.GeolocationCache;
|
||||||
import com.djrapitops.plan.system.database.databases.Database;
|
import com.djrapitops.plan.system.database.databases.Database;
|
||||||
import com.djrapitops.plan.system.importing.importers.OfflinePlayerImporter;
|
import com.djrapitops.plan.system.importing.importers.OfflinePlayerImporter;
|
||||||
import com.djrapitops.plan.system.info.server.ServerInfo;
|
import com.djrapitops.plan.system.info.server.ServerInfo;
|
||||||
@ -15,20 +16,23 @@ import javax.inject.Singleton;
|
|||||||
@Singleton
|
@Singleton
|
||||||
public class BukkitImportSystem extends ImportSystem {
|
public class BukkitImportSystem extends ImportSystem {
|
||||||
|
|
||||||
|
private final GeolocationCache geolocationCache;
|
||||||
private final Database database;
|
private final Database database;
|
||||||
private final ServerInfo serverInfo;
|
private final ServerInfo serverInfo;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public BukkitImportSystem(
|
public BukkitImportSystem(
|
||||||
|
GeolocationCache geolocationCache,
|
||||||
Database database,
|
Database database,
|
||||||
ServerInfo serverInfo
|
ServerInfo serverInfo
|
||||||
) {
|
) {
|
||||||
|
this.geolocationCache = geolocationCache;
|
||||||
this.database = database;
|
this.database = database;
|
||||||
this.serverInfo = serverInfo;
|
this.serverInfo = serverInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void registerImporters() {
|
void registerImporters() {
|
||||||
registerImporter(new OfflinePlayerImporter(database, serverInfo));
|
registerImporter(new OfflinePlayerImporter(geolocationCache, database, serverInfo));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -36,12 +36,19 @@ import java.util.stream.Collectors;
|
|||||||
*/
|
*/
|
||||||
public abstract class Importer {
|
public abstract class Importer {
|
||||||
|
|
||||||
|
private final GeolocationCache geolocationCache;
|
||||||
private final Database database;
|
private final Database database;
|
||||||
private final UUID serverUUID;
|
private final UUID serverUUID;
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
protected Importer(Database database, ServerInfo serverInfo, String name) {
|
protected Importer(
|
||||||
|
GeolocationCache geolocationCache,
|
||||||
|
Database database,
|
||||||
|
ServerInfo serverInfo,
|
||||||
|
String name
|
||||||
|
) {
|
||||||
|
this.geolocationCache = geolocationCache;
|
||||||
this.database = database;
|
this.database = database;
|
||||||
this.serverUUID = serverInfo.getServerUUID();
|
this.serverUUID = serverInfo.getServerUUID();
|
||||||
|
|
||||||
@ -188,7 +195,7 @@ public abstract class Importer {
|
|||||||
|
|
||||||
return userImportData.getIps().parallelStream()
|
return userImportData.getIps().parallelStream()
|
||||||
.map(ip -> {
|
.map(ip -> {
|
||||||
String geoLoc = GeolocationCache.getCountry(ip);
|
String geoLoc = geolocationCache.getCountry(ip);
|
||||||
try {
|
try {
|
||||||
return new GeoInfo(ip, geoLoc, date, new SHA256Hash(ip).create());
|
return new GeoInfo(ip, geoLoc, date, new SHA256Hash(ip).create());
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.djrapitops.plan.system.importing.importers;
|
package com.djrapitops.plan.system.importing.importers;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.system.cache.GeolocationCache;
|
||||||
import com.djrapitops.plan.system.database.databases.Database;
|
import com.djrapitops.plan.system.database.databases.Database;
|
||||||
import com.djrapitops.plan.system.importing.data.ServerImportData;
|
import com.djrapitops.plan.system.importing.data.ServerImportData;
|
||||||
import com.djrapitops.plan.system.importing.data.UserImportData;
|
import com.djrapitops.plan.system.importing.data.UserImportData;
|
||||||
@ -27,10 +28,11 @@ public class OfflinePlayerImporter extends Importer {
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public OfflinePlayerImporter(
|
public OfflinePlayerImporter(
|
||||||
|
GeolocationCache geolocationCache,
|
||||||
Database database,
|
Database database,
|
||||||
ServerInfo serverInfo
|
ServerInfo serverInfo
|
||||||
) {
|
) {
|
||||||
super(database, serverInfo, "offline");
|
super(geolocationCache, database, serverInfo, "offline");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -25,21 +25,24 @@ public class IPUpdateProcessor implements CriticalRunnable {
|
|||||||
private final long time;
|
private final long time;
|
||||||
|
|
||||||
private final Database database;
|
private final Database database;
|
||||||
|
private final GeolocationCache geolocationCache;
|
||||||
|
|
||||||
IPUpdateProcessor(
|
IPUpdateProcessor(
|
||||||
UUID uuid, InetAddress ip, long time,
|
UUID uuid, InetAddress ip, long time,
|
||||||
Database database
|
Database database,
|
||||||
|
GeolocationCache geolocationCache
|
||||||
) {
|
) {
|
||||||
this.uuid = uuid;
|
this.uuid = uuid;
|
||||||
this.ip = ip;
|
this.ip = ip;
|
||||||
this.time = time;
|
this.time = time;
|
||||||
this.database = database;
|
this.database = database;
|
||||||
|
this.geolocationCache = geolocationCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
String country = GeolocationCache.getCountry(ip.getHostAddress());
|
String country = geolocationCache.getCountry(ip.getHostAddress());
|
||||||
GeoInfo geoInfo = new GeoInfo(ip, country, time);
|
GeoInfo geoInfo = new GeoInfo(ip, country, time);
|
||||||
database.save().geoInfo(uuid, geoInfo);
|
database.save().geoInfo(uuid, geoInfo);
|
||||||
} catch (NoSuchAlgorithmException ignore) {
|
} catch (NoSuchAlgorithmException ignore) {
|
||||||
|
@ -2,6 +2,7 @@ package com.djrapitops.plan.system.processing.processors.player;
|
|||||||
|
|
||||||
import com.djrapitops.plan.data.store.objects.DateObj;
|
import com.djrapitops.plan.data.store.objects.DateObj;
|
||||||
import com.djrapitops.plan.system.cache.DataCache;
|
import com.djrapitops.plan.system.cache.DataCache;
|
||||||
|
import com.djrapitops.plan.system.cache.GeolocationCache;
|
||||||
import com.djrapitops.plan.system.database.databases.Database;
|
import com.djrapitops.plan.system.database.databases.Database;
|
||||||
import com.djrapitops.plan.system.processing.Processing;
|
import com.djrapitops.plan.system.processing.Processing;
|
||||||
import dagger.Lazy;
|
import dagger.Lazy;
|
||||||
@ -24,16 +25,19 @@ public class PlayerProcessors {
|
|||||||
private final Lazy<Processing> processing;
|
private final Lazy<Processing> processing;
|
||||||
private final Lazy<Database> database;
|
private final Lazy<Database> database;
|
||||||
private final Lazy<DataCache> dataCache;
|
private final Lazy<DataCache> dataCache;
|
||||||
|
private final Lazy<GeolocationCache> geolocationCache;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public PlayerProcessors(
|
public PlayerProcessors(
|
||||||
Lazy<Processing> processing,
|
Lazy<Processing> processing,
|
||||||
Lazy<Database> database,
|
Lazy<Database> database,
|
||||||
Lazy<DataCache> dataCache
|
Lazy<DataCache> dataCache,
|
||||||
|
Lazy<GeolocationCache> geolocationCache
|
||||||
) {
|
) {
|
||||||
this.processing = processing;
|
this.processing = processing;
|
||||||
this.database = database;
|
this.database = database;
|
||||||
this.dataCache = dataCache;
|
this.dataCache = dataCache;
|
||||||
|
this.geolocationCache = geolocationCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BanAndOpProcessor banAndOpProcessor(UUID uuid, Supplier<Boolean> banned, boolean op) {
|
public BanAndOpProcessor banAndOpProcessor(UUID uuid, Supplier<Boolean> banned, boolean op) {
|
||||||
@ -49,7 +53,7 @@ public class PlayerProcessors {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public IPUpdateProcessor ipUpdateProcessor(UUID uuid, InetAddress ip, long time) {
|
public IPUpdateProcessor ipUpdateProcessor(UUID uuid, InetAddress ip, long time) {
|
||||||
return new IPUpdateProcessor(uuid, ip, time, database.get());
|
return new IPUpdateProcessor(uuid, ip, time, database.get(), geolocationCache.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
public KickProcessor kickProcessor(UUID uuid) {
|
public KickProcessor kickProcessor(UUID uuid) {
|
||||||
|
@ -1,80 +0,0 @@
|
|||||||
package com.djrapitops.plan.data.cache;
|
|
||||||
|
|
||||||
import com.djrapitops.plan.system.cache.CacheSystem;
|
|
||||||
import com.djrapitops.plan.system.cache.GeolocationCache;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.ClassRule;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.rules.TemporaryFolder;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
|
||||||
import utilities.mocks.SystemMockUtil;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static junit.framework.TestCase.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Fuzzlemann
|
|
||||||
*/
|
|
||||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
|
||||||
public class GeolocationCacheTest {
|
|
||||||
|
|
||||||
private final Map<String, String> ipsToCountries = new HashMap<>();
|
|
||||||
|
|
||||||
@ClassRule
|
|
||||||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
|
||||||
|
|
||||||
@BeforeClass
|
|
||||||
public static void setUpClass() throws Exception {
|
|
||||||
SystemMockUtil.setUp(temporaryFolder.getRoot())
|
|
||||||
.enableConfigSystem()
|
|
||||||
.enableCacheSystem();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
CacheSystem.getInstance().getGeolocationCache().clearCache();
|
|
||||||
|
|
||||||
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", "Local Machine");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCountryGetting() {
|
|
||||||
for (Map.Entry<String, String> entry : ipsToCountries.entrySet()) {
|
|
||||||
String ip = entry.getKey();
|
|
||||||
String expCountry = entry.getValue();
|
|
||||||
|
|
||||||
String country = GeolocationCache.getCountry(ip);
|
|
||||||
|
|
||||||
assertEquals(country, expCountry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCaching() {
|
|
||||||
for (Map.Entry<String, String> entry : ipsToCountries.entrySet()) {
|
|
||||||
String ip = entry.getKey();
|
|
||||||
String expIp = entry.getValue();
|
|
||||||
|
|
||||||
assertFalse(GeolocationCache.isCached(ip));
|
|
||||||
String countrySecondCall = GeolocationCache.getCountry(ip);
|
|
||||||
assertTrue(GeolocationCache.isCached(ip));
|
|
||||||
|
|
||||||
String countryThirdCall = GeolocationCache.getCountry(ip);
|
|
||||||
|
|
||||||
assertEquals(countrySecondCall, countryThirdCall);
|
|
||||||
assertEquals(countryThirdCall, expIp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,55 +1,92 @@
|
|||||||
package com.djrapitops.plan.system.cache;
|
package com.djrapitops.plan.system.cache;
|
||||||
|
|
||||||
import com.djrapitops.plan.Plan;
|
import com.djrapitops.plan.system.file.FileSystem;
|
||||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
import com.djrapitops.plan.system.locale.Locale;
|
||||||
import com.djrapitops.plan.system.PlanSystem;
|
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||||
import com.djrapitops.plugin.StaticHolder;
|
import com.djrapitops.plugin.logging.console.TestPluginLogger;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.TemporaryFolder;
|
import org.junit.rules.TemporaryFolder;
|
||||||
import utilities.mocks.BukkitMockUtil;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
import utilities.mocks.SystemMockUtil;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static junit.framework.TestCase.*;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for GeolocationCache.
|
* @author Fuzzlemann
|
||||||
*
|
|
||||||
* @author Rsl1122
|
|
||||||
*/
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||||
public class GeolocationCacheTest {
|
public class GeolocationCacheTest {
|
||||||
|
|
||||||
|
private final Map<String, String> ipsToCountries = new HashMap<>();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private FileSystem fileSystem;
|
||||||
|
@Mock
|
||||||
|
private PlanConfig config;
|
||||||
|
private GeolocationCache geolocationCache;
|
||||||
|
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
private static Plan planMock;
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUpClass() throws Exception {
|
public static void setUpClass() throws Exception {
|
||||||
BukkitMockUtil mockUtil = BukkitMockUtil.setUp()
|
SystemMockUtil.setUp(temporaryFolder.getRoot())
|
||||||
.withDataFolder(temporaryFolder.getRoot())
|
.enableConfigSystem()
|
||||||
.withLogging()
|
.enableCacheSystem();
|
||||||
.withPluginDescription()
|
}
|
||||||
.withResourceFetchingFromJar()
|
|
||||||
.withServer();
|
@Before
|
||||||
planMock = mockUtil.getPlanMock();
|
public void setUp() throws IOException {
|
||||||
StaticHolder.saveInstance(GeolocationCacheTest.class, planMock.getClass());
|
doReturn(temporaryFolder.newFile("GeoIP.dat")).when(fileSystem.getFileFromPluginFolder("GeoIP.dat"));
|
||||||
|
geolocationCache = new GeolocationCache(new Locale(), fileSystem, config, new TestPluginLogger());
|
||||||
|
|
||||||
|
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", "Local Machine");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
public void testCountryGetting() {
|
||||||
public void testGeolocationCache() throws EnableException {
|
for (Map.Entry<String, String> entry : ipsToCountries.entrySet()) {
|
||||||
// Settings.WEBSERVER_PORT.setTemporaryValue(9005);
|
String ip = entry.getKey();
|
||||||
PlanSystem system = null; //TODO
|
String expCountry = entry.getValue();
|
||||||
try {
|
|
||||||
system.enable();
|
|
||||||
|
|
||||||
String expected = "Germany";
|
String country = geolocationCache.getCountry(ip);
|
||||||
String result = GeolocationCache.getCountry("141.52.255.1");
|
|
||||||
assertEquals(expected, result);
|
assertEquals(country, expCountry);
|
||||||
} finally {
|
|
||||||
system.disable();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCaching() {
|
||||||
|
for (Map.Entry<String, String> entry : ipsToCountries.entrySet()) {
|
||||||
|
String ip = entry.getKey();
|
||||||
|
String expIp = entry.getValue();
|
||||||
|
|
||||||
|
assertFalse(geolocationCache.isCached(ip));
|
||||||
|
String countrySecondCall = geolocationCache.getCountry(ip);
|
||||||
|
assertTrue(geolocationCache.isCached(ip));
|
||||||
|
|
||||||
|
String countryThirdCall = geolocationCache.getCountry(ip);
|
||||||
|
|
||||||
|
assertEquals(countrySecondCall, countryThirdCall);
|
||||||
|
assertEquals(countryThirdCall, expIp);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user