Author: stliu
Date: 2010-09-28 06:52:55 -0400 (Tue, 28 Sep 2010)
New Revision: 20736
Added:
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/CacheableItem.hbm.xml
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/CacheableItem.java
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/InsertedDataTest.java
Log:
JBPAPP-5152 HHH-5490 dirty data be inserted into 2L cache
Added:
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/CacheableItem.hbm.xml
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/CacheableItem.hbm.xml
(rev 0)
+++
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/CacheableItem.hbm.xml 2010-09-28
10:52:55 UTC (rev 20736)
@@ -0,0 +1,38 @@
+<?xml version="1.0"?>
+<!--
+ ~ Hibernate, Relational Persistence for Idiomatic Java
+ ~
+ ~ Copyright (c) 2010, 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
+ -->
+<!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping package="org.hibernate.test.cache">
+
+ <class name="CacheableItem">
+ <id name="id" type="long">
+ <generator class="increment"/>
+ </id>
+ <property name="name" type="string" />
+ </class>
+
+</hibernate-mapping>
Added: core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/CacheableItem.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/CacheableItem.java
(rev 0)
+++
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/CacheableItem.java 2010-09-28
10:52:55 UTC (rev 20736)
@@ -0,0 +1,57 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, 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.test.cache;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class CacheableItem {
+ private Long id;
+ private String name;
+
+ public CacheableItem() {
+ }
+
+ public CacheableItem(String name) {
+ this.name = name;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Added:
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/InsertedDataTest.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/InsertedDataTest.java
(rev 0)
+++
core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cache/InsertedDataTest.java 2010-09-28
10:52:55 UTC (rev 20736)
@@ -0,0 +1,240 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, 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.test.cache;
+
+import java.util.Map;
+
+import org.hibernate.Session;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.junit.functional.FunctionalTestCase;
+
+/**
+ * Tests for handling of data just inserted during a transaction being read from the
database
+ * and placed into cache. Initially these cases went through putFromRead which causes
problems because it
+ * loses the context of that data having just been read.
+ *
+ * @author Steve Ebersole
+ */
+public class InsertedDataTest extends FunctionalTestCase {
+ private static final String REGION_NAME = CacheableItem.class.getName();
+
+ public InsertedDataTest(String string) {
+ super( string );
+ }
+
+ public String[] getMappings() {
+ return new String[] { "cache/CacheableItem.hbm.xml" };
+ }
+
+ public String getCacheConcurrencyStrategy() {
+ return "read-write";
+ }
+
+ public void configure(Configuration cfg) {
+ super.configure( cfg );
+ cfg.setProperty( Environment.CACHE_REGION_PREFIX, "" );
+ cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
+ }
+
+ public void testInsert() {
+ getSessions().evict(CacheableItem.class);
+ getSessions().getStatistics().clear();
+
+ Session s = openSession();
+ s.beginTransaction();
+ CacheableItem item = new CacheableItem( "data" );
+ s.save( item );
+ s.getTransaction().commit();
+ s.close();
+
+ Map cacheMap = getSessions().getStatistics().getSecondLevelCacheStatistics( REGION_NAME
).getEntries();
+ assertEquals( 1, cacheMap.size() );
+
+ s = openSession();
+ s.beginTransaction();
+ s.createQuery( "delete CacheableItem" ).executeUpdate();
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ public void testInsertWithRollback() {
+ getSessions().evict(CacheableItem.class);
+ getSessions().getStatistics().clear();
+
+ Session s = openSession();
+ s.beginTransaction();
+ CacheableItem item = new CacheableItem( "data" );
+ s.save( item );
+ s.flush();
+ s.getTransaction().rollback();
+ s.close();
+
+ Map cacheMap = getSessions().getStatistics().getSecondLevelCacheStatistics( REGION_NAME
).getEntries();
+ assertEquals( 0, cacheMap.size() );
+ }
+
+ public void testInsertThenUpdate() {
+ getSessions().evict(CacheableItem.class);
+ getSessions().getStatistics().clear();
+
+ Session s = openSession();
+ s.beginTransaction();
+ CacheableItem item = new CacheableItem( "data" );
+ s.save( item );
+ s.flush();
+ item.setName( "new data" );
+ s.getTransaction().commit();
+ s.close();
+
+ Map cacheMap = getSessions().getStatistics().getSecondLevelCacheStatistics( REGION_NAME
).getEntries();
+ assertEquals( 1, cacheMap.size() );
+
+ s = openSession();
+ s.beginTransaction();
+ s.createQuery( "delete CacheableItem" ).executeUpdate();
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ public void testInsertThenUpdateThenRollback() {
+ getSessions().evict(CacheableItem.class);
+ getSessions().getStatistics().clear();
+
+ Session s = openSession();
+ s.beginTransaction();
+ CacheableItem item = new CacheableItem( "data" );
+ s.save( item );
+ s.flush();
+ item.setName( "new data" );
+ s.getTransaction().rollback();
+ s.close();
+
+ Map cacheMap = getSessions().getStatistics().getSecondLevelCacheStatistics( REGION_NAME
).getEntries();
+ assertEquals( 0, cacheMap.size() );
+
+ s = openSession();
+ s.beginTransaction();
+ s.createQuery( "delete CacheableItem" ).executeUpdate();
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ public void testInsertWithRefresh() {
+ getSessions().evict(CacheableItem.class);
+ getSessions().getStatistics().clear();
+
+ Session s = openSession();
+ s.beginTransaction();
+ CacheableItem item = new CacheableItem( "data" );
+ s.save( item );
+ s.flush();
+ s.refresh( item );
+ s.getTransaction().commit();
+ s.close();
+
+ Map cacheMap = getSessions().getStatistics().getSecondLevelCacheStatistics( REGION_NAME
).getEntries();
+ assertEquals( 1, cacheMap.size() );
+
+ s = openSession();
+ s.beginTransaction();
+ s.createQuery( "delete CacheableItem" ).executeUpdate();
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ public void testInsertWithRefreshThenRollback() {
+ getSessions().evict(CacheableItem.class);
+ getSessions().getStatistics().clear();
+
+ Session s = openSession();
+ s.beginTransaction();
+ CacheableItem item = new CacheableItem( "data" );
+ s.save( item );
+ s.flush();
+ s.refresh( item );
+ s.getTransaction().rollback();
+ s.close();
+
+ Map cacheMap = getSessions().getStatistics().getSecondLevelCacheStatistics( REGION_NAME
).getEntries();
+ assertEquals( 0, cacheMap.size() );
+
+ s = openSession();
+ s.beginTransaction();
+ item = (CacheableItem) s.get( CacheableItem.class, item.getId() );
+ s.getTransaction().commit();
+ s.close();
+
+ assertNull( "it should be null", item );
+ }
+
+ public void testInsertWithClear() {
+ getSessions().evict(CacheableItem.class);
+ getSessions().getStatistics().clear();
+
+ Session s = openSession();
+ s.beginTransaction();
+ CacheableItem item = new CacheableItem( "data" );
+ s.save( item );
+ s.flush();
+ s.clear();
+ s.getTransaction().commit();
+ s.close();
+
+ Map cacheMap = getSessions().getStatistics().getSecondLevelCacheStatistics( REGION_NAME
).getEntries();
+ assertEquals( 1, cacheMap.size() );
+
+ s = openSession();
+ s.beginTransaction();
+ s.createQuery( "delete CacheableItem" ).executeUpdate();
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ public void testInsertWithClearThenRollback() {
+ getSessions().evict(CacheableItem.class);
+ getSessions().getStatistics().clear();
+
+ Session s = openSession();
+ s.beginTransaction();
+ CacheableItem item = new CacheableItem( "data" );
+ s.save( item );
+ s.flush();
+ s.clear();
+ item = (CacheableItem) s.get( CacheableItem.class, item.getId() );
+ s.getTransaction().rollback();
+ s.close();
+
+ Map cacheMap = getSessions().getStatistics().getSecondLevelCacheStatistics( REGION_NAME
).getEntries();
+ assertEquals( 0, cacheMap.size() );
+
+ s = openSession();
+ s.beginTransaction();
+ item = (CacheableItem) s.get( CacheableItem.class, item.getId() );
+ s.getTransaction().commit();
+ s.close();
+
+ assertNull( "it should be null", item );
+ }
+}