refactor(test): Refactor DatabaseTest

This commit is contained in:
Christian Koop 2024-03-26 21:09:14 +01:00
parent 2e891f736e
commit 2add5b642f
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3

View File

@ -17,27 +17,12 @@ import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.UUID; import java.util.UUID;
public class DatabaseTest { public class DatabaseTest {
//Create database tests for DataManager
static { static {
// Disable tips and logo for Jooq
System.setProperty("org.jooq.no-tips", "true"); System.setProperty("org.jooq.no-tips", "true");
System.setProperty("org.jooq.no-logo", "true"); System.setProperty("org.jooq.no-logo", "true");
} }
private static boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}
return directoryToBeDeleted.delete();
}
@Test @Test
public void testId() { public void testId() {
File dbDir = new File("./db_test"); File dbDir = new File("./db_test");
@ -116,7 +101,6 @@ public class DatabaseTest {
} }
dataManager.shutdownNow(); dataManager.shutdownNow();
if (dbDir.exists()) { if (dbDir.exists()) {
@ -128,7 +112,7 @@ public class DatabaseTest {
} }
@Test @Test
public static void testConvert() { public void testConvert() {
File dbDir = new File("./db_test"); File dbDir = new File("./db_test");
File logsDir = new File("./logs"); File logsDir = new File("./logs");
@ -161,7 +145,7 @@ public class DatabaseTest {
try { try {
// Export schema // Export schema
DatabaseMetaData meta = sqliteConnection.getMetaData(); DatabaseMetaData meta = sqliteConnection.getMetaData();
ResultSet tables = meta.getTables(null, null, null, new String[]{"TABLE"}); ResultSet tables = meta.getTables(null, null, null, new String[] {"TABLE"});
while (tables.next()) { while (tables.next()) {
String tableName = tables.getString("TABLE_NAME"); String tableName = tables.getString("TABLE_NAME");
@ -263,30 +247,17 @@ public class DatabaseTest {
} }
} }
private static String getTableColumns(Connection sqliteConnection, String tableName) { private static boolean deleteDirectory(File directoryToBeDeleted) {
StringBuilder columns = new StringBuilder(); File[] allContents = directoryToBeDeleted.listFiles();
try { if (allContents != null) {
DatabaseMetaData meta = sqliteConnection.getMetaData(); for (File file : allContents) {
ResultSet rs = meta.getColumns(null, null, tableName, null); deleteDirectory(file);
while (rs.next()) {
String columnName = rs.getString("COLUMN_NAME");
String columnType = rs.getString("TYPE_NAME");
columns.append(columnName).append(" ").append(columnType).append(", ");
} }
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} }
return directoryToBeDeleted.delete();
columns.setLength(columns.length() - 2);
return columns.toString();
} }
private static class DataTestId implements Data { private static class DataTestId implements Data {
private int id; private int id;
private String name; private String name;
private int points; private int points;
@ -337,12 +308,14 @@ public class DatabaseTest {
if (!(obj instanceof DataTestId)) return false; if (!(obj instanceof DataTestId)) return false;
DataTestId other = (DataTestId) obj; DataTestId other = (DataTestId) obj;
return id == other.id && name.equals(other.name) && points == other.points && otherPoints == other.otherPoints; return this.id == other.id &&
this.name.equals(other.name) &&
this.points == other.points &&
this.otherPoints == other.otherPoints;
} }
} }
private static class DataTestUUID implements Data { private static class DataTestUUID implements Data {
private UUID uuid; private UUID uuid;
private String name; private String name;
private int points; private int points;
@ -393,16 +366,19 @@ public class DatabaseTest {
if (!(obj instanceof DataTestUUID)) return false; if (!(obj instanceof DataTestUUID)) return false;
DataTestUUID other = (DataTestUUID) obj; DataTestUUID other = (DataTestUUID) obj;
return uuid.equals(other.uuid) && name.equals(other.name) && points == other.points && otherPoints == other.otherPoints; return this.uuid.equals(other.uuid) &&
this.name.equals(other.name) &&
this.points == other.points &&
this.otherPoints == other.otherPoints;
} }
@Override @Override
public String toString() { public String toString() {
return "DataTestUUID{" + return "DataTestUUID{" +
"uuid=" + uuid + "uuid=" + this.uuid +
", name='" + name + '\'' + ", name='" + this.name + '\'' +
", points=" + points + ", points=" + this.points +
", otherPoints=" + otherPoints + ", otherPoints=" + this.otherPoints +
'}'; '}';
} }
} }