Kick count, tests for the added Keys, equals methods, test fixes

This commit is contained in:
Rsl1122 2018-06-02 08:37:24 +03:00
parent efda292a37
commit efb1e33673
9 changed files with 128 additions and 36 deletions

View File

@ -66,11 +66,22 @@ public class GeoInfo {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
GeoInfo geoInfo = (GeoInfo) o; GeoInfo geoInfo = (GeoInfo) o;
return Objects.equal(ip, geoInfo.ip) && return Objects.equal(ip, geoInfo.ip) &&
Objects.equal(geolocation, geoInfo.geolocation); Objects.equal(geolocation, geoInfo.geolocation) &&
Objects.equal(ipHash, geoInfo.ipHash);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(ip, geolocation); return Objects.hashCode(ip, geolocation, ipHash);
}
@Override
public String toString() {
return "GeoInfo{" +
"ip='" + ip + '\'' +
", geolocation='" + geolocation + '\'' +
", ipHash='" + ipHash + '\'' +
", lastUsed=" + lastUsed +
'}';
} }
} }

View File

@ -22,6 +22,6 @@ public class PlayerKeys {
public static final Key<Long> REGISTERED = new Key<>(Long.class, "registered"); public static final Key<Long> REGISTERED = new Key<>(Long.class, "registered");
public static final Key<DateMap<UUID>> KICKS = new Key<>(new Type<DateMap<UUID>>() {}, "kicks"); public static final Key<Integer> KICK_COUNT = new Key<>(Integer.class, "kick_count");
public static final Key<DateMap<GeoInfo>> GEO_INFO = new Key<>(new Type<DateMap<GeoInfo>>() {}, "geo_info"); public static final Key<DateMap<GeoInfo>> GEO_INFO = new Key<>(new Type<DateMap<GeoInfo>>() {}, "geo_info");
} }

View File

