Added some simple tests for using the JPA Cache API

This commit is contained in:
Steve Ebersole 2013-09-18 07:51:01 -05:00
parent a2881b3a35
commit 37d2e1804a
2 changed files with 152 additions and 0 deletions

View File

@ -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 ) );
}
}

View File

@ -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();
}
}