From aec21d21f8483e34c9c4ba7586cc97f1cf940eb3 Mon Sep 17 00:00:00 2001 From: David Ellingsworth Date: Thu, 15 Oct 2020 17:54:27 -0400 Subject: [PATCH] HHH-14216 Add one-to-one second level cache tests. --- .../test/onetoone/cache/Details.hbm.xml | 33 +++++ .../test/onetoone/cache/Details.java | 38 ++++++ .../test/onetoone/cache/DetailsByFK.java | 9 ++ .../test/onetoone/cache/DetailsByRef.java | 9 ++ .../onetoone/cache/OneToOneCacheTest.java | 120 ++++++++++++++++++ .../test/onetoone/cache/Person.hbm.xml | 30 +++++ .../hibernate/test/onetoone/cache/Person.java | 43 +++++++ .../test/onetoone/cache/PersonByFK.java | 9 ++ .../test/onetoone/cache/PersonByRef.java | 9 ++ 9 files changed, 300 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Details.hbm.xml create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Details.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/DetailsByFK.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/DetailsByRef.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/OneToOneCacheTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Person.hbm.xml create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Person.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/PersonByFK.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/PersonByRef.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Details.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Details.hbm.xml new file mode 100644 index 0000000000..447bc6dd03 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Details.hbm.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + person + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Details.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Details.java new file mode 100644 index 0000000000..b0c108f507 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Details.java @@ -0,0 +1,38 @@ +/* + * 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.onetoone.cache; +import java.io.Serializable; + +public abstract class Details implements Serializable { + private int id; + private String data; + private Person person; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public Person getPerson() { + return person; + } + + protected void setPerson(Person person) { + this.person = person; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/DetailsByFK.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/DetailsByFK.java new file mode 100644 index 0000000000..3ca7616bc9 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/DetailsByFK.java @@ -0,0 +1,9 @@ +/* + * 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.onetoone.cache; + +public class DetailsByFK extends Details {} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/DetailsByRef.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/DetailsByRef.java new file mode 100644 index 0000000000..3a8777a148 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/DetailsByRef.java @@ -0,0 +1,9 @@ +/* + * 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.onetoone.cache; + +public class DetailsByRef extends Details {} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/OneToOneCacheTest.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/OneToOneCacheTest.java new file mode 100644 index 0000000000..d14e6c56a4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/OneToOneCacheTest.java @@ -0,0 +1,120 @@ +package org.hibernate.test.onetoone.cache; + +import static org.junit.Assert.assertEquals; + +import java.io.Serializable; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.cache.spi.CacheImplementor; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Configuration; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.stat.spi.StatisticsImplementor; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +public class OneToOneCacheTest extends BaseCoreFunctionalTestCase { + @Override + public String[] getMappings() { + return new String[] { + "onetoone/cache/Details.hbm.xml", + "onetoone/cache/Person.hbm.xml", + }; + } + + @Override + protected void configure(Configuration configuration) { + configuration.setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true"); + configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true"); + } + + private void OneToOneTest(Class personClass, + Class detailsClass) throws Exception { + + // Initialize the database with data. + List ids = createPersonsAndDetails(personClass, detailsClass); + + // Clear the second level cache and the statistics. + SessionFactoryImplementor sfi = sessionFactory(); + CacheImplementor cache = sfi.getCache(); + StatisticsImplementor statistics = sfi.getStatistics(); + + cache.evictEntityData(personClass); + cache.evictEntityData(detailsClass); + cache.evictQueryRegions(); + + statistics.clear(); + + // Fill the empty caches with data. + this.getPersons(personClass, ids); + + // Verify that no data was retrieved from the cache. + assertEquals("Second level cache hit count", 0, statistics.getSecondLevelCacheHitCount()); + + statistics.clear(); + + this.getPersons(personClass, ids); + + // Verify that all data was retrieved from the cache. + assertEquals("Second level cache miss count", 0, statistics.getSecondLevelCacheMissCount()); + } + + private List createPersonsAndDetails(Class personClass, + Class detailsClass) throws Exception { + Session s = openSession(); + Transaction tx = s.beginTransaction(); + + Constructor ctorPerson = personClass.getConstructor(); + Constructor ctorDetails = detailsClass.getConstructor(); + List ids = new ArrayList(); + + for (int i = 0; i < 6; i++) { + Person person = ctorPerson.newInstance(); + + if (i % 2 == 0) { + Details details = ctorDetails.newInstance(); + + details.setData(String.format("%s%d", detailsClass.getName(), i)); + person.setDetails(details); + } + + person.setName(String.format("%s%d", personClass.getName(), i)); + + ids.add(s.save(person)); + } + + tx.commit(); + s.close(); + + return ids; + } + + private List getPersons(Class personClass, List ids) { + Session s = openSession(); + Transaction tx = s.beginTransaction(); + List people = new ArrayList(); + + for (Serializable id : ids) { + people.add(s.get(personClass, id)); + } + + tx.commit(); + s.close(); + + return people; + } + + @Test + public void OneToOneCacheByForeignKey() throws Exception { + OneToOneTest(PersonByFK.class, DetailsByFK.class); + } + + @Test + public void OneToOneCacheByRef() throws Exception { + OneToOneTest(PersonByRef.class, DetailsByRef.class); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Person.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Person.hbm.xml new file mode 100644 index 0000000000..3548936c4c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Person.hbm.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Person.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Person.java new file mode 100644 index 0000000000..0b4fc141f2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/Person.java @@ -0,0 +1,43 @@ +/* + * 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.onetoone.cache; +import java.io.Serializable; + +public abstract class Person implements Serializable { + private int id; + private String name; + private Details details; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Details getDetails() { + return details; + } + + public void setDetails(Details details) { + if (details != null) { + details.setPerson(this); + } + + this.details = details; + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/PersonByFK.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/PersonByFK.java new file mode 100644 index 0000000000..d431ab05c6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/PersonByFK.java @@ -0,0 +1,9 @@ +/* + * 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.onetoone.cache; + +public class PersonByFK extends Person {} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/PersonByRef.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/PersonByRef.java new file mode 100644 index 0000000000..3262d528bb --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/cache/PersonByRef.java @@ -0,0 +1,9 @@ +/* + * 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.onetoone.cache; + +public class PersonByRef extends Person {} \ No newline at end of file