From 37d2e1804a5038481b901ccf357f2147dba7533d Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 18 Sep 2013 07:51:01 -0500 Subject: [PATCH] Added some simple tests for using the JPA Cache API --- .../cacheable/api/JpaCacheApiUsageTest.java | 79 +++++++++++++++++++ .../jpa/test/cacheable/api/Order.java | 73 +++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/cacheable/api/JpaCacheApiUsageTest.java create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/cacheable/api/Order.java diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/cacheable/api/JpaCacheApiUsageTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/cacheable/api/JpaCacheApiUsageTest.java new file mode 100644 index 0000000000..a819cf312d --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/cacheable/api/JpaCacheApiUsageTest.java @@ -0,0 +1,79 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.jpa.test.cacheable.api; + +import javax.persistence.EntityManager; +import javax.persistence.SharedCacheMode; + +import java.util.Map; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Test; + +import org.hibernate.testing.cache.CachingRegionFactory; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Steve Ebersole + */ +public class JpaCacheApiUsageTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Order.class }; + } + + @Override + @SuppressWarnings("unchecked") + protected void addConfigOptions(Map options) { +// options.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" ); + options.put( AvailableSettings.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() ); +// options.put( AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, "read-write" ); + options.put( org.hibernate.jpa.AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.ALL ); + } + + @Test + public void testEviction() { + // first create an Order + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + em.persist( new Order( 1, 500 ) ); + em.getTransaction().commit(); + em.close(); + + assertTrue( entityManagerFactory().getCache().contains( Order.class, 1 ) ); + + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + assertTrue( entityManagerFactory().getCache().contains( Order.class, 1 ) ); + em.createQuery( "delete Order" ).executeUpdate(); + em.getTransaction().commit(); + em.close(); + + assertFalse( entityManagerFactory().getCache().contains( Order.class, 1 ) ); + } +} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/cacheable/api/Order.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/cacheable/api/Order.java new file mode 100644 index 0000000000..29fef3e57f --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/cacheable/api/Order.java @@ -0,0 +1,73 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.jpa.test.cacheable.api; + +import javax.persistence.Cacheable; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author Steve Ebersole + */ +@Entity +@Cacheable( value = true ) +@Table( name = "T_ORDER" ) +public class Order { + private int id; + private int total; + + public Order() { + } + + public Order(int total) { + this.total = total; + } + + public Order(int id, int total) { + this.id = id; + this.total = total; + } + + @Id + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getTotal() { + return total; + } + + public void setTotal(int total) { + this.total = total; + } + + public String toString() { + return "Order id=" + getId() + ", total=" + getTotal(); + } +}