2025-03-10 08:38:21 +01:00
|
|
|
package com.commafeed;
|
|
|
|
|
|
|
|
|
|
import jakarta.enterprise.inject.spi.CDI;
|
2025-06-04 08:35:03 +02:00
|
|
|
import jakarta.persistence.EntityManager;
|
2025-03-10 08:38:21 +01:00
|
|
|
|
2025-06-04 08:35:03 +02:00
|
|
|
import org.hibernate.Session;
|
2025-03-10 08:38:21 +01:00
|
|
|
import org.kohsuke.MetaInfServices;
|
|
|
|
|
|
2026-03-04 13:49:44 +01:00
|
|
|
import io.quarkus.runtime.ShutdownEvent;
|
|
|
|
|
import io.quarkus.runtime.StartupEvent;
|
2025-03-10 08:38:21 +01:00
|
|
|
import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
|
|
|
|
|
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets database between tests
|
|
|
|
|
*/
|
|
|
|
|
@MetaInfServices
|
|
|
|
|
public class DatabaseReset implements QuarkusTestBeforeEachCallback {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void beforeEach(QuarkusTestMethodContext context) {
|
2026-03-04 13:49:44 +01:00
|
|
|
// stop the application to make sure that there are no active transactions when we truncate the tables
|
|
|
|
|
getBean(CommaFeedApplication.class).stop(new ShutdownEvent());
|
|
|
|
|
|
|
|
|
|
// truncate all tables so that we have a clean slate for the next test
|
|
|
|
|
getBean(EntityManager.class).unwrap(Session.class).getSessionFactory().getSchemaManager().truncateMappedObjects();
|
|
|
|
|
|
|
|
|
|
// restart the application
|
|
|
|
|
getBean(CommaFeedApplication.class).start(new StartupEvent());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static <T> T getBean(Class<T> clazz) {
|
|
|
|
|
return CDI.current().select(clazz).get();
|
2025-03-10 08:38:21 +01:00
|
|
|
}
|
|
|
|
|
}
|