Author: bstansberry(a)jboss.com
Date: 2008-04-01 16:42:48 -0400 (Tue, 01 Apr 2008)
New Revision: 5488
Added:
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/hibernate/
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/hibernate/HibernateIntegrationTestUtil.java
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/hibernate/UpdateTimestampsCachingTest.java
Log:
Add test of Hibernate's invalidate-timestamp-in-afterCompletion() usage
Added:
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/hibernate/HibernateIntegrationTestUtil.java
===================================================================
---
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/hibernate/HibernateIntegrationTestUtil.java
(rev 0)
+++
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/hibernate/HibernateIntegrationTestUtil.java 2008-04-01
20:42:48 UTC (rev 5488)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.jboss.cache.integration.hibernate;
+
+import org.jboss.cache.Fqn;
+
+/**
+ * Utilities related to the Hibernate integration tests.
+ *
+ * @author Brian Stansberry
+ */
+public class HibernateIntegrationTestUtil
+{
+ public static final Fqn<String> TS_BASE_FQN = Fqn.fromString("/TS");
+ public static final Fqn<String> REGION_PREFIX_FQN =
Fqn.fromString("/test");
+ public static final Fqn<String> TS_FQN = new Fqn<String>(TS_BASE_FQN, new
Fqn<String>(REGION_PREFIX_FQN,
Fqn.fromString("/org/hibernate/cache/UpdateTimestampsCache")));
+ public static final String ITEM = "item";
+
+ /**
+ * Prevent instantiation.
+ */
+ private HibernateIntegrationTestUtil()
+ {
+ // no-op
+ }
+}
Added:
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/hibernate/UpdateTimestampsCachingTest.java
===================================================================
---
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/hibernate/UpdateTimestampsCachingTest.java
(rev 0)
+++
core/branches/2.1.X/src/test/java/org/jboss/cache/integration/hibernate/UpdateTimestampsCachingTest.java 2008-04-01
20:42:48 UTC (rev 5488)
@@ -0,0 +1,146 @@
+package org.jboss.cache.integration.hibernate;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.jboss.cache.integration.hibernate.HibernateIntegrationTestUtil.*;
+
+import javax.transaction.Synchronization;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheFactory;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.config.Configuration;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Tests that mimic the Hibernate Second Level Cache UpdateTimestamps
+ * use case.
+ * <p>
+ * FIXME: Make this much more closely duplicate the real Hibernate usage;
+ * use a CacheManager with configs consistent with Hibernate, etc.
+ * </p>
+ *
+ * @author Brian Stansberry
+ */
+@Test(groups = "integration")
+public class UpdateTimestampsCachingTest
+{
+ private static final Log log = LogFactory.getLog(UpdateTimestampsCachingTest.class);
+
+ private static final Fqn<String> ENTITY_TYPE_FQN =
Fqn.fromString("/com/foo/MyEntity");
+
+ private Set<Cache<String, Object>> caches;
+
+ @BeforeMethod(alwaysRun = true)
+ public void setUp() throws Exception
+ {
+ caches = new HashSet<Cache<String, Object>>();
+ }
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown()
+ {
+ for (Cache<String, Object> cache : caches)
+ cache.stop();
+ }
+
+ private Cache<String, Object> createCache(boolean optimistic)
+ {
+ CacheFactory<String, Object> cf = new DefaultCacheFactory<String,
Object>();
+ Cache<String, Object> cache =
cf.createCache("META-INF/conf-test/local-tx-service.xml", false);
+ cache.getConfiguration().setNodeLockingScheme(optimistic ?
Configuration.NodeLockingScheme.OPTIMISTIC :
Configuration.NodeLockingScheme.PESSIMISTIC);
+ cache.start();
+ caches.add(cache);
+ return cache;
+ }
+
+ public void testTimestampUpdateInAfterCompletionPessimistic() throws Exception
+ {
+ timestampUpdateInAfterCompletionTest(false);
+ }
+
+ public void testTimestampUpdateInAfterCompletionOptimistic() throws Exception
+ {
+ timestampUpdateInAfterCompletionTest(true);
+ }
+
+ /**
+ * FIXME Make this use a cluster, not just a single cache
+ *
+ * @param optimistic
+ * @throws Exception
+ */
+ private void timestampUpdateInAfterCompletionTest(boolean optimistic) throws
Exception
+ {
+ Cache<String, Object> cache = createCache(optimistic);
+ TransactionManager tm =
cache.getConfiguration().getRuntimeConfig().getTransactionManager();
+
+ tm.begin();
+ Transaction tx = tm.getTransaction();
+ UpdateTimestampsSynchronization sync = new
UpdateTimestampsSynchronization(ENTITY_TYPE_FQN, cache, tm);
+ tx.registerSynchronization(sync);
+
+ Fqn<String> fqn = new Fqn<String>(REGION_PREFIX_FQN, ENTITY_TYPE_FQN);
+ fqn = new Fqn<String>(fqn, "1");
+ cache.put(fqn, ITEM, "value");
+
+ tm.commit();
+
+ fqn = new Fqn<String>(HibernateIntegrationTestUtil.TS_FQN, ENTITY_TYPE_FQN);
+ assertEquals(sync.getTimestamp(), cache.get(fqn, ITEM));
+ }
+
+ private class UpdateTimestampsSynchronization implements Synchronization
+ {
+ private final Cache<String, Object> cache;
+ private final Fqn<String> entityType;
+ private final TransactionManager tm;
+ private Long timestamp;
+
+ UpdateTimestampsSynchronization(Fqn<String> entityType, Cache<String,
Object> cache, TransactionManager tm)
+ {
+ this.entityType = entityType;
+ this.cache = cache;
+ this.tm = tm;
+ }
+
+ public Long getTimestamp()
+ {
+ return timestamp;
+ }
+
+ public void beforeCompletion()
+ {
+ // no-op
+ }
+
+ public void afterCompletion(int status)
+ {
+ Fqn<String> fqn = new Fqn<String>(TS_FQN, entityType);
+ try
+ {
+ timestamp = new Long(System.currentTimeMillis());
+
+ Transaction tx = tm.suspend();
+
cache.getInvocationContext().getOptionOverrides().setForceAsynchronous(true);
+ cache.put(fqn, ITEM, timestamp);
+ tm.resume(tx);
+ log.info("Updated timestamp " + entityType);
+ }
+ catch (Exception e)
+ {
+ log.error("Problem updating timestamp " + entityType, e);
+ throw new RuntimeException(e);
+ }
+ }
+ }
+}