From cc43aaefb9385c108a34f65c42de6d9f453e89d7 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 25 Nov 2021 12:40:47 +0100 Subject: [PATCH] Re-enable additional tests --- .../{ => orm}/test/onetomany/Child.java | 2 +- .../orm/test/onetomany/OneToManyTest.java | 98 +++++++++ .../{ => orm}/test/onetomany/Parent.hbm.xml | 2 +- .../{ => orm}/test/onetomany/Parent.java | 2 +- .../{ => orm}/test/stats/Continent.hbm.xml | 2 +- .../{ => orm}/test/stats/Continent.java | 2 +- .../{ => orm}/test/stats/Continent2.hbm.xml | 2 +- .../{ => orm}/test/stats/Country.java | 2 +- .../stats/ExplicitQueryStatsMaxSizeTest.java | 32 ++- .../{ => orm}/test/stats/Locality.java | 2 +- .../{ => orm}/test/stats/Province.java | 2 +- .../test/stats/QueryStatsMaxSizeTest.java | 42 ++-- .../orm/test/stats/SessionStatsTest.java | 86 ++++++++ .../hibernate/{ => orm}/test/stats/State.java | 2 +- .../stats/StatisticsWithNoCachingTest.java | 45 ++++ .../StatisticsWithNoQueryCachingTest.java | 41 ++++ .../{ => orm}/test/stats/StatsTest.java | 195 +++++++++--------- .../test/onetomany/OneToManyTest.java | 96 --------- .../test/stats/SessionStatsTest.java | 74 ------- .../stats/StatisticsWithNoCachingTest.java | 34 --- .../StatisticsWithNoQueryCachingTest.java | 36 ---- 21 files changed, 406 insertions(+), 393 deletions(-) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/onetomany/Child.java (95%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/OneToManyTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/onetomany/Parent.hbm.xml (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/onetomany/Parent.java (96%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/Continent.hbm.xml (95%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/Continent.java (95%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/Continent2.hbm.xml (93%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/Country.java (95%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/ExplicitQueryStatsMaxSizeTest.java (71%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/Locality.java (95%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/Province.java (89%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/QueryStatsMaxSizeTest.java (59%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/stats/SessionStatsTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/State.java (89%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatisticsWithNoCachingTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatisticsWithNoQueryCachingTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/stats/StatsTest.java (54%) delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetomany/OneToManyTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/stats/SessionStatsTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/stats/StatisticsWithNoCachingTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/stats/StatisticsWithNoQueryCachingTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetomany/Child.java b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/Child.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/onetomany/Child.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/Child.java index cfe87cedb6..a59b20fe01 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/onetomany/Child.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/Child.java @@ -6,7 +6,7 @@ */ //$Id: Child.java 4378 2004-08-19 10:12:11Z oneovthafew $ -package org.hibernate.test.onetomany; +package org.hibernate.orm.test.onetomany; /** diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/OneToManyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/OneToManyTest.java new file mode 100644 index 0000000000..4a1e453632 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/OneToManyTest.java @@ -0,0 +1,98 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.onetomany; + + +import org.hibernate.testing.orm.junit.DialectFeatureChecks; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Gavin King + */ +@DomainModel( + xmlMappings = "org/hibernate/orm/test/onetomany/Parent.hbm.xml" +) +@SessionFactory +public class OneToManyTest { + + @SuppressWarnings({ "unchecked", "UnusedAssignment" }) + @Test + @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsTemporaryTable.class) + public void testOneToManyLinkTable(SessionFactoryScope scope) { + Child c = new Child(); + Parent p = new Parent(); + scope.inTransaction( + session -> { + c.setName( "Child One" ); + p.setName( "Parent" ); + p.getChildren().add( c ); + c.setParent( p ); + session.save( p ); + session.flush(); + + p.getChildren().remove( c ); + c.setParent( null ); + session.flush(); + + p.getChildren().add( c ); + c.setParent( p ); + } + ); + + scope.inTransaction( + session -> { + c.setParent( null ); + session.update( c ); + } + ); + + scope.inTransaction( + session -> { + c.setParent( p ); + session.update( c ); + } + ); + + + scope.inTransaction( + session -> { + Child child = (Child) session.createQuery( "from Child" ).uniqueResult(); + session.createQuery( "from Child c left join fetch c.parent" ).list(); + session.createQuery( "from Child c inner join fetch c.parent" ).list(); + session.clear(); + session.createQuery( "from Parent p left join fetch p.children" ) + .uniqueResult(); + } + ); + + scope.inTransaction( + session -> { + session.createQuery( "delete from Child" ).executeUpdate(); + session.createQuery( "delete from Parent" ).executeUpdate(); + } + ); + } + + @Test + public void testManyToManySize(SessionFactoryScope scope) { + + scope.inTransaction( + session -> + assertEquals( + 0, + session.createQuery( "from Parent p where size(p.children) = 0" ).list().size() + ) + ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetomany/Parent.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/Parent.hbm.xml similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/onetomany/Parent.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/Parent.hbm.xml index 1d54e9e84d..08a57156f3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/onetomany/Parent.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/onetomany/Parent.hbm.xml @@ -19,7 +19,7 @@ --> + package="org.hibernate.orm.test.onetomany"> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/Continent.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Continent.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/stats/Continent.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/stats/Continent.java index 4bfb82489f..62cdfe0fbc 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/Continent.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Continent.java @@ -6,7 +6,7 @@ */ //$Id: Continent.java 6736 2005-05-09 16:09:38Z epbernard $ -package org.hibernate.test.stats; +package org.hibernate.orm.test.stats; import java.util.Set; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/Continent2.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Continent2.hbm.xml similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/stats/Continent2.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/stats/Continent2.hbm.xml index bd10e52ac5..fb4c38818c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/Continent2.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Continent2.hbm.xml @@ -9,7 +9,7 @@ "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/Country.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Country.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/stats/Country.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/stats/Country.java index 5a3e2ae622..5d4545fcb5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/Country.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Country.java @@ -6,7 +6,7 @@ */ //$Id: Country.java 6736 2005-05-09 16:09:38Z epbernard $ -package org.hibernate.test.stats; +package org.hibernate.orm.test.stats; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/ExplicitQueryStatsMaxSizeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/ExplicitQueryStatsMaxSizeTest.java similarity index 71% rename from hibernate-core/src/test/java/org/hibernate/test/stats/ExplicitQueryStatsMaxSizeTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/stats/ExplicitQueryStatsMaxSizeTest.java index e937004108..d88bf4a108 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/ExplicitQueryStatsMaxSizeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/ExplicitQueryStatsMaxSizeTest.java @@ -4,41 +4,39 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.stats; - -import java.util.Map; -import jakarta.persistence.EntityManagerFactory; +package org.hibernate.orm.test.stats; import org.hibernate.SessionFactory; import org.hibernate.cfg.AvailableSettings; import org.hibernate.stat.spi.StatisticsImplementor; -import org.junit.Test; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.EntityManagerFactory; + +import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; /** * @author Vlad Mihalcea */ +@Jpa( + properties = { @Setting(name = AvailableSettings.QUERY_STATISTICS_MAX_SIZE, value = "100") } +) public class ExplicitQueryStatsMaxSizeTest extends QueryStatsMaxSizeTest { - public static final int QUERY_STATISTICS_MAX_SIZE = 100; - - @Override - protected void addConfigOptions(Map options) { - super.addConfigOptions( options ); - options.put( AvailableSettings.QUERY_STATISTICS_MAX_SIZE, QUERY_STATISTICS_MAX_SIZE ); - } @Override protected int expectedQueryStatisticsMaxSize() { - return QUERY_STATISTICS_MAX_SIZE; + return 100; } @Test - public void testMaxSize() { - doInJPA( this::entityManagerFactory, entityManager -> { + public void testMaxSize(EntityManagerFactoryScope scope) { + scope.inTransaction( entityManager -> { EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory(); SessionFactory sessionFactory = entityManagerFactory.unwrap( SessionFactory.class ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/Locality.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Locality.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/stats/Locality.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/stats/Locality.java index 59437183cb..406c83ef0f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/Locality.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Locality.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.stats; +package org.hibernate.orm.test.stats; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/Province.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Province.java similarity index 89% rename from hibernate-core/src/test/java/org/hibernate/test/stats/Province.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/stats/Province.java index 3567ca008f..5e8c2db38e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/Province.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/Province.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.stats; +package org.hibernate.orm.test.stats; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/QueryStatsMaxSizeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/QueryStatsMaxSizeTest.java similarity index 59% rename from hibernate-core/src/test/java/org/hibernate/test/stats/QueryStatsMaxSizeTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/stats/QueryStatsMaxSizeTest.java index 607723a49e..1a305c82e8 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/QueryStatsMaxSizeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/QueryStatsMaxSizeTest.java @@ -4,46 +4,36 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.stats; +package org.hibernate.orm.test.stats; -import java.util.Map; +import org.hibernate.SessionFactory; +import org.hibernate.annotations.NaturalId; +import org.hibernate.stat.Statistics; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.Test; import jakarta.persistence.Entity; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Id; -import org.hibernate.SessionFactory; -import org.hibernate.annotations.NaturalId; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase; -import org.hibernate.stat.Statistics; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.Test; - -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; /** * @author Vlad Mihalcea */ -public class QueryStatsMaxSizeTest extends BaseEntityManagerFunctionalTestCase { +@Jpa( + annotatedClasses = QueryStatsMaxSizeTest.Employee.class, + generateStatistics = true +) - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Employee.class, - }; - } - - @Override - protected void addConfigOptions(Map options) { - options.put( AvailableSettings.GENERATE_STATISTICS, "true" ); - } +public class QueryStatsMaxSizeTest { @Test - public void test() { - doInJPA( this::entityManagerFactory, entityManager -> { + public void test(EntityManagerFactoryScope scope) { + scope.inTransaction( entityManager -> { EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory(); SessionFactory sessionFactory = entityManagerFactory.unwrap( SessionFactory.class ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stats/SessionStatsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/SessionStatsTest.java new file mode 100644 index 0000000000..9c46dbf782 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/SessionStatsTest.java @@ -0,0 +1,86 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.stats; + +import java.util.HashSet; + +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.stat.SessionStatistics; +import org.hibernate.stat.Statistics; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Emmanuel Bernard + */ +@DomainModel( + xmlMappings = "org/hibernate/orm/test/stats/Continent2.hbm.xml" +) +@SessionFactory +public class SessionStatsTest { + + @Test + public void testSessionStatistics(SessionFactoryScope scope) { + boolean isStatsEnabled = scope.fromSession( + session -> { + try { + Transaction tx = session.beginTransaction(); + Statistics stats = scope.getSessionFactory().getStatistics(); + stats.clear(); + boolean isStats = stats.isStatisticsEnabled(); + stats.setStatisticsEnabled( true ); + Continent europe = fillDb( session ); + tx.commit(); + session.clear(); + tx = session.beginTransaction(); + SessionStatistics sessionStats = session.getStatistics(); + assertEquals( 0, sessionStats.getEntityKeys().size() ); + assertEquals( 0, sessionStats.getEntityCount() ); + assertEquals( 0, sessionStats.getCollectionKeys().size() ); + assertEquals( 0, sessionStats.getCollectionCount() ); + europe = (Continent) session.get( Continent.class, europe.getId() ); + Hibernate.initialize( europe.getCountries() ); + Hibernate.initialize( europe.getCountries().iterator().next() ); + assertEquals( 2, sessionStats.getEntityKeys().size() ); + assertEquals( 2, sessionStats.getEntityCount() ); + assertEquals( 1, sessionStats.getCollectionKeys().size() ); + assertEquals( 1, sessionStats.getCollectionCount() ); + tx.commit(); + return isStats; + } + finally { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + } + } + ); + + scope.getSessionFactory().getStatistics().setStatisticsEnabled( isStatsEnabled ); + + } + + private Continent fillDb(Session s) { + Continent europe = new Continent(); + europe.setName( "Europe" ); + Country france = new Country(); + france.setName( "France" ); + europe.setCountries( new HashSet() ); + europe.getCountries().add( france ); + s.persist( france ); + s.persist( europe ); + return europe; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/State.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/State.java similarity index 89% rename from hibernate-core/src/test/java/org/hibernate/test/stats/State.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/stats/State.java index 8fd4eba7e9..fd11ead660 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/State.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/State.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.stats; +package org.hibernate.orm.test.stats; /** diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatisticsWithNoCachingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatisticsWithNoCachingTest.java new file mode 100644 index 0000000000..f65527668f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatisticsWithNoCachingTest.java @@ -0,0 +1,45 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.stats; + +import org.hibernate.cache.internal.NoCachingRegionFactory; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.stat.Statistics; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.SettingProvider; +import org.junit.jupiter.api.Test; + +/** + * @author Gail Badner + */ +@DomainModel +@SessionFactory +@ServiceRegistry( + settingProviders = @SettingProvider(provider = StatisticsWithNoCachingTest.RegionFacrotySettingProvider.class, settingName = AvailableSettings.CACHE_REGION_FACTORY) +) +public class StatisticsWithNoCachingTest { + + public static class RegionFacrotySettingProvider implements SettingProvider.Provider { + + @Override + public String getSetting() { + return NoCachingRegionFactory.class.getName(); + } + } + + @Test + @TestForIssue(jiraKey = "HHH-12508") + public void testUncachedRegion(SessionFactoryScope scope) { + final Statistics statistics = scope.getSessionFactory().getStatistics(); + statistics.getSecondLevelCacheStatistics( "hibernate.test.unknown" ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatisticsWithNoQueryCachingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatisticsWithNoQueryCachingTest.java new file mode 100644 index 0000000000..5d75abeaa5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatisticsWithNoQueryCachingTest.java @@ -0,0 +1,41 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.stats; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.stat.Statistics; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * @author Gail Badner + */ +@DomainModel +@SessionFactory +@ServiceRegistry( + settings = { + @Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"), + @Setting( name = AvailableSettings.USE_QUERY_CACHE, value = "false") + } +) +public class StatisticsWithNoQueryCachingTest { + + @Test + @TestForIssue( jiraKey = "HHH-13645") + public void testUncachedRegion(SessionFactoryScope scope) { + final Statistics statistics = scope.getSessionFactory().getStatistics(); + assertNull( statistics.getCacheRegionStatistics( "hibernate.test.unknown" ) ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatsTest.java similarity index 54% rename from hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatsTest.java index f77e1cd71a..293577eb33 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/stats/StatsTest.java @@ -4,43 +4,37 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.stats; +package org.hibernate.orm.test.stats; import java.util.HashSet; -import java.util.Iterator; -import org.hibernate.Hibernate; import org.hibernate.ScrollableResults; import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.Transaction; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.stat.QueryStatistics; +import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +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 static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; /** * Show the difference between fetch and load * * @author Emmanuel Bernard */ -public class StatsTest extends BaseUnitTestCase { - public String[] getMappings() { - return new String[] { "stats/Continent.hbm.xml" }; - } +@DomainModel( + xmlMappings = "org/hibernate/orm/test/stats/Continent.hbm.xml" +) +@org.hibernate.testing.orm.junit.SessionFactory( + generateStatistics = true +) +public class StatsTest { - private Configuration buildBaseConfiguration() { - return new Configuration() - .addResource( "org/hibernate/test/stats/Continent.hbm.xml" ) - .setProperty( AvailableSettings.GENERATE_STATISTICS, "true" ); - } -// // @Test // @SuppressWarnings( {"UnusedAssignment"}) // public void testCollectionFetchVsLoad() throws Exception { @@ -144,101 +138,102 @@ public class StatsTest extends BaseUnitTestCase { // } @Test - public void testQueryStatGathering() { - try (SessionFactory sf = buildBaseConfiguration() - .setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" ) - .buildSessionFactory()) { - - Session s = sf.openSession(); - Transaction tx = s.beginTransaction(); - fillDb( s ); - tx.commit(); - s.close(); - - s = sf.openSession(); - tx = s.beginTransaction(); - final String continents = "from Continent"; - int results = s.createQuery( continents ).list().size(); - QueryStatistics continentStats = sf.getStatistics().getQueryStatistics( continents ); - assertNotNull( "stats were null", continentStats ); - assertEquals( "unexpected execution count", 1, continentStats.getExecutionCount() ); - assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() ); - long maxTime = continentStats.getExecutionMaxTime(); - assertEquals( maxTime, sf.getStatistics().getQueryExecutionMaxTime() ); + public void testQueryStatGathering(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + final String continents = "from Continent"; + int results = session.createQuery( continents ).list().size(); + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); + QueryStatistics continentStats = statistics.getQueryStatistics( continents ); + assertNotNull( continentStats, "stats were null" ); + assertEquals( 1, continentStats.getExecutionCount(), "unexpected execution count" ); + assertEquals( results, continentStats.getExecutionRowCount(), "unexpected row count" ); + long maxTime = continentStats.getExecutionMaxTime(); + assertEquals( maxTime, statistics.getQueryExecutionMaxTime() ); // assertEquals( continents, stats.getQueryExecutionMaxTimeQueryString() ); - s.createQuery( continents ).list().iterator(); - assertEquals( "unexpected execution count", 2, continentStats.getExecutionCount() ); - assertEquals( "unexpected row count", 2, continentStats.getExecutionRowCount() ); + session.createQuery( continents ).list().iterator(); + assertEquals( 2, continentStats.getExecutionCount(), "unexpected execution count" ); + assertEquals( 2, continentStats.getExecutionRowCount(), "unexpected row count" ); - ScrollableResults scrollableResults = s.createQuery( continents ).scroll(); - // same deal with scroll()... - assertEquals( "unexpected execution count", 2, continentStats.getExecutionCount() ); - assertEquals( "unexpected row count", 2, continentStats.getExecutionRowCount() ); - // scroll through data because SybaseASE15Dialect throws NullPointerException - // if data is not read before closing the ResultSet - while ( scrollableResults.next() ) { - // do nothing - } - scrollableResults.close(); - tx.commit(); - s.close(); + try (ScrollableResults scrollableResults = session.createQuery( continents ).scroll()) { + // same deal with scroll()... + assertEquals( 2, continentStats.getExecutionCount(), "unexpected execution count" ); + assertEquals( 2, continentStats.getExecutionRowCount(), "unexpected row count" ); + // scroll through data because SybaseASE15Dialect throws NullPointerException + // if data is not read before closing the ResultSet + while ( scrollableResults.next() ) { + // do nothing + } + } + } + ); - // explicitly check that statistics for "split queries" get collected - // under the original query - sf.getStatistics().clear(); + // explicitly check that statistics for "split queries" get collected + // under the original query + scope.getSessionFactory().getStatistics().clear(); - s = sf.openSession(); - tx = s.beginTransaction(); - final String localities = "from Locality"; - results = s.createQuery( localities ).list().size(); - QueryStatistics localityStats = sf.getStatistics().getQueryStatistics( localities ); - assertNotNull( "stats were null", localityStats ); - // ...one for each split query - assertEquals( "unexpected execution count", 2, localityStats.getExecutionCount() ); - assertEquals( "unexpected row count", results, localityStats.getExecutionRowCount() ); - maxTime = localityStats.getExecutionMaxTime(); - assertEquals( maxTime, sf.getStatistics().getQueryExecutionMaxTime() ); + scope.inTransaction( + session -> { + final String localities = "from Locality"; + int results = session.createQuery( localities ).list().size(); + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); + QueryStatistics localityStats = statistics.getQueryStatistics( localities ); + assertNotNull( localityStats, "stats were null" ); + // ...one for each split query + assertEquals( 2, localityStats.getExecutionCount(), "unexpected execution count" ); + assertEquals( results, localityStats.getExecutionRowCount(), "unexpected row count" ); + long maxTime = localityStats.getExecutionMaxTime(); + assertEquals( maxTime, statistics.getQueryExecutionMaxTime() ); // assertEquals( localities, stats.getQueryExecutionMaxTimeQueryString() ); - tx.commit(); - s.close(); - assertFalse( s.isOpen() ); + } + ); - // native sql queries - sf.getStatistics().clear(); + // native sql queries + scope.getSessionFactory().getStatistics().clear(); - s = sf.openSession(); - tx = s.beginTransaction(); - final String sql = "select id, name from Country"; - results = s.createNativeQuery( sql ).addEntity( Country.class ).list().size(); - QueryStatistics sqlStats = sf.getStatistics().getQueryStatistics( sql ); - assertNotNull( "sql stats were null", sqlStats ); - assertEquals( "unexpected execution count", 1, sqlStats.getExecutionCount() ); - assertEquals( "unexpected row count", results, sqlStats.getExecutionRowCount() ); - maxTime = sqlStats.getExecutionMaxTime(); - assertEquals( maxTime, sf.getStatistics().getQueryExecutionMaxTime() ); + scope.inTransaction( + session -> { + final String sql = "select id, name from Country"; + int results = session.createNativeQuery( sql ).addEntity( Country.class ).list().size(); + final StatisticsImplementor statistics = scope.getSessionFactory().getStatistics(); + QueryStatistics sqlStats = statistics.getQueryStatistics( sql ); + assertNotNull( sqlStats, "sql stats were null" ); + assertEquals( 1, sqlStats.getExecutionCount(), "unexpected execution count" ); + assertEquals( results, sqlStats.getExecutionRowCount(), "unexpected row count" ); + long maxTime = sqlStats.getExecutionMaxTime(); + assertEquals( maxTime, statistics.getQueryExecutionMaxTime() ); // assertEquals( sql, stats.getQueryExecutionMaxTimeQueryString() ); - tx.commit(); - s.close(); + } + ); + } - s = sf.openSession(); - tx = s.beginTransaction(); - cleanDb( s ); - tx.commit(); - s.close(); - } + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> + fillDb( session ) + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + cleanDb( session ) + ); } private Continent fillDb(Session s) { Continent europe = new Continent(); - europe.setName("Europe"); + europe.setName( "Europe" ); Country france = new Country(); - france.setName("France"); + france.setName( "France" ); europe.setCountries( new HashSet() ); - europe.getCountries().add(france); - s.persist(france); - s.persist(europe); + europe.getCountries().add( france ); + s.persist( france ); + s.persist( europe ); return europe; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetomany/OneToManyTest.java b/hibernate-core/src/test/java/org/hibernate/test/onetomany/OneToManyTest.java deleted file mode 100644 index cde82350f8..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/onetomany/OneToManyTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.onetomany; - -import org.hibernate.testing.DialectChecks; -import org.hibernate.testing.RequiresDialectFeature; -import org.junit.Test; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; - -/** - * @author Gavin King - */ -public class OneToManyTest extends BaseCoreFunctionalTestCase { - public String[] getMappings() { - return new String[] { "onetomany/Parent.hbm.xml" }; - } - - @SuppressWarnings( {"unchecked", "UnusedAssignment"}) - @Test - @RequiresDialectFeature(DialectChecks.SupportsTemporaryTable.class) - public void testOneToManyLinkTable() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Child c = new Child(); - c.setName("Child One"); - Parent p = new Parent(); - p.setName("Parent"); - p.getChildren().add(c); - c.setParent(p); - s.save(p); - s.flush(); - - p.getChildren().remove(c); - c.setParent(null); - s.flush(); - - p.getChildren().add(c); - c.setParent(p); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - c.setParent(null); - s.update(c); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - c.setParent(p); - s.update(c); - t.commit(); - s.close(); - - - s = openSession(); - t = s.beginTransaction(); - c = (Child) s.createQuery("from Child").uniqueResult(); - s.createQuery("from Child c left join fetch c.parent").list(); - s.createQuery("from Child c inner join fetch c.parent").list(); - s.clear(); - p = (Parent) s.createQuery("from Parent p left join fetch p.children").uniqueResult(); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - s.createQuery("delete from Child").executeUpdate(); - s.createQuery("delete from Parent").executeUpdate(); - t.commit(); - s.close(); - - } - - @Test - public void testManyToManySize() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - assertEquals( 0, s.createQuery("from Parent p where size(p.children) = 0").list().size() ); - assertEquals( 0, s.createQuery("from Parent p where p.children.size = 0").list().size() ); - t.commit(); - s.close(); - } - -} - diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/SessionStatsTest.java b/hibernate-core/src/test/java/org/hibernate/test/stats/SessionStatsTest.java deleted file mode 100644 index 87c3ec6281..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/SessionStatsTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.stats; - -import java.util.HashSet; - -import org.junit.Test; - -import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.stat.SessionStatistics; -import org.hibernate.stat.Statistics; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; - -/** - * @author Emmanuel Bernard - */ -public class SessionStatsTest extends BaseCoreFunctionalTestCase { - @Override - public String[] getMappings() { - return new String[] { "stats/Continent2.hbm.xml" }; - } - - @Test - public void testSessionStatistics() throws Exception { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - Statistics stats = sessionFactory().getStatistics(); - stats.clear(); - boolean isStats = stats.isStatisticsEnabled(); - stats.setStatisticsEnabled(true); - Continent europe = fillDb(s); - tx.commit(); - s.clear(); - tx = s.beginTransaction(); - SessionStatistics sessionStats = s.getStatistics(); - assertEquals( 0, sessionStats.getEntityKeys().size() ); - assertEquals( 0, sessionStats.getEntityCount() ); - assertEquals( 0, sessionStats.getCollectionKeys().size() ); - assertEquals( 0, sessionStats.getCollectionCount() ); - europe = (Continent) s.get( Continent.class, europe.getId() ); - Hibernate.initialize( europe.getCountries() ); - Hibernate.initialize( europe.getCountries().iterator().next() ); - assertEquals( 2, sessionStats.getEntityKeys().size() ); - assertEquals( 2, sessionStats.getEntityCount() ); - assertEquals( 1, sessionStats.getCollectionKeys().size() ); - assertEquals( 1, sessionStats.getCollectionCount() ); - tx.commit(); - s.close(); - - stats.setStatisticsEnabled( isStats); - - } - - private Continent fillDb(Session s) { - Continent europe = new Continent(); - europe.setName("Europe"); - Country france = new Country(); - france.setName("France"); - europe.setCountries( new HashSet() ); - europe.getCountries().add(france); - s.persist(france); - s.persist(europe); - return europe; - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/StatisticsWithNoCachingTest.java b/hibernate-core/src/test/java/org/hibernate/test/stats/StatisticsWithNoCachingTest.java deleted file mode 100644 index f0e930f3ad..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/StatisticsWithNoCachingTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.stats; - -import org.hibernate.cache.internal.NoCachingRegionFactory; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.stat.Statistics; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -/** - * @author Gail Badner - */ -public class StatisticsWithNoCachingTest extends BaseCoreFunctionalTestCase { - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.CACHE_REGION_FACTORY, NoCachingRegionFactory.class.getName() ); - } - - @Test - @TestForIssue( jiraKey = "HHH-12508") - public void testUncachedRegion() { - final Statistics statistics = sessionFactory().getStatistics(); - statistics.getSecondLevelCacheStatistics( "hibernate.test.unknown" ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/StatisticsWithNoQueryCachingTest.java b/hibernate-core/src/test/java/org/hibernate/test/stats/StatisticsWithNoQueryCachingTest.java deleted file mode 100644 index 2fd39d0a6d..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/StatisticsWithNoQueryCachingTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.stats; - -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; -import org.hibernate.stat.Statistics; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -import static org.junit.Assert.assertNull; - -/** - * @author Gail Badner - */ -public class StatisticsWithNoQueryCachingTest extends BaseCoreFunctionalTestCase { - - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" ); - configuration.setProperty( AvailableSettings.USE_QUERY_CACHE, "false" ); - } - - @Test - @TestForIssue( jiraKey = "HHH-13645") - public void testUncachedRegion() { - final Statistics statistics = sessionFactory().getStatistics(); - assertNull( statistics.getCacheRegionStatistics( "hibernate.test.unknown" ) ); - } -}