@ -1,5 +1,6 @@
package com.djrapitops.plan.data.store.objects; package com.djrapitops.plan.data.store.objects;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
/** /**
@ -30,4 +31,29 @@ public class Nickname {
public UUID getServerUUID() { public UUID getServerUUID() {
return serverUUID; return serverUUID;
} }
@Override
public String toString() {
return "Nickname{" +
"name='" + name + '\'' +
", lastUsed=" + lastUsed +
", serverUUID=" + serverUUID +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Nickname)) return false;
Nickname nickname = (Nickname) o;
return lastUsed == nickname.lastUsed &&
Objects.equals(name, nickname.name) &&
Objects.equals(serverUUID, nickname.serverUUID);
}
@Override
public int hashCode() {
return Objects.hash(name, lastUsed, serverUUID);
}
} }

View File

@ -475,9 +475,11 @@ public class UsersTable extends UserIDTable {
if (set.next()) { if (set.next()) {
long registered = set.getLong(Col.REGISTERED.get()); long registered = set.getLong(Col.REGISTERED.get());
String name = set.getString(Col.USER_NAME.get()); String name = set.getString(Col.USER_NAME.get());
int timesKicked = set.getInt(Col.TIMES_KICKED.get());
container.putRawData(PlayerKeys.REGISTERED, registered); container.putRawData(PlayerKeys.REGISTERED, registered);
container.putRawData(PlayerKeys.NAME, name); container.putRawData(PlayerKeys.NAME, name);
container.putRawData(PlayerKeys.KICK_COUNT, timesKicked);
} }
return container; return container;
@ -489,6 +491,7 @@ public class UsersTable extends UserIDTable {
returnValue.putRawData(PlayerKeys.UUID, uuid); returnValue.putRawData(PlayerKeys.UUID, uuid);
returnValue.putSupplier(PlayerKeys.REGISTERED, () -> returnValue.getUnsafe(key).getUnsafe(PlayerKeys.REGISTERED)); returnValue.putSupplier(PlayerKeys.REGISTERED, () -> returnValue.getUnsafe(key).getUnsafe(PlayerKeys.REGISTERED));
returnValue.putSupplier(PlayerKeys.NAME, () -> returnValue.getUnsafe(key).getUnsafe(PlayerKeys.NAME)); returnValue.putSupplier(PlayerKeys.NAME, () -> returnValue.getUnsafe(key).getUnsafe(PlayerKeys.NAME));
returnValue.putSupplier(PlayerKeys.KICK_COUNT, () -> returnValue.getUnsafe(key).getUnsafe(PlayerKeys.KICK_COUNT));
return returnValue; return returnValue;
} }

View File

@ -6,7 +6,7 @@ package com.djrapitops.plan.system.info.server;
import com.djrapitops.plan.PlanBungee; import com.djrapitops.plan.PlanBungee;
import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.api.exceptions.database.DBException; import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.webserver.WebServerSystem; import com.djrapitops.plan.system.webserver.WebServerSystem;
import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.api.utility.log.Log;
@ -38,7 +38,7 @@ public class BungeeServerInfo extends ServerInfo {
} else { } else {
server = registerBungeeInfo(db); server = registerBungeeInfo(db);
} }
} catch (DBException e) { } catch (DBOpException e) {
throw new EnableException("Failed to read Server information from Database."); throw new EnableException("Failed to read Server information from Database.");
} }
return server; return server;

View File

@ -16,8 +16,6 @@ import java.util.TimeZone;
*/ */
public class FormatUtils { public class FormatUtils {
private static final DecimalFormat df = new DecimalFormat(Settings.FORMAT_DECIMALS.toString());
/** /**
* Constructor used to hide the public constructor * Constructor used to hide the public constructor
*/ */
@ -210,7 +208,7 @@ public class FormatUtils {
* @return String format of the double. * @return String format of the double.
*/ */
public static String cutDecimals(double d) { public static String cutDecimals(double d) {
return df.format(d); return new DecimalFormat(Settings.FORMAT_DECIMALS.toString()).format(d);
} }
public static String formatIP(String ip) { public static String formatIP(String ip) {

View File

@ -0,0 +1,26 @@
package com.djrapitops.plan.data.container;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import static org.junit.Assert.assertEquals;
/**
* Test for functionality of GeoInfo object.
*
* @author Rsl1122
*/
public class GeoInfoTest {
@Test
public void automaticallyHidesLast16Bits() throws UnsupportedEncodingException, NoSuchAlgorithmException {
String test = "1.2.3.4";
String expected = "1.2.xx.xx";
String result = new GeoInfo(test, "Irrelevant", 3).getIp();
assertEquals(expected, result);
}
}

View File

@ -3,11 +3,14 @@ package com.djrapitops.plan.system.cache;
import com.djrapitops.plan.Plan; import com.djrapitops.plan.Plan;
import com.djrapitops.plan.api.exceptions.EnableException; import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.system.BukkitSystem; import com.djrapitops.plan.system.BukkitSystem;
import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plugin.StaticHolder; import com.djrapitops.plugin.StaticHolder;
import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import utilities.Teardown;
import utilities.mocks.BukkitMockUtil; import utilities.mocks.BukkitMockUtil;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -25,6 +28,7 @@ public class GeolocationCacheTest {
@BeforeClass @BeforeClass
public static void setUpClass() throws Exception { public static void setUpClass() throws Exception {
Teardown.resetSettingsTempValues();
BukkitMockUtil mockUtil = BukkitMockUtil.setUp() BukkitMockUtil mockUtil = BukkitMockUtil.setUp()
.withDataFolder(temporaryFolder.getRoot()) .withDataFolder(temporaryFolder.getRoot())
.withLogging() .withLogging()
@ -35,14 +39,24 @@ public class GeolocationCacheTest {
StaticHolder.saveInstance(GeolocationCacheTest.class, planMock.getClass()); StaticHolder.saveInstance(GeolocationCacheTest.class, planMock.getClass());
} }
@AfterClass
public static void tearDownClass() {
Teardown.resetSettingsTempValues();
}
@Test @Test
public void testGeolocationCache() throws EnableException { public void testGeolocationCache() throws EnableException {
Settings.WEBSERVER_PORT.setTemporaryValue(9005);
BukkitSystem system = new BukkitSystem(planMock); BukkitSystem system = new BukkitSystem(planMock);
system.enable(); try {
system.enable();
String expected = "Germany"; String expected = "Germany";
String result = GeolocationCache.getCountry("141.52.255.1"); String result = GeolocationCache.getCountry("141.52.255.1");
assertEquals(expected, result); assertEquals(expected, result);
} finally {
system.disable();
}
} }
} }

View File

@ -13,6 +13,8 @@ import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.data.container.*; import com.djrapitops.plan.data.container.*;
import com.djrapitops.plan.data.store.containers.PlayerContainer; import com.djrapitops.plan.data.store.containers.PlayerContainer;
import com.djrapitops.plan.data.store.keys.PlayerKeys; import com.djrapitops.plan.data.store.keys.PlayerKeys;
import com.djrapitops.plan.data.store.objects.DateMap;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.data.time.GMTimes; import com.djrapitops.plan.data.time.GMTimes;
import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.system.database.databases.sql.SQLDB; import com.djrapitops.plan.system.database.databases.sql.SQLDB;
@ -39,7 +41,6 @@ import java.io.UnsupportedEncodingException;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean; import java.lang.management.OperatingSystemMXBean;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.*; import java.util.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -226,6 +227,7 @@ public class SQLiteTest {
private void saveUserOne(SQLDB database) { private void saveUserOne(SQLDB database) {
database.getUsersTable().registerUser(playerUUID, 123456789L, "Test"); database.getUsersTable().registerUser(playerUUID, 123456789L, "Test");
database.getUsersTable().kicked(playerUUID);
} }
private void saveUserTwo() { private void saveUserTwo() {
@ -237,7 +239,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testActionsTable() throws SQLException { public void testActionsTable() {
saveUserOne(); saveUserOne();
ActionsTable actionsTable = db.getActionsTable(); ActionsTable actionsTable = db.getActionsTable();
@ -251,7 +253,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testIPTable() throws SQLException, DBInitException { public void testIPTable() throws DBInitException {
saveUserOne(); saveUserOne();
GeoInfoTable geoInfoTable = db.getGeoInfoTable(); GeoInfoTable geoInfoTable = db.getGeoInfoTable();
@ -276,7 +278,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testNicknamesTable() throws SQLException, DBInitException { public void testNicknamesTable() throws DBInitException {
saveUserOne(); saveUserOne();
NicknamesTable nickTable = db.getNicknamesTable(); NicknamesTable nickTable = db.getNicknamesTable();
@ -357,7 +359,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testSessionPlaytimeSaving() throws SQLException, DBInitException { public void testSessionPlaytimeSaving() throws DBInitException {
saveTwoWorlds(); saveTwoWorlds();
saveUserOne(); saveUserOne();
saveUserTwo(); saveUserTwo();
@ -388,7 +390,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testSessionSaving() throws SQLException, DBInitException { public void testSessionSaving() throws DBInitException {
saveUserOne(); saveUserOne();
saveUserTwo(); saveUserTwo();
@ -453,7 +455,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testUserInfoTableRegisterRegistered() throws SQLException, DBInitException { public void testUserInfoTableRegisterRegistered() throws DBInitException {
saveUserOne(); saveUserOne();
UsersTable usersTable = db.getUsersTable(); UsersTable usersTable = db.getUsersTable();
assertTrue(usersTable.isRegistered(playerUUID)); assertTrue(usersTable.isRegistered(playerUUID));
@ -512,7 +514,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testUsersTableUpdateName() throws SQLException, DBInitException { public void testUsersTableUpdateName() throws DBInitException {
saveUserOne(); saveUserOne();
UsersTable usersTable = db.getUsersTable(); UsersTable usersTable = db.getUsersTable();
@ -529,10 +531,10 @@ public class SQLiteTest {
} }
@Test @Test
public void testUsersTableKickSaving() throws SQLException, DBInitException { public void testUsersTableKickSaving() throws DBInitException {
saveUserOne(); saveUserOne();
UsersTable usersTable = db.getUsersTable(); UsersTable usersTable = db.getUsersTable();
assertEquals(0, usersTable.getTimesKicked(playerUUID)); assertEquals(1, usersTable.getTimesKicked(playerUUID));
int random = new Random().nextInt(20); int random = new Random().nextInt(20);
@ -540,11 +542,11 @@ public class SQLiteTest {
usersTable.kicked(playerUUID); usersTable.kicked(playerUUID);
} }
commitTest(); commitTest();
assertEquals(random + 1, usersTable.getTimesKicked(playerUUID)); assertEquals(random + 2, usersTable.getTimesKicked(playerUUID));
} }
@Test @Test
public void testRemovalSingleUser() throws SQLException { public void testRemovalSingleUser() {
saveUserTwo(); saveUserTwo();
UserInfoTable userInfoTable = db.getUserInfoTable(); UserInfoTable userInfoTable = db.getUserInfoTable();
@ -580,7 +582,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testRemovalEverything() throws SQLException, UnsupportedEncodingException, NoSuchAlgorithmException { public void testRemovalEverything() throws UnsupportedEncodingException, NoSuchAlgorithmException {
UserInfoTable userInfoTable = db.getUserInfoTable(); UserInfoTable userInfoTable = db.getUserInfoTable();
UsersTable usersTable = db.getUsersTable(); UsersTable usersTable = db.getUsersTable();
SessionsTable sessionsTable = db.getSessionsTable(); SessionsTable sessionsTable = db.getSessionsTable();
@ -712,7 +714,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testSessionTableGetInfoOfServer() throws SQLException, DBInitException { public void testSessionTableGetInfoOfServer() throws DBInitException {
saveUserOne(); saveUserOne();
saveUserTwo(); saveUserTwo();
@ -739,7 +741,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testKillTableGetKillsOfServer() throws SQLException, DBInitException { public void testKillTableGetKillsOfServer() throws DBInitException {
saveUserOne(); saveUserOne();
saveUserTwo(); saveUserTwo();
@ -758,7 +760,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testBackupAndRestore() throws SQLException, DBException, UnsupportedEncodingException, NoSuchAlgorithmException { public void testBackupAndRestore() throws DBException, UnsupportedEncodingException, NoSuchAlgorithmException {
System.out.println("- Creating Backup Database -"); System.out.println("- Creating Backup Database -");
SQLiteDB backup = new SQLiteDB("debug-backup" + System.currentTimeMillis()); SQLiteDB backup = new SQLiteDB("debug-backup" + System.currentTimeMillis());
backup.init(); backup.init();
@ -793,7 +795,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testSaveWorldTimes() throws SQLException { public void testSaveWorldTimes() {
saveUserOne(); saveUserOne();
WorldTimes worldTimes = createWorldTimes(); WorldTimes worldTimes = createWorldTimes();
WorldTimesTable worldTimesTable = db.getWorldTimesTable(); WorldTimesTable worldTimesTable = db.getWorldTimesTable();
@ -808,7 +810,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testSaveAllWorldTimes() throws SQLException { public void testSaveAllWorldTimes() {
saveUserOne(); saveUserOne();
WorldTimes worldTimes = createWorldTimes(); WorldTimes worldTimes = createWorldTimes();
System.out.println(worldTimes); System.out.println(worldTimes);
@ -830,7 +832,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testSaveSessionsWorldTimes() throws SQLException { public void testSaveSessionsWorldTimes() {
SessionsTable sessionsTable = db.getSessionsTable(); SessionsTable sessionsTable = db.getSessionsTable();
saveUserOne(); saveUserOne();
@ -855,14 +857,14 @@ public class SQLiteTest {
} }
@Test @Test
public void testGetUserWorldTimes() throws SQLException { public void testGetUserWorldTimes() {
testSaveSessionsWorldTimes(); testSaveSessionsWorldTimes();
WorldTimes worldTimesOfUser = db.getWorldTimesTable().getWorldTimesOfUser(playerUUID); WorldTimes worldTimesOfUser = db.getWorldTimesTable().getWorldTimesOfUser(playerUUID);
assertEquals(createWorldTimes(), worldTimesOfUser); assertEquals(createWorldTimes(), worldTimesOfUser);
} }
@Test @Test
public void testGetServerWorldTimes() throws SQLException { public void testGetServerWorldTimes() {
testSaveSessionsWorldTimes(); testSaveSessionsWorldTimes();
WorldTimes worldTimesOfServer = db.getWorldTimesTable().getWorldTimesOfServer(TestConstants.SERVER_UUID); WorldTimes worldTimesOfServer = db.getWorldTimesTable().getWorldTimesOfServer(TestConstants.SERVER_UUID);
assertEquals(createWorldTimes(), worldTimesOfServer); assertEquals(createWorldTimes(), worldTimesOfServer);
@ -896,7 +898,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testWorldTableGetWorldNamesNoException() throws SQLException, UnsupportedEncodingException, NoSuchAlgorithmException { public void testWorldTableGetWorldNamesNoException() throws UnsupportedEncodingException, NoSuchAlgorithmException {
saveAllData(db); saveAllData(db);
Set<String> worldNames = db.getWorldTable().getWorldNames(TestConstants.SERVER_UUID); Set<String> worldNames = db.getWorldTable().getWorldNames(TestConstants.SERVER_UUID);
assertEquals(new HashSet<>(worlds), worldNames); assertEquals(new HashSet<>(worlds), worldNames);
@ -942,7 +944,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testWorldTableAlterV16() throws SQLException { public void testWorldTableAlterV16() {
saveUserOne(); saveUserOne();
new Table("test", db) { new Table("test", db) {
@Override @Override
@ -1030,7 +1032,7 @@ public class SQLiteTest {
} }
@Test @Test
public void testNewContainerForPlayer() throws UnsupportedEncodingException, SQLException, NoSuchAlgorithmException { public void testNewContainerForPlayer() throws UnsupportedEncodingException, NoSuchAlgorithmException {
saveAllData(db); saveAllData(db);
long start = System.nanoTime(); long start = System.nanoTime();
@ -1040,7 +1042,10 @@ public class SQLiteTest {
assertTrue(container.supports(PlayerKeys.UUID)); assertTrue(container.supports(PlayerKeys.UUID));
assertTrue(container.supports(PlayerKeys.REGISTERED)); assertTrue(container.supports(PlayerKeys.REGISTERED));
assertTrue(container.supports(PlayerKeys.NAME)); assertTrue(container.supports(PlayerKeys.NAME));
assertTrue(container.supports(PlayerKeys.KICK_COUNT));
assertTrue(container.supports(PlayerKeys.GEO_INFO)); assertTrue(container.supports(PlayerKeys.GEO_INFO));
assertTrue(container.supports(PlayerKeys.NICKNAMES));
long end = System.nanoTime(); long end = System.nanoTime();
@ -1049,5 +1054,14 @@ public class SQLiteTest {
OptionalAssert.equals(playerUUID, container.getValue(PlayerKeys.UUID)); OptionalAssert.equals(playerUUID, container.getValue(PlayerKeys.UUID));
OptionalAssert.equals(123456789L, container.getValue(PlayerKeys.REGISTERED)); OptionalAssert.equals(123456789L, container.getValue(PlayerKeys.REGISTERED));
OptionalAssert.equals("Test", container.getValue(PlayerKeys.NAME)); OptionalAssert.equals("Test", container.getValue(PlayerKeys.NAME));
OptionalAssert.equals(1, container.getValue(PlayerKeys.KICK_COUNT));
DateMap<GeoInfo> expectedGeoInfo = GeoInfo.intoDateMap(
Collections.singletonList(new GeoInfo("1.2.3.4", "TestLoc", 223456789, "ZpT4PJ9HbaMfXfa8xSADTn5X1CHSR7nTT0ntv8hKdkw="))
);
OptionalAssert.equals(expectedGeoInfo, container.getValue(PlayerKeys.GEO_INFO));
List<Nickname> expectedNicknames = Collections.singletonList(new Nickname("TestNick", -1, TestConstants.SERVER_UUID));
OptionalAssert.equals(expectedNicknames, container.getValue(PlayerKeys.NICKNAMES));
} }
} }