mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-10 02:17:41 +01:00
Database preparing test utility
This commit is contained in:
parent
a28e1e0a64
commit
239f087f41
@ -23,7 +23,13 @@ package utilities;
|
|||||||
*/
|
*/
|
||||||
public class CIProperties {
|
public class CIProperties {
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public static final String IS_CI_SERVICE = "TRAVIS";
|
public static final String IS_CI_SERVICE = "TRAVIS";
|
||||||
|
public static final String MYSQL_DATABASE = "MYSQL_DB";
|
||||||
|
// Notice to CI configurer: This user should only have local access.
|
||||||
|
// (To prevent outside connections with CI user)
|
||||||
|
public static final String MYSQL_USER = "MYSQL_USER";
|
||||||
|
public static final String MYSQL_PASS = "MYSQL_PASS";
|
||||||
public static final String CHROME_DRIVER = "CHROMEDRIVER";
|
public static final String CHROME_DRIVER = "CHROMEDRIVER";
|
||||||
|
|
||||||
private CIProperties() {
|
private CIProperties() {
|
||||||
|
101
Plan/common/src/test/java/utilities/DBPreparer.java
Normal file
101
Plan/common/src/test/java/utilities/DBPreparer.java
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of Player Analytics (Plan).
|
||||||
|
*
|
||||||
|
* Plan is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Plan is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package utilities;
|
||||||
|
|
||||||
|
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||||
|
import com.djrapitops.plan.db.DBType;
|
||||||
|
import com.djrapitops.plan.db.Database;
|
||||||
|
import com.djrapitops.plan.db.SQLDB;
|
||||||
|
import com.djrapitops.plan.db.access.transactions.Transaction;
|
||||||
|
import com.djrapitops.plan.system.PlanSystem;
|
||||||
|
import com.djrapitops.plan.system.database.DBSystem;
|
||||||
|
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||||
|
import com.djrapitops.plan.system.settings.paths.DatabaseSettings;
|
||||||
|
import com.djrapitops.plan.system.settings.paths.WebserverSettings;
|
||||||
|
import com.djrapitops.plugin.utilities.Format;
|
||||||
|
import com.djrapitops.plugin.utilities.Verify;
|
||||||
|
import com.google.common.util.concurrent.MoreExecutors;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class DBPreparer {
|
||||||
|
|
||||||
|
private final PlanSystem system;
|
||||||
|
private int testPortNumber;
|
||||||
|
|
||||||
|
public DBPreparer(PlanSystem system, int testPortNumber) {
|
||||||
|
this.system = system;
|
||||||
|
this.testPortNumber = testPortNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Database> prepareSQLite() throws EnableException {
|
||||||
|
String dbName = DBType.SQLITE.getName();
|
||||||
|
return Optional.of(prepareDBByName(dbName));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Database> prepareH2() throws EnableException {
|
||||||
|
String dbName = DBType.H2.getName();
|
||||||
|
return Optional.of(prepareDBByName(dbName));
|
||||||
|
}
|
||||||
|
|
||||||
|
private SQLDB prepareDBByName(String dbName) throws EnableException {
|
||||||
|
PlanConfig config = system.getConfigSystem().getConfig();
|
||||||
|
config.set(WebserverSettings.PORT, testPortNumber);
|
||||||
|
config.set(DatabaseSettings.TYPE, dbName);
|
||||||
|
system.enable();
|
||||||
|
|
||||||
|
DBSystem dbSystem = system.getDatabaseSystem();
|
||||||
|
SQLDB db = (SQLDB) dbSystem.getActiveDatabaseByName(dbName);
|
||||||
|
db.setTransactionExecutorServiceProvider(MoreExecutors::newDirectExecutorService);
|
||||||
|
db.init();
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Database> prepareMySQL() throws EnableException {
|
||||||
|
String database = System.getenv(CIProperties.MYSQL_DATABASE);
|
||||||
|
String user = System.getenv(CIProperties.MYSQL_USER);
|
||||||
|
String pass = System.getenv(CIProperties.MYSQL_PASS);
|
||||||
|
if (Verify.containsNull(database, user, pass)) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt to Prevent SQL Injection with Environment variable.
|
||||||
|
String formattedDatabase = new Format(database)
|
||||||
|
.removeSymbols()
|
||||||
|
.toString();
|
||||||
|
|
||||||
|
String dbName = DBType.MYSQL.getName();
|
||||||
|
|
||||||
|
PlanConfig config = system.getConfigSystem().getConfig();
|
||||||
|
config.set(DatabaseSettings.MYSQL_DATABASE, formattedDatabase);
|
||||||
|
config.set(DatabaseSettings.MYSQL_USER, user);
|
||||||
|
config.set(DatabaseSettings.MYSQL_PASS, pass);
|
||||||
|
config.set(DatabaseSettings.MYSQL_HOST, "127.0.0.1");
|
||||||
|
config.set(DatabaseSettings.TYPE, dbName);
|
||||||
|
|
||||||
|
SQLDB mysql = prepareDBByName(dbName);
|
||||||
|
mysql.executeTransaction(new Transaction() {
|
||||||
|
@Override
|
||||||
|
protected void performOperations() {
|
||||||
|
execute("DROP DATABASE " + formattedDatabase);
|
||||||
|
execute("CREATE DATABASE " + formattedDatabase);
|
||||||
|
execute("USE " + formattedDatabase);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return Optional.of(mysql);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user