mirror of
https://github.com/trainerlord/WorldSystem.git
synced 2025-01-06 18:47:52 +01:00
added abstract database and tests
This commit is contained in:
parent
3b07a23b40
commit
b364b3da74
@ -121,6 +121,8 @@ repositories {
|
||||
dependencies {
|
||||
implementation("org.yaml:snakeyaml:2.2")
|
||||
implementation("net.kyori:adventure-text-minimessage:4.16.0")
|
||||
implementation 'org.xerial:sqlite-jdbc:3.30.1'
|
||||
|
||||
compileOnly lombok()
|
||||
annotationProcessor lombok()
|
||||
//Test dependencies
|
||||
|
@ -1,10 +1,37 @@
|
||||
package de.butzlabben.WorldSystem.data;
|
||||
|
||||
import de.butzlabben.world.config.PluginConfig;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
public abstract class AbstractSqlLiteDatabase {
|
||||
|
||||
public AbstractSqlLiteDatabase() {
|
||||
private Connection connection;
|
||||
private Statement state;
|
||||
|
||||
public AbstractSqlLiteDatabase() throws SQLException {
|
||||
this.connection = DriverManager.getConnection("jdbc:sqlite:" + PluginConfig.getSqliteFile());
|
||||
this.construct_database();
|
||||
}
|
||||
|
||||
public AbstractSqlLiteDatabase(String db_path) throws SQLException {
|
||||
this.connection = DriverManager.getConnection("jdbc:sqlite:" + db_path);
|
||||
this.state = this.connection.createStatement();
|
||||
this.construct_database();
|
||||
}
|
||||
|
||||
protected ResultSet query(String sql) throws SQLException {
|
||||
return this.state.executeQuery(sql);
|
||||
}
|
||||
|
||||
protected abstract void construct_database();
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
this.state.close();
|
||||
this.connection.close();
|
||||
} catch (SQLException e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,28 @@
|
||||
package de.butzlabben.WorldSystem.data;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class MockDatabase extends AbstractSqlLiteDatabase {
|
||||
|
||||
public MockDatabase() throws SQLException {
|
||||
super("./mock.db");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void construct_database() {
|
||||
try {
|
||||
this.query("CREATE TABLE IF NOT EXISTS RealTable (id INTEGER PRIMARY KEY, quantity INTEGER);").close();
|
||||
} catch (SQLException e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
public boolean does_table_exist(String Table_Name) throws SQLException {
|
||||
ResultSet res = this.query(String.format("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = '%s';", Table_Name));
|
||||
res.next();
|
||||
int count = res.getInt("count(*)");
|
||||
System.out.println(count);
|
||||
return (count == 1);
|
||||
}
|
||||
}
|
||||
|
@ -2,13 +2,22 @@ package de.butzlabben.WorldSystem.data;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class TestAbstractSqlLiteDatabase {
|
||||
|
||||
@Test
|
||||
public void testPrintMessage() {
|
||||
System.out.println("Inside testPrintMessage()");
|
||||
assertEquals("", "");
|
||||
public void testContructDatabase() throws SQLException {
|
||||
MockDatabase db = new MockDatabase();
|
||||
assertTrue(db.does_table_exist("RealTable"));
|
||||
db.close();
|
||||
}
|
||||
@Test
|
||||
public void testNonExistantTable() throws SQLException {
|
||||
MockDatabase db = new MockDatabase();
|
||||
assertFalse(db.does_table_exist("NotRealTable"));
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user