From b364b3da74923ac787a3ec67733dc6b4e5fb9e0f Mon Sep 17 00:00:00 2001 From: danielb Date: Thu, 23 May 2024 17:00:02 +0000 Subject: [PATCH] added abstract database and tests --- build.gradle | 2 ++ mock.db | Bin 0 -> 16384 bytes .../data/AbstractSqlLiteDatabase.java | 29 +++++++++++++++++- .../WorldSystem/data/MockDatabase.java | 24 +++++++++++++++ .../data/TestAbstractSqlLiteDatabase.java | 17 +++++++--- 5 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 mock.db diff --git a/build.gradle b/build.gradle index 15a4d82..ba6a1b7 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/mock.db b/mock.db new file mode 100644 index 0000000000000000000000000000000000000000..fe94be8d93abf6a92594bc3220932b76c241d270 GIT binary patch literal 16384 zcmeI#&r8EF6u|Lh9qb^p-gekw@903hc=Tc?L56c^>}8%7u|lEs7ZLQhf5gAdf5HF2 zZnVXE5C)#gHxQEMg_rcxUf%W9EcJ=pKfdJ7%aPhsTB{QgrBqP#Zqcju`&n0kKgK{E z9)ICA ziDDyhG?^Kxkddi$5-}h5!NxAb#o1-=1W CrD1mf literal 0 HcmV?d00001 diff --git a/src/main/java/de/butzlabben/WorldSystem/data/AbstractSqlLiteDatabase.java b/src/main/java/de/butzlabben/WorldSystem/data/AbstractSqlLiteDatabase.java index 86bcacb..c3e683a 100644 --- a/src/main/java/de/butzlabben/WorldSystem/data/AbstractSqlLiteDatabase.java +++ b/src/main/java/de/butzlabben/WorldSystem/data/AbstractSqlLiteDatabase.java @@ -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) { + + } + } } diff --git a/src/test/java/de/butzlabben/WorldSystem/data/MockDatabase.java b/src/test/java/de/butzlabben/WorldSystem/data/MockDatabase.java index dd89978..16ebd8c 100644 --- a/src/test/java/de/butzlabben/WorldSystem/data/MockDatabase.java +++ b/src/test/java/de/butzlabben/WorldSystem/data/MockDatabase.java @@ -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); + } } diff --git a/src/test/java/de/butzlabben/WorldSystem/data/TestAbstractSqlLiteDatabase.java b/src/test/java/de/butzlabben/WorldSystem/data/TestAbstractSqlLiteDatabase.java index 5fca3c3..07d289a 100644 --- a/src/test/java/de/butzlabben/WorldSystem/data/TestAbstractSqlLiteDatabase.java +++ b/src/test/java/de/butzlabben/WorldSystem/data/TestAbstractSqlLiteDatabase.java @@ -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(); } }