mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-23 00:21:43 +01:00
Tests for SettingsTable
- Fixed ConfigNode equals method - Fixed SettingsTable SQL
This commit is contained in:
parent
c68c95a608
commit
187e4b8ce1
@ -1,7 +1,6 @@
|
||||
package com.djrapitops.plan.system.database.databases.sql.tables;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBInitException;
|
||||
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.ExecStatement;
|
||||
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
|
||||
@ -55,26 +54,22 @@ public class SettingsTable extends Table {
|
||||
* @param config Config of the server.
|
||||
*/
|
||||
public void storeConfig(UUID serverUUID, Config config) {
|
||||
String updateOnDuplicateKeyStatement = "INSERT INTO " + tableName + " (" +
|
||||
Col.SERVER_UUID + ", " +
|
||||
Col.UPDATED + ", " +
|
||||
Col.CONFIG_CONTENT +
|
||||
") VALUES (?, ?, ?)" +
|
||||
" ON DUPLICATE KEY UPDATE" +
|
||||
" " + Col.UPDATED + "=?," +
|
||||
" " + Col.CONFIG_CONTENT + "=?";
|
||||
String replaceIntoStatement = "REPLACE INTO " + tableName + " (" +
|
||||
Col.SERVER_UUID + ", " +
|
||||
Col.UPDATED + ", " +
|
||||
Col.CONFIG_CONTENT +
|
||||
") VALUES (?, ?, ?)";
|
||||
|
||||
String sql = db.getType() == DBType.H2 ? updateOnDuplicateKeyStatement : replaceIntoStatement;
|
||||
|
||||
TextStringBuilder configTextBuilder = new TextStringBuilder();
|
||||
List<String> lines = new ConfigWriter().parseLines(config);
|
||||
configTextBuilder.appendWithSeparators(lines, "\n");
|
||||
String configSettings = configTextBuilder.toString();
|
||||
if (isConfigStored(serverUUID)) {
|
||||
updateConfig(serverUUID, configSettings);
|
||||
} else {
|
||||
insertConfig(serverUUID, configSettings);
|
||||
}
|
||||
}
|
||||
|
||||
private void insertConfig(UUID serverUUID, String configSettings) {
|
||||
String sql = "INSERT INTO " + tableName + " (" +
|
||||
Col.SERVER_UUID + ", " +
|
||||
Col.UPDATED + ", " +
|
||||
Col.CONFIG_CONTENT + ") VALUES (?,?,?)";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
@ -82,11 +77,39 @@ public class SettingsTable extends Table {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
statement.setLong(2, System.currentTimeMillis());
|
||||
statement.setString(3, configSettings);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (db.getType() == DBType.H2) {
|
||||
statement.setLong(4, System.currentTimeMillis());
|
||||
statement.setString(5, configSettings);
|
||||
}
|
||||
private void updateConfig(UUID serverUUID, String configSettings) {
|
||||
String sql = "UPDATE " + tableName + " SET " +
|
||||
Col.CONFIG_CONTENT + "=?," +
|
||||
Col.UPDATED + "=? WHERE " +
|
||||
Col.SERVER_UUID + "=? AND " +
|
||||
Col.CONFIG_CONTENT + "!=?";
|
||||
|
||||
execute(new ExecStatement(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, configSettings);
|
||||
statement.setLong(2, System.currentTimeMillis());
|
||||
statement.setString(3, serverUUID.toString());
|
||||
statement.setString(4, configSettings);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean isConfigStored(UUID serverUUID) {
|
||||
String sql = "SELECT " + Col.SERVER_UUID + " FROM " + tableName + " WHERE " + Col.SERVER_UUID + "=? LIMIT 1";
|
||||
return query(new QueryStatement<Boolean>(sql) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setString(1, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean processResults(ResultSet set) throws SQLException {
|
||||
return set.next() && set.getString(Col.SERVER_UUID.get()).equals(serverUUID.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Configuration utility for storing settings in a .yml file.
|
||||
@ -85,4 +86,16 @@ public class Config extends ConfigNode {
|
||||
public void save() throws IOException {
|
||||
new ConfigWriter(configFilePath).write(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null) return false;
|
||||
return super.equals(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), configFilePath);
|
||||
}
|
||||
}
|
||||
|
@ -327,7 +327,7 @@ public class ConfigNode {
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!(o instanceof ConfigNode)) return false;
|
||||
ConfigNode that = (ConfigNode) o;
|
||||
return Objects.equals(key, that.key) &&
|
||||
nodeOrder.equals(that.nodeOrder) &&
|
||||
@ -340,4 +340,14 @@ public class ConfigNode {
|
||||
public int hashCode() {
|
||||
return Objects.hash(key, childNodes, comment, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConfigNode{" +
|
||||
"key='" + key + '\'' +
|
||||
", nodeOrder=" + nodeOrder +
|
||||
", comment=" + comment +
|
||||
", value='" + value + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -114,4 +114,11 @@ public class PlanConfig extends Config {
|
||||
public WorldAliasSettings getWorldAliasSettings() {
|
||||
return worldAliasSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null) return false;
|
||||
return super.equals(o);
|
||||
}
|
||||
}
|
@ -34,6 +34,8 @@ import com.djrapitops.plan.system.database.databases.sql.H2DB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.tables.*;
|
||||
import com.djrapitops.plan.system.info.server.Server;
|
||||
import com.djrapitops.plan.system.settings.config.Config;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.settings.paths.WebserverSettings;
|
||||
import com.djrapitops.plan.utilities.SHA256Hash;
|
||||
import org.junit.*;
|
||||
@ -1024,4 +1026,29 @@ public abstract class CommonDBTest {
|
||||
assertEquals(userName, result.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configIsStoredInTheDatabase() {
|
||||
PlanConfig config = system.getConfigSystem().getConfig();
|
||||
|
||||
SettingsTable settingsTable = db.getSettingsTable();
|
||||
settingsTable.storeConfig(serverUUID, config);
|
||||
|
||||
Optional<Config> foundConfig = settingsTable.fetchNewerConfig(0, serverUUID);
|
||||
assertTrue(foundConfig.isPresent());
|
||||
assertEquals(config, foundConfig.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unchangedConfigDoesNotUpdateInDatabase() {
|
||||
configIsStoredInTheDatabase();
|
||||
long savedMs = System.currentTimeMillis();
|
||||
|
||||
PlanConfig config = system.getConfigSystem().getConfig();
|
||||
|
||||
SettingsTable settingsTable = db.getSettingsTable();
|
||||
settingsTable.storeConfig(serverUUID, config);
|
||||
|
||||
assertFalse(settingsTable.fetchNewerConfig(savedMs, serverUUID).isPresent());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user