HHH-14808 Fix PredicateSmokeTest

Refactors SpatialTestBase to use a SessionFactoryAware
implementation that performs necessary initialisations
(e.g. H2).
This commit is contained in:
Karel Maesen 2021-11-22 19:43:00 +01:00 committed by Steve Ebersole
parent eaea919cf5
commit aded53a760
7 changed files with 83 additions and 83 deletions

View File

@ -10,11 +10,11 @@ package org.hibernate.spatial.integration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.persistence.Query;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.spatial.testing.IsSupportedBySpatial;
import org.hibernate.spatial.testing.SpatialSessionFactoryAware;
import org.hibernate.spatial.testing.datareader.TestDataElement;
import org.hibernate.spatial.testing.domain.GeomEntity;
import org.hibernate.spatial.testing.domain.GeomEntityLike;
@ -25,12 +25,13 @@ import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
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.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import jakarta.persistence.Query;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@ -39,11 +40,9 @@ import static org.junit.jupiter.api.Assertions.assertNull;
* Created by Karel Maesen, Geovise BVBA on 15/02/2018.
*/
@DomainModel(modelDescriptorClasses = SpatialDomainModel.class)
@RequiresDialectFeature( feature = IsSupportedBySpatial.class)
@RequiresDialectFeature(feature = IsSupportedBySpatial.class)
@SessionFactory
public class StoreAndRetrieveTests extends SpatialTestDataProvider implements SessionFactoryScopeAware {
SessionFactoryScope scope;
public class StoreAndRetrieveTests extends SpatialSessionFactoryAware {
private Map<Integer, Object> stored = new HashMap<>();
@ -52,14 +51,14 @@ public class StoreAndRetrieveTests extends SpatialTestDataProvider implements Se
public void testStoringGeomEntity(final Class entityClass) {
//check whether we retrieve exactly what we store
scope.inTransaction( session -> storeTestObjects ( session, entityClass) );
scope.inTransaction( session -> storeTestObjects( session, entityClass ) );
scope.inTransaction( session -> retrieveAndCompare( session, entityClass ) );
}
@AfterEach
public void cleanTables(SessionFactoryScope scope) {
scope.inTransaction( session -> session.createQuery( "delete from GeomEntity").executeUpdate() );
scope.inTransaction( session -> session.createQuery( "delete from JtsGeomEntity").executeUpdate() );
scope.inTransaction( session -> session.createQuery( "delete from GeomEntity" ).executeUpdate() );
scope.inTransaction( session -> session.createQuery( "delete from JtsGeomEntity" ).executeUpdate() );
}
@SuppressWarnings("unchecked")
@ -111,8 +110,5 @@ public class StoreAndRetrieveTests extends SpatialTestDataProvider implements Se
assertNull( entity.getGeom() );
}
@Override
public void injectSessionFactoryScope(SessionFactoryScope scope) {
this.scope = scope;
}
}

View File

@ -23,13 +23,9 @@ import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;
import org.geolatte.geom.G2D;
import org.geolatte.geom.Geometry;
import static org.geolatte.geom.builder.DSL.g;
import static org.geolatte.geom.builder.DSL.polygon;
import static org.geolatte.geom.builder.DSL.ring;
import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84;
@RequiresDialectFeature(feature = IsSupportedBySpatial.class)
@SessionFactory

View File

@ -11,14 +11,13 @@ import java.util.List;
import org.hibernate.spatial.predicate.GeolatteSpatialPredicates;
import org.hibernate.spatial.testing.IsSupportedBySpatial;
import org.hibernate.spatial.testing.SpatialSessionFactoryAware;
import org.hibernate.spatial.testing.domain.GeomEntity;
import org.hibernate.spatial.testing.domain.SpatialDomainModel;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.SessionFactoryScopeAware;
import org.junit.jupiter.api.Test;
import jakarta.persistence.criteria.CriteriaBuilder;
@ -33,11 +32,10 @@ import static org.geolatte.geom.builder.DSL.ring;
import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84;
@DomainModel(modelDescriptorClasses = SpatialDomainModel.class)
@RequiresDialectFeature(feature = IsSupportedBySpatial.class)
@SessionFactory
public class PredicateSmokeTest implements SessionFactoryScopeAware {
@RequiresDialectFeature(feature = IsSupportedBySpatial.class)
public class PredicateSmokeTest extends SpatialSessionFactoryAware {
private SessionFactoryScope scope;
Polygon<G2D> poly = polygon(
WGS84,
ring( g( 0, 0 ), g( 0, 10 ), g( 10, 10 ), g( 10, 0 ), g( 0, 0 ) )
@ -56,9 +54,4 @@ public class PredicateSmokeTest implements SessionFactoryScopeAware {
} );
}
@Override
public void injectSessionFactoryScope(SessionFactoryScope scope) {
this.scope = scope;
}
}

