From 60d3914769088d32726ef086dc9c700187be66c5 Mon Sep 17 00:00:00 2001 From: Karel Maesen Date: Thu, 26 Aug 2021 23:10:28 +0200 Subject: [PATCH] HHH-14738 Refactor common test setup to base class --- .../spatial/testing/SpatialTestBase.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialTestBase.java diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialTestBase.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialTestBase.java new file mode 100644 index 0000000000..2b213d9240 --- /dev/null +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialTestBase.java @@ -0,0 +1,52 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ + +package org.hibernate.spatial.testing; + +import org.hibernate.spatial.integration.SpatialTestDataProvider; +import org.hibernate.spatial.testing.datareader.TestSupport; +import org.hibernate.spatial.testing.domain.GeomEntity; +import org.hibernate.spatial.testing.domain.JtsGeomEntity; +import org.hibernate.spatial.testing.domain.SpatialDomainModel; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.SessionFactoryScopeAware; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +@DomainModel(modelDescriptorClasses = SpatialDomainModel.class) +abstract public class SpatialTestFactoryBase + extends SpatialTestDataProvider implements SessionFactoryScopeAware { + + protected SessionFactoryScope scope; + + @Override + public void injectSessionFactoryScope(SessionFactoryScope scope) { + this.scope = scope; + } + + @BeforeEach + public void beforeEach() { + scope.inTransaction( session -> super.entities( + JtsGeomEntity.class, + TestSupport.TestDataPurpose.SpatialFunctionsData + ) + .forEach( session::save ) ); + scope.inTransaction( session -> super.entities( + GeomEntity.class, + TestSupport.TestDataPurpose.SpatialFunctionsData + ).forEach( session::save ) ); + } + + @AfterEach + public void cleanup() { + scope.inTransaction( session -> session.createQuery( "delete from GeomEntity" ).executeUpdate() ); + scope.inTransaction( session -> session.createQuery( "delete from JtsGeomEntity" ).executeUpdate() ); + } + +}