mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-09 09:57:35 +01:00
More tests against issues found during manual testing
During testing it was found that: - MySQL patches are still failing if they failed before (test_sessions has foreign keys) - Time.Thresholds.AFK_threshold was empty on fresh install (Leading to NPE that was prevented)
This commit is contained in:
parent
3ad9277c99
commit
ff266893d1
@ -18,9 +18,13 @@ package com.djrapitops.plan;
|
|||||||
|
|
||||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||||
import com.djrapitops.plan.system.PlanSystem;
|
import com.djrapitops.plan.system.PlanSystem;
|
||||||
|
import com.djrapitops.plan.system.settings.ConfigSettingKeyTest;
|
||||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||||
import com.djrapitops.plan.system.settings.paths.WebserverSettings;
|
import com.djrapitops.plan.system.settings.paths.WebserverSettings;
|
||||||
|
import com.djrapitops.plan.system.settings.paths.key.Setting;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.TemporaryFolder;
|
import org.junit.rules.TemporaryFolder;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -29,6 +33,10 @@ import rules.BukkitComponentMocker;
|
|||||||
import rules.ComponentMocker;
|
import rules.ComponentMocker;
|
||||||
import utilities.RandomData;
|
import utilities.RandomData;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for Bukkit PlanSystem.
|
* Test for Bukkit PlanSystem.
|
||||||
*
|
*
|
||||||
@ -39,20 +47,40 @@ public class BukkitSystemTest {
|
|||||||
|
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
@ClassRule
|
|
||||||
public static ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
|
@Rule
|
||||||
|
public ComponentMocker component = new BukkitComponentMocker(temporaryFolder);
|
||||||
|
|
||||||
private final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
|
private final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
|
||||||
|
private PlanSystem system;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void prepareSystem() {
|
||||||
|
system = component.getPlanSystem();
|
||||||
|
system.getConfigSystem().getConfig()
|
||||||
|
.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEnable() throws EnableException {
|
public void bukkitSystemEnables() throws EnableException {
|
||||||
PlanSystem bukkitSystem = component.getPlanSystem();
|
|
||||||
try {
|
try {
|
||||||
PlanConfig config = bukkitSystem.getConfigSystem().getConfig();
|
system.enable();
|
||||||
config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
|
assertTrue(system.isEnabled());
|
||||||
bukkitSystem.enable();
|
|
||||||
} finally {
|
} finally {
|
||||||
bukkitSystem.disable();
|
system.disable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void bukkitSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException {
|
||||||
|
try {
|
||||||
|
system.enable();
|
||||||
|
PlanConfig config = system.getConfigSystem().getConfig();
|
||||||
|
|
||||||
|
Collection<Setting> serverSettings = ConfigSettingKeyTest.getServerSettings();
|
||||||
|
ConfigSettingKeyTest.assertValidDefaultValuesForAllSettings(config, serverSettings);
|
||||||
|
} finally {
|
||||||
|
system.disable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,8 @@ import rules.BungeeComponentMocker;
|
|||||||
import rules.ComponentMocker;
|
import rules.ComponentMocker;
|
||||||
import utilities.RandomData;
|
import utilities.RandomData;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for Bungee PlanSystem.
|
* Test for Bungee PlanSystem.
|
||||||
*
|
*
|
||||||
@ -83,6 +85,7 @@ public class BungeeSystemTest {
|
|||||||
dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile());
|
dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile());
|
||||||
|
|
||||||
bungeeSystem.enable();
|
bungeeSystem.enable();
|
||||||
|
assertTrue(bungeeSystem.isEnabled());
|
||||||
} finally {
|
} finally {
|
||||||
bungeeSystem.disable();
|
bungeeSystem.disable();
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.djrapitops.plan.system.database.databases.sql.patches;
|
package com.djrapitops.plan.system.database.databases.sql.patches;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.api.exceptions.database.DBOpException;
|
||||||
import com.djrapitops.plan.system.database.databases.DBType;
|
import com.djrapitops.plan.system.database.databases.DBType;
|
||||||
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
|
||||||
@ -150,12 +151,41 @@ public abstract class Patch {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String keyName = getForeignKeyConstraintName(table, referencedTable, referencedColumn);
|
String keyName = getForeignKeyConstraintName(table, referencedTable, referencedColumn);
|
||||||
|
if (keyName == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Uses information from https://stackoverflow.com/a/34574758
|
// Uses information from https://stackoverflow.com/a/34574758
|
||||||
db.execute("ALTER TABLE " + table +
|
db.execute("ALTER TABLE " + table +
|
||||||
" DROP FOREIGN KEY " + keyName);
|
" DROP FOREIGN KEY " + keyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void ensureNoForeignKeyConstraints(String table) {
|
||||||
|
if (dbType != DBType.MYSQL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String keySQL = "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE" +
|
||||||
|
" WHERE REFERENCED_TABLE_SCHEMA = ?" +
|
||||||
|
" AND TABLE_NAME = ?";
|
||||||
|
String keyName = query(new QueryStatement<String>(keySQL) {
|
||||||
|
@Override
|
||||||
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
|
statement.setString(1, db.getConfig().get(DatabaseSettings.MYSQL_DATABASE));
|
||||||
|
statement.setString(2, table);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String processResults(ResultSet set) throws SQLException {
|
||||||
|
if (set.next()) {
|
||||||
|
return set.getString("CONSTRAINT_NAME");
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Verify.isTrue(keyName == null, () -> new DBOpException("Table '" + table + "' has constraint '" + keyName + "'"));
|
||||||
|
}
|
||||||
|
|
||||||
private String getForeignKeyConstraintName(String table, String referencedTable, String referencedColumn) {
|
private String getForeignKeyConstraintName(String table, String referencedTable, String referencedColumn) {
|
||||||
// Uses information from https://stackoverflow.com/a/201678
|
// Uses information from https://stackoverflow.com/a/201678
|
||||||
String keySQL = "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE" +
|
String keySQL = "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE" +
|
||||||
@ -163,7 +193,7 @@ public abstract class Patch {
|
|||||||
" AND TABLE_NAME = ?" +
|
" AND TABLE_NAME = ?" +
|
||||||
" AND REFERENCED_TABLE_NAME = ?" +
|
" AND REFERENCED_TABLE_NAME = ?" +
|
||||||
" AND REFERENCED_COLUMN_NAME = ?";
|
" AND REFERENCED_COLUMN_NAME = ?";
|
||||||
String keyName = query(new QueryStatement<String>(keySQL) {
|
return query(new QueryStatement<String>(keySQL) {
|
||||||
@Override
|
@Override
|
||||||
public void prepare(PreparedStatement statement) throws SQLException {
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
statement.setString(1, db.getConfig().get(DatabaseSettings.MYSQL_DATABASE));
|
statement.setString(1, db.getConfig().get(DatabaseSettings.MYSQL_DATABASE));
|
||||||
@ -181,7 +211,6 @@ public abstract class Patch {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return Verify.nullCheck(keyName, () -> new IllegalArgumentException("No constraint found with " + table + ", " + referencedTable + "." + referencedColumn));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected UUID getServerUUID() {
|
protected UUID getServerUUID() {
|
||||||
|
@ -49,7 +49,10 @@ public class SessionsOptimizationPatch extends Patch {
|
|||||||
dropForeignKey(WorldTimesTable.TABLE_NAME, tableName, Col.ID.get());
|
dropForeignKey(WorldTimesTable.TABLE_NAME, tableName, Col.ID.get());
|
||||||
dropForeignKey(KillsTable.TABLE_NAME, tableName, Col.ID.get());
|
dropForeignKey(KillsTable.TABLE_NAME, tableName, Col.ID.get());
|
||||||
|
|
||||||
|
ensureNoForeignKeyConstraints(tableName);
|
||||||
|
|
||||||
tempOldTable();
|
tempOldTable();
|
||||||
|
|
||||||
db.getSessionsTable().createTable();
|
db.getSessionsTable().createTable();
|
||||||
|
|
||||||
db.execute("INSERT INTO " + tableName + " (" +
|
db.execute("INSERT INTO " + tableName + " (" +
|
||||||
|
@ -46,6 +46,8 @@ public class WorldsOptimizationPatch extends Patch {
|
|||||||
try {
|
try {
|
||||||
dropForeignKey(WorldTimesTable.TABLE_NAME, tableName, Col.ID.get());
|
dropForeignKey(WorldTimesTable.TABLE_NAME, tableName, Col.ID.get());
|
||||||
|
|
||||||
|
ensureNoForeignKeyConstraints(tableName);
|
||||||
|
|
||||||
tempOldTable();
|
tempOldTable();
|
||||||
db.getWorldTable().createTable();
|
db.getWorldTable().createTable();
|
||||||
|
|
||||||
|
@ -40,7 +40,10 @@ public class TimeSetting extends Setting<Long> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long getValueFrom(ConfigNode node) {
|
public Long getValueFrom(ConfigNode node) {
|
||||||
long duration = node.getLong(path);
|
Long duration = node.getLong(path);
|
||||||
|
if (duration == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
String unitName = node.getString(path + ".Unit");
|
String unitName = node.getString(path + ".Unit");
|
||||||
try {
|
try {
|
||||||
TimeUnit unit = TimeUnit.valueOf(unitName.toUpperCase());
|
TimeUnit unit = TimeUnit.valueOf(unitName.toUpperCase());
|
||||||
|
@ -44,21 +44,7 @@ public class ConfigSettingKeyTest {
|
|||||||
@ClassRule
|
@ClassRule
|
||||||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
|
||||||
@Test
|
public static void assertValidDefaultValuesForAllSettings(PlanConfig config, Iterable<Setting> settings) {
|
||||||
public void serverConfigHasValidDefaultValues() throws IOException, IllegalAccessException {
|
|
||||||
PlanConfig planConfig = createConfig("config.yml");
|
|
||||||
Collection<Setting> settings = getServerSettings();
|
|
||||||
hasValidDefaultValuesForAllSettings(planConfig, settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void proxyConfigHasValidDefaultValues() throws IOException, IllegalAccessException {
|
|
||||||
PlanConfig planConfig = createConfig("bungeeconfig.yml");
|
|
||||||
Collection<Setting> settings = getProxySettings();
|
|
||||||
hasValidDefaultValuesForAllSettings(planConfig, settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void hasValidDefaultValuesForAllSettings(PlanConfig config, Iterable<Setting> settings) {
|
|
||||||
List<String> fails = new ArrayList<>();
|
List<String> fails = new ArrayList<>();
|
||||||
for (Setting setting : settings) {
|
for (Setting setting : settings) {
|
||||||
checkSettingForFailures(config, setting).ifPresent(fails::add);
|
checkSettingForFailures(config, setting).ifPresent(fails::add);
|
||||||
@ -66,7 +52,7 @@ public class ConfigSettingKeyTest {
|
|||||||
assertTrue(fails.isEmpty(), fails::toString);
|
assertTrue(fails.isEmpty(), fails::toString);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<String> checkSettingForFailures(PlanConfig config, Setting setting) {
|
private static Optional<String> checkSettingForFailures(PlanConfig config, Setting setting) {
|
||||||
try {
|
try {
|
||||||
if (!config.contains(setting.getPath())) {
|
if (!config.contains(setting.getPath())) {
|
||||||
return Optional.of("Did not contain " + setting.getPath());
|
return Optional.of("Did not contain " + setting.getPath());
|
||||||
@ -79,7 +65,7 @@ public class ConfigSettingKeyTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<Setting> getServerSettings() throws IllegalAccessException {
|
public static Collection<Setting> getServerSettings() throws IllegalAccessException {
|
||||||
List<Setting> settings = new ArrayList<>();
|
List<Setting> settings = new ArrayList<>();
|
||||||
for (Class settingKeyClass : new Class[]{
|
for (Class settingKeyClass : new Class[]{
|
||||||
DatabaseSettings.class,
|
DatabaseSettings.class,
|
||||||
@ -97,7 +83,7 @@ public class ConfigSettingKeyTest {
|
|||||||
return settings;
|
return settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<Setting> getProxySettings() throws IllegalAccessException {
|
public static Collection<Setting> getProxySettings() throws IllegalAccessException {
|
||||||
List<Setting> settings = new ArrayList<>();
|
List<Setting> settings = new ArrayList<>();
|
||||||
for (Class settingKeyClass : new Class[]{
|
for (Class settingKeyClass : new Class[]{
|
||||||
DatabaseSettings.class,
|
DatabaseSettings.class,
|
||||||
@ -128,6 +114,20 @@ public class ConfigSettingKeyTest {
|
|||||||
return settings;
|
return settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void serverConfigHasValidDefaultValues() throws IOException, IllegalAccessException {
|
||||||
|
PlanConfig planConfig = createConfig("config.yml");
|
||||||
|
Collection<Setting> settings = getServerSettings();
|
||||||
|
assertValidDefaultValuesForAllSettings(planConfig, settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void proxyConfigHasValidDefaultValues() throws IOException, IllegalAccessException {
|
||||||
|
PlanConfig planConfig = createConfig("bungeeconfig.yml");
|
||||||
|
Collection<Setting> settings = getProxySettings();
|
||||||
|
assertValidDefaultValuesForAllSettings(planConfig, settings);
|
||||||
|
}
|
||||||
|
|
||||||
private PlanConfig createConfig(String copyDefaultSettingsFrom) throws IOException {
|
private PlanConfig createConfig(String copyDefaultSettingsFrom) throws IOException {
|
||||||
File configFile = temporaryFolder.newFile();
|
File configFile = temporaryFolder.newFile();
|
||||||
TestResources.copyResourceIntoFile(configFile, "/" + copyDefaultSettingsFrom);
|
TestResources.copyResourceIntoFile(configFile, "/" + copyDefaultSettingsFrom);
|
||||||
|
@ -18,8 +18,13 @@ package com.djrapitops.plan;
|
|||||||
|
|
||||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||||
import com.djrapitops.plan.system.PlanSystem;
|
import com.djrapitops.plan.system.PlanSystem;
|
||||||
|
import com.djrapitops.plan.system.settings.ConfigSettingKeyTest;
|
||||||
|
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||||
import com.djrapitops.plan.system.settings.paths.WebserverSettings;
|
import com.djrapitops.plan.system.settings.paths.WebserverSettings;
|
||||||
|
import com.djrapitops.plan.system.settings.paths.key.Setting;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.TemporaryFolder;
|
import org.junit.rules.TemporaryFolder;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -28,6 +33,10 @@ import rules.ComponentMocker;
|
|||||||
import rules.SpongeComponentMocker;
|
import rules.SpongeComponentMocker;
|
||||||
import utilities.RandomData;
|
import utilities.RandomData;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for Sponge PlanSystem.
|
* Test for Sponge PlanSystem.
|
||||||
*
|
*
|
||||||
@ -38,19 +47,41 @@ public class SpongeSystemTest {
|
|||||||
|
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
@ClassRule
|
|
||||||
public static ComponentMocker component = new SpongeComponentMocker(temporaryFolder);
|
@Rule
|
||||||
|
public ComponentMocker component = new SpongeComponentMocker(temporaryFolder);
|
||||||
|
|
||||||
private final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
|
private final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
|
||||||
|
|
||||||
|
private PlanSystem system;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void prepareSpongeSystem() {
|
||||||
|
system = component.getPlanSystem();
|
||||||
|
system.getConfigSystem().getConfig()
|
||||||
|
.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEnable() throws EnableException {
|
public void spongeSystemEnables() throws EnableException {
|
||||||
PlanSystem spongeSystem = component.getPlanSystem();
|
|
||||||
try {
|
try {
|
||||||
spongeSystem.getConfigSystem().getConfig().set(WebserverSettings.PORT, TEST_PORT_NUMBER);
|
system.enable();
|
||||||
spongeSystem.enable();
|
assertTrue(system.isEnabled());
|
||||||
} finally {
|
} finally {
|
||||||
spongeSystem.disable();
|
system.disable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void spongeSystemHasDefaultConfigValuesAfterEnable() throws EnableException, IllegalAccessException {
|
||||||
|
try {
|
||||||
|
system.enable();
|
||||||
|
PlanConfig config = system.getConfigSystem().getConfig();
|
||||||
|
|
||||||
|
Collection<Setting> serverSettings = ConfigSettingKeyTest.getServerSettings();
|
||||||
|
ConfigSettingKeyTest.assertValidDefaultValuesForAllSettings(config, serverSettings);
|
||||||
|
} finally {
|
||||||
|
system.disable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,8 @@ import rules.ComponentMocker;
|
|||||||
import rules.VelocityComponentMocker;
|
import rules.VelocityComponentMocker;
|
||||||
import utilities.RandomData;
|
import utilities.RandomData;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for Velocity PlanSystem.
|
* Test for Velocity PlanSystem.
|
||||||
*
|
*
|
||||||
@ -57,6 +59,7 @@ public class VelocitySystemTest {
|
|||||||
dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile());
|
dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile());
|
||||||
|
|
||||||
velocitySystem.enable();
|
velocitySystem.enable();
|
||||||
|
assertTrue(velocitySystem.isEnabled());
|
||||||
} finally {
|
} finally {
|
||||||
velocitySystem.disable();
|
velocitySystem.disable();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user