diff --git a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/TestDatabaseHelperImpl.java b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/TestDatabaseHelperImpl.java index aa2703b..58ed6b5 100644 --- a/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/TestDatabaseHelperImpl.java +++ b/src/test/java/de/kreth/vaadin/clubhelper/vaadinclubhelper/dao/TestDatabaseHelperImpl.java @@ -2,10 +2,10 @@ package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; +import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.ResultSet; +import java.sql.SQLException; import java.sql.Statement; import java.time.LocalDate; import java.time.ZoneId; @@ -17,9 +17,7 @@ import java.util.function.Consumer; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; -import javax.persistence.Query; -import org.apache.commons.io.IOUtils; import org.hibernate.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -30,28 +28,46 @@ import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubeventHasPerson; import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; @Component -public final class TestDatabaseHelperImpl implements TestDatabaseHelper { +public class TestDatabaseHelperImpl implements TestDatabaseHelper { private static final AtomicInteger eventIdSequence = new AtomicInteger(); - private final String truncateString = createTruncateString(); - @Autowired protected EntityManager entityManager; - private String createTruncateString() { - try { - InputStream resource = getClass().getResourceAsStream("/truncateTables.sql"); - return IOUtils.toString(resource, StandardCharsets.UTF_8); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - @Override public void cleanDatabase() { - Query truncateQuery = entityManager.createNativeQuery(truncateString); - transactional(() -> truncateQuery.executeUpdate()); + + Session session = (Session) entityManager; + session.doWork(conn -> { + + List tableNames = loadTables(conn); + try (Statement stm = conn.createStatement()) { + + stm.addBatch("SET FOREIGN_KEY_CHECKS=0"); + for (String table : tableNames) { + stm.addBatch("TRUNCATE TABLE " + table); + } + stm.addBatch("SET FOREIGN_KEY_CHECKS=1"); + stm.executeBatch(); + } + if (conn.getAutoCommit() == false) { + conn.commit(); + } + }); + } + + public List loadTables(Connection conn) throws SQLException { + List tableNames = new ArrayList<>(); + + DatabaseMetaData meta = conn.getMetaData(); + String[] types = { "TABLE" }; + try (ResultSet tables = meta.getTables(null, null, null, types)) { + while (tables.next()) { + tableNames.add(tables.getString("TABLE_NAME")); + } + } + return tableNames; } /** diff --git a/src/test/resources/truncateTables.sql b/src/test/resources/truncateTables.sql deleted file mode 100644 index 10d0fc4..0000000 --- a/src/test/resources/truncateTables.sql +++ /dev/null @@ -1,21 +0,0 @@ -SET FOREIGN_KEY_CHECKS=0; - --- TRUNCATE TABLE clubevent_addon; --- TRUNCATE TABLE event_has_altersgruppe; -TRUNCATE TABLE altersgruppe; -TRUNCATE TABLE pflichten; -TRUNCATE TABLE clubevent_has_person; -TRUNCATE TABLE ClubEvent; --- TRUNCATE TABLE persongroup; -TRUNCATE TABLE startpass_startrechte; -TRUNCATE TABLE deleted_entries; -TRUNCATE TABLE clubevent; -TRUNCATE TABLE relative; -TRUNCATE TABLE groupdef; -TRUNCATE TABLE startpaesse; -TRUNCATE TABLE contact; -TRUNCATE TABLE adress; -TRUNCATE TABLE attendance; -TRUNCATE TABLE person; - -SET FOREIGN_KEY_CHECKS=1; \ No newline at end of file