diff --git a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/AltibaseDialectTestCase.java b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/AltibaseDialectTestCase.java index a7d1b960ed43..3361af7c0c1f 100644 --- a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/AltibaseDialectTestCase.java +++ b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/AltibaseDialectTestCase.java @@ -4,63 +4,62 @@ */ package org.hibernate.community.dialect; -import java.util.Locale; - import org.hibernate.dialect.DatabaseVersion; import org.hibernate.dialect.Dialect; import org.hibernate.orm.test.dialect.LimitQueryOptions; import org.hibernate.query.spi.Limit; +import org.hibernate.testing.orm.junit.BaseUnitTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Locale; -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; /** * Unit test of the behavior of the AltibaseDialect utility methods * * @author Geoffrey Park */ -public class AltibaseDialectTestCase extends BaseUnitTestCase { +@BaseUnitTest +public class AltibaseDialectTestCase { private Dialect dialect; - @Before + @BeforeEach public void setUp() { - dialect = new AltibaseDialect( DatabaseVersion.make( 7, 3 )); + dialect = new AltibaseDialect( DatabaseVersion.make( 7, 3 ) ); } - @After + @AfterEach public void tearDown() { dialect = null; } @Test public void testSupportLimits() { - assertTrue(dialect.getLimitHandler().supportsLimit()); + assertThat( dialect.getLimitHandler().supportsLimit() ).isTrue(); } @Test public void testSelectWithLimitOnly() { - assertEquals( "select c1, c2 from t1 order by c1, c2 desc limit ?", - withLimit("select c1, c2 from t1 order by c1, c2 desc", - toRowSelection( 0, 15 ) ).toLowerCase( Locale.ROOT)); + assertThat( withLimit( "select c1, c2 from t1 order by c1, c2 desc", + toRowSelection( 0, 15 ) ).toLowerCase( Locale.ROOT ) ) + .isEqualTo( "select c1, c2 from t1 order by c1, c2 desc limit ?" ); } @Test public void testSelectWithOffsetLimit() { - assertEquals( "select c1, c2 from t1 order by c1, c2 desc limit 1+?,?", - withLimit("select c1, c2 from t1 order by c1, c2 desc", - toRowSelection( 5, 15 ) ).toLowerCase(Locale.ROOT)); + assertThat( withLimit( "select c1, c2 from t1 order by c1, c2 desc", + toRowSelection( 5, 15 ) ).toLowerCase( Locale.ROOT ) ) + .isEqualTo( "select c1, c2 from t1 order by c1, c2 desc limit 1+?,?" ); } @Test public void testSelectWithNoLimit() { - assertEquals( "select c1, c2 from t1 order by c1, c2 desc", - withLimit( "select c1, c2 from t1 order by c1, c2 desc", - null ).toLowerCase(Locale.ROOT)); + assertThat( withLimit( "select c1, c2 from t1 order by c1, c2 desc", null ).toLowerCase( Locale.ROOT ) ) + .isEqualTo( "select c1, c2 from t1 order by c1, c2 desc" ); } private String withLimit(String sql, Limit limit) { diff --git a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/CommunityDialectFactoryTest.java b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/CommunityDialectFactoryTest.java index b5fcc2508230..89ed72330df8 100644 --- a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/CommunityDialectFactoryTest.java +++ b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/CommunityDialectFactoryTest.java @@ -4,32 +4,34 @@ */ package org.hibernate.community.dialect; -import java.util.HashMap; - import org.hibernate.boot.registry.BootstrapServiceRegistry; import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.dialect.Dialect; import org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl; import org.hibernate.engine.jdbc.dialect.spi.DialectResolver; import org.hibernate.orm.test.dialect.resolver.TestingDialectResolutionInfo; import org.hibernate.service.spi.ServiceRegistryImplementor; - -import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.orm.junit.BaseUnitTest; import org.hibernate.testing.util.ServiceRegistryUtil; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import java.util.HashMap; + +import static org.assertj.core.api.Assertions.assertThat; /** * @author Steve Ebersole */ -public class CommunityDialectFactoryTest extends BaseUnitTestCase { +@BaseUnitTest +public class CommunityDialectFactoryTest { private StandardServiceRegistry registry; private DialectFactoryImpl dialectFactory; - @Before + @BeforeEach public void setUp() { final BootstrapServiceRegistry bootReg = new BootstrapServiceRegistryBuilder().applyClassLoader( CommunityDialectFactoryTest.class.getClassLoader() @@ -40,6 +42,13 @@ public void setUp() { dialectFactory.injectServices( (ServiceRegistryImplementor) registry ); } + @AfterEach + public void tearDown() { + if ( registry != null ) { + StandardServiceRegistryBuilder.destroy( registry ); + } + } + @Test public void testPreregisteredDialects() { DialectResolver resolver = new CommunityDialectResolver(); @@ -77,8 +86,9 @@ private void testDetermination( dialectFactory.setDialectResolver( resolver ); Dialect resolved = dialectFactory.buildDialect( new HashMap<>(), - () -> TestingDialectResolutionInfo.forDatabaseInfo( databaseName, driverName, majorVersion, minorVersion ) + () -> TestingDialectResolutionInfo.forDatabaseInfo( databaseName, driverName, majorVersion, + minorVersion ) ); - assertEquals( expected, resolved.getClass() ); + assertThat( resolved.getClass() ).isEqualTo( expected ); } } diff --git a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/CommunityDialectSelectorTest.java b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/CommunityDialectSelectorTest.java index 9167514d611f..9eb9234d158a 100644 --- a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/CommunityDialectSelectorTest.java +++ b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/CommunityDialectSelectorTest.java @@ -5,9 +5,10 @@ package org.hibernate.community.dialect; import org.hibernate.dialect.Dialect; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Assert; -import org.junit.Test; public class CommunityDialectSelectorTest { @@ -36,8 +37,8 @@ private void testDialectNamingResolution(final Class dialectClass) { simpleName = simpleName.substring( 0, simpleName.length() - "Dialect".length() ); } Class aClass = strategySelector.resolve( simpleName ); - Assert.assertNotNull( aClass ); - Assert.assertEquals( dialectClass, aClass ); + assertThat( aClass ).isNotNull(); + assertThat( aClass ).isEqualTo( dialectClass ); } } diff --git a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DB2LegacyDialectTestCase.java b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DB2LegacyDialectTestCase.java index 0f449d6bef7f..4dbe2fcb39ff 100644 --- a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DB2LegacyDialectTestCase.java +++ b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DB2LegacyDialectTestCase.java @@ -4,20 +4,18 @@ */ package org.hibernate.community.dialect; -import java.sql.Types; - import org.hibernate.engine.jdbc.Size; import org.hibernate.orm.test.dialect.LimitQueryOptions; import org.hibernate.query.spi.Limit; +import org.hibernate.testing.orm.junit.BaseUnitTest; +import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.type.spi.TypeConfiguration; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import org.hibernate.testing.orm.junit.JiraKey; -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.junit.Before; -import org.junit.Test; +import java.sql.Types; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * DB2 dialect related test cases @@ -25,11 +23,12 @@ * @author Hardy Ferentschik */ -public class DB2LegacyDialectTestCase extends BaseUnitTestCase { +@BaseUnitTest +public class DB2LegacyDialectTestCase { private final DB2LegacyDialect dialect = new DB2LegacyDialect(); private TypeConfiguration typeConfiguration; - @Before + @BeforeEach public void setup() { typeConfiguration = new TypeConfiguration(); dialect.contributeTypes( () -> typeConfiguration, null ); @@ -39,52 +38,42 @@ public void setup() { @JiraKey(value = "HHH-6866") public void testGetDefaultBinaryTypeName() { String actual = typeConfiguration.getDdlTypeRegistry().getTypeName( Types.BINARY, dialect ); - assertEquals( - "The default column length is 255, but char length on DB2 is limited to 254", - "varchar($l) for bit data", - actual - ); + assertThat( actual ) + .describedAs( "The default column length is 255, but char length on DB2 is limited to 254" ) + .isEqualTo( "varchar($l) for bit data" ); } @Test @JiraKey(value = "HHH-6866") public void testGetExplicitBinaryTypeName() { // lower bound - String actual = typeConfiguration.getDdlTypeRegistry().getTypeName( Types.BINARY, Size.length( 1) ); - assertEquals( - "Wrong binary type", - "char(1) for bit data", - actual - ); + String actual = typeConfiguration.getDdlTypeRegistry().getTypeName( Types.BINARY, Size.length( 1 ) ); + assertThat( actual ) + .describedAs( "Wrong binary type" ) + .isEqualTo( "char(1) for bit data" ); // upper bound - actual = typeConfiguration.getDdlTypeRegistry().getTypeName( Types.BINARY, Size.length( 254) ); - assertEquals( - "Wrong binary type. 254 is the max length in DB2", - "char(254) for bit data", - actual - ); + actual = typeConfiguration.getDdlTypeRegistry().getTypeName( Types.BINARY, Size.length( 254 ) ); + assertThat( actual ) + .describedAs( "Wrong binary type. 254 is the max length in DB2" ) + .isEqualTo( "char(254) for bit data" ); // exceeding upper bound - actual = typeConfiguration.getDdlTypeRegistry().getTypeName( Types.BINARY, Size.length( 255) ); - assertEquals( - "Wrong binary type. Should be varchar for length > 254", - "varchar(255) for bit data", - actual - ); + actual = typeConfiguration.getDdlTypeRegistry().getTypeName( Types.BINARY, Size.length( 255 ) ); + assertThat( actual ) + .describedAs( "Wrong binary type. Should be varchar for length > 254" ) + .isEqualTo( "varchar(255) for bit data" ); } @Test @JiraKey(value = "HHH-12369") public void testIntegerOverflowForMaxResults() { Limit rowSelection = new Limit(); - rowSelection.setFirstRow(1); - rowSelection.setMaxRows(Integer.MAX_VALUE); + rowSelection.setFirstRow( 1 ); + rowSelection.setMaxRows( Integer.MAX_VALUE ); String sql = dialect.getLimitHandler().processSql( "select a.id from tbl_a a order by a.id", -1, null, new LimitQueryOptions( rowSelection ) ); - assertTrue( - "Integer overflow for max rows in: " + sql, - sql.contains("fetch first 2147483647 rows only") - ); + assertThat( sql ).describedAs( "Integer overflow for max rows in: " + sql ) + .contains( "fetch first 2147483647 rows only" ); } } diff --git a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyDialectTestCase.java b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyDialectTestCase.java index a6678e672bb5..aa8e9ac6e35d 100644 --- a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyDialectTestCase.java +++ b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyDialectTestCase.java @@ -4,25 +4,22 @@ */ package org.hibernate.community.dialect; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import jakarta.persistence.UniqueConstraint; import org.hibernate.orm.test.dialect.LimitQueryOptions; import org.hibernate.query.spi.Limit; - -import static org.junit.Assert.assertEquals; - import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.RequiresDialect; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import jakarta.persistence.Table; -import jakarta.persistence.UniqueConstraint; +import static org.assertj.core.api.Assertions.assertThat; /** * Testing of patched support for Derby limit and offset queries; see HHH-3972 @@ -34,18 +31,18 @@ public class DerbyDialectTestCase { @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitClause(SessionFactoryScope scope) { final int limit = 50; final String input = "select * from tablename t where t.cat = 5"; final String expected = "select * from tablename t where t.cat = 5 fetch first ? rows only"; final String actual = withLimit( input, toRowSelection( 0, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitWithOffsetClause(SessionFactoryScope scope) { final int limit = 50; final int offset = 200; @@ -53,11 +50,11 @@ public void testInsertLimitWithOffsetClause(SessionFactoryScope scope) { final String expected = "select * from tablename t where t.cat = 5 offset ? rows fetch next ? rows only"; final String actual = withLimit( input, toRowSelection( offset, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitWithForUpdateClause(SessionFactoryScope scope) { final int limit = 50; final int offset = 200; @@ -65,11 +62,11 @@ public void testInsertLimitWithForUpdateClause(SessionFactoryScope scope) { final String expected = "select c11 as col1, c12 as col2, c13 as col13 from t1 offset ? rows fetch next ? rows only for update of c11, c13"; final String actual = withLimit( input, toRowSelection( offset, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitWithWithClause(SessionFactoryScope scope) { final int limit = 50; final int offset = 200; @@ -77,11 +74,11 @@ public void testInsertLimitWithWithClause(SessionFactoryScope scope) { final String expected = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' offset ? rows fetch next ? rows only with rr"; final String actual = withLimit( input, toRowSelection( offset, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitWithForUpdateAndWithClauses(SessionFactoryScope scope) { final int limit = 50; final int offset = 200; @@ -89,14 +86,19 @@ public void testInsertLimitWithForUpdateAndWithClauses(SessionFactoryScope scope final String expected = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' offset ? rows fetch next ? rows only for update of c11,c13 with rr"; final String actual = withLimit( input, toRowSelection( offset, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } - @RequiresDialect( DerbyDialect.class ) - @Test void testInsertConflictOnConstraintDoNothing(SessionFactoryScope scope) { + + @RequiresDialect(DerbyDialect.class) + @Test + void testInsertConflictOnConstraintDoNothing(SessionFactoryScope scope) { scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); - scope.inTransaction( s -> s.persist(new Constrained())); - scope.inTransaction( s -> s.createMutationQuery("insert into Constrained(id, name, count) values (4,'Gavin',69) on conflict on constraint count_name_key do nothing").executeUpdate()); - scope.inSession( s -> Assertions.assertEquals( 69, s.createSelectionQuery( "select count from Constrained", int.class).getSingleResult())); + scope.inTransaction( s -> s.persist( new Constrained() ) ); + scope.inTransaction( s -> s.createMutationQuery( + "insert into Constrained(id, name, count) values (4,'Gavin',69) on conflict on constraint count_name_key do nothing" ) + .executeUpdate() ); + scope.inSession( s -> Assertions.assertEquals( 69, + s.createSelectionQuery( "select count from Constrained", int.class ).getSingleResult() ) ); } private String withLimit(String sql, Limit limit) { @@ -111,7 +113,7 @@ private Limit toRowSelection(int firstRow, int maxRows) { } @Entity(name = "Constrained") - @Table(uniqueConstraints = @UniqueConstraint(name = "count_name_key", columnNames = {"count","name"})) + @Table(uniqueConstraints = @UniqueConstraint(name = "count_name_key", columnNames = {"count", "name"})) static class Constrained { @Id @GeneratedValue diff --git a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyJpaTckUsageTest.java b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyJpaTckUsageTest.java index 1aa41aa1b25d..3815393ca376 100644 --- a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyJpaTckUsageTest.java +++ b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyJpaTckUsageTest.java @@ -4,34 +4,31 @@ */ package org.hibernate.community.dialect; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.List; - +import jakarta.persistence.StoredProcedureQuery; import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess; import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; import org.hibernate.orm.test.jpa.procedure.User; - +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.orm.junit.Jpa; import org.hibernate.testing.orm.junit.RequiresDialect; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import jakarta.persistence.EntityManager; -import jakarta.persistence.StoredProcedureQuery; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; /** * Tests various JPA usage scenarios for performing stored procedures. Inspired by the awesomely well-done JPA TCK @@ -39,187 +36,140 @@ * @author Steve Ebersole */ @RequiresDialect(DerbyDialect.class) -public class DerbyJpaTckUsageTest extends BaseEntityManagerFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ User.class}; - } - - @Test - public void testMultipleGetUpdateCountCalls() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - - try { - StoredProcedureQuery query = em.createStoredProcedureQuery( "findOneUser" ); - // this is what the TCK attempts to do, don't shoot the messenger... - query.getUpdateCount(); - // yep, twice - query.getUpdateCount(); +@Jpa( + annotatedClasses = { + User.class } - finally { - em.getTransaction().commit(); - em.close(); - } - } +) +public class DerbyJpaTckUsageTest { @Test - public void testBasicScalarResults() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - - try { - StoredProcedureQuery query = em.createStoredProcedureQuery( "findOneUser" ); - boolean isResult = query.execute(); - assertTrue( isResult ); - int updateCount = query.getUpdateCount(); - - boolean results = false; - do { - List list = query.getResultList(); - assertEquals( 1, list.size() ); - - results = query.hasMoreResults(); - // and it only sets the updateCount once lol - } while ( results || updateCount != -1); - } - finally { - em.getTransaction().commit(); - em.close(); - } + public void testMultipleGetUpdateCountCalls(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "findOneUser" ); + // this is what the TCK attempts to do, don't shoot the messenger... + query.getUpdateCount(); + // yep, twice + query.getUpdateCount(); + } + ); } @Test - @FailureExpected( jiraKey = "HHH-8416", reason = "JPA TCK challenge" ) - public void testHasMoreResultsHandlingTckChallenge() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - - try { - StoredProcedureQuery query = em.createStoredProcedureQuery( "findOneUser", User.class ); - assertTrue( query.execute() ); - assertTrue( query.hasMoreResults() ); - query.getResultList(); - assertFalse( query.hasMoreResults() ); - } - finally { - em.getTransaction().commit(); - em.close(); - } + public void testBasicScalarResults(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + StoredProcedureQuery query = em.createStoredProcedureQuery( "findOneUser" ); + boolean isResult = query.execute(); + assertThat( isResult ).isTrue(); + int updateCount = query.getUpdateCount(); + + boolean results = false; + do { + List list = query.getResultList(); + assertThat( list ).hasSize( 1 ); + + results = query.hasMoreResults(); + // and it only sets the updateCount once lol + } + while ( results || updateCount != -1 ); + } + ); } @Test - public void testHasMoreResultsHandling() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - - try { - StoredProcedureQuery query = em.createStoredProcedureQuery( "findOneUser", User.class ); - assertTrue( query.execute() ); - query.getResultList(); - assertFalse( query.hasMoreResults() ); - } - finally { - em.getTransaction().commit(); - em.close(); - } + @FailureExpected(jiraKey = "HHH-8416", reason = "JPA TCK challenge") + public void testHasMoreResultsHandlingTckChallenge(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + StoredProcedureQuery query = em.createStoredProcedureQuery( "findOneUser", User.class ); + assertThat( query.execute() ).isTrue(); + assertThat( query.hasMoreResults() ).isTrue(); + query.getResultList(); + assertThat( query.hasMoreResults() ).isFalse(); + } + ); } @Test - public void testResultClassHandling() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - - try { - StoredProcedureQuery query = em.createStoredProcedureQuery( "findOneUser", User.class ); - boolean isResult = query.execute(); - assertTrue( isResult ); - int updateCount = query.getUpdateCount(); - - boolean results = false; - do { - List list = query.getResultList(); - assertEquals( 1, list.size() ); - assertTyping( User.class, list.get( 0 ) ); - - results = query.hasMoreResults(); - // and it only sets the updateCount once lol - } while ( results || updateCount != -1); - } - finally { - em.getTransaction().commit(); - em.close(); - } + public void testHasMoreResultsHandling(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + StoredProcedureQuery query = em.createStoredProcedureQuery( "findOneUser", User.class ); + assertThat( query.execute() ).isTrue(); + query.getResultList(); + assertThat( query.hasMoreResults() ).isFalse(); + } + ); } @Test - public void testSettingInParamDefinedOnNamedStoredProcedureQuery() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - try { - StoredProcedureQuery query = em.createNamedStoredProcedureQuery( "positional-param" ); - query.setParameter( 1, 1 ); - } - finally { - em.getTransaction().commit(); - em.close(); - } + public void testResultClassHandling(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + StoredProcedureQuery query = em.createStoredProcedureQuery( "findOneUser", User.class ); + boolean isResult = query.execute(); + assertThat( isResult ).isTrue(); + int updateCount = query.getUpdateCount(); + + boolean results = false; + do { + List list = query.getResultList(); + assertThat( list ).hasSize( 1 ); + assertTyping( User.class, list.get( 0 ) ); + + results = query.hasMoreResults(); + // and it only sets the updateCount once lol + } + while ( results || updateCount != -1 ); + } + ); } @Test - public void testSettingNonExistingParams() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - - try { - // non-existing positional param - try { - StoredProcedureQuery query = em.createNamedStoredProcedureQuery( "positional-param" ); - query.setParameter( 99, 1 ); - fail( "Expecting an exception" ); - } - catch (IllegalArgumentException expected) { - // this is the expected condition - } - - // non-existing named param - try { - StoredProcedureQuery query = em.createNamedStoredProcedureQuery( "positional-param" ); - query.setParameter( "does-not-exist", 1 ); - fail( "Expecting an exception" ); - } - catch (IllegalArgumentException expected) { - // this is the expected condition - } - } - finally { - em.getTransaction().commit(); - em.close(); - } + public void testSettingInParamDefinedOnNamedStoredProcedureQuery(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + StoredProcedureQuery query = em.createNamedStoredProcedureQuery( "positional-param" ); + query.setParameter( 1, 1 ); + } + ); } @Test - @FailureExpected( jiraKey = "HHH-8395", reason = "Out of the frying pan into the fire: https://issues.apache.org/jira/browse/DERBY-211" ) - public void testExecuteUpdate() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - - try { - StoredProcedureQuery query = em.createStoredProcedureQuery( "deleteAllUsers" ); - int count = query.executeUpdate(); - // this fails because the Derby EmbeddedDriver is returning zero here rather than the actual updateCount :( - // https://issues.apache.org/jira/browse/DERBY-211 - assertEquals( 1, count ); - } - finally { - em.getTransaction().commit(); - em.close(); - } + public void testSettingNonExistingParams(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + // non-existing positional param + assertThrows( IllegalArgumentException.class, () -> { + StoredProcedureQuery query = em.createNamedStoredProcedureQuery( "positional-param" ); + query.setParameter( 99, 1 ); + } ); + + // non-existing named param + assertThrows( IllegalArgumentException.class, () -> { + StoredProcedureQuery query = em.createNamedStoredProcedureQuery( "positional-param" ); + query.setParameter( "does-not-exist", 1 ); + fail( "Expecting an exception" ); + } ); + } + ); } - public void testParameterRegistration() { - + @Test + @FailureExpected(jiraKey = "HHH-8395", + reason = "Out of the frying pan into the fire: https://issues.apache.org/jira/browse/DERBY-211") + public void testExecuteUpdate(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> { + StoredProcedureQuery query = em.createStoredProcedureQuery( "deleteAllUsers" ); + int count = query.executeUpdate(); + // this fails because the Derby EmbeddedDriver is returning zero here rather than the actual updateCount :( + // https://issues.apache.org/jira/browse/DERBY-211 + assertThat( count ).isEqualTo( 1 ); + } + ); } // todo : look at ways to allow "Auxiliary DB Objects" to the db via EMF bootstrapping. @@ -247,25 +197,26 @@ public void testParameterRegistration() { // public static final String deleteAllUsers_DROP_CMD = "DROP ALIAS deleteAllUsers IF EXISTS"; - @Before - public void startUp() { - + @BeforeEach + public void startUp(EntityManagerFactoryScope scope) { // create the procedures - createTestUser( entityManagerFactory() ); - createProcedures( entityManagerFactory() ); + createTestUser( scope ); + createProcedures( scope ); } - @After - public void tearDown() { - - deleteTestUser( entityManagerFactory() ); - dropProcedures( entityManagerFactory() ); - + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) { + deleteTestUser( scope ); + dropProcedures( scope ); } - private void createProcedures(SessionFactoryImplementor emf) { - final JdbcConnectionAccess connectionAccess = emf.getServiceRegistry().getService( JdbcServices.class ).getBootstrapJdbcConnectionAccess(); + private void createProcedures(EntityManagerFactoryScope scope) { + SessionFactoryImplementor sessionFactoryImplementor = scope.getEntityManagerFactory() + .unwrap( SessionFactoryImplementor.class ); + final JdbcConnectionAccess connectionAccess = sessionFactoryImplementor.getServiceRegistry() + .getService( JdbcServices.class ) + .getBootstrapJdbcConnectionAccess(); final Connection conn; try { conn = connectionAccess.obtainConnection(); @@ -294,7 +245,7 @@ private void createProcedures(SessionFactoryImplementor emf) { conn.commit(); } catch (SQLException e) { - System.out.println( "Unable to commit transaction after creating creating procedures"); + System.out.println( "Unable to commit transaction after creating creating procedures" ); } try { @@ -317,19 +268,19 @@ private void dropProcedures(Statement statement) throws SQLException { private void createProcedureFindOneUser(Statement statement) throws SQLException { statement.execute( "CREATE PROCEDURE findOneUser() " + - "language java " + - "dynamic result sets 1 " + - "external name 'org.hibernate.community.dialect.DerbyJpaTckUsageTest.findOneUser' " + - "parameter style java" + "language java " + + "dynamic result sets 1 " + + "external name 'org.hibernate.community.dialect.DerbyJpaTckUsageTest.findOneUser' " + + "parameter style java" ); } private void createProcedureDeleteAllUsers(Statement statement) throws SQLException { statement.execute( "CREATE PROCEDURE deleteAllUsers() " + - "language java " + - "external name 'org.hibernate.community.dialect.DerbyJpaTckUsageTest.deleteAllUsers' " + - "parameter style java" + "language java " + + "external name 'org.hibernate.community.dialect.DerbyJpaTckUsageTest.deleteAllUsers' " + + "parameter style java" ); } @@ -361,26 +312,22 @@ public static void deleteAllUsers() throws SQLException { conn.close(); } - private void createTestUser(SessionFactoryImplementor entityManagerFactory) { - EntityManager em = entityManagerFactory.createEntityManager(); - em.getTransaction().begin(); - - em.persist( new User( 1, "steve" ) ); - em.getTransaction().commit(); - em.close(); + private void createTestUser(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> em.persist( new User( 1, "steve" ) ) + ); } - private void deleteTestUser(SessionFactoryImplementor entityManagerFactory) { - EntityManager em = entityManagerFactory.createEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete from User" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); + private void deleteTestUser(EntityManagerFactoryScope scope) { + scope.inTransaction( + em -> em.createQuery( "delete from User" ).executeUpdate() + ); } - private void dropProcedures(SessionFactoryImplementor emf) { - final SessionFactoryImplementor sf = emf.unwrap( SessionFactoryImplementor.class ); - final JdbcConnectionAccess connectionAccess = sf.getServiceRegistry().getService( JdbcServices.class ).getBootstrapJdbcConnectionAccess(); + private void dropProcedures(EntityManagerFactoryScope scope) { + final SessionFactoryImplementor sf = scope.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class ); + final JdbcConnectionAccess connectionAccess = sf.getServiceRegistry().getService( JdbcServices.class ) + .getBootstrapJdbcConnectionAccess(); final Connection conn; try { conn = connectionAccess.obtainConnection(); @@ -400,7 +347,7 @@ private void dropProcedures(SessionFactoryImplementor emf) { conn.commit(); } catch (SQLException e) { - System.out.println( "Unable to commit transaction after creating dropping procedures"); + System.out.println( "Unable to commit transaction after creating dropping procedures" ); } try { diff --git a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyLegacyDialectTestCase.java b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyLegacyDialectTestCase.java index a224bfb40bc9..8bd7f9ef800e 100644 --- a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyLegacyDialectTestCase.java +++ b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyLegacyDialectTestCase.java @@ -7,33 +7,33 @@ import org.hibernate.dialect.DatabaseVersion; import org.hibernate.orm.test.dialect.LimitQueryOptions; import org.hibernate.query.spi.Limit; - -import static org.junit.Assert.assertEquals; - +import org.hibernate.testing.orm.junit.BaseUnitTest; import org.hibernate.testing.orm.junit.JiraKey; -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; /** * Testing of patched support for Derby limit and offset queries; see HHH-3972 * * @author Evan Leonard */ -public class DerbyLegacyDialectTestCase extends BaseUnitTestCase { +@BaseUnitTest +public class DerbyLegacyDialectTestCase { @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitClause() { final int limit = 50; final String input = "select * from tablename t where t.cat = 5"; final String expected = "select * from tablename t where t.cat = 5 fetch first " + limit + " rows only"; final String actual = withLimit( input, toRowSelection( 0, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitWithOffsetClause() { final int limit = 50; final int offset = 200; @@ -41,50 +41,51 @@ public void testInsertLimitWithOffsetClause() { final String expected = "select * from tablename t where t.cat = 5 offset " + offset + " rows fetch next " + limit + " rows only"; final String actual = withLimit( input, toRowSelection( offset, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitWithForUpdateClause() { final int limit = 50; final int offset = 200; final String input = "select c11 as col1, c12 as col2, c13 as col13 from t1 for update of c11, c13"; final String expected = "select c11 as col1, c12 as col2, c13 as col13 from t1 offset " + offset - + " rows fetch next " + limit + " rows only for update of c11, c13"; + + " rows fetch next " + limit + " rows only for update of c11, c13"; final String actual = withLimit( input, toRowSelection( offset, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitWithWithClause() { final int limit = 50; final int offset = 200; final String input = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' with rr"; final String expected = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' offset " + offset - + " rows fetch next " + limit + " rows only with rr"; + + " rows fetch next " + limit + " rows only with rr"; final String actual = withLimit( input, toRowSelection( offset, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } @Test - @JiraKey( value = "HHH-3972" ) + @JiraKey(value = "HHH-3972") public void testInsertLimitWithForUpdateAndWithClauses() { final int limit = 50; final int offset = 200; final String input = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' for update of c11,c13 with rr"; final String expected = "select c11 as col1, c12 as col2, c13 as col13 from t1 where flight_id between 'AA1111' and 'AA1112' offset " + offset - + " rows fetch next " + limit + " rows only for update of c11,c13 with rr"; + + " rows fetch next " + limit + " rows only for update of c11,c13 with rr"; final String actual = withLimit( input, toRowSelection( offset, limit ) ); - assertEquals( expected, actual ); + assertThat( actual ).isEqualTo( expected ); } private String withLimit(String sql, Limit limit) { - return new DerbyLegacyDialect( DatabaseVersion.make( 10, 5 ) ).getLimitHandler().processSql( sql, -1, null, new LimitQueryOptions( limit ) ); + return new DerbyLegacyDialect( DatabaseVersion.make( 10, 5 ) ).getLimitHandler() + .processSql( sql, -1, null, new LimitQueryOptions( limit ) ); } private Limit toRowSelection(int firstRow, int maxRows) { diff --git a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/SQLServer2005DialectTestCase.java b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/SQLServer2005DialectTestCase.java index 6144b87416ce..92ddeb7e65a0 100644 --- a/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/SQLServer2005DialectTestCase.java +++ b/hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/SQLServer2005DialectTestCase.java @@ -4,22 +4,21 @@ */ package org.hibernate.community.dialect; -import java.util.Locale; - import org.hibernate.LockMode; import org.hibernate.LockOptions; import org.hibernate.dialect.DatabaseVersion; import org.hibernate.orm.test.dialect.LimitQueryOptions; import org.hibernate.query.spi.Limit; - +import org.hibernate.testing.orm.junit.BaseUnitTest; import org.hibernate.testing.orm.junit.JiraKey; -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import java.util.Locale; + +import static org.assertj.core.api.Assertions.assertThat; import static org.hibernate.Timeouts.NO_WAIT; -import static org.junit.Assert.assertEquals; /** * Unit test of the behavior of the SQLServerDialect utility methods @@ -28,15 +27,16 @@ * @author Lukasz Antoniak * @author Chris Cranford */ -public class SQLServer2005DialectTestCase extends BaseUnitTestCase { +@BaseUnitTest +public class SQLServer2005DialectTestCase { private SQLServerLegacyDialect dialect; - @Before + @BeforeAll public void setup() { dialect = new SQLServerLegacyDialect( DatabaseVersion.make( 9 ) ); } - @After + @AfterAll public void tearDown() { dialect = null; } @@ -45,63 +45,55 @@ public void tearDown() { public void testGetLimitString() { String input = "select distinct f1 as f53245 from table849752 order by f234, f67 desc"; - assertEquals( - "with query_ as (select row_.*,row_number() over (order by current_timestamp) as rownumber_ from (" + + String expected = "with query_ as (select row_.*,row_number() over (order by current_timestamp) as rownumber_ from (" + "select distinct top(?) f1 as f53245 from table849752 order by f234, f67 desc) row_)" + - " select f53245 from query_ where rownumber_>=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC"; - assertEquals( - "with query_ as (select row_.*,row_number() over (order by current_timestamp) as rownumber_ from (" + + String expected = "with query_ as (select row_.*,row_number() over (order by current_timestamp) as rownumber_ from (" + "select top(?) t1.c1 as col_0_0, (select case when count(t2.c1)>0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC) row_) " + - "select col_0_0,col_1_0 from query_ where rownumber_>=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC"; - assertEquals( - "select top(?) t1.c1 as col_0_0, (select case when count(t2.c1)>0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC", - withLimit( query, toRowSelection( 0, 5 ) ) - ); + String expected = "select top(?) t1.c1 as col_0_0, (select case when count(t2.c1)>0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC"; + assertThat( withLimit( query, toRowSelection( 0, 5 ) ) ).isEqualTo( expected ); } @Test @@ -417,35 +368,27 @@ public void testGetLimitStringUsingCTEQueryNoOffset() { // test top-based CTE with single CTE query_ definition with no odd formatting final String query1 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT c1, c2 FROM a"; - assertEquals( - "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT top(?) c1, c2 FROM a", - withLimit( query1, selection ) - ); + String expected = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT top(?) c1, c2 FROM a"; + assertThat( withLimit( query1, selection ) ).isEqualTo( expected ); // test top-based CTE with single CTE query_ definition and various tab, newline spaces final String query2 = " \n\tWITH a (c1\n\t,c2)\t\nAS (SELECT\n\tc1,c2 FROM t)\t\nSELECT c1, c2 FROM a"; - assertEquals( - "WITH a (c1\n\t,c2)\t\nAS (SELECT\n\tc1,c2 FROM t)\t\nSELECT top(?) c1, c2 FROM a", - withLimit( query2, selection ) - ); + expected = "WITH a (c1\n\t,c2)\t\nAS (SELECT\n\tc1,c2 FROM t)\t\nSELECT top(?) c1, c2 FROM a"; + assertThat( withLimit( query2, selection ) ).isEqualTo( expected ); // test top-based CTE with multiple CTE query_ definitions with no odd formatting final String query3 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t1), b (b1, b2) AS (SELECT b1, b2 FROM t2) " + - "SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; - assertEquals( - "WITH a (c1, c2) AS (SELECT c1, c2 FROM t1), b (b1, b2) AS (SELECT b1, b2 FROM t2) " + - "SELECT top(?) c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1", - withLimit( query3, selection ) - ); + "SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; + expected = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t1), b (b1, b2) AS (SELECT b1, b2 FROM t2) " + + "SELECT top(?) c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; + assertThat( withLimit( query3, selection ) ).isEqualTo( expected ); // test top-based CTE with multiple CTE query_ definitions and various tab, newline spaces final String query4 = " \n\r\tWITH a (c1, c2) AS\n\r (SELECT c1, c2 FROM t1)\n\r, b (b1, b2)\tAS\t" + - "(SELECT b1, b2 FROM t2) SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; - assertEquals( - "WITH a (c1, c2) AS\n\r (SELECT c1, c2 FROM t1)\n\r, b (b1, b2)\tAS\t(SELECT b1, b2 FROM t2)" + - " SELECT top(?) c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1", - withLimit( query4, selection ) - ); + "(SELECT b1, b2 FROM t2) SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; + expected = "WITH a (c1, c2) AS\n\r (SELECT c1, c2 FROM t1)\n\r, b (b1, b2)\tAS\t(SELECT b1, b2 FROM t2)" + + " SELECT top(?) c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; + assertThat( withLimit( query4, selection ) ).isEqualTo( expected ); } @Test @@ -455,45 +398,37 @@ public void testGetLimitStringUsingCTEQueryWithOffset() { // test non-top based CTE with single CTE query_ definition with no odd formatting final String query1 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT c1, c2 FROM a"; - assertEquals( - "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) , query_ as (select row_.*,row_number() over " + + String expected = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) , query_ as (select row_.*,row_number() over " + "(order by current_timestamp) as rownumber_ from (SELECT c1 as col0_, c2 as col1_ " + "FROM a) row_) select col0_,col1_ from query_ where rownumber_>=? " + - "and rownumber_=" + - "? and rownumber_=" + + "? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC"; - assertEquals( - "with query_ as (select row_.*,row_number() over (order by current_timestamp) as rownumber_ from (" + + String expected = "with query_ as (select row_.*,row_number() over (order by current_timestamp) as rownumber_ from (" + "select top(?) t1.c1 as col_0_0, (select case when count(t2.c1)>0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC) row_) " + - "select col_0_0,col_1_0 from query_ where rownumber_>=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC"; - assertEquals( - "select top(?) t1.c1 as col_0_0, (select case when count(t2.c1)>0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC", - withLimit( query, toRowSelection( 0, 5 ) ) - ); + String expected = "select top(?) t1.c1 as col_0_0, (select case when count(t2.c1)>0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC"; + assertThat( withLimit( query, toRowSelection( 0, 5 ) ) ).isEqualTo( expected ); } @Test @@ -418,35 +368,27 @@ public void testGetLimitStringUsingCTEQueryNoOffset() { // test top-based CTE with single CTE query_ definition with no odd formatting final String query1 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT c1, c2 FROM a"; - assertEquals( - "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT top(?) c1, c2 FROM a", - withLimit( query1, selection ) - ); + String expected = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT top(?) c1, c2 FROM a"; + assertThat( withLimit( query1, selection ) ).isEqualTo( expected ); // test top-based CTE with single CTE query_ definition and various tab, newline spaces final String query2 = " \n\tWITH a (c1\n\t,c2)\t\nAS (SELECT\n\tc1,c2 FROM t)\t\nSELECT c1, c2 FROM a"; - assertEquals( - "WITH a (c1\n\t,c2)\t\nAS (SELECT\n\tc1,c2 FROM t)\t\nSELECT top(?) c1, c2 FROM a", - withLimit( query2, selection ) - ); + expected = "WITH a (c1\n\t,c2)\t\nAS (SELECT\n\tc1,c2 FROM t)\t\nSELECT top(?) c1, c2 FROM a"; + assertThat( withLimit( query2, selection ) ).isEqualTo( expected ); // test top-based CTE with multiple CTE query_ definitions with no odd formatting final String query3 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t1), b (b1, b2) AS (SELECT b1, b2 FROM t2) " + - "SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; - assertEquals( - "WITH a (c1, c2) AS (SELECT c1, c2 FROM t1), b (b1, b2) AS (SELECT b1, b2 FROM t2) " + - "SELECT top(?) c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1", - withLimit( query3, selection ) - ); + "SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; + expected = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t1), b (b1, b2) AS (SELECT b1, b2 FROM t2) " + + "SELECT top(?) c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; + assertThat( withLimit( query3, selection ) ).isEqualTo( expected ); // test top-based CTE with multiple CTE query_ definitions and various tab, newline spaces final String query4 = " \n\r\tWITH a (c1, c2) AS\n\r (SELECT c1, c2 FROM t1)\n\r, b (b1, b2)\tAS\t" + - "(SELECT b1, b2 FROM t2) SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; - assertEquals( - "WITH a (c1, c2) AS\n\r (SELECT c1, c2 FROM t1)\n\r, b (b1, b2)\tAS\t(SELECT b1, b2 FROM t2)" + - " SELECT top(?) c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1", - withLimit( query4, selection ) - ); + "(SELECT b1, b2 FROM t2) SELECT c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; + expected = "WITH a (c1, c2) AS\n\r (SELECT c1, c2 FROM t1)\n\r, b (b1, b2)\tAS\t(SELECT b1, b2 FROM t2)" + + " SELECT top(?) c1, c2, b1, b2 FROM t1, t2 WHERE t1.c1 = t2.b1"; + assertThat( withLimit( query4, selection ) ).isEqualTo( expected ); } @Test @@ -456,45 +398,37 @@ public void testGetLimitStringUsingCTEQueryWithOffset() { // test non-top based CTE with single CTE query_ definition with no odd formatting final String query1 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT c1, c2 FROM a"; - assertEquals( - "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) , query_ as (select row_.*,row_number() over " + + String expected = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) , query_ as (select row_.*,row_number() over " + "(order by current_timestamp) as rownumber_ from (SELECT c1 as col0_, c2 as col1_ " + "FROM a) row_) select col0_,col1_ from query_ where rownumber_>=? " + - "and rownumber_=" + - "? and rownumber_=" + + "? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_=? and rownumber_ { + Simple simple = new Simple( 10L ); + simple.setName( "Simple Dialect Function Test" ); + simple.setAddress( "Simple Address" ); + simple.setPay( 45.8F ); + simple.setCount( 2 ); + session.persist( simple ); + + // Test to make sure allocating a specified object operates correctly. + assertThat( session.createQuery( + "select new org.hibernate.test.legacy.S(s.count, s.address) from Simple s" ).list() ) + .hasSize( 1 ); + + // Quick check the base dialect functions operate correctly + assertThat( session.createQuery( "select max(s.count) from Simple s" ).list() ) + .hasSize( 1 ); + assertThat( session.createQuery( "select count(*) from Simple s" ).list() ) + .hasSize( 1 ); + + List rset = session.createQuery( + "select s.name, sysdate, floor(s.pay), round(s.pay,0) from Simple s" ) + .list(); + assertThat( (((Object[]) rset.get( 0 ))[0]) ) + .describedAs( "Name string should have been returned" ) + .isNotNull(); + assertThat( (((Object[]) rset.get( 0 ))[1]) ) + .describedAs( "Todays Date should have been returned" ) + .isNotNull(); + assertThat( ((Object[]) rset.get( 0 ))[2] ) + .describedAs( "floor(45.8) result was incorrect " ) + .isEqualTo( 45 ); + assertThat( ((Object[]) rset.get( 0 ))[3] ) + .describedAs( "round(45.8) result was incorrect " ) + .isEqualTo( 46F ); + + simple.setPay( -45.8F ); + simple = session.merge( simple ); + + // Test type conversions while using nested functions (Float to Int). + rset = session.createQuery( "select abs(round(s.pay,0)) from Simple s" ).list(); + assertThat( rset.get( 0 ) ) + .describedAs( "abs(round(-45.8)) result was incorrect " ).isEqualTo( 46F ); + + // Test a larger depth 3 function example - Not a useful combo other than for testing + assertThat( session.createQuery( "select floor(round(sysdate,1)) from Simple s" ).list() ) + .hasSize( 1 ); + + // Test the oracle standard NVL funtion as a test of multi-param functions... + simple.setPay( null ); + simple = session.merge( simple ); + Double value = session.createQuery( + "select mod( nvl(s.pay, 5000), 2 ) from Simple as s where s.id = 10", Double.class ).list() + .get( 0 ); + assertThat( value.intValue() ).isEqualTo( 0 ); + + // Test the hsql standard MOD funtion as a test of multi-param functions... + value = session.createQuery( "select MOD(s.count, 2) from Simple as s where s.id = 10", + Double.class ) + .list() + .get( 0 ); + assertThat( value.intValue() ).isEqualTo( 0 ); - Simple simple = new Simple( Long.valueOf( 10 ) ); - simple.setName("Simple Dialect Function Test"); - simple.setAddress("Simple Address"); - simple.setPay(new Float(45.8)); - simple.setCount(2); - s.persist( simple ); + session.remove( simple ); + } + ); + } + + public void testSetProperties(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Simple simple = new Simple( Long.valueOf( 10 ) ); + simple.setName( "Simple 1" ); + session.persist( simple ); + Query q = session.createQuery( "from Simple s where s.name=:name and s.count=:count", + Simple.class ); + q.setProperties( simple ); + assertThat( q.list().get( 0 ) ).isSameAs( simple ); + //misuse of "Single" as a propertyobject, but it was the first testclass i found with a collection ;) + Single single = new Single() { // trivial hack to test properties with arrays. + @SuppressWarnings({"unchecked"}) + String[] getStuff() { + return (String[]) getSeveral().toArray( new String[0] ); + } + }; + + List l = new ArrayList<>(); + l.add( "Simple 1" ); + l.add( "Slimeball" ); + single.setSeveral( l ); + q = session.createQuery( "from Simple s where s.name in (:several)", Simple.class ); + q.setProperties( single ); + assertThat( q.list().get( 0 ) ).isSameAs( simple ); + + + q = session.createQuery( "from Simple s where s.name in (:stuff)", Simple.class ); + q.setProperties( single ); + assertThat( q.list().get( 0 ) ).isSameAs( simple ); + session.remove( simple ); + } + ); + } - // Test to make sure allocating a specified object operates correctly. - assertTrue( - s.createQuery( "select new org.hibernate.test.legacy.S(s.count, s.address) from Simple s" ).list().size() == 1 + public void testBroken(SessionFactoryScope scope) { + Broken broken = new Fixed(); + scope.inTransaction( + s -> { + broken.setId( 123L ); + broken.setOtherId( "foobar" ); + s.persist( broken ); + s.flush(); + broken.setTimestamp( new Date() ); + } ); - // Quick check the base dialect functions operate correctly - assertTrue( - s.createQuery( "select max(s.count) from Simple s" ).list().size() == 1 + Broken b = scope.fromTransaction( + session -> session.merge( broken ) ); - assertTrue( - s.createQuery( "select count(*) from Simple s" ).list().size() == 1 + + Broken b1 = scope.fromTransaction( + session -> + session.getReference( Broken.class, b ) ); - List rset = s.createQuery( "select s.name, sysdate, floor(s.pay), round(s.pay,0) from Simple s" ).list(); - assertNotNull("Name string should have been returned",(((Object[])rset.get(0))[0])); - assertNotNull("Todays Date should have been returned",(((Object[])rset.get(0))[1])); - assertEquals("floor(45.8) result was incorrect ", new Integer(45), ( (Object[]) rset.get(0) )[2] ); - assertEquals("round(45.8) result was incorrect ", new Float(46), ( (Object[]) rset.get(0) )[3] ); + scope.inTransaction( + session -> + session.remove( b1 ) + ); + } - simple.setPay(new Float(-45.8)); - simple = s.merge(simple); + public void testNothinToUpdate(SessionFactoryScope scope) { + Simple s = new Simple( 10L ); + scope.inTransaction( + session -> { + s.setName( "Simple 1" ); + session.persist( s ); + } + ); + Simple simple = scope.fromTransaction( + session -> + session.merge( s ) + ); - // Test type conversions while using nested functions (Float to Int). - rset = s.createQuery( "select abs(round(s.pay,0)) from Simple s" ).list(); - assertEquals("abs(round(-45.8)) result was incorrect ", new Float(46), rset.get(0)); + scope.inTransaction( + session -> + session.remove( session.merge( simple ) ) + ); + } - // Test a larger depth 3 function example - Not a useful combo other than for testing - assertTrue( - s.createQuery( "select floor(round(sysdate,1)) from Simple s" ).list().size() == 1 + public void testCachedQuery(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Simple simple = new Simple( Long.valueOf( 10 ) ); + simple.setName( "Simple 1" ); + session.persist( simple ); + } ); - // Test the oracle standard NVL funtion as a test of multi-param functions... - simple.setPay(null); - simple = s.merge(simple); - Double value = (Double) s.createQuery("select mod( nvl(s.pay, 5000), 2 ) from Simple as s where s.id = 10").list().get(0); - assertTrue( 0 == value.intValue() ); + Simple s = scope.fromTransaction( + session -> { + Query q = session.createQuery( "from Simple s where s.name=?", Simple.class ); + q.setCacheable( true ); + q.setParameter( 0, "Simple 1" ); + assertThat( q.list() ).hasSize( 1 ); + + q = session.createQuery( "from Simple s where s.name=:name", Simple.class ); + q.setCacheable( true ); + q.setParameter( "name", "Simple 1" ); + assertThat( q.list() ).hasSize( 1 ); + Simple simple = q.list().get( 0 ); + + q.setParameter( "name", "Simple 2" ); + assertThat( q.list() ).hasSize( 0 ); + simple.setName( "Simple 2" ); + assertThat( q.list() ).hasSize( 1 ); + return simple; - // Test the hsql standard MOD funtion as a test of multi-param functions... - value = (Double) s.createQuery( "select MOD(s.count, 2) from Simple as s where s.id = 10" ) - .list() - .get(0); - assertTrue( 0 == value.intValue() ); + } + ); + scope.inTransaction( + session -> { + Query q = session.createQuery( "from Simple s where s.name=:name", Simple.class ); + q.setParameter( "name", "Simple 2" ); + q.setCacheable( true ); + assertThat( q.list() ).hasSize( 1 ); + } + ); - s.remove(simple); - t.commit(); - s.close(); - } + scope.inTransaction( + session -> + session.remove( session.merge( s ) ) + ); + scope.inTransaction( + session -> { + Query q = session.createQuery( "from Simple s where s.name=?", Simple.class ); + q.setCacheable( true ); + q.setParameter( 0, "Simple 1" ); + assertThat( q.list() ).hasSize( 0 ); - public void testSetProperties() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Simple simple = new Simple( Long.valueOf( 10 ) ); - simple.setName("Simple 1"); - s.persist( simple ); - Query q = s.createQuery("from Simple s where s.name=:name and s.count=:count"); - q.setProperties(simple); - assertTrue( q.list().get(0)==simple ); - //misuse of "Single" as a propertyobject, but it was the first testclass i found with a collection ;) - Single single = new Single() { // trivial hack to test properties with arrays. - @SuppressWarnings( {"unchecked"}) - String[] getStuff() { - return (String[]) getSeveral().toArray(new String[getSeveral().size()]); - } - }; - - List l = new ArrayList(); - l.add("Simple 1"); - l.add("Slimeball"); - single.setSeveral(l); - q = s.createQuery("from Simple s where s.name in (:several)"); - q.setProperties(single); - assertTrue( q.list().get(0)==simple ); - - - q = s.createQuery("from Simple s where s.name in (:stuff)"); - q.setProperties(single); - assertTrue( q.list().get(0)==simple ); - s.remove(simple); - t.commit(); - s.close(); + } + ); } - public void testBroken() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Broken b = new Fixed(); - b.setId( Long.valueOf( 123 )); - b.setOtherId("foobar"); - s.persist(b); - s.flush(); - b.setTimestamp( new Date() ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - b = s.merge(b); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - b = s.getReference( Broken.class, b ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - s.remove(b); - t.commit(); - s.close(); - } + public void testCachedQueryRegion(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Simple simple = new Simple( Long.valueOf( 10 ) ); + simple.setName( "Simple 1" ); + session.persist( simple ); + } + ); - public void testNothinToUpdate() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Simple simple = new Simple( Long.valueOf(10) ); - simple.setName("Simple 1"); - s.persist( simple ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - simple = s.merge( simple ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - simple = s.merge( simple ); - s.remove(simple); - t.commit(); - s.close(); - } + Simple s = scope.fromTransaction( + session -> { + Query q = session.createQuery( "from Simple s where s.name=?", Simple.class ); + q.setCacheRegion( "foo" ); + q.setCacheable( true ); + q.setParameter( 0, "Simple 1" ); + assertThat( q.list() ).hasSize( 1 ); + + q = session.createQuery( "from Simple s where s.name=:name", Simple.class ); + q.setCacheRegion( "foo" ); + q.setCacheable( true ); + q.setParameter( "name", "Simple 1" ); + assertThat( q.list() ).hasSize( 1 ); + Simple simple = q.list().get( 0 ); + + q.setParameter( "name", "Simple 2" ); + assertThat( q.list() ).hasSize( 0 ); + simple.setName( "Simple 2" ); + assertThat( q.list() ).hasSize( 1 ); + return simple; + } + ); - public void testCachedQuery() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Simple simple = new Simple( Long.valueOf(10) ); - simple.setName("Simple 1"); - s.persist( simple ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - Query q = s.createQuery("from Simple s where s.name=?"); - q.setCacheable(true); - q.setParameter(0, "Simple 1"); - assertTrue( q.list().size()==1 ); - assertTrue( q.list().size()==1 ); - assertTrue( q.list().size()==1 ); - q = s.createQuery("from Simple s where s.name=:name"); - q.setCacheable(true); - q.setParameter("name", "Simple 1"); - assertTrue( q.list().size()==1 ); - simple = (Simple) q.list().get(0); - - q.setParameter("name", "Simple 2"); - assertTrue( q.list().size()==0 ); - assertTrue( q.list().size()==0 ); - simple.setName("Simple 2"); - assertTrue( q.list().size()==1 ); - assertTrue( q.list().size()==1 ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - q = s.createQuery("from Simple s where s.name=:name"); - q.setParameter("name", "Simple 2"); - q.setCacheable(true); - assertTrue( q.list().size()==1 ); - assertTrue( q.list().size()==1 ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - simple = s.merge( simple ); - s.remove(simple); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - q = s.createQuery("from Simple s where s.name=?"); - q.setCacheable(true); - q.setParameter(0, "Simple 1"); - assertTrue( q.list().size()==0 ); - assertTrue( q.list().size()==0 ); - t.commit(); - s.close(); - } + scope.inTransaction( + session -> + session.remove( session.merge( s ) ) + + ); - public void testCachedQueryRegion() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Simple simple = new Simple( Long.valueOf(10) ); - simple.setName("Simple 1"); - s.persist( simple ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - Query q = s.createQuery("from Simple s where s.name=?"); - q.setCacheRegion("foo"); - q.setCacheable(true); - q.setParameter(0, "Simple 1"); - assertTrue( q.list().size()==1 ); - assertTrue( q.list().size()==1 ); - assertTrue( q.list().size()==1 ); - q = s.createQuery("from Simple s where s.name=:name"); - q.setCacheRegion("foo"); - q.setCacheable(true); - q.setParameter("name", "Simple 1"); - assertTrue( q.list().size()==1 ); - simple = (Simple) q.list().get(0); - - q.setParameter("name", "Simple 2"); - assertTrue( q.list().size()==0 ); - assertTrue( q.list().size()==0 ); - simple.setName("Simple 2"); - assertTrue( q.list().size()==1 ); - assertTrue( q.list().size()==1 ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - simple = s.merge( simple ); - s.remove(simple); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - q = s.createQuery("from Simple s where s.name=?"); - q.setCacheRegion("foo"); - q.setCacheable(true); - q.setParameter(0, "Simple 1"); - assertTrue( q.list().size()==0 ); - assertTrue( q.list().size()==0 ); - t.commit(); - s.close(); + scope.inTransaction( + session -> { + Query q = session.createQuery( "from Simple s where s.name=?", Simple.class ); + q.setCacheRegion( "foo" ); + q.setCacheable( true ); + q.setParameter( 0, "Simple 1" ); + assertThat( q.list() ).hasSize( 0 ); + } + ); } - public void testSQLFunctions() { - try(Session s = openSession()) { - Transaction t = s.beginTransaction(); - Simple simple = new Simple( Long.valueOf( 10 ) ); - simple.setName( "Simple 1" ); - s.persist( simple ); - - s.createQuery( "from Simple s where repeat('foo', 3) = 'foofoofoo'" ).list(); - s.createQuery( "from Simple s where repeat(s.name, 3) = 'foofoofoo'" ).list(); - s.createQuery( "from Simple s where repeat( lower(s.name), (3 + (1-1)) / 2) = 'foofoofoo'" ).list(); - - assertTrue( - s.createQuery( "from Simple s where upper( s.name ) ='SIMPLE 1'" ).list().size() == 1 - ); - assertTrue( - s.createQuery( - "from Simple s where not( upper( s.name ) ='yada' or 1=2 or 'foo'='bar' or not('foo'='foo') or 'foo' like 'bar' )" - ).list() - .size() == 1 - ); - - assertTrue( - s.createQuery( "from Simple s where lower( s.name || ' foo' ) ='simple 1 foo'" ).list().size() == 1 - ); - assertTrue( - s.createQuery( "from Simple s where lower( concat(s.name, ' foo') ) ='simple 1 foo'" ) - .list() - .size() == 1 - ); - - Simple other = new Simple( Long.valueOf( 20 ) ); - other.setName( "Simple 2" ); - other.setCount( 12 ); - simple.setOther( other ); - s.persist( other ); - //s.find("from Simple s where s.name ## 'cat|rat|bag'"); - assertTrue( - s.createQuery( "from Simple s where upper( s.other.name ) ='SIMPLE 2'" ).list().size() == 1 - ); - assertTrue( - s.createQuery( "from Simple s where not ( upper( s.other.name ) ='SIMPLE 2' )" ).list().size() == 0 - ); - assertTrue( - s.createQuery( - "select distinct s from Simple s where ( ( s.other.count + 3 ) = (15*2)/2 and s.count = 69) or ( ( s.other.count + 2 ) / 7 ) = 2" - ).list() - .size() == 1 - ); - assertTrue( - s.createQuery( - "select s from Simple s where ( ( s.other.count + 3 ) = (15*2)/2 and s.count = 69) or ( ( s.other.count + 2 ) / 7 ) = 2 order by s.other.count" - ).list() - .size() == 1 - ); - Simple min = new Simple( Long.valueOf( 30 ) ); - min.setCount( -1 ); - s.persist( min ); - - assertTrue( - s.createQuery( "from Simple s where s.count > ( select min(sim.count) from Simple sim )" ) - .list() - .size() == 2 - ); - t.commit(); - t = s.beginTransaction(); - assertTrue( - s.createQuery( - "from Simple s where s = some( select sim from Simple sim where sim.count>=0 ) and s.count >= 0" - ).list() - .size() == 2 - ); - assertTrue( + public void testSQLFunctions(SessionFactoryScope scope) { + scope.inTransaction( + s -> { + Simple simple = new Simple( 10L ); + simple.setName( "Simple 1" ); + s.persist( simple ); + + s.createQuery( "from Simple s where repeat('foo', 3) = 'foofoofoo'", Simple.class ).list(); + s.createQuery( "from Simple s where repeat(s.name, 3) = 'foofoofoo'", Simple.class ).list(); + s.createQuery( "from Simple s where repeat( lower(s.name), (3 + (1-1)) / 2) = 'foofoofoo'", + Simple.class ).list(); + + assertThat( + s.createQuery( "from Simple s where upper( s.name ) ='SIMPLE 1'", Simple.class ) + .list() ).hasSize( 1 ); + assertThat( + s.createQuery( + "from Simple s where not( upper( s.name ) ='yada' or 1=2 or 'foo'='bar' or not('foo'='foo') or 'foo' like 'bar' )", + Simple.class ).list() ).hasSize( 1 ); + + assertThat( s.createQuery( "from Simple s where lower( s.name || ' foo' ) ='simple 1 foo'", + Simple.class ).list() ).hasSize( 1 ); + assertThat( s.createQuery( "from Simple s where lower( concat(s.name, ' foo') ) ='simple 1 foo'", + Simple.class ).list() ).hasSize( 1 ); + + Simple other = new Simple( 20L ); + other.setName( "Simple 2" ); + other.setCount( 12 ); + simple.setOther( other ); + s.persist( other ); + //s.find("from Simple s where s.name ## 'cat|rat|bag'"); + assertThat( s.createQuery( "from Simple s where upper( s.other.name ) ='SIMPLE 2'", Simple.class ) + .list() ).hasSize( 1 ); + assertThat( + s.createQuery( + "from Simple s where not ( upper( s.other.name ) ='SIMPLE 2', Simple.class )", + Simple.class ) + .list() ).hasSize( 0 ); + assertThat( + s.createQuery( + "select distinct s from Simple s where ( ( s.other.count + 3 ) = (15*2)/2 and s.count = 69) or ( ( s.other.count + 2 ) / 7 ) = 2", + Simple.class + ).list() ).hasSize( 1 ); + assertThat( + s.createQuery( + "select s from Simple s where ( ( s.other.count + 3 ) = (15*2)/2 and s.count = 69) or ( ( s.other.count + 2 ) / 7 ) = 2 order by s.other.count", + Simple.class + ).list() ).hasSize( 1 ); + Simple min = new Simple( 30L ); + min.setCount( -1 ); + s.persist( min ); + + assertThat( + s.createQuery( "from Simple s where s.count > ( select min(sim.count) from Simple sim )", + Simple.class ) + .list() ).hasSize( 2 ); + s.getTransaction().commit(); + s.beginTransaction(); + assertThat( + s.createQuery( + "from Simple s where s = some( select sim from Simple sim where sim.count>=0 ) and s.count >= 0", + Simple.class + ).list() ).hasSize( 2 ); + assertThat( + s.createQuery( + "from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count ) and s.other.count > 0", + Simple.class + ).list() ).hasSize( 1 ); + + List list = s.createQuery( + "select sum(s.count) from Simple s group by s.count having sum(s.count) > 10" ) + .list(); + assertThat( list ).hasSize( 1 ); + assertThat( list.get( 0 ) ).isEqualTo( 12L ); + list = s.createQuery( "select s.count from Simple s group by s.count having s.count = 12" ).list(); + assertThat( list ).isNotEmpty(); + s.createQuery( - "from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count ) and s.other.count > 0" - ).list() - .size() == 1 - ); - - List list = s.createQuery( "select sum(s.count) from Simple s group by s.count having sum(s.count) > 10" ) - .list(); - assertEquals( 1, list.size() ); - assertEquals( Long.valueOf( 12 ), list.get( 0 ) ); - list = s.createQuery( "select s.count from Simple s group by s.count having s.count = 12" ).list(); - assertFalse( list.isEmpty() ); - - s.createQuery( - "select s.id, s.count, count(t), max(t.date) from Simple s, Simple t where s.count = t.count group by s.id, s.count order by s.count" - ).list(); - - Query q = s.createQuery( "from Simple s" ); - q.setMaxResults( 10 ); - assertTrue( q.list().size() == 3 ); - q = s.createQuery( "from Simple s" ); - q.setMaxResults( 1 ); - assertTrue( q.list().size() == 1 ); - q = s.createQuery( "from Simple s" ); - assertTrue( q.list().size() == 3 ); - q = s.createQuery( "from Simple s where s.name = ?" ); - q.setParameter( 0, "Simple 1" ); - assertTrue( q.list().size() == 1 ); - q = s.createQuery( "from Simple s where s.name = ? and upper(s.name) = ?" ); - q.setParameter( 1, "SIMPLE 1" ); - q.setParameter( 0, "Simple 1" ); - q.setFirstResult( 0 ); - assertFalse( q.list().isEmpty() ); - q = s.createQuery( - "from Simple s where s.name = :foo and upper(s.name) = :bar or s.count=:count or s.count=:count + 1" ); - q.setParameter( "bar", "SIMPLE 1" ); - q.setParameter( "foo", "Simple 1" ); - q.setParameter( "count", 69 ); - q.setFirstResult( 0 ); - assertFalse( q.list().isEmpty() ); - q = s.createQuery( "select s.id from Simple s" ); - q.setFirstResult( 1 ); - q.setMaxResults( 2 ); - list = q.list(); - for ( Object l : list ) { - assertTrue( l instanceof Long ); - } + "select s.id, s.count, count(t), max(t.date) from Simple s, Simple t where s.count = t.count group by s.id, s.count order by s.count" + ).list(); + + Query q = s.createQuery( "from Simple s", Simple.class ); + q.setMaxResults( 10 ); + assertThat( q.list() ).hasSize( 3 ); + q = s.createQuery( "from Simple s", Simple.class ); + q.setMaxResults( 1 ); + assertThat( q.list() ).hasSize( 1 ); + q = s.createQuery( "from Simple s", Simple.class ); + assertThat( q.list() ).hasSize( 3 ); + q = s.createQuery( "from Simple s where s.name = ?", Simple.class ); + q.setParameter( 0, "Simple 1" ); + assertThat( q.list() ).hasSize( 1 ); + q = s.createQuery( "from Simple s where s.name = ? and upper(s.name) = ?", Simple.class ); + q.setParameter( 1, "SIMPLE 1" ); + q.setParameter( 0, "Simple 1" ); + q.setFirstResult( 0 ); + assertThat( q.list() ).isNotEmpty(); + q = s.createQuery( + "from Simple s where s.name = :foo and upper(s.name) = :bar or s.count=:count or s.count=:count + 1", + Simple.class ); + q.setParameter( "bar", "SIMPLE 1" ); + q.setParameter( "foo", "Simple 1" ); + q.setParameter( "count", 69 ); + q.setFirstResult( 0 ); + assertThat( q.list() ).isNotEmpty(); + q = s.createQuery( "select s.id from Simple s", Simple.class ); + q.setFirstResult( 1 ); + q.setMaxResults( 2 ); + list = q.list(); + for ( Object l : list ) { + assertThat( l ).isInstanceOf( Long.class ); + } // int i=0; // while ( list.hasNext() ) { // assertTrue( list.next() instanceof Long ); // i++; // } - assertEquals( 2, list.size() ); - q = s.createQuery( "select all s, s.other from Simple s where s = :s" ); - q.setParameter( "s", simple ); - assertTrue( q.list().size() == 1 ); - - - q = s.createQuery( "from Simple s where s.name in (:name_list) and s.count > :count", Simple.class ); - HashSet set = new HashSet(); - set.add( "Simple 1" ); - set.add( "foo" ); - q.setParameterList( "name_list", set ); - q.setParameter( "count", new Integer( -1 ) ); - assertTrue( q.list().size() == 1 ); - - try (ScrollableResults sr = s.createQuery( "from Simple s" ).scroll()) { - sr.next(); - sr.get(); - } - - s.remove( other ); - s.remove( simple ); - s.remove( min ); - t.commit(); - } + assertThat( q.list() ).hasSize( 2 ); + q = s.createQuery( "select all s, s.other from Simple s where s = :s" ); + q.setParameter( "s", simple ); + assertThat( q.list() ).hasSize( 1 ); + + + q = s.createQuery( "from Simple s where s.name in (:name_list) and s.count > :count", + Simple.class ); + HashSet set = new HashSet<>(); + set.add( "Simple 1" ); + set.add( "foo" ); + q.setParameterList( "name_list", set ); + q.setParameter( "count", -1 ); + assertThat( q.list() ).hasSize( 1 ); + + try (ScrollableResults sr = s.createQuery( "from Simple s", Simple.class ).scroll()) { + sr.next(); + sr.get(); + } + s.remove( other ); + s.remove( simple ); + s.remove( min ); + } + ); } - public void testBlobClob() throws Exception { - Session s = openSession(); - s.beginTransaction(); - Blobber b = new Blobber(); - b.setBlob( getLobHelper().createBlob( "foo/bar/baz".getBytes() ) ); - b.setClob( getLobHelper().createClob("foo/bar/baz") ); - s.persist(b); - //s.refresh(b); - //assertTrue( b.getClob() instanceof ClobImpl ); - s.flush(); - s.refresh(b); - //b.getBlob().setBytes( 2, "abc".getBytes() ); - log.debug("levinson: just bfore b.getClob()"); - b.getClob().getSubString(2, 3); - //b.getClob().setString(2, "abc"); - s.flush(); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.beginTransaction(); - b = s.getReference( Blobber.class, b.getId() ); - Blobber b2 = new Blobber(); - s.persist(b2); - b2.setBlob( b.getBlob() ); - b.setBlob(null); - //assertTrue( b.getClob().getSubString(1, 3).equals("fab") ); - b.getClob().getSubString(1, 6); - //b.getClob().setString(1, "qwerty"); - s.flush(); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.beginTransaction(); - b = s.getReference( Blobber.class, b.getId() ); - b.setClob( getLobHelper().createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb") ); - s.flush(); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.beginTransaction(); - b = s.getReference( Blobber.class, b.getId() ); - assertTrue( b.getClob().getSubString(1, 7).equals("xcvfxvc") ); - //b.getClob().setString(5, "1234567890"); - s.flush(); - s.getTransaction().commit(); - s.close(); + public void testBlobClob(SessionFactoryScope scope) { + Blobber blobber = new Blobber(); + scope.inTransaction( + session -> { + blobber.setBlob( getLobHelper().createBlob( "foo/bar/baz".getBytes() ) ); + blobber.setClob( getLobHelper().createClob( "foo/bar/baz" ) ); + session.persist( blobber ); + //s.refresh(b); + //assertTrue( b.getClob() instanceof ClobImpl ); + session.flush(); + session.refresh( blobber ); + //b.getBlob().setBytes( 2, "abc".getBytes() ); + try { + blobber.getClob().getSubString( 2, 3 ); + } + catch (SQLException e) { + throw new RuntimeException( e ); + } + //b.getClob().setString(2, "abc"); + session.flush(); + } + ); + + scope.inTransaction( + session -> { + Blobber b = session.getReference( Blobber.class, blobber.getId() ); + Blobber b2 = new Blobber(); + session.persist( b2 ); + b2.setBlob( b.getBlob() ); + b.setBlob( null ); + //assertTrue( b.getClob().getSubString(1, 3).equals("fab") ); + try { + b.getClob().getSubString( 1, 6 ); + } + catch (SQLException e) { + throw new RuntimeException( e ); + } + //b.getClob().setString(1, "qwerty"); + session.flush(); + } + ); + + scope.inTransaction( + session -> { + Blobber b = session.getReference( Blobber.class, blobber.getId() ); + b.setClob( getLobHelper().createClob( "xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb" ) ); + session.flush(); + } + ); + + scope.inTransaction( + session -> { + Blobber b = session.getReference( Blobber.class, blobber.getId() ); + try { + assertThat( b.getClob().getSubString( 1, 7 ) ).isEqualTo( "xcvfxvc" ); + } + catch (SQLException e) { + throw new RuntimeException( e ); + } + //b.getClob().setString(5, "1234567890"); + session.flush(); + } + ); } - public void testSqlFunctionAsAlias() { + public void testSqlFunctionAsAlias(SessionFactoryScope scope) { String functionName = locateAppropriateDialectFunctionNameForAliasTest(); - if (functionName == null) { - log.info("Dialect does not list any no-arg functions"); - return; - } - - log.info("Using function named [" + functionName + "] for 'function as alias' test"); String query = "select " + functionName + " from Simple as " + functionName + " where " + functionName + ".id = 10"; - Session s = openSession(); - Transaction t = s.beginTransaction(); - Simple simple = new Simple( Long.valueOf(10) ); - simple.setName("Simple 1"); - s.persist( simple ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - List result = s.createQuery( query ).list(); - assertTrue( result.size() == 1 ); - assertTrue(result.get(0) instanceof Simple); - s.remove( result.get(0) ); - t.commit(); - s.close(); + scope.inTransaction( + session -> { + Simple simple = new Simple( 10L ); + simple.setName( "Simple 1" ); + session.persist( simple ); + } + ); + + scope.inTransaction( + session -> { + List result = session.createQuery( query ).list(); + assertThat( result ).hasSize( 1 ); + assertThat( result.get( 0 ) ).isInstanceOf( Simple.class ); + session.remove( result.get( 0 ) ); + } + ); } - @SuppressWarnings( {"ForLoopReplaceableByForEach"}) private String locateAppropriateDialectFunctionNameForAliasTest() { // for (Iterator itr = getDialect().getFunctions().entrySet().iterator(); itr.hasNext(); ) { // final Map.Entry entry = (Map.Entry) itr.next(); @@ -558,193 +551,203 @@ private String locateAppropriateDialectFunctionNameForAliasTest() { return null; } - public void testCachedQueryOnInsert() throws Exception { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Simple simple = new Simple( Long.valueOf(10) ); - simple.setName("Simple 1"); - s.persist( simple ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - Query q = s.createQuery("from Simple s"); - List list = q.setCacheable(true).list(); - assertTrue( list.size()==1 ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - q = s.createQuery("from Simple s"); - list = q.setCacheable(true).list(); - assertTrue( list.size()==1 ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - Simple simple2 = new Simple( Long.valueOf(12) ); - simple2.setCount(133); - s.persist( simple2 ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - q = s.createQuery("from Simple s"); - list = q.setCacheable(true).list(); - assertTrue( list.size()==2 ); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - q = s.createQuery("from Simple s"); - list = q.setCacheable(true).list(); - assertTrue( list.size()==2 ); - for ( Object o : list ) { - s.remove( o ); - } - t.commit(); - s.close(); + public void testCachedQueryOnInsert(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Simple simple = new Simple( 10L ); + simple.setName( "Simple 1" ); + session.persist( simple ); + } + ); + + scope.inTransaction( + session -> { + Query q = session.createQuery( "from Simple s", Simple.class ); + List list = q.setCacheable( true ).list(); + assertThat( list ).hasSize( 1 ); + } + ); + + scope.inTransaction( + session -> { + Query q = session.createQuery( "from Simple s", Simple.class ); + List list = q.setCacheable( true ).list(); + assertThat( list ).hasSize( 1 ); + } + ); + + scope.inTransaction( + session -> { + Simple simple2 = new Simple( 12L ); + simple2.setCount( 133 ); + session.persist( simple2 ); + } + ); + + scope.inTransaction( + session -> { + Query q = session.createQuery( "from Simple s", Simple.class ); + List list = q.setCacheable( true ).list(); + assertThat( list ).hasSize( 2 ); + } + ); + + scope.inTransaction( + session -> { + Query q = session.createQuery( "from Simple s", Simple.class ); + List list = q.setCacheable( true ).list(); + assertThat( list ).hasSize( 2 ); + for ( Object o : list ) { + session.remove( o ); + } + } + ); } - public void testInterSystemsFunctions() throws Exception { + public void testInterSystemsFunctions(SessionFactoryScope scope) { Calendar cal = new GregorianCalendar(); - cal.set(1977,6,3,0,0,0); - java.sql.Timestamp testvalue = new java.sql.Timestamp(cal.getTimeInMillis()); - testvalue.setNanos(0); + cal.set( 1977, 6, 3, 0, 0, 0 ); + java.sql.Timestamp testvalue = new java.sql.Timestamp( cal.getTimeInMillis() ); + testvalue.setNanos( 0 ); Calendar cal3 = new GregorianCalendar(); - cal3.set(1976,2,3,0,0,0); - java.sql.Timestamp testvalue3 = new java.sql.Timestamp(cal3.getTimeInMillis()); - testvalue3.setNanos(0); - - final Session s = openSession(); - s.beginTransaction(); - try { - s.doWork( - new Work() { - @Override - public void execute(Connection connection) throws SQLException { - Statement stmt = ((SessionImplementor)s).getJdbcCoordinator().getStatementPreparer().createStatement(); - ((SessionImplementor)s).getJdbcCoordinator().getResultSetReturn().executeUpdate( stmt, "DROP FUNCTION spLock FROM TestInterSystemsFunctionsClass" ); - } + cal3.set( 1976, 2, 3, 0, 0, 0 ); + java.sql.Timestamp testvalue3 = new java.sql.Timestamp( cal3.getTimeInMillis() ); + testvalue3.setNanos( 0 ); + + scope.inTransaction( + session -> { + try { + session.doWork( + connection -> { + Statement stmt = session.getJdbcCoordinator() + .getStatementPreparer() + .createStatement(); + session.getJdbcCoordinator().getResultSetReturn() + .executeUpdate( stmt, + "DROP FUNCTION spLock FROM TestInterSystemsFunctionsClass" ); + } + ); } - ); - } - catch (Exception ex) { - System.out.println("as we expected stored procedure sp does not exist when we drop it"); + catch (Exception ex) { + System.out.println( "as we expected stored procedure sp does not exist when we drop it" ); - } - s.getTransaction().commit(); - - s.beginTransaction(); - s.doWork( - new Work() { - @Override - public void execute(Connection connection) throws SQLException { - Statement stmt = ( (SessionImplementor) s ).getJdbcCoordinator() - .getStatementPreparer() - .createStatement(); - String create_function = "CREATE FUNCTION SQLUser.TestInterSystemsFunctionsClass_spLock\n" + - " ( INOUT pHandle %SQLProcContext, \n" + - " ROWID INTEGER \n" + - " )\n" + - " FOR User.TestInterSystemsFunctionsClass " + - " PROCEDURE\n" + - " RETURNS INTEGER\n" + - " LANGUAGE OBJECTSCRIPT\n" + - " {\n" + - " q 0\n" + - " }"; - ( (SessionImplementor) s ).getJdbcCoordinator().getResultSetReturn().executeUpdate( - stmt, - create_function - ); } + session.getTransaction().commit(); + + session.beginTransaction(); + session.doWork( + connection -> { + Statement stmt = session.getJdbcCoordinator() + .getStatementPreparer() + .createStatement(); + String create_function = "CREATE FUNCTION SQLUser.TestInterSystemsFunctionsClass_spLock\n" + + " ( INOUT pHandle %SQLProcContext, \n" + + " ROWID INTEGER \n" + + " )\n" + + " FOR User.TestInterSystemsFunctionsClass " + + " PROCEDURE\n" + + " RETURNS INTEGER\n" + + " LANGUAGE OBJECTSCRIPT\n" + + " {\n" + + " q 0\n" + + " }"; + session.getJdbcCoordinator().getResultSetReturn().executeUpdate( + stmt, + create_function + ); + } + ); + session.getTransaction().commit(); + + session.beginTransaction(); + + TestInterSystemsFunctionsClass object = new TestInterSystemsFunctionsClass( 10L ); + object.setDateText( "1977-07-03" ); + object.setDate1( testvalue ); + object.setDate3( testvalue3 ); + session.persist( object ); + } + ); + + scope.inTransaction( + s2 -> { + TestInterSystemsFunctionsClass test = s2.get( TestInterSystemsFunctionsClass.class, 10L ); + assertThat( test.getDate1() ).isEqualTo( testvalue ); + test = s2.byId( TestInterSystemsFunctionsClass.class ) + .with( LockMode.NONE ) + .load( 10L ); + assertThat( test.getDate1() ).isEqualTo( testvalue ); + Date value = s2.createQuery( + "select nvl(o.date,o.dateText) from TestInterSystemsFunctionsClass as o", Date.class ) + .list() + .get( 0 ); + assertThat( value ).isEqualTo( testvalue ); + Object nv = s2.createQuery( + "select nullif(o.dateText,o.dateText) from TestInterSystemsFunctionsClass as o", + Object.class ) + .list() + .get( 0 ); + assertThat( nv ).isNull(); + String dateText = s2.createQuery( + "select nvl(o.dateText,o.date) from TestInterSystemsFunctionsClass as o", String.class + ).list() + .get( 0 ); + assertThat( dateText ).isEqualTo( "1977-07-03" ); + value = s2.createQuery( + "select ifnull(o.date,o.date1) from TestInterSystemsFunctionsClass as o", Date.class ) + .list() + .get( 0 ); + assertThat( value ).isEqualTo( testvalue ); + value = s2.createQuery( + "select ifnull(o.date3,o.date,o.date1) from TestInterSystemsFunctionsClass as o", + Date.class ) + .list() + .get( 0 ); + assertThat( value ).isEqualTo( testvalue ); + Integer pos = s2.createQuery( + "select position('07', o.dateText) from TestInterSystemsFunctionsClass as o", + Integer.class ) + .list() + .get( 0 ); + assertThat( pos ).isEqualTo( 6 ); + String st = s2.createQuery( + "select convert(o.date1, SQL_TIME) from TestInterSystemsFunctionsClass as o", String.class ) + .list() + .get( 0 ); + assertThat( st ).isEqualTo( "00:00:00" ); + Time tm = s2.createQuery( + "select cast(o.date1, time) from TestInterSystemsFunctionsClass as o", Time.class + ).list() + .get( 0 ); + assertThat( tm.toString() ).isEqualTo( "00:00:00" ); + Double diff = s2.createQuery( + "select timestampdiff(SQL_TSI_FRAC_SECOND, o.date3, o.date1) from TestInterSystemsFunctionsClass as o", + Double.class + ).list() + .get( 0 ); + assertThat( diff ).isNotEqualTo( 0.0 ); + diff = s2.createQuery( + "select timestampdiff(SQL_TSI_MONTH, o.date3, o.date1) from TestInterSystemsFunctionsClass as o", + Double.class + ).list() + .get( 0 ); + assertThat( diff ).isEqualTo( 16.0 ); + diff = s2.createQuery( + "select timestampdiff(SQL_TSI_WEEK, o.date3, o.date1) from TestInterSystemsFunctionsClass as o", + Double.class + ).list() + .get( 0 ); + assertThat( diff ).isGreaterThanOrEqualTo( 16 * 4 ); + diff = s2.createQuery( + "select timestampdiff(SQL_TSI_YEAR, o.date3, o.date1) from TestInterSystemsFunctionsClass as o", + Double.class + ).list() + .get( 0 ); + assertThat( diff ).isEqualTo( 1.0 ); } ); - s.getTransaction().commit(); - - s.beginTransaction(); - - TestInterSystemsFunctionsClass object = new TestInterSystemsFunctionsClass( Long.valueOf( 10 ) ); - object.setDateText( "1977-07-03" ); - object.setDate1( testvalue ); - object.setDate3( testvalue3 ); - s.persist( object ); - s.getTransaction().commit(); - s.close(); - - Session s2 = openSession(); - s2.beginTransaction(); - TestInterSystemsFunctionsClass test = s2.get(TestInterSystemsFunctionsClass.class, 10L ); - assertTrue( test.getDate1().equals(testvalue)); - test = (TestInterSystemsFunctionsClass) s2.byId( TestInterSystemsFunctionsClass.class ) - .with( LockMode.NONE ) - .load( 10L ); - assertTrue( test.getDate1().equals(testvalue)); - Date value = (Date) s2.createQuery( "select nvl(o.date,o.dateText) from TestInterSystemsFunctionsClass as o" ) - .list() - .get(0); - assertTrue( value.equals(testvalue)); - Object nv = s2.createQuery( "select nullif(o.dateText,o.dateText) from TestInterSystemsFunctionsClass as o" ) - .list() - .get(0); - assertTrue( nv == null); - String dateText = (String) s2.createQuery( - "select nvl(o.dateText,o.date) from TestInterSystemsFunctionsClass as o" - ).list() - .get(0); - assertTrue( dateText.equals("1977-07-03")); - value = (Date) s2.createQuery( "select ifnull(o.date,o.date1) from TestInterSystemsFunctionsClass as o" ) - .list() - .get(0); - assertTrue( value.equals(testvalue)); - value = (Date) s2.createQuery( "select ifnull(o.date3,o.date,o.date1) from TestInterSystemsFunctionsClass as o" ) - .list() - .get(0); - assertTrue( value.equals(testvalue)); - Integer pos = (Integer) s2.createQuery( - "select position('07', o.dateText) from TestInterSystemsFunctionsClass as o" - ).list() - .get(0); - assertTrue(pos.intValue() == 6); - String st = (String) s2.createQuery( "select convert(o.date1, SQL_TIME) from TestInterSystemsFunctionsClass as o" ) - .list() - .get(0); - assertTrue( st.equals("00:00:00")); - java.sql.Time tm = (java.sql.Time) s2.createQuery( - "select cast(o.date1, time) from TestInterSystemsFunctionsClass as o" - ).list() - .get(0); - assertTrue( tm.toString().equals("00:00:00")); - Double diff = (Double) s2.createQuery( - "select timestampdiff(SQL_TSI_FRAC_SECOND, o.date3, o.date1) from TestInterSystemsFunctionsClass as o" - ).list() - .get(0); - assertTrue(diff.doubleValue() != 0.0); - diff = (Double) s2.createQuery( - "select timestampdiff(SQL_TSI_MONTH, o.date3, o.date1) from TestInterSystemsFunctionsClass as o" - ).list() - .get(0); - assertTrue(diff.doubleValue() == 16.0); - diff = (Double) s2.createQuery( - "select timestampdiff(SQL_TSI_WEEK, o.date3, o.date1) from TestInterSystemsFunctionsClass as o" - ).list() - .get(0); - assertTrue(diff.doubleValue() >= 16*4); - diff = (Double) s2.createQuery( - "select timestampdiff(SQL_TSI_YEAR, o.date3, o.date1) from TestInterSystemsFunctionsClass as o" - ).list() - .get(0); - assertTrue(diff.doubleValue() == 1.0); - - s2.getTransaction().commit(); - s2.close(); + } } diff --git a/hibernate-spatial/gradle.properties b/hibernate-spatial/gradle.properties deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/hibernate-spatial/hibernate-spatial.gradle b/hibernate-spatial/hibernate-spatial.gradle index 02d9ebb87097..6c621b3a76fd 100644 --- a/hibernate-spatial/hibernate-spatial.gradle +++ b/hibernate-spatial/hibernate-spatial.gradle @@ -20,10 +20,6 @@ dependencies { testImplementation project( ':hibernate-ant' ) testImplementation project( path: ':hibernate-core', configuration: 'tests' ) - // todo : to get rid of these - testImplementation testLibs.junit4 - testImplementation testLibs.junit4Engine - testImplementation jakartaLibs.validation testImplementation libs.jandex testImplementation libs.classmate diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/hana/TestHANASpatialFunctions.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/hana/TestHANASpatialFunctions.java index fac90713ce65..f65087fd3d9b 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/hana/TestHANASpatialFunctions.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/hana/TestHANASpatialFunctions.java @@ -4,35 +4,33 @@ */ package org.hibernate.spatial.dialect.hana; -import java.lang.invoke.MethodHandles; -import java.sql.SQLException; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; import jakarta.persistence.Query; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.cfg.Configuration; import org.hibernate.spatial.HSMessageLogger; import org.hibernate.spatial.testing.SpatialFunctionalTestCase; import org.hibernate.spatial.testing.dialects.hana.HANAExpectationsFactory; - -import org.hibernate.testing.RequiresDialect; -import org.junit.Ignore; -import org.junit.Test; - +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.jboss.logging.Logger; - +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.Point; import org.locationtech.jts.io.WKBWriter; import org.locationtech.jts.io.WKTWriter; +import java.lang.invoke.MethodHandles; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + import static java.lang.String.format; +import static org.junit.jupiter.api.Assertions.assertThrows; + //TODO - see what tests are still needed, when we update/fix the HANA spatial support -@RequiresDialect(value = HANASpatialDialect.class, comment = "This test tests the HANA spatial functions not covered by Hibernate Spatial", jiraKey = "HHH-12426") -@Ignore +@RequiresDialect(value = HANASpatialDialect.class, + comment = "This test tests the HANA spatial functions not covered by Hibernate Spatial, HHH-12426") +@Disabled @Deprecated public class TestHANASpatialFunctions extends SpatialFunctionalTestCase { @@ -44,11 +42,6 @@ public class TestHANASpatialFunctions extends SpatialFunctionalTestCase { protected HANAExpectationsFactory hanaExpectationsFactory; - @Override - protected void afterConfigurationBuilt(Configuration cfg) { - super.afterConfigurationBuilt( cfg ); - this.hanaExpectationsFactory = (HANAExpectationsFactory) this.expectationsFactory; - } @Override protected HSMessageLogger getLogger() { @@ -56,224 +49,224 @@ protected HSMessageLogger getLogger() { } @Test - public void test_alphashape_on_jts() throws SQLException { - alphashape( JTS ); + public void test_alphashape_on_jts(SessionFactoryScope scope) throws SQLException { + alphashape( JTS, scope ); } @Test - public void test_alphashape_on_geolatte() throws SQLException { - alphashape( GEOLATTE ); + public void test_alphashape_on_geolatte(SessionFactoryScope scope) throws SQLException { + alphashape( GEOLATTE, scope ); } - public void alphashape(String pckg) throws SQLException { + public void alphashape(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getAlphaShape( 1 ); String hql = format( Locale.ENGLISH, "SELECT id, alphashape(geom, 1) FROM %s where geometrytype(geom) in ('ST_Point', 'ST_MultiPoint')", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_area_on_jts() throws SQLException { - area( JTS ); + public void test_area_on_jts(SessionFactoryScope scope) throws SQLException { + area( JTS, scope ); } @Test - public void test_area_on_geolatte() throws SQLException { - area( GEOLATTE ); + public void test_area_on_geolatte(SessionFactoryScope scope) throws SQLException { + area( GEOLATTE, scope ); } - public void area(String pckg) throws SQLException { + public void area(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getArea(); String hql = format( "SELECT id, area(geom) FROM %s where geometrytype(geom) in ('ST_Polygon', 'ST_MultiPolygon')", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_asewkb_on_jts() throws SQLException { - asewkb( JTS ); + public void test_asewkb_on_jts(SessionFactoryScope scope) throws SQLException { + asewkb( JTS, scope ); } @Test - public void test_asewkb_on_geolatte() throws SQLException { - asewkb( GEOLATTE ); + public void test_asewkb_on_geolatte(SessionFactoryScope scope) throws SQLException { + asewkb( GEOLATTE, scope ); } - public void asewkb(String pckg) throws SQLException { + public void asewkb(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getAsEWKB(); String hql = format( "SELECT id, asewkb(geom) FROM %s", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_asewkt_on_jts() throws SQLException { - asewkt( JTS ); + public void test_asewkt_on_jts(SessionFactoryScope scope) throws SQLException { + asewkt( JTS, scope ); } @Test - public void test_asewkt_on_geolatte() throws SQLException { - asewkt( GEOLATTE ); + public void test_asewkt_on_geolatte(SessionFactoryScope scope) throws SQLException { + asewkt( GEOLATTE, scope ); } - public void asewkt(String pckg) throws SQLException { + public void asewkt(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getAsEWKT(); String hql = format( "SELECT id, asewkt(geom) FROM %s", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_asgeojson_on_jts() throws SQLException { - asgeojson( JTS ); + public void test_asgeojson_on_jts(SessionFactoryScope scope) throws SQLException { + asgeojson( JTS, scope ); } @Test - public void test_asgeojson_on_geolatte() throws SQLException { - asgeojson( GEOLATTE ); + public void test_asgeojson_on_geolatte(SessionFactoryScope scope) throws SQLException { + asgeojson( GEOLATTE, scope ); } - public void asgeojson(String pckg) throws SQLException { + public void asgeojson(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getAsGeoJSON(); String hql = format( "SELECT id, asgeojson(geom) FROM %s", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_assvg_on_jts() throws SQLException { - assvg( JTS ); + public void test_assvg_on_jts(SessionFactoryScope scope) throws SQLException { + assvg( JTS, scope ); } @Test - public void test_assvg_on_geolatte() throws SQLException { - assvg( GEOLATTE ); + public void test_assvg_on_geolatte(SessionFactoryScope scope) throws SQLException { + assvg( GEOLATTE, scope ); } - public void assvg(String pckg) throws SQLException { + public void assvg(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getAsSVG(); String hql = format( "SELECT id, assvg(geom) FROM %s", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_assvgaggr_on_jts() throws SQLException { - assvgaggr( JTS ); + public void test_assvgaggr_on_jts(SessionFactoryScope scope) throws SQLException { + assvgaggr( JTS, scope ); } @Test - public void test_assvgaggr_on_geolatte() throws SQLException { - assvgaggr( GEOLATTE ); + public void test_assvgaggr_on_geolatte(SessionFactoryScope scope) throws SQLException { + assvgaggr( GEOLATTE, scope ); } - public void assvgaggr(String pckg) throws SQLException { + public void assvgaggr(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getAsSVGAggr(); String hql = format( "SELECT cast(count(g) as int), assvgaggr(geom) FROM %s g", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_aswkb_on_jts() throws SQLException { - aswkb( JTS ); + public void test_aswkb_on_jts(SessionFactoryScope scope) throws SQLException { + aswkb( JTS, scope ); } @Test - public void test_aswkb_on_geolatte() throws SQLException { - aswkb( GEOLATTE ); + public void test_aswkb_on_geolatte(SessionFactoryScope scope) throws SQLException { + aswkb( GEOLATTE, scope ); } - public void aswkb(String pckg) throws SQLException { + public void aswkb(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getAsWKB(); String hql = format( "SELECT id, aswkb(geom) FROM %s", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_aswkt_on_jts() throws SQLException { - aswkt( JTS ); + public void test_aswkt_on_jts(SessionFactoryScope scope) throws SQLException { + aswkt( JTS, scope ); } @Test - public void test_aswkt_on_geolatte() throws SQLException { - aswkt( GEOLATTE ); + public void test_aswkt_on_geolatte(SessionFactoryScope scope) throws SQLException { + aswkt( GEOLATTE, scope ); } - public void aswkt(String pckg) throws SQLException { + public void aswkt(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getAsWKT(); String hql = format( "SELECT id, aswkt(geom) FROM %s", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_convexhullaggr_on_jts() throws SQLException { - convexhullaggr( JTS ); + public void test_convexhullaggr_on_jts(SessionFactoryScope scope) throws SQLException { + convexhullaggr( JTS, scope ); } @Test - public void test_convexhullaggr_on_geolatte() throws SQLException { - convexhullaggr( GEOLATTE ); + public void test_convexhullaggr_on_geolatte(SessionFactoryScope scope) throws SQLException { + convexhullaggr( GEOLATTE, scope ); } - public void convexhullaggr(String pckg) throws SQLException { + public void convexhullaggr(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getConvexHullAggr(); String hql = format( "SELECT cast(count(g) as int), convexhullaggr(geom) FROM %s g", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_centroid_on_jts() throws SQLException { - centroid( JTS ); + public void test_centroid_on_jts(SessionFactoryScope scope) throws SQLException { + centroid( JTS, scope ); } @Test - public void test_centroid_on_geolatte() throws SQLException { - centroid( GEOLATTE ); + public void test_centroid_on_geolatte(SessionFactoryScope scope) throws SQLException { + centroid( GEOLATTE, scope ); } - public void centroid(String pckg) throws SQLException { + public void centroid(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getCentroid(); String hql = format( "SELECT id, centroid(geom) FROM %s g where geometrytype(geom) = 'ST_Polygon'", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_coorddim_on_jts() throws SQLException { - coorddim( JTS ); + public void test_coorddim_on_jts(SessionFactoryScope scope) throws SQLException { + coorddim( JTS, scope ); } @Test - public void test_coorddim_on_geolatte() throws SQLException { - coorddim( GEOLATTE ); + public void test_coorddim_on_geolatte(SessionFactoryScope scope) throws SQLException { + coorddim( GEOLATTE, scope ); } - public void coorddim(String pckg) throws SQLException { + public void coorddim(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getCoordDim(); String hql = format( "SELECT id, coorddim(geom) FROM %s", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_coveredby_on_jts() throws SQLException { - coveredby( JTS ); + public void test_coveredby_on_jts(SessionFactoryScope scope) throws SQLException { + coveredby( JTS, scope ); } @Test - public void test_coveredby_on_geolatte() throws SQLException { - coveredby( GEOLATTE ); + public void test_coveredby_on_geolatte(SessionFactoryScope scope) throws SQLException { + coveredby( GEOLATTE, scope ); } - public void coveredby(String pckg) throws SQLException { + public void coveredby(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getCoveredBy( expectationsFactory.getTestPolygon() ); String hql = format( "SELECT id, coveredby(geom, :filter) FROM %s where coveredby(geom, :filter) = true and srid(geom) = %d", @@ -281,20 +274,20 @@ public void coveredby(String pckg) throws SQLException { expectationsFactory.getTestSrid() ); Map params = createQueryParams( "filter", expectationsFactory.getTestPolygon() ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_covers_on_jts() throws SQLException { - covers( JTS ); + public void test_covers_on_jts(SessionFactoryScope scope) throws SQLException { + covers( JTS, scope ); } @Test - public void test_covers_on_geolatte() throws SQLException { - covers( GEOLATTE ); + public void test_covers_on_geolatte(SessionFactoryScope scope) throws SQLException { + covers( GEOLATTE, scope ); } - public void covers(String pckg) throws SQLException { + public void covers(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getCovers( expectationsFactory.getTestPolygon() ); String hql = format( "SELECT id, covers(geom, :filter) FROM %s where covers(geom, :filter) = true and srid(geom) = %d", @@ -302,77 +295,77 @@ public void covers(String pckg) throws SQLException { expectationsFactory.getTestSrid() ); Map params = createQueryParams( "filter", expectationsFactory.getTestPolygon() ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_endpoint_on_jts() throws SQLException { - endpoint( JTS ); + public void test_endpoint_on_jts(SessionFactoryScope scope) throws SQLException { + endpoint( JTS, scope ); } @Test - public void test_endpoint_on_geolatte() throws SQLException { - endpoint( GEOLATTE ); + public void test_endpoint_on_geolatte(SessionFactoryScope scope) throws SQLException { + endpoint( GEOLATTE, scope ); } - public void endpoint(String pckg) throws SQLException { + public void endpoint(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getEndPoint(); String hql = format( "SELECT id, endpoint(geom) FROM %s g where geometrytype(geom) = 'ST_LineString'", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_envelopeaggr_on_jts() throws SQLException { - envelopeaggr( JTS ); + public void test_envelopeaggr_on_jts(SessionFactoryScope scope) throws SQLException { + envelopeaggr( JTS, scope ); } @Test - public void test_envelopeaggr_on_geolatte() throws SQLException { - envelopeaggr( GEOLATTE ); + public void test_envelopeaggr_on_geolatte(SessionFactoryScope scope) throws SQLException { + envelopeaggr( GEOLATTE, scope ); } - public void envelopeaggr(String pckg) throws SQLException { + public void envelopeaggr(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getEnvelopeAggr(); String hql = format( "SELECT cast(count(g) as int), envelopeaggr(geom) FROM %s g", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_exteriorring_on_jts() throws SQLException { - exteriorring( JTS ); + public void test_exteriorring_on_jts(SessionFactoryScope scope) throws SQLException { + exteriorring( JTS, scope ); } @Test - public void test_exteriorring_on_geolatte() throws SQLException { - exteriorring( GEOLATTE ); + public void test_exteriorring_on_geolatte(SessionFactoryScope scope) throws SQLException { + exteriorring( GEOLATTE, scope ); } - public void exteriorring(String pckg) throws SQLException { + public void exteriorring(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getExteriorRing(); String hql = format( "SELECT id, exteriorring(geom) FROM %s g where geometrytype(geom) = 'ST_Polygon'", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_geomfromewkb_on_jts() throws SQLException { - geomfromewkb( JTS ); + public void test_geomfromewkb_on_jts(SessionFactoryScope scope) throws SQLException { + geomfromewkb( JTS, scope ); } @Test - public void test_geomfromewkb_on_geolatte() throws SQLException { - geomfromewkb( GEOLATTE ); + public void test_geomfromewkb_on_geolatte(SessionFactoryScope scope) throws SQLException { + geomfromewkb( GEOLATTE, scope ); } - public void geomfromewkb(String pckg) throws SQLException { + public void geomfromewkb(String pckg, SessionFactoryScope scope) throws SQLException { WKBWriter writer = new WKBWriter( 2, true ); byte[] ewkb = writer.write( expectationsFactory.getTestPolygon() ); Map dbexpected = hanaExpectationsFactory.getGeomFromEWKB( ewkb ); @@ -382,22 +375,23 @@ public void geomfromewkb(String pckg) throws SQLException { entityName( pckg ) ); Map params = createQueryParams( "param", ewkb ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_geomfromewkt_on_jts() throws SQLException { - geomfromewkt( JTS ); + public void test_geomfromewkt_on_jts(SessionFactoryScope scope) throws SQLException { + geomfromewkt( JTS, scope ); } @Test - public void test_geomfromewkt_on_geolatte() throws SQLException { - geomfromewkt( GEOLATTE ); + public void test_geomfromewkt_on_geolatte(SessionFactoryScope scope) throws SQLException { + geomfromewkt( GEOLATTE, scope ); } - public void geomfromewkt(String pckg) throws SQLException { + public void geomfromewkt(String pckg, SessionFactoryScope scope) throws SQLException { WKTWriter writer = new WKTWriter(); - String ewkt = "SRID=" + expectationsFactory.getTestSrid() + ";" + writer.write( expectationsFactory.getTestPolygon() ); + String ewkt = "SRID=" + expectationsFactory.getTestSrid() + ";" + writer.write( + expectationsFactory.getTestPolygon() ); Map dbexpected = hanaExpectationsFactory.getGeomFromEWKT( ewkt ); String hql = format( "SELECT 1, cast(geomfromewkt(:param) as %s) FROM %s g", @@ -405,20 +399,20 @@ public void geomfromewkt(String pckg) throws SQLException { entityName( pckg ) ); Map params = createQueryParams( "param", ewkt ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_geomfromtext_on_jts() throws SQLException { - geomfromtext( JTS ); + public void test_geomfromtext_on_jts(SessionFactoryScope scope) throws SQLException { + geomfromtext( JTS, scope ); } @Test - public void test_geomfromtext_on_geolatte() throws SQLException { - geomfromtext( GEOLATTE ); + public void test_geomfromtext_on_geolatte(SessionFactoryScope scope) throws SQLException { + geomfromtext( GEOLATTE, scope ); } - public void geomfromtext(String pckg) throws SQLException { + public void geomfromtext(String pckg, SessionFactoryScope scope) throws SQLException { String text = expectationsFactory.getTestPolygon().toText(); Map dbexpected = hanaExpectationsFactory.getGeomFromText( text ); String hql = format( @@ -427,20 +421,20 @@ public void geomfromtext(String pckg) throws SQLException { entityName( pckg ) ); Map params = createQueryParams( "param", text ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_geomfromwkb_on_jts() throws SQLException { - geomfromwkb( JTS ); + public void test_geomfromwkb_on_jts(SessionFactoryScope scope) throws SQLException { + geomfromwkb( JTS, scope ); } @Test - public void test_geomfromwkb_on_geolatte() throws SQLException { - geomfromwkb( GEOLATTE ); + public void test_geomfromwkb_on_geolatte(SessionFactoryScope scope) throws SQLException { + geomfromwkb( GEOLATTE, scope ); } - public void geomfromwkb(String pckg) throws SQLException { + public void geomfromwkb(String pckg, SessionFactoryScope scope) throws SQLException { WKBWriter writer = new WKBWriter( 2, false ); byte[] wkb = writer.write( expectationsFactory.getTestPolygon() ); Map dbexpected = hanaExpectationsFactory.getGeomFromWKB( wkb ); @@ -450,20 +444,20 @@ public void geomfromwkb(String pckg) throws SQLException { entityName( pckg ) ); Map params = createQueryParams( "param", wkb ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_geomfromwkt_on_jts() throws SQLException { - geomfromwkt( JTS ); + public void test_geomfromwkt_on_jts(SessionFactoryScope scope) throws SQLException { + geomfromwkt( JTS, scope ); } @Test - public void test_geomfromwkt_on_geolatte() throws SQLException { - geomfromwkt( GEOLATTE ); + public void test_geomfromwkt_on_geolatte(SessionFactoryScope scope) throws SQLException { + geomfromwkt( GEOLATTE, scope ); } - public void geomfromwkt(String pckg) throws SQLException { + public void geomfromwkt(String pckg, SessionFactoryScope scope) throws SQLException { WKTWriter writer = new WKTWriter(); String wkt = writer.write( expectationsFactory.getTestPolygon() ); Map dbexpected = hanaExpectationsFactory.getGeomFromWKT( wkt ); @@ -473,20 +467,20 @@ public void geomfromwkt(String pckg) throws SQLException { entityName( pckg ) ); Map params = createQueryParams( "param", wkt ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_geometryn_on_jts() throws SQLException { - geometryn( JTS ); + public void test_geometryn_on_jts(SessionFactoryScope scope) throws SQLException { + geometryn( JTS, scope ); } @Test - public void test_geometryn_on_geolatte() throws SQLException { - geometryn( GEOLATTE ); + public void test_geometryn_on_geolatte(SessionFactoryScope scope) throws SQLException { + geometryn( GEOLATTE, scope ); } - public void geometryn(String pckg) throws SQLException { + public void geometryn(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getGeometryN( 1 ); String hql = format( "SELECT id, cast(geometryn(geom, :n) as %s) FROM %s g where geometrytype(geom) = 'ST_GeometryCollection'", @@ -494,20 +488,20 @@ public void geometryn(String pckg) throws SQLException { entityName( pckg ) ); Map params = createQueryParams( "n", 1 ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_interiorringn_on_jts() throws SQLException { - interiorringn( JTS ); + public void test_interiorringn_on_jts(SessionFactoryScope scope) throws SQLException { + interiorringn( JTS, scope ); } @Test - public void test_interiorringn_on_geolatte() throws SQLException { - interiorringn( GEOLATTE ); + public void test_interiorringn_on_geolatte(SessionFactoryScope scope) throws SQLException { + interiorringn( GEOLATTE, scope ); } - public void interiorringn(String pckg) throws SQLException { + public void interiorringn(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getInteriorRingN( 1 ); String hql = format( "SELECT id, cast(interiorringn(geom, :n) as %s) FROM %s g where geometrytype(geom) = 'ST_Polygon'", @@ -515,39 +509,39 @@ public void interiorringn(String pckg) throws SQLException { entityName( pckg ) ); Map params = createQueryParams( "n", 1 ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_intersectionaggr_on_jts() throws SQLException { - intersectionaggr( JTS ); + public void test_intersectionaggr_on_jts(SessionFactoryScope scope) throws SQLException { + intersectionaggr( JTS, scope ); } @Test - public void test_intersectionaggr_on_geolatte() throws SQLException { - intersectionaggr( GEOLATTE ); + public void test_intersectionaggr_on_geolatte(SessionFactoryScope scope) throws SQLException { + intersectionaggr( GEOLATTE, scope ); } - public void intersectionaggr(String pckg) throws SQLException { + public void intersectionaggr(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getIntersectionAggr(); String hql = format( "SELECT cast(count(g) as int), intersectionaggr(geom) FROM %s g", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_intersectsrect_on_jts() throws SQLException { - intersectsrect( JTS ); + public void test_intersectsrect_on_jts(SessionFactoryScope scope) throws SQLException { + intersectsrect( JTS, scope ); } @Test - public void test_intersectsrect_on_geolatte() throws SQLException { - intersectsrect( GEOLATTE ); + public void test_intersectsrect_on_geolatte(SessionFactoryScope scope) throws SQLException { + intersectsrect( GEOLATTE, scope ); } - public void intersectsrect(String pckg) throws SQLException { + public void intersectsrect(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getIntersectsRect( (Point) expectationsFactory.getTestPoint().reverse(), expectationsFactory.getTestPoint() @@ -559,339 +553,340 @@ public void intersectsrect(String pckg) throws SQLException { ); Map params = createQueryParams( "pmin", expectationsFactory.getTestPoint().reverse() ); params.put( "pmax", expectationsFactory.getTestPoint() ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_is3d_on_jts() throws SQLException { - is3d( JTS ); + public void test_is3d_on_jts(SessionFactoryScope scope) throws SQLException { + is3d( JTS, scope ); } @Test - public void test_is3d_on_geolatte() throws SQLException { - is3d( GEOLATTE ); + public void test_is3d_on_geolatte(SessionFactoryScope scope) throws SQLException { + is3d( GEOLATTE, scope ); } - public void is3d(String pckg) throws SQLException { + public void is3d(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getIs3D(); String hql = format( "SELECT id, is3d(geom) FROM %s where is3d(geom) = true and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_isclosed_on_jts() throws SQLException { - isclosed( JTS ); + public void test_isclosed_on_jts(SessionFactoryScope scope) throws SQLException { + isclosed( JTS, scope ); } @Test - public void test_isclosed_on_geolatte() throws SQLException { - isclosed( GEOLATTE ); + public void test_isclosed_on_geolatte(SessionFactoryScope scope) throws SQLException { + isclosed( GEOLATTE, scope ); } - public void isclosed(String pckg) throws SQLException { + public void isclosed(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getIsClosed(); String hql = format( "SELECT id, isclosed(geom) FROM %s where geometrytype(geom) in ('ST_LineString', 'ST_MultiLineString') and isclosed(geom) = true and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_ismeasured_on_jts() throws SQLException { - ismeasured( JTS ); + public void test_ismeasured_on_jts(SessionFactoryScope scope) throws SQLException { + ismeasured( JTS, scope ); } @Test - public void test_ismeasured_on_geolatte() throws SQLException { - ismeasured( GEOLATTE ); + public void test_ismeasured_on_geolatte(SessionFactoryScope scope) throws SQLException { + ismeasured( GEOLATTE, scope ); } - public void ismeasured(String pckg) throws SQLException { + public void ismeasured(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getIsMeasured(); String hql = format( "SELECT id, ismeasured(geom) FROM %s where ismeasured(geom) = true and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_isring_on_jts() throws SQLException { - isring( JTS ); + public void test_isring_on_jts(SessionFactoryScope scope) throws SQLException { + isring( JTS, scope ); } @Test - public void test_isring_on_geolatte() throws SQLException { - isring( GEOLATTE ); + public void test_isring_on_geolatte(SessionFactoryScope scope) throws SQLException { + isring( GEOLATTE, scope ); } - public void isring(String pckg) throws SQLException { + public void isring(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getIsRing(); String hql = format( "SELECT id, isring(geom) FROM %s where geometrytype(geom) in ('ST_LineString') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_isvalid_on_jts() throws SQLException { - isvalid( JTS ); + public void test_isvalid_on_jts(SessionFactoryScope scope) throws SQLException { + isvalid( JTS, scope ); } @Test - public void test_isvalid_on_geolatte() throws SQLException { - isvalid( GEOLATTE ); + public void test_isvalid_on_geolatte(SessionFactoryScope scope) throws SQLException { + isvalid( GEOLATTE, scope ); } - public void isvalid(String pckg) throws SQLException { + public void isvalid(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getIsValid(); String hql = format( "SELECT id, isvalid(geom) FROM %s where isvalid(geom) = true and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_length_on_jts() throws SQLException { - length( JTS ); + public void test_length_on_jts(SessionFactoryScope scope) throws SQLException { + length( JTS, scope ); } @Test - public void test_length_on_geolatte() throws SQLException { - length( GEOLATTE ); + public void test_length_on_geolatte(SessionFactoryScope scope) throws SQLException { + length( GEOLATTE, scope ); } - public void length(String pckg) throws SQLException { + public void length(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getLength(); String hql = format( "SELECT id, length(geom) FROM %s where geometrytype(geom) in ('ST_LineString', 'ST_MultiLineString') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_m_on_jts() throws SQLException { - m( JTS ); + public void test_m_on_jts(SessionFactoryScope scope) throws SQLException { + m( JTS, scope ); } @Test - public void test_m_on_geolatte() throws SQLException { - m( GEOLATTE ); + public void test_m_on_geolatte(SessionFactoryScope scope) throws SQLException { + m( GEOLATTE, scope ); } - public void m(String pckg) throws SQLException { + public void m(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getM(); String hql = format( "SELECT id, m(geom) FROM %s where geometrytype(geom) in ('ST_Point') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_mmax_on_jts() throws SQLException { - mmax( JTS ); + public void test_mmax_on_jts(SessionFactoryScope scope) throws SQLException { + mmax( JTS, scope ); } @Test - public void test_mmax_on_geolatte() throws SQLException { - mmax( GEOLATTE ); + public void test_mmax_on_geolatte(SessionFactoryScope scope) throws SQLException { + mmax( GEOLATTE, scope ); } - public void mmax(String pckg) throws SQLException { + public void mmax(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getMMax(); String hql = format( "SELECT id, mmax(geom) FROM %s where srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_mmin_on_jts() throws SQLException { - mmin( JTS ); + public void test_mmin_on_jts(SessionFactoryScope scope) throws SQLException { + mmin( JTS, scope ); } @Test - public void test_mmin_on_geolatte() throws SQLException { - mmin( GEOLATTE ); + public void test_mmin_on_geolatte(SessionFactoryScope scope) throws SQLException { + mmin( GEOLATTE, scope ); } - public void mmin(String pckg) throws SQLException { + public void mmin(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getMMin(); String hql = format( "SELECT id, mmin(geom) FROM %s where srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_numgeometries_on_jts() throws SQLException { - numgeometries( JTS ); + public void test_numgeometries_on_jts(SessionFactoryScope scope) throws SQLException { + numgeometries( JTS, scope ); } @Test - public void test_numgeometries_on_geolatte() throws SQLException { - numgeometries( GEOLATTE ); + public void test_numgeometries_on_geolatte(SessionFactoryScope scope) throws SQLException { + numgeometries( GEOLATTE, scope ); } - public void numgeometries(String pckg) throws SQLException { + public void numgeometries(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getNumGeometries(); String hql = format( "SELECT id, numgeometries(geom) FROM %s where geometrytype(geom) in ('ST_GeometryCollection') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_numinteriorring_on_jts() throws SQLException { - numinteriorring( JTS ); + public void test_numinteriorring_on_jts(SessionFactoryScope scope) throws SQLException { + numinteriorring( JTS, scope ); } @Test - public void test_numnuminteriorring_on_geolatte() throws SQLException { - numinteriorring( GEOLATTE ); + public void test_numnuminteriorring_on_geolatte(SessionFactoryScope scope) throws SQLException { + numinteriorring( GEOLATTE, scope ); } - public void numinteriorring(String pckg) throws SQLException { + public void numinteriorring(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getNumInteriorRing(); String hql = format( "SELECT id, numinteriorring(geom) FROM %s where geometrytype(geom) in ('ST_Polygon') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_numinteriorrings_on_jts() throws SQLException { - numinteriorrings( JTS ); + public void test_numinteriorrings_on_jts(SessionFactoryScope scope) throws SQLException { + numinteriorrings( JTS, scope ); } @Test - public void test_numnuminteriorrings_on_geolatte() throws SQLException { - numinteriorrings( GEOLATTE ); + public void test_numnuminteriorrings_on_geolatte(SessionFactoryScope scope) throws SQLException { + numinteriorrings( GEOLATTE, scope ); } - public void numinteriorrings(String pckg) throws SQLException { + public void numinteriorrings(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getNumInteriorRings(); String hql = format( "SELECT id, numinteriorrings(geom) FROM %s where geometrytype(geom) in ('ST_Polygon') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_numpoints_on_jts() throws SQLException { - numpoints( JTS ); + public void test_numpoints_on_jts(SessionFactoryScope scope) throws SQLException { + numpoints( JTS, scope ); } @Test - public void test_numpoints_on_geolatte() throws SQLException { - numpoints( GEOLATTE ); + public void test_numpoints_on_geolatte(SessionFactoryScope scope) throws SQLException { + numpoints( GEOLATTE, scope ); } - public void numpoints(String pckg) throws SQLException { + public void numpoints(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getNumPoints(); String hql = format( "SELECT id, numpoints(geom) FROM %s where geometrytype(geom) in ('ST_LineString') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_orderingequals_on_jts() throws SQLException { - orderingequals( JTS ); + public void test_orderingequals_on_jts(SessionFactoryScope scope) throws SQLException { + orderingequals( JTS, scope ); } @Test - public void test_orderingequals_on_geolatte() throws SQLException { - orderingequals( GEOLATTE ); + public void test_orderingequals_on_geolatte(SessionFactoryScope scope) throws SQLException { + orderingequals( GEOLATTE, scope ); } - public void orderingequals(String pckg) throws SQLException { - Map dbexpected = hanaExpectationsFactory.getOrderingEquals( expectationsFactory.getTestPolygon() ); + public void orderingequals(String pckg, SessionFactoryScope scope) throws SQLException { + Map dbexpected = hanaExpectationsFactory.getOrderingEquals( + expectationsFactory.getTestPolygon() ); String hql = format( "SELECT id, orderingequals(geom, :filter) FROM %s where orderingequals(geom, :filter) = true and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); Map params = createQueryParams( "filter", expectationsFactory.getTestPolygon() ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } @Test - public void test_perimeter_on_jts() throws SQLException { - perimeter( JTS ); + public void test_perimeter_on_jts(SessionFactoryScope scope) throws SQLException { + perimeter( JTS, scope ); } @Test - public void test_perimeter_on_geolatte() throws SQLException { - perimeter( GEOLATTE ); + public void test_perimeter_on_geolatte(SessionFactoryScope scope) throws SQLException { + perimeter( GEOLATTE, scope ); } - public void perimeter(String pckg) throws SQLException { + public void perimeter(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getPerimeter(); String hql = format( "SELECT id, perimeter(geom) FROM %s where geometrytype(geom) in ('ST_Polygon', 'ST_MultiPolygon') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_pointonsurface_on_jts() throws SQLException { - pointonsurface( JTS ); + public void test_pointonsurface_on_jts(SessionFactoryScope scope) throws SQLException { + pointonsurface( JTS, scope ); } @Test - public void test_pointonsurface_on_geolatte() throws SQLException { - pointonsurface( GEOLATTE ); + public void test_pointonsurface_on_geolatte(SessionFactoryScope scope) throws SQLException { + pointonsurface( GEOLATTE, scope ); } - public void pointonsurface(String pckg) throws SQLException { + public void pointonsurface(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getPointOnSurface(); String hql = format( "SELECT id, pointonsurface(geom) FROM %s where geometrytype(geom) in ('ST_Polygon', 'ST_MultiPolygon') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_pointn_on_jts() throws SQLException { - pointn( JTS ); + public void test_pointn_on_jts(SessionFactoryScope scope) throws SQLException { + pointn( JTS, scope ); } @Test - public void test_pointn_on_geolatte() throws SQLException { - pointn( GEOLATTE ); + public void test_pointn_on_geolatte(SessionFactoryScope scope) throws SQLException { + pointn( GEOLATTE, scope ); } - public void pointn(String pckg) throws SQLException { + public void pointn(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getPointN( 1 ); String hql = format( "SELECT id, pointn(geom, :n) FROM %s where geometrytype(geom) in ('ST_LineString') and srid(geom) = %d", @@ -899,267 +894,268 @@ public void pointn(String pckg) throws SQLException { expectationsFactory.getTestSrid() ); Map params = createQueryParams( "n", 1 ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } // ST_GEOMETRY columns are not supported - @Test(expected = SQLException.class) - public void test_snaptogrid_on_jts() throws SQLException { - snaptogrid( JTS ); + @Test + public void test_snaptogrid_on_jts(SessionFactoryScope scope) { + assertThrows( SQLException.class, () -> snaptogrid( JTS, scope ) ); } // ST_GEOMETRY columns are not supported - @Test(expected = SQLException.class) - public void test_snaptogrid_on_geolatte() throws SQLException { - snaptogrid( GEOLATTE ); + @Test + public void test_snaptogrid_on_geolatte(SessionFactoryScope scope) { + assertThrows( SQLException.class, () -> snaptogrid( GEOLATTE, scope ) ); } - public void snaptogrid(String pckg) throws SQLException { + public void snaptogrid(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getSnapToGrid(); String hql = format( "SELECT id, snaptogrid(geom) FROM %s where srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_startpoint_on_jts() throws SQLException { - startpoint( JTS ); + public void test_startpoint_on_jts(SessionFactoryScope scope) throws SQLException { + startpoint( JTS, scope ); } @Test - public void test_startpoint_on_geolatte() throws SQLException { - startpoint( GEOLATTE ); + public void test_startpoint_on_geolatte(SessionFactoryScope scope) throws SQLException { + startpoint( GEOLATTE, scope ); } - public void startpoint(String pckg) throws SQLException { + public void startpoint(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getStartPoint(); String hql = format( "SELECT id, startpoint(geom) FROM %s g where geometrytype(geom) = 'ST_LineString'", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_unionaggr_on_jts() throws SQLException { - unionaggr( JTS ); + public void test_unionaggr_on_jts(SessionFactoryScope scope) throws SQLException { + unionaggr( JTS, scope ); } @Test - public void test_unionaggr_on_geolatte() throws SQLException { - unionaggr( GEOLATTE ); + public void test_unionaggr_on_geolatte(SessionFactoryScope scope) throws SQLException { + unionaggr( GEOLATTE, scope ); } - public void unionaggr(String pckg) throws SQLException { + public void unionaggr(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getUnionAggr(); String hql = format( "SELECT cast(count(g) as int), unionaggr(geom) FROM %s g", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_x_on_jts() throws SQLException { - x( JTS ); + public void test_x_on_jts(SessionFactoryScope scope) throws SQLException { + x( JTS, scope ); } @Test - public void test_x_on_geolatte() throws SQLException { - x( GEOLATTE ); + public void test_x_on_geolatte(SessionFactoryScope scope) throws SQLException { + x( GEOLATTE, scope ); } - public void x(String pckg) throws SQLException { + public void x(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getX(); String hql = format( "SELECT id, x(geom) FROM %s where geometrytype(geom) in ('ST_Point') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_xmax_on_jts() throws SQLException { - xmax( JTS ); + public void test_xmax_on_jts(SessionFactoryScope scope) throws SQLException { + xmax( JTS, scope ); } @Test - public void test_xmax_on_geolatte() throws SQLException { - xmax( GEOLATTE ); + public void test_xmax_on_geolatte(SessionFactoryScope scope) throws SQLException { + xmax( GEOLATTE, scope ); } - public void xmax(String pckg) throws SQLException { + public void xmax(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getXMax(); String hql = format( "SELECT id, xmax(geom) FROM %s where srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_xmin_on_jts() throws SQLException { - xmin( JTS ); + public void test_xmin_on_jts(SessionFactoryScope scope) throws SQLException { + xmin( JTS, scope ); } @Test - public void test_xmin_on_geolatte() throws SQLException { - xmin( GEOLATTE ); + public void test_xmin_on_geolatte(SessionFactoryScope scope) throws SQLException { + xmin( GEOLATTE, scope ); } - public void xmin(String pckg) throws SQLException { + public void xmin(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getXMin(); String hql = format( "SELECT id, xmin(geom) FROM %s where srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_y_on_jts() throws SQLException { - y( JTS ); + public void test_y_on_jts(SessionFactoryScope scope) throws SQLException { + y( JTS, scope ); } @Test - public void test_y_on_geolatte() throws SQLException { - y( GEOLATTE ); + public void test_y_on_geolatte(SessionFactoryScope scope) throws SQLException { + y( GEOLATTE, scope ); } - public void y(String pckg) throws SQLException { + public void y(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getY(); String hql = format( "SELECT id, y(geom) FROM %s where geometrytype(geom) in ('ST_Point') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_ymax_on_jts() throws SQLException { - ymax( JTS ); + public void test_ymax_on_jts(SessionFactoryScope scope) throws SQLException { + ymax( JTS, scope ); } @Test - public void test_ymax_on_geolatte() throws SQLException { - ymax( GEOLATTE ); + public void test_ymax_on_geolatte(SessionFactoryScope scope) throws SQLException { + ymax( GEOLATTE, scope ); } - public void ymax(String pckg) throws SQLException { + public void ymax(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getYMax(); String hql = format( "SELECT id, ymax(geom) FROM %s where srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_ymin_on_jts() throws SQLException { - ymin( JTS ); + public void test_ymin_on_jts(SessionFactoryScope scope) throws SQLException { + ymin( JTS, scope ); } @Test - public void test_ymin_on_geolatte() throws SQLException { - ymin( GEOLATTE ); + public void test_ymin_on_geolatte(SessionFactoryScope scope) throws SQLException { + ymin( GEOLATTE, scope ); } - public void ymin(String pckg) throws SQLException { + public void ymin(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getYMin(); String hql = format( "SELECT id, ymin(geom) FROM %s where srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_z_on_jts() throws SQLException { - z( JTS ); + public void test_z_on_jts(SessionFactoryScope scope) throws SQLException { + z( JTS, scope ); } @Test - public void test_z_on_geolatte() throws SQLException { - z( GEOLATTE ); + public void test_z_on_geolatte(SessionFactoryScope scope) throws SQLException { + z( GEOLATTE, scope ); } - public void z(String pckg) throws SQLException { + public void z(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getZ(); String hql = format( "SELECT id, z(geom) FROM %s where geometrytype(geom) in ('ST_Point') and srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_zmax_on_jts() throws SQLException { - zmax( JTS ); + public void test_zmax_on_jts(SessionFactoryScope scope) throws SQLException { + zmax( JTS, scope ); } @Test - public void test_zmax_on_geolatte() throws SQLException { - zmax( GEOLATTE ); + public void test_zmax_on_geolatte(SessionFactoryScope scope) throws SQLException { + zmax( GEOLATTE, scope ); } - public void zmax(String pckg) throws SQLException { + public void zmax(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getZMax(); String hql = format( "SELECT id, zmax(geom) FROM %s where srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_zmin_on_jts() throws SQLException { - zmin( JTS ); + public void test_zmin_on_jts(SessionFactoryScope scope) throws SQLException { + zmin( JTS, scope ); } @Test - public void test_zmin_on_geolatte() throws SQLException { - zmin( GEOLATTE ); + public void test_zmin_on_geolatte(SessionFactoryScope scope) throws SQLException { + zmin( GEOLATTE, scope ); } - public void zmin(String pckg) throws SQLException { + public void zmin(String pckg, SessionFactoryScope scope) throws SQLException { Map dbexpected = hanaExpectationsFactory.getZMin(); String hql = format( "SELECT id, zmin(geom) FROM %s where srid(geom) = %d", entityName( pckg ), expectationsFactory.getTestSrid() ); - retrieveHQLResultsAndCompare( dbexpected, hql, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, pckg, scope ); } @Test - public void test_nestedfunction_on_jts() throws SQLException { - nestedfunction( JTS ); + public void test_nestedfunction_on_jts(SessionFactoryScope scope) throws SQLException { + nestedfunction( JTS, scope ); } @Test - public void test_nestedfunction_on_geolatte() throws SQLException { - nestedfunction( GEOLATTE ); + public void test_nestedfunction_on_geolatte(SessionFactoryScope scope) throws SQLException { + nestedfunction( GEOLATTE, scope ); } - public void nestedfunction(String pckg) throws SQLException { - Map dbexpected = hanaExpectationsFactory.getNestedFunctionInner( expectationsFactory.getTestPolygon() ); + public void nestedfunction(String pckg, SessionFactoryScope scope) throws SQLException { + Map dbexpected = hanaExpectationsFactory.getNestedFunctionInner( + expectationsFactory.getTestPolygon() ); String hql = format( "SELECT id, geom FROM %s g where dwithin(geom, srid(:filter, 0), 1) = true", entityName( pckg ) ); Map params = createQueryParams( "filter", expectationsFactory.getTestPolygon() ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); dbexpected = hanaExpectationsFactory.getNestedFunctionOuter( expectationsFactory.getTestPolygon() ); hql = format( "SELECT id, geom FROM %s g where dwithin(:filter, srid(geom, 0), 1) = true", entityName( pckg ) ); - retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg ); + retrieveHQLResultsAndCompare( dbexpected, hql, params, pckg, scope ); } private String getGeometryTypeFromPackage(String pckg) { @@ -1179,41 +1175,28 @@ private Map createQueryParams(String filterParamName, Object val return params; } - public void retrieveHQLResultsAndCompare(Map dbexpected, String hql, String geometryType) { - retrieveHQLResultsAndCompare( dbexpected, hql, null, geometryType ); + public void retrieveHQLResultsAndCompare(Map dbexpected, String hql, String geometryType, SessionFactoryScope scope) { + retrieveHQLResultsAndCompare( dbexpected, hql, null, geometryType, scope ); } protected void retrieveHQLResultsAndCompare( Map dbexpected, String hql, Map params, - String geometryType) { - Map hsreceived = new HashMap(); - doInSession( hql, hsreceived, params ); + String geometryType, + SessionFactoryScope scope) { + Map hsreceived = new HashMap<>(); + scope.inTransaction( + session -> { + Query query = session.createQuery( hql ); + setParameters( params, query ); + addQueryResults( hsreceived, query ); + } + ); compare( dbexpected, hsreceived, geometryType ); } - private void doInSession(String hql, Map result, Map params) { - Session session = null; - Transaction tx = null; - try { - session = openSession(); - tx = session.beginTransaction(); - Query query = session.createQuery( hql ); - setParameters( params, query ); - addQueryResults( result, query ); - } - finally { - if ( tx != null ) { - tx.rollback(); - } - if ( session != null ) { - session.close(); - } - } - } - private void setParameters(Map params, Query query) { if ( params == null ) { return; diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisTest.java index 3f0cc7aba962..1994532acb1f 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisTest.java @@ -4,32 +4,31 @@ */ package org.hibernate.spatial.dialect.postgis; -import java.util.List; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; - -import org.hibernate.dialect.PostgreSQLDialect; - -import org.hibernate.testing.RequiresDialect; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Ignore; -import org.junit.Test; - import org.geolatte.geom.C2D; import org.geolatte.geom.Point; import org.geolatte.geom.Polygon; import org.geolatte.geom.crs.CoordinateReferenceSystem; import org.geolatte.geom.crs.CoordinateReferenceSystems; +import org.hibernate.dialect.PostgreSQLDialect; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; import static org.geolatte.geom.builder.DSL.c; import static org.geolatte.geom.builder.DSL.point; import static org.geolatte.geom.builder.DSL.polygon; import static org.geolatte.geom.builder.DSL.ring; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; /** * Integration tests for Postgis @@ -37,69 +36,62 @@ * @author Vlad Mihalcea, Karel Maesen */ @RequiresDialect(PostgreSQLDialect.class) - -public class PostgisTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + PostgisTest.Event.class + } +) +@SessionFactory +public class PostgisTest { public static CoordinateReferenceSystem crs = CoordinateReferenceSystems.PROJECTED_2D_METER; private final Polygon window = polygon( crs, ring( c( 1, 1 ), c( 1, 20 ), - c( 20, 20 ), c( 20, 1 ), c( 1, 1 ) + c( 20, 20 ), c( 20, 1 ), c( 1, 1 ) ) ); - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Event.class, - }; + @AfterEach + public void cleanUp(SessionFactoryScope scope) { + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); } - @After - public void cleanUp() { - doInHibernate( this::sessionFactory, session -> { - session.createQuery( "delete from Event" ).executeUpdate(); - } ); - } - - @Test - public void testBuffer() { - Long addressId = insertEvent( c( 10, 5 ) ); + public void testBuffer(SessionFactoryScope scope) { + insertEvent( c( 10, 5 ), scope ); - doInHibernate( this::sessionFactory, session -> { - List events = session.createQuery( - "select e " + + scope.inTransaction( + session -> { + List events = session.createQuery( + "select e " + "from Event e " + "where within( e.location, buffer(:window, 100)) = true", Event.class ) - .setParameter( "window", window ) - .getResultList(); + .setParameter( "window", window ) + .getResultList(); - assertEquals( 1, events.size() ); - - } ); + assertThat( events ).hasSize( 1 ); + } + ); } @Test - @Ignore + @Disabled //TODO -- register these extra functions - public void testMakeEnvelope() { - Long addressId = insertEvent( c( 10, 5 ) ); + public void testMakeEnvelope(SessionFactoryScope scope) { + insertEvent( c( 10, 5 ), scope ); - doInHibernate( this::sessionFactory, session -> { + scope.inTransaction( session -> { List events = session.createQuery( "select e " + - "from Event e " + - "where within(e.location, makeenvelope(0, 0, 11, 11, -1 )) = true", Event.class ) + "from Event e " + + "where within(e.location, makeenvelope(0, 0, 11, 11, -1 )) = true", Event.class ) .getResultList(); - assertEquals( 1, events.size() ); - + assertThat( events ).hasSize( 1 ); } ); - } - private Long insertEvent(C2D position) { - return doInHibernate( this::sessionFactory, session -> { + private Long insertEvent(C2D position, SessionFactoryScope scope) { + return scope.fromTransaction( session -> { Event event = new Event(); event.setName( "Hibernate ORM presentation" ); Point pnt = point( crs, position ); diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisUnmarshalTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisUnmarshalTest.java index 528046aa4541..beefd343af5e 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisUnmarshalTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/PostgisUnmarshalTest.java @@ -4,10 +4,6 @@ */ package org.hibernate.spatial.dialect.postgis; -import java.sql.SQLException; - -import org.junit.Test; - import org.geolatte.geom.ByteOrder; import org.geolatte.geom.C2D; import org.geolatte.geom.G2D; @@ -16,12 +12,15 @@ import org.geolatte.geom.codec.Wkt; import org.geolatte.geom.crs.CoordinateReferenceSystem; import org.geolatte.geom.crs.CoordinateReferenceSystems; +import org.junit.jupiter.api.Test; import org.postgresql.util.PGobject; +import java.sql.SQLException; + +import static org.assertj.core.api.Java6Assertions.assertThat; import static org.geolatte.geom.builder.DSL.c; import static org.geolatte.geom.builder.DSL.g; import static org.geolatte.geom.builder.DSL.linestring; -import static org.junit.Assert.assertEquals; /** * Tests the different ways Postgis seraialises Geometries @@ -38,7 +37,6 @@ public class PostgisUnmarshalTest { c( 6.133, 53.244 ) ); - @Test public void testWktWithSrid() throws SQLException { String ewkt = Wkt.toWkt( geom ); @@ -63,13 +61,12 @@ public void testWkbNDR() throws SQLException { testCase( wkb, geom ); } - public void testCase(String pgValue, Geometry expected) throws SQLException { PGobject pgo = new PGobject(); pgo.setValue( pgValue ); Geometry received = PGGeometryJdbcType.INSTANCE_WKB_2.toGeometry( pgo ); - assertEquals( String.format( "Failure on %s", pgValue ), expected, received ); + assertThat( received ) + .describedAs( String.format( "Failure on %s", pgValue ) ) + .isEqualTo( expected ); } - - } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/hhh14523/DirtyCheckingTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/hhh14523/DirtyCheckingTest.java index 2b83656cbe63..685456143284 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/hhh14523/DirtyCheckingTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/hhh14523/DirtyCheckingTest.java @@ -4,85 +4,74 @@ */ package org.hibernate.spatial.dialect.postgis.hhh14523; -import java.io.Serializable; -import java.util.List; import jakarta.persistence.Column; import jakarta.persistence.Entity; -import jakarta.persistence.EntityManager; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; -import jakarta.persistence.Query; import jakarta.persistence.SequenceGenerator; import jakarta.persistence.Table; - -import org.hibernate.dialect.PostgreSQLDialect; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; - -import org.hibernate.testing.RequiresDialect; -import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; - +import jakarta.persistence.TypedQuery; import org.geolatte.geom.codec.Wkt; import org.geolatte.geom.jts.JTS; -import org.locationtech.jts.geom.GeometryFactory; +import org.hibernate.dialect.PostgreSQLDialect; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.locationtech.jts.geom.Point; +import java.io.Serializable; +import java.util.List; + import static org.junit.Assert.assertEquals; @JiraKey(value = "HHH-14523") @RequiresDialect(PostgreSQLDialect.class) -public class DirtyCheckingTest extends BaseEntityManagerFunctionalTestCase { - - private GeometryFactory gfact = new GeometryFactory(); - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { +@Jpa( + annotatedClasses = { TestEntity.class - }; - } - - public void createtestEntity() { - Point pnt = (Point) JTS.to( Wkt.fromWkt( "POINT Z( 3.41127795 8.11062269 2.611)", Wkt.Dialect.SFA_1_2_1 ) ); - EntityManager entityManager = createEntityManager(); - TestEntity test1 = new TestEntity( "radar 5", pnt ); - - entityManager.getTransaction().begin(); - entityManager.persist( test1 ); - entityManager.getTransaction().commit(); - - entityManager.close(); + } +) +public class DirtyCheckingTest { + + @BeforeAll + public void createtestEntity(EntityManagerFactoryScope scope) { + Point pnt = createPoint( "POINT Z( 3.41127795 8.11062269 2.611)" ); + scope.inTransaction( + entityManager -> + entityManager.persist( new TestEntity( "radar 5", pnt ) ) + ); } // Entities are auto-discovered, so just add them anywhere on class-path // Add your tests, using standard JUnit. @Test - public void hhh14523() throws Exception { - - createtestEntity(); - - EntityManager entityManager = createEntityManager(); - entityManager.getTransaction().begin(); - Query query = entityManager.createQuery( "select t from TestEntity t" ); - TestEntity ent = (TestEntity) query.getResultList().get( 0 ); - Point newPnt = (Point) JTS.to( Wkt.fromWkt( "POINT Z( 3.41127795 8.11062269 8.611)", Wkt.Dialect.SFA_1_2_1 ) ); - ent.setGeom( newPnt ); - entityManager.getTransaction().commit(); - entityManager.close(); - - - entityManager = createEntityManager(); - entityManager.getTransaction().begin(); - List entities = entityManager.createQuery( "select t from TestEntity t" ).getResultList(); - TestEntity ent2 = entities.get( 0 ); - try { - assertEquals( 8.611, ent2.getGeom().getCoordinate().getZ(), 0.00001 ); - } - finally { - entityManager.getTransaction().commit(); - } - entityManager.close(); + public void hhh14523(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + TypedQuery query = entityManager + .createQuery( "select t from TestEntity t", TestEntity.class ); + TestEntity ent = query.getResultList().get( 0 ); + ent.setGeom( createPoint( "POINT Z( 3.41127795 8.11062269 8.611)" ) ); + } + ); + + scope.inTransaction( + entityManager -> { + List entities = entityManager + .createQuery( "select t from TestEntity t", TestEntity.class ) + .getResultList(); + TestEntity ent2 = entities.get( 0 ); + assertEquals( 8.611, ent2.getGeom().getCoordinate().getZ(), 0.00001 ); + } + ); + } + + private static Point createPoint(String wkt) { + return (Point) JTS.to( Wkt.fromWkt( wkt, Wkt.Dialect.SFA_1_2_1 ) ); } } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/hhh14932/TestUsingPostgisWkb221AndLater.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/hhh14932/TestUsingPostgisWkb221AndLater.java index b7dcd8d30732..05cb26789626 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/hhh14932/TestUsingPostgisWkb221AndLater.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/dialect/postgis/hhh14932/TestUsingPostgisWkb221AndLater.java @@ -4,52 +4,53 @@ */ package org.hibernate.spatial.dialect.postgis.hhh14932; -import java.util.List; - -import org.hibernate.dialect.PostgreSQLDialect; - -import org.hibernate.testing.RequiresDialect; -import org.hibernate.testing.orm.junit.JiraKey; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; - import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; import org.geolatte.geom.G2D; import org.geolatte.geom.Point; +import org.hibernate.dialect.PostgreSQLDialect; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.JiraKey; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.geolatte.geom.builder.DSL.point; import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84; -import static org.junit.Assert.assertEquals; @JiraKey(value = "HHH-14932") - @RequiresDialect(PostgreSQLDialect.class) -public class TestUsingPostgisWkb221AndLater extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + TestUsingPostgisWkb221AndLater.Foo.class + } +) +@SessionFactory +public class TestUsingPostgisWkb221AndLater { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Foo.class }; - } - @Before - public void setup() { - inTransaction( session -> session.persist( new Foo( + @BeforeAll + public void setup(SessionFactoryScope scope) { + scope.inTransaction( session -> session.persist( new Foo( 1, point( WGS84 ) ) ) ); } @Test - public void test() { - inTransaction( session -> { + public void test(SessionFactoryScope scope) { + scope.inTransaction( session -> { List list = session .createQuery( "from Foo", Foo.class ) .getResultList(); - assertEquals( point( WGS84 ), list.get( 0 ).point ); + assertThat( list.get( 0 ).point ).isEqualTo( point( WGS84 ) ); } ); } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/geometry/JTSUtilsTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/geometry/JTSUtilsTest.java index 4cd74568c995..21734dc78056 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/geometry/JTSUtilsTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/geometry/JTSUtilsTest.java @@ -5,14 +5,15 @@ package org.hibernate.spatial.geometry; import org.hibernate.testing.orm.junit.JiraKey; -import org.junit.Test; import org.geolatte.geom.codec.Wkt; import org.geolatte.geom.jts.JTS; +import org.junit.jupiter.api.Test; import org.locationtech.jts.geom.Geometry; import org.geolatte.geom.jts.JTSUtils; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; + public class JTSUtilsTest { @@ -24,8 +25,8 @@ public void testGeometryCollection() { Geometry elem1a = JTS.to( Wkt.fromWkt( "GEOMETRYCOLLECTION(POINT(2 3))" ) ); Geometry elem1b = JTS.to( Wkt.fromWkt( "GEOMETRYCOLLECTION(POINT(2 3))" ) ); - assertTrue( JTSUtils.equalsExact3D( elem2a, elem2b ) ); - assertTrue( JTSUtils.equalsExact3D( elem1a, elem1b ) ); + assertThat( JTSUtils.equalsExact3D( elem2a, elem2b ) ).isTrue(); + assertThat( JTSUtils.equalsExact3D( elem1a, elem1b ) ).isTrue(); } } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialFunctionalTestCase.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialFunctionalTestCase.java index bfc3e674bf51..df2675361255 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialFunctionalTestCase.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialFunctionalTestCase.java @@ -4,14 +4,10 @@ */ package org.hibernate.spatial.testing; -import java.util.List; -import java.util.Map; import jakarta.persistence.Query; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.cfg.Configuration; +import org.geolatte.geom.GeometryEquality; import org.hibernate.dialect.Dialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.service.ServiceRegistry; import org.hibernate.spatial.HSMessageLogger; import org.hibernate.spatial.SpatialFunction; @@ -19,15 +15,15 @@ import org.hibernate.spatial.testing.datareader.TestSupport; import org.hibernate.spatial.testing.domain.GeomEntity; import org.hibernate.spatial.testing.domain.JtsGeomEntity; - -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - +import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest; +import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.locationtech.jts.geom.Geometry; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.fail; /** * @author Karel Maesen, Geovise BVBA @@ -35,7 +31,7 @@ * TODO Remove me! */ @Deprecated -public abstract class SpatialFunctionalTestCase extends BaseCoreFunctionalTestCase { +public abstract class SpatialFunctionalTestCase extends BaseSessionFactoryFunctionalTest { protected final static String JTS = "jts"; protected final static String GEOLATTE = "geolatte"; @@ -43,6 +39,9 @@ public abstract class SpatialFunctionalTestCase extends BaseCoreFunctionalTestCa protected TestData testData; protected AbstractExpectationsFactory expectationsFactory; + protected GeometryEquality geometryEquality; + + /** * Inserts the test data via a direct route (JDBC). @@ -53,32 +52,19 @@ public void prepareTest() { /** * Removes the test data. */ - public void cleanupTest() { - cleanUpTest( "jts" ); - cleanUpTest( "geolatte" ); + public void cleanupTest(SessionFactoryScope scope) { + cleanUpTest( "jts", scope ); + cleanUpTest( "geolatte", scope ); } - private void cleanUpTest(String pckg) { - Session session = null; - Transaction tx = null; - try { - session = openSession(); - tx = session.beginTransaction(); - String hql = String.format( "delete from %s", entityName( pckg ) ); - Query q = session.createQuery( hql ); - q.executeUpdate(); - tx.commit(); - } - catch (Exception e) { - if ( tx != null ) { - tx.rollback(); - } - } - finally { - if ( session != null ) { - session.close(); - } - } + private void cleanUpTest(String pckg, SessionFactoryScope scope) { + scope.inTransaction( + session -> { + String hql = String.format( "delete from %s", entityName( pckg ) ); + Query q = session.createQuery( hql ); + q.executeUpdate(); + } + ); } /** @@ -87,9 +73,12 @@ private void cleanUpTest(String pckg) { * * @return */ - protected void afterConfigurationBuilt(Configuration cfg) { - super.afterConfigurationBuilt( cfg ); - initializeSpatialTestSupport( serviceRegistry() ); +// protected void afterConfigurationBuilt(Configuration cfg) { +// initializeSpatialTestSupport( serviceRegistry() ); +// } + @Override + protected void sessionFactoryBuilt(SessionFactoryImplementor factory) { + initializeSpatialTestSupport( factory.getServiceRegistry() ); } private void initializeSpatialTestSupport(ServiceRegistry serviceRegistry) { @@ -123,7 +112,6 @@ protected Class[] getAnnotatedClasses() { * Returns true if the spatial dialect supports the specified function * * @param spatialFunction - * * @return */ public boolean isSupportedByDialect(SpatialFunction spatialFunction) { @@ -168,45 +156,48 @@ protected void compare(Map expected, Map received, S } protected void compare(Integer id, Object expected, Object received, String geometryType) { - assertTrue( expected != null || received == null ); + assertThat( expected != null || received == null ).isTrue(); if ( expected instanceof byte[] ) { - assertArrayEquals( "Failure on testsuite-suite for case " + id, (byte[]) expected, (byte[]) received ); + assertThat( (byte[]) received ) + .describedAs( "Failure on testsuite-suite for case " + id ) + .isEqualTo( (byte[]) expected ); } else if ( expected instanceof Geometry ) { if ( JTS.equals( geometryType ) ) { - if ( !( received instanceof Geometry ) ) { + if ( !(received instanceof Geometry) ) { fail( "Expected a JTS Geometry, but received an object of type " + received.getClass() .getCanonicalName() ); } - assertEquals( - "Failure on testsuite-suite for case " + id, - expected, received - ); + assertThat( received ) + .describedAs( "Failure on testsuite-suite for case " + id ) + .isEqualTo( expected ); } else { - if ( !( received instanceof org.geolatte.geom.Geometry ) ) { + if ( !(received instanceof org.geolatte.geom.Geometry) ) { fail( "Expected a Geolatte Geometry, but received an object of type " + received.getClass() .getCanonicalName() ); } - assertEquals( - "Failure on testsuite-suite for case " + id, - expected, - org.geolatte.geom.jts.JTS.to( (org.geolatte.geom.Geometry) received ) - ); - } + assertThat( geometryEquality.equals( org.geolatte.geom.jts.JTS.from( (Geometry) expected ), (org.geolatte.geom.Geometry) received ) ) + .describedAs( "Failure on testsuite-suite for case " + id ) + .isTrue(); + } } else { if ( expected instanceof Long ) { - assertEquals( "Failure on testsuite-suite for case " + id, ( (Long) expected ).intValue(), received ); + assertThat( received ) + .describedAs( "Failure on testsuite-suite for case " + id ) + .isEqualTo( ((Long) expected).intValue() ); } else { - assertEquals( "Failure on testsuite-suite for case " + id, expected, received ); + assertThat( received ) + .describedAs( "Failure on testsuite-suite for case " + id ) + .isEqualTo( expected ); } } } diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialTest.java index 6e4ca00b27c3..b66d815c178a 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/SpatialTest.java @@ -8,12 +8,13 @@ import jakarta.persistence.Id; import org.hibernate.dialect.PostgreSQLDialect; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.RequiresDialect; -import org.junit.Ignore; -import org.junit.Test; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.locationtech.jts.geom.Coordinate; //tag::spatial-types-mapping-example[] import org.locationtech.jts.geom.Point; @@ -22,29 +23,26 @@ import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Polygon; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; /** * @author Vlad Mihalcea */ @RequiresDialect(PostgreSQLDialect.class) -@Ignore -public class SpatialTest extends BaseEntityManagerFunctionalTestCase { +@Disabled +@Jpa( + annotatedClasses = { + SpatialTest.Event.class + } +) +public class SpatialTest { private final GeometryFactory geometryFactory = new GeometryFactory(); - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Event.class, - }; - } - @Test - public void test() { - Long addressId = doInJPA( this::entityManagerFactory, entityManager -> { + public void test(EntityManagerFactoryScope scope) { + Long addressId = scope.fromTransaction( entityManager -> { //tag::spatial-types-point-creation-example[] Event event = new Event(); event.setId( 1L); @@ -57,14 +55,14 @@ public void test() { return event.getId(); }); - doInJPA( this::entityManagerFactory, entityManager -> { + scope.inTransaction( entityManager -> { Event event = entityManager.find( Event.class, addressId); Coordinate coordinate = event.getLocation().getCoordinate(); assertEquals( 10.0d, coordinate.getOrdinate( Coordinate.X), 0.1); assertEquals( 5.0d, coordinate.getOrdinate( Coordinate.Y), 0.1); }); - doInJPA( this::entityManagerFactory, entityManager -> { + scope.inTransaction( entityManager -> { Coordinate [] coordinates = new Coordinate[] { new Coordinate(1,1), new Coordinate(20,1), new Coordinate(20,20), new Coordinate(1,20), new Coordinate(1,1) diff --git a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/converter/GeometryConverterTest.java b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/converter/GeometryConverterTest.java index 031392412833..452fc84007ed 100644 --- a/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/converter/GeometryConverterTest.java +++ b/hibernate-spatial/src/test/java/org/hibernate/spatial/testing/converter/GeometryConverterTest.java @@ -4,6 +4,7 @@ */ package org.hibernate.spatial.testing.converter; +import org.geolatte.geom.Geometry; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; @@ -11,26 +12,25 @@ import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.H2Dialect; import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.spatial.GeolatteGeometryJavaType; +import org.hibernate.testing.orm.junit.BaseUnitTest; import org.hibernate.tool.schema.Action; +import org.hibernate.type.Type; +import org.hibernate.type.descriptor.converter.spi.BasicValueConverter; +import org.hibernate.type.descriptor.converter.spi.JpaAttributeConverter; import org.hibernate.type.internal.ConvertedBasicTypeImpl; import org.hibernate.type.spi.TypeConfiguration; +import org.junit.jupiter.api.Test; -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; -import org.geolatte.geom.Geometry; - -import static org.hamcrest.CoreMatchers.sameInstance; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; /** * @author Steve Ebersole */ -public class GeometryConverterTest extends BaseUnitTestCase { +@BaseUnitTest +public class GeometryConverterTest { @Test public void testConverterUsage() { @@ -48,25 +48,20 @@ public void testConverterUsage() { final TypeConfiguration typeConfiguration = sessionFactory.getMappingMetamodel().getTypeConfiguration(); - assertThat( - typeConfiguration.getJavaTypeRegistry().getDescriptor( Geometry.class ), - sameInstance( GeolatteGeometryJavaType.GEOMETRY_INSTANCE ) - ); + assertThat( typeConfiguration.getJavaTypeRegistry().getDescriptor( Geometry.class ) ) + .isSameAs( GeolatteGeometryJavaType.GEOMETRY_INSTANCE ); // todo (5.3) : what to assert wrt to SqlTypeDescriptor? Anything? - final EntityPersister entityPersister = sessionFactory.getMappingMetamodel().getEntityDescriptor( MyEntity.class ); - final ConvertedBasicTypeImpl geometryAttributeType = assertTyping( - ConvertedBasicTypeImpl.class, - entityPersister.getPropertyType( "geometry" ) - ); - - final JpaAttributeConverter converter = assertTyping( - JpaAttributeConverter.class, - geometryAttributeType.getValueConverter() - ); + final EntityPersister entityPersister = sessionFactory.getMappingMetamodel() + .getEntityDescriptor( MyEntity.class ); + Type geometryAttributeType = entityPersister.getPropertyType( "geometry" ); + assertThat( geometryAttributeType ).isInstanceOf( ConvertedBasicTypeImpl.class ); + BasicValueConverter valueConverter = ((ConvertedBasicTypeImpl) geometryAttributeType).getValueConverter(); + assertThat( valueConverter ).isInstanceOf( JpaAttributeConverter.class ); - assert GeometryConverter.class.equals( converter.getConverterBean().getBeanClass() ); + assertThat( ((JpaAttributeConverter) valueConverter).getConverterBean().getBeanClass() ).isEqualTo( + GeometryConverter.class ); } } }