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:
Rsl1122 2019-01-04 17:09:17 +02:00
parent 3ad9277c99
commit ff266893d1
9 changed files with 138 additions and 36 deletions

View File

@ -18,9 +18,13 @@ package com.djrapitops.plan;
import com.djrapitops.plan.api.exceptions.EnableException;
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.key.Setting;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
@ -29,6 +33,10 @@ import rules.BukkitComponentMocker;
import rules.ComponentMocker;
import utilities.RandomData;
import java.util.Collection;
import static org.junit.Assert.assertTrue;
/**
* Test for Bukkit PlanSystem.
*
@ -39,20 +47,40 @@ public class BukkitSystemTest {
@ClassRule
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 PlanSystem system;
@Before
public void prepareSystem() {
system = component.getPlanSystem();
system.getConfigSystem().getConfig()
.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
}
@Test
public void testEnable() throws EnableException {
PlanSystem bukkitSystem = component.getPlanSystem();
public void bukkitSystemEnables() throws EnableException {
try {
PlanConfig config = bukkitSystem.getConfigSystem().getConfig();
config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
bukkitSystem.enable();
system.enable();
assertTrue(system.isEnabled());
} 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();
}
}
}

View File

@ -33,6 +33,8 @@ import rules.BungeeComponentMocker;
import rules.ComponentMocker;
import utilities.RandomData;
import static org.junit.Assert.assertTrue;
/**
* Test for Bungee PlanSystem.
*
@ -83,6 +85,7 @@ public class BungeeSystemTest {
dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile());
bungeeSystem.enable();
assertTrue(bungeeSystem.isEnabled());
} finally {
bungeeSystem.disable();
}

View File

@ -16,6 +16,7 @@
*/
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.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
@ -150,12 +151,41 @@ public abstract class Patch {
return;
}
String keyName = getForeignKeyConstraintName(table, referencedTable, referencedColumn);
if (keyName == null) {
return;
}
// Uses information from https://stackoverflow.com/a/34574758
db.execute("ALTER TABLE " + table +
" 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) {
// Uses information from https://stackoverflow.com/a/201678
String keySQL = "SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE" +
@ -163,7 +193,7 @@ public abstract class Patch {
" AND TABLE_NAME = ?" +
" AND REFERENCED_TABLE_NAME = ?" +
" AND REFERENCED_COLUMN_NAME = ?";
String keyName = query(new QueryStatement<String>(keySQL) {
return query(new QueryStatement<String>(keySQL) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
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() {

View File

@ -49,7 +49,10 @@ public class SessionsOptimizationPatch extends Patch {
dropForeignKey(WorldTimesTable.TABLE_NAME, tableName, Col.ID.get());
dropForeignKey(KillsTable.TABLE_NAME, tableName, Col.ID.get());
ensureNoForeignKeyConstraints(tableName);
tempOldTable();
db.getSessionsTable().createTable();
db.execute("INSERT INTO " + tableName + " (" +

View File

@ -46,6 +46,8 @@ public class WorldsOptimizationPatch extends Patch {
try {
dropForeignKey(WorldTimesTable.TABLE_NAME, tableName, Col.ID.get());
ensureNoForeignKeyConstraints(tableName);
tempOldTable();
db.getWorldTable().createTable();

View File

@ -40,7 +40,10 @@ public class TimeSetting extends Setting<Long> {
@Override
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");
try {
TimeUnit unit = TimeUnit.valueOf(unitName.toUpperCase());

View File

@ -44,21 +44,7 @@ public class ConfigSettingKeyTest {
@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
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) {
public static void assertValidDefaultValuesForAllSettings(PlanConfig config, Iterable<Setting> settings) {
List<String> fails = new ArrayList<>();
for (Setting setting : settings) {
checkSettingForFailures(config, setting).ifPresent(fails::add);
@ -66,7 +52,7 @@ public class ConfigSettingKeyTest {
assertTrue(fails.isEmpty(), fails::toString);
}
private Optional<String> checkSettingForFailures(PlanConfig config, Setting setting) {
private static Optional<String> checkSettingForFailures(PlanConfig config, Setting setting) {
try {
if (!config.contains(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<>();
for (Class settingKeyClass : new Class[]{
DatabaseSettings.class,
@ -97,7 +83,7 @@ public class ConfigSettingKeyTest {
return settings;
}
private Collection<Setting> getProxySettings() throws IllegalAccessException {
public static Collection<Setting> getProxySettings() throws IllegalAccessException {
List<Setting> settings = new ArrayList<>();
for (Class settingKeyClass : new Class[]{
DatabaseSettings.class,
@ -128,6 +114,20 @@ public class ConfigSettingKeyTest {
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 {
File configFile = temporaryFolder.newFile();
TestResources.copyResourceIntoFile(configFile, "/" + copyDefaultSettingsFrom);

View File

@ -18,8 +18,13 @@ package com.djrapitops.plan;
import com.djrapitops.plan.api.exceptions.EnableException;
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.key.Setting;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
@ -28,6 +33,10 @@ import rules.ComponentMocker;
import rules.SpongeComponentMocker;
import utilities.RandomData;
import java.util.Collection;
import static org.junit.Assert.assertTrue;
/**
* Test for Sponge PlanSystem.
*
@ -38,19 +47,41 @@ public class SpongeSystemTest {
@ClassRule
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 PlanSystem system;
@Before
public void prepareSpongeSystem() {
system = component.getPlanSystem();
system.getConfigSystem().getConfig()
.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
}
@Test
public void testEnable() throws EnableException {
PlanSystem spongeSystem = component.getPlanSystem();
public void spongeSystemEnables() throws EnableException {
try {
spongeSystem.getConfigSystem().getConfig().set(WebserverSettings.PORT, TEST_PORT_NUMBER);
spongeSystem.enable();
system.enable();
assertTrue(system.isEnabled());
} 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();
}
}
}

View File

@ -30,6 +30,8 @@ import rules.ComponentMocker;
import rules.VelocityComponentMocker;
import utilities.RandomData;
import static org.junit.Assert.assertTrue;
/**
* Test for Velocity PlanSystem.
*
@ -57,6 +59,7 @@ public class VelocitySystemTest {
dbSystem.setActiveDatabase(dbSystem.getSqLiteFactory().usingDefaultFile());
velocitySystem.enable();
assertTrue(velocitySystem.isEnabled());
} finally {
velocitySystem.disable();
}