View File

@ -12,10 +12,6 @@ import java.lang.reflect.Method;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Root;
import org.hibernate.spatial.integration.Model;
import org.hibernate.spatial.predicate.GeolatteSpatialPredicates;
@ -31,13 +27,11 @@ import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;
import org.junit.platform.commons.JUnitException;
import org.geolatte.geom.G2D;
import org.geolatte.geom.Polygon;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.Root;
import static org.geolatte.geom.builder.DSL.g;
import static org.geolatte.geom.builder.DSL.polygon;
import static org.geolatte.geom.builder.DSL.ring;
import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@ -0,0 +1,60 @@
/*
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.spatial.testing;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Set;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.spatial.CommonSpatialFunction;
import org.hibernate.spatial.integration.SpatialTestDataProvider;
import org.hibernate.testing.orm.junit.DialectContext;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.SessionFactoryScopeAware;
import org.h2gis.functions.factory.H2GISFunctions;
public abstract class SpatialSessionFactoryAware extends SpatialTestDataProvider implements SessionFactoryScopeAware {
protected SessionFactoryScope scope;
protected Set<String> supportedFunctions;
@Override
public void injectSessionFactoryScope(SessionFactoryScope scope) {
this.scope = scope;
//scope is set to null during test cleanup
if ( scope != null ) {
this.supportedFunctions = scope.getSessionFactory()
.getQueryEngine()
.getSqmFunctionRegistry()
.getFunctions()
.keySet();
if ( DialectContext.getDialect() instanceof H2Dialect ) {
initH2GISExtensionsForInMemDb();
}
}
}
public boolean isSupported(CommonSpatialFunction function) {
return supportedFunctions.contains( function.name() );
}
protected void initH2GISExtensionsForInMemDb() {
this.scope.inSession( session -> {
try {
Connection cn = session.getJdbcConnectionAccess().obtainConnection();
H2GISFunctions.load( cn );
}
catch (SQLException e) {
throw new RuntimeException( e );
}
} );
}
}

View File

@ -7,13 +7,7 @@
package org.hibernate.spatial.testing;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Set;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.spatial.CommonSpatialFunction;
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;
@ -22,37 +16,15 @@ import org.hibernate.spatial.testing.domain.SpatialDomainModel;
import org.hibernate.testing.orm.junit.DialectContext;
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;
import org.h2gis.functions.factory.H2GISFunctions;
@DomainModel(modelDescriptorClasses = SpatialDomainModel.class)
abstract public class SpatialTestBase
extends SpatialTestDataProvider implements SessionFactoryScopeAware {
protected SessionFactoryScope scope;
protected Set<String> supportedFunctions;
extends SpatialSessionFactoryAware {
public abstract TestSupport.TestDataPurpose purpose();
@Override
public void injectSessionFactoryScope(SessionFactoryScope scope) {
this.scope = scope;
//scope is set to null during test cleanup
if ( scope != null ) {
this.supportedFunctions = scope.getSessionFactory()
.getQueryEngine()
.getSqmFunctionRegistry()
.getFunctions()
.keySet();
if ( DialectContext.getDialect() instanceof H2Dialect ) {
initH2GISExtensionsForInMemDb();
}
}
}
@BeforeEach
public void beforeEach() {
scope.inTransaction( session -> super.entities(
@ -72,21 +44,5 @@ abstract public class SpatialTestBase
scope.inTransaction( session -> session.createQuery( "delete from JtsGeomEntity" ).executeUpdate() );
}
public boolean isSupported(CommonSpatialFunction function) {
return supportedFunctions.contains( function.name() );
}
private void initH2GISExtensionsForInMemDb() {
this.scope.inSession( session -> {
try {
Connection cn = session.getJdbcConnectionAccess().obtainConnection();
H2GISFunctions.load( cn );
}
catch (SQLException e) {
throw new RuntimeException( e );
}
} );
}
}

View File

@ -52,6 +52,11 @@ public class PredicateRegexes {
"select .* from .* where st_within\\(.*geom\\s*,.s*?\\)\\s*=\\s*?.*"
);
add(
"intersects",
"select .* from .* where st_intersects\\(.*geom\\s*,.*" + geomFromTextFunction + "\\(.*\\)\\s*=.*",
"select .* from .* where st_intersects\\(.*geom\\s*,.s*?\\)\\s*=\\s*?.*"
);
add(
"eq",
"select .* from .* where st_equals\\(.*geom\\s*,.*" + geomFromTextFunction + "\\(.*\\)\\s*=.*",