Add initial "clustered integration" test
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@14362 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
bae1559748
commit
8f458e07e7
48
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/Contact.hbm.xml
vendored
Executable file
48
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/Contact.hbm.xml
vendored
Executable file
|
@ -0,0 +1,48 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
|
||||
~
|
||||
~ 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
|
||||
-->
|
||||
<hibernate-mapping
|
||||
package="org.hibernate.test.cache.jbc2.functional">
|
||||
|
||||
<class name="Contact" table="Contacts">
|
||||
|
||||
<cache usage="transactional"/>
|
||||
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
|
||||
<property name="name" not-null="true"/>
|
||||
<property name="tlf" not-null="true"/>
|
||||
|
||||
<many-to-one name="customer"
|
||||
class="org.hibernate.test.cache.jbc2.functional.Customer"
|
||||
column="CUSTOMER_ID"/>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
92
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/Contact.java
vendored
Executable file
92
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/Contact.java
vendored
Executable file
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
|
||||
*
|
||||
* 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.jbc2.functional;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
/**
|
||||
* Entity that has a many-to-one relationship to a Customer
|
||||
*/
|
||||
public class Contact implements Serializable
|
||||
{
|
||||
/** The serialVersionUID */
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
Integer id;
|
||||
String name;
|
||||
String tlf;
|
||||
Customer customer;
|
||||
|
||||
public Contact()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Id
|
||||
public Integer getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getTlf()
|
||||
{
|
||||
return tlf;
|
||||
}
|
||||
|
||||
public void setTlf(String tlf)
|
||||
{
|
||||
this.tlf = tlf;
|
||||
}
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="CUST_ID")
|
||||
public Customer getCustomer()
|
||||
{
|
||||
return customer;
|
||||
}
|
||||
|
||||
public void setCustomer(Customer customer)
|
||||
{
|
||||
this.customer = customer;
|
||||
}
|
||||
|
||||
}
|
50
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/Customer.hbm.xml
vendored
Executable file
50
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/Customer.hbm.xml
vendored
Executable file
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
|
||||
~
|
||||
~ 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
|
||||
-->
|
||||
<hibernate-mapping
|
||||
package="org.hibernate.test.cache.jbc2.functional">
|
||||
|
||||
<class name="Customer" table="Customers">
|
||||
|
||||
<cache usage="transactional"/>
|
||||
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
|
||||
<property name="name" not-null="true"/>
|
||||
|
||||
<set name="contacts" cascade="all" lazy="false">
|
||||
<cache usage="transactional"/>
|
||||
<key column="CUSTOMER_ID"/>
|
||||
<one-to-many class="org.hibernate.test.cache.jbc2.functional.Contact"/>
|
||||
</set>
|
||||
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
81
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/Customer.java
vendored
Executable file
81
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/Customer.java
vendored
Executable file
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
|
||||
*
|
||||
* 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.jbc2.functional;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Id;
|
||||
|
||||
/**
|
||||
* Company customer
|
||||
*
|
||||
* @author Emmanuel Bernard
|
||||
* @author Kabir Khan
|
||||
*/
|
||||
public class Customer implements Serializable
|
||||
{
|
||||
/** The serialVersionUID */
|
||||
private static final long serialVersionUID = 1L;
|
||||
Integer id;
|
||||
String name;
|
||||
|
||||
private transient Set<Contact> contacts;
|
||||
|
||||
public Customer()
|
||||
{
|
||||
}
|
||||
|
||||
@Id
|
||||
public Integer getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String string)
|
||||
{
|
||||
name = string;
|
||||
}
|
||||
|
||||
public Set<Contact> getContacts()
|
||||
{
|
||||
return contacts;
|
||||
}
|
||||
|
||||
public void setContacts(Set<Contact> contacts)
|
||||
{
|
||||
this.contacts = contacts;
|
||||
}
|
||||
}
|
||||
|
200
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/DualNodeTestCaseBase.java
vendored
Normal file
200
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/DualNodeTestCaseBase.java
vendored
Normal file
|
@ -0,0 +1,200 @@
|
|||
/*
|
||||
* Copyright (c) 2008, Red Hat Middleware, LLC. All rights reserved.
|
||||
*
|
||||
* 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, v. 2.1. This program is distributed in the
|
||||
* hope that it will be useful, but WITHOUT A 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, v.2.1 along with this
|
||||
* distribution; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* Red Hat Author(s): Brian Stansberry
|
||||
*/
|
||||
|
||||
package org.hibernate.test.cache.jbc2.functional;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Mappings;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.SessionFactoryImplementor;
|
||||
import org.hibernate.junit.functional.ExecutionEnvironment;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Base class for tests that need to create two separate SessionFactory
|
||||
* instances to simulate a two-node cluster.
|
||||
*
|
||||
* @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
|
||||
*/
|
||||
public abstract class DualNodeTestCaseBase extends CacheTestCaseBase
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger( CacheTestCaseBase.class );
|
||||
|
||||
private ExecutionEnvironment secondNodeEnvironment;
|
||||
private org.hibernate.classic.Session secondNodeSession;
|
||||
|
||||
/**
|
||||
* Create a new DualNodeTestCaseBase.
|
||||
*
|
||||
* @param x
|
||||
*/
|
||||
public DualNodeTestCaseBase(String x)
|
||||
{
|
||||
super(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(Configuration cfg)
|
||||
{
|
||||
standardConfigure(cfg);
|
||||
configureFirstNode(cfg);
|
||||
}
|
||||
|
||||
private void standardConfigure(Configuration cfg) {
|
||||
super.configure(cfg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply any node-specific configurations to our first node.
|
||||
*
|
||||
* @param the Configuration to update.
|
||||
*/
|
||||
protected abstract void configureFirstNode(Configuration cfg);
|
||||
|
||||
/**
|
||||
* Apply any node-specific configurations to our second node.
|
||||
*
|
||||
* @param the Configuration to update.
|
||||
*/
|
||||
protected abstract void configureSecondNode(Configuration cfg);
|
||||
|
||||
@Override
|
||||
protected void prepareTest() throws Exception
|
||||
{
|
||||
log.info( "Building second node locally managed execution env" );
|
||||
secondNodeEnvironment = new ExecutionEnvironment( new SecondNodeSettings() );
|
||||
secondNodeEnvironment.initialize();
|
||||
|
||||
super.prepareTest();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable
|
||||
{
|
||||
try {
|
||||
super.runTest();
|
||||
}
|
||||
finally {
|
||||
|
||||
if ( secondNodeSession != null && secondNodeSession.isOpen() ) {
|
||||
if ( secondNodeSession.isConnected() ) {
|
||||
secondNodeSession.connection().rollback();
|
||||
}
|
||||
secondNodeSession.close();
|
||||
secondNodeSession = null;
|
||||
fail( "unclosed session" );
|
||||
}
|
||||
else {
|
||||
secondNodeSession = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cleanupTest() throws Exception
|
||||
{
|
||||
super.cleanupTest();
|
||||
|
||||
log.info( "Destroying second node locally managed execution env" );
|
||||
secondNodeEnvironment.complete();
|
||||
secondNodeEnvironment = null;
|
||||
}
|
||||
|
||||
public ExecutionEnvironment getSecondNodeEnvironment() {
|
||||
return secondNodeEnvironment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings impl that delegates most calls to the DualNodeTestCase itself,
|
||||
* but overrides the configure method to allow separate cache settings
|
||||
* for the second node.
|
||||
*/
|
||||
public class SecondNodeSettings implements ExecutionEnvironment.Settings {
|
||||
|
||||
private DualNodeTestCaseBase delegate;
|
||||
|
||||
public SecondNodeSettings() {
|
||||
this.delegate = DualNodeTestCaseBase.this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the important one -- we extend the delegate's work by
|
||||
* adding second-node specific settings
|
||||
*/
|
||||
public void configure(Configuration arg0)
|
||||
{
|
||||
delegate.standardConfigure(arg0);
|
||||
configureSecondNode(arg0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable creating of schemas; we let the primary session factory
|
||||
* do that to our shared database.
|
||||
*/
|
||||
public boolean createSchema()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable creating of schemas; we let the primary session factory
|
||||
* do that to our shared database.
|
||||
*/
|
||||
public boolean recreateSchemaAfterFailure()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void afterConfigurationBuilt(Mappings arg0, Dialect arg1)
|
||||
{
|
||||
delegate.afterConfigurationBuilt(arg0, arg1);
|
||||
}
|
||||
|
||||
public void afterSessionFactoryBuilt(SessionFactoryImplementor arg0)
|
||||
{
|
||||
delegate.afterSessionFactoryBuilt(arg0);
|
||||
}
|
||||
|
||||
public boolean appliesTo(Dialect arg0)
|
||||
{
|
||||
return delegate.appliesTo(arg0);
|
||||
}
|
||||
|
||||
public String getBaseForMappings()
|
||||
{
|
||||
return delegate.getBaseForMappings();
|
||||
}
|
||||
|
||||
public String getCacheConcurrencyStrategy()
|
||||
{
|
||||
return delegate.getCacheConcurrencyStrategy();
|
||||
}
|
||||
|
||||
public String[] getMappings()
|
||||
{
|
||||
return delegate.getMappings();
|
||||
}
|
||||
|
||||
public boolean overrideCacheStrategy()
|
||||
{
|
||||
return delegate.overrideCacheStrategy();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
|
||||
*
|
||||
* 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.jbc2.functional;
|
||||
|
||||
|
||||
/**
|
||||
* Executes the superclass tests, but with Hibernate and JBoss Cache
|
||||
* configured for optimistic locking.
|
||||
*
|
||||
* @author Brian Stansberry
|
||||
*/
|
||||
public class OptimisticEntityReplicationTest extends PessimisticEntityReplicationTest
|
||||
{
|
||||
|
||||
public OptimisticEntityReplicationTest(String name)
|
||||
{
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getEntityCacheConfigName()
|
||||
{
|
||||
return "optimistic-shared";
|
||||
}
|
||||
|
||||
}
|
356
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/PessimisticEntityReplicationTest.java
vendored
Executable file
356
cache-jbosscache2/src/test/java/org/hibernate/test/cache/jbc2/functional/PessimisticEntityReplicationTest.java
vendored
Executable file
|
@ -0,0 +1,356 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
|
||||
*
|
||||
* 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.jbc2.functional;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.transaction.TransactionManager;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cache.RegionFactory;
|
||||
import org.hibernate.cache.jbc2.builder.MultiplexingCacheInstanceManager;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.test.cache.jbc2.functional.util.TestCacheInstanceManager;
|
||||
import org.hibernate.test.cache.jbc2.functional.util.TestJBossCacheRegionFactory;
|
||||
import org.jboss.cache.Cache;
|
||||
import org.jboss.cache.CacheManager;
|
||||
import org.jboss.cache.Fqn;
|
||||
import org.jboss.cache.notifications.annotation.CacheListener;
|
||||
import org.jboss.cache.notifications.annotation.NodeVisited;
|
||||
import org.jboss.cache.notifications.event.NodeVisitedEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Port of the earlier JBoss EJB3 project's
|
||||
* org.jboss.ejb3.test.clusteredentity.unit.EntityUnitTestCase
|
||||
*
|
||||
*/
|
||||
public class PessimisticEntityReplicationTest
|
||||
extends DualNodeTestCaseBase
|
||||
{
|
||||
protected final Logger log = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private static final long SLEEP_TIME = 100l;
|
||||
|
||||
private static final String LOCAL = "local";
|
||||
private static final String REMOTE = "remote";
|
||||
|
||||
private static final Integer CUSTOMER_ID = new Integer(1);
|
||||
|
||||
static int test = 0;
|
||||
|
||||
public PessimisticEntityReplicationTest(String name)
|
||||
{
|
||||
super(name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void configure(Configuration cfg)
|
||||
{
|
||||
super.configure(cfg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected Class<? extends RegionFactory> getCacheRegionFactory()
|
||||
{
|
||||
return TestJBossCacheRegionFactory.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean getUseQueryCache()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureCacheFactory(Configuration cfg)
|
||||
{
|
||||
cfg.setProperty(MultiplexingCacheInstanceManager.ENTITY_CACHE_RESOURCE_PROP,
|
||||
getEntityCacheConfigName());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureFirstNode(Configuration cfg)
|
||||
{
|
||||
cfg.setProperty(TestCacheInstanceManager.CACHE_MANAGER_NAME_PROP,
|
||||
LOCAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureSecondNode(Configuration cfg)
|
||||
{
|
||||
cfg.setProperty(TestCacheInstanceManager.CACHE_MANAGER_NAME_PROP,
|
||||
REMOTE);
|
||||
}
|
||||
|
||||
protected String getEntityCacheConfigName() {
|
||||
return "pessimistic-shared";
|
||||
}
|
||||
|
||||
|
||||
public void testAll() throws Exception
|
||||
{
|
||||
System.out.println("*** testAll()");
|
||||
|
||||
// Bind a listener to the "local" cache
|
||||
// Our region factory makes its CacheManager available to us
|
||||
CacheManager localManager = TestCacheInstanceManager.getTestCacheManager(LOCAL);
|
||||
Cache localCache = localManager.getCache(getEntityCacheConfigName(), true);
|
||||
MyListener localListener = new MyListener();
|
||||
localCache.addCacheListener(localListener);
|
||||
|
||||
TransactionManager localTM = localCache.getConfiguration().getRuntimeConfig().getTransactionManager();
|
||||
|
||||
// Bind a listener to the "remote" cache
|
||||
CacheManager remoteManager = TestCacheInstanceManager.getTestCacheManager(REMOTE);
|
||||
Cache remoteCache = remoteManager.getCache(getEntityCacheConfigName(), true);
|
||||
MyListener remoteListener = new MyListener();
|
||||
remoteCache.addCacheListener(remoteListener);
|
||||
|
||||
TransactionManager remoteTM = remoteCache.getConfiguration().getRuntimeConfig().getTransactionManager();
|
||||
|
||||
SessionFactory localFactory = getEnvironment().getSessionFactory();
|
||||
SessionFactory remoteFactory = getSecondNodeEnvironment().getSessionFactory();
|
||||
|
||||
try
|
||||
{
|
||||
System.out.println("Create node 0");
|
||||
IdContainer ids = createCustomer(localFactory, localTM);
|
||||
|
||||
// Sleep a bit to let async commit propagate. Really just to
|
||||
// help keep the logs organized for debugging any issues
|
||||
sleep(SLEEP_TIME);
|
||||
|
||||
System.out.println("Find node 0");
|
||||
// This actually brings the collection into the cache
|
||||
getCustomer(ids.customerId, localFactory, localTM);
|
||||
|
||||
// Now the collection is in the cache so, we the 2nd "get"
|
||||
// should read everything from the cache
|
||||
System.out.println("Find(2) node 0");
|
||||
localListener.clear();
|
||||
getCustomer(ids.customerId, localFactory, localTM);
|
||||
|
||||
//Check the read came from the cache
|
||||
System.out.println("Check cache 0");
|
||||
assertLoadedFromCache(localListener, ids.customerId, ids.contactIds);
|
||||
|
||||
// The above placement of the collection in the cache is replicated async
|
||||
// so pause a bit before checking node 1
|
||||
sleep(SLEEP_TIME);
|
||||
|
||||
System.out.println("Find node 1");
|
||||
getCustomer(ids.customerId, remoteFactory, remoteTM);
|
||||
|
||||
//Check everything was in cache
|
||||
System.out.println("Check cache 1");
|
||||
assertLoadedFromCache(remoteListener, ids.customerId, ids.contactIds);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// cleanup the db
|
||||
System.out.println("Cleaning up");
|
||||
cleanup(localFactory, localTM);
|
||||
}
|
||||
}
|
||||
|
||||
private IdContainer createCustomer(SessionFactory sessionFactory, TransactionManager tm)
|
||||
throws Exception
|
||||
{
|
||||
System.out.println("CREATE CUSTOMER");
|
||||
|
||||
tm.begin();
|
||||
|
||||
try
|
||||
{
|
||||
Session session = sessionFactory.getCurrentSession();
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("JBoss");
|
||||
Set<Contact> contacts = new HashSet<Contact>();
|
||||
|
||||
Contact kabir = new Contact();
|
||||
kabir.setCustomer(customer);
|
||||
kabir.setName("Kabir");
|
||||
kabir.setTlf("1111");
|
||||
contacts.add(kabir);
|
||||
|
||||
Contact bill = new Contact();
|
||||
bill.setCustomer(customer);
|
||||
bill.setName("Bill");
|
||||
bill.setTlf("2222");
|
||||
contacts.add(bill);
|
||||
|
||||
customer.setContacts(contacts);
|
||||
|
||||
session.save(customer);
|
||||
tm.commit();
|
||||
|
||||
IdContainer ids = new IdContainer();
|
||||
ids.customerId = customer.getId();
|
||||
Set contactIds = new HashSet();
|
||||
contactIds.add(kabir.getId());
|
||||
contactIds.add(bill.getId());
|
||||
ids.contactIds = contactIds;
|
||||
|
||||
return ids;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("Caught exception creating customer", e);
|
||||
try {
|
||||
tm.rollback();
|
||||
}
|
||||
catch (Exception e1) {
|
||||
log.error("Exception rolling back txn", e1);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
System.out.println("CREATE CUSTOMER - END");
|
||||
}
|
||||
}
|
||||
|
||||
private Customer getCustomer(Integer id, SessionFactory sessionFactory, TransactionManager tm)
|
||||
throws Exception
|
||||
{
|
||||
System.out.println("FIND CUSTOMER");
|
||||
|
||||
tm.begin();
|
||||
try
|
||||
{
|
||||
Session session = sessionFactory.getCurrentSession();
|
||||
Customer customer = (Customer) session.get(Customer.class, id);
|
||||
// Access all the contacts
|
||||
for (Iterator it = customer.getContacts().iterator(); it.hasNext();) {
|
||||
((Contact) it.next()).getName();
|
||||
}
|
||||
tm.commit();
|
||||
return customer;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
try {
|
||||
tm.rollback();
|
||||
}
|
||||
catch (Exception e1) {
|
||||
log.error("Exception rolling back txn", e1);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
System.out.println("FIND CUSTOMER - END");
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanup(SessionFactory sessionFactory, TransactionManager tm) throws Exception
|
||||
{
|
||||
tm.begin();
|
||||
try
|
||||
{
|
||||
Session session = sessionFactory.getCurrentSession();
|
||||
Customer c = (Customer) session.get(Customer.class, CUSTOMER_ID);
|
||||
if (c != null)
|
||||
{
|
||||
Set contacts = c.getContacts();
|
||||
for (Iterator it = contacts.iterator(); it.hasNext();)
|
||||
session.delete(it.next());
|
||||
c.setContacts(null);
|
||||
session.delete(c);
|
||||
}
|
||||
|
||||
tm.commit();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
try {
|
||||
tm.rollback();
|
||||
}
|
||||
catch (Exception e1) {
|
||||
log.error("Exception rolling back txn", e1);
|
||||
}
|
||||
log.error("Caught exception in cleanup", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertLoadedFromCache(MyListener listener, Integer custId, Set contactIds)
|
||||
{
|
||||
assertTrue("Customer#" + custId + " was in cache", listener.visited.contains("Customer#" + custId));
|
||||
for (Iterator it = contactIds.iterator(); it.hasNext();) {
|
||||
Integer contactId = (Integer) it.next();
|
||||
assertTrue("Contact#"+ contactId + " was in cache", listener.visited.contains("Contact#"+ contactId));
|
||||
assertTrue("Contact#"+ contactId + " was in cache", listener.visited.contains("Contact#"+ contactId));
|
||||
}
|
||||
assertTrue("Customer.contacts" + custId + " was in cache",
|
||||
listener.visited.contains("Customer.contacts#" + custId));
|
||||
}
|
||||
|
||||
@CacheListener
|
||||
public class MyListener
|
||||
{
|
||||
HashSet<String> visited = new HashSet<String>();
|
||||
|
||||
public void clear()
|
||||
{
|
||||
visited.clear();
|
||||
}
|
||||
|
||||
@NodeVisited
|
||||
public void nodeVisited(NodeVisitedEvent event)
|
||||
{
|
||||
System.out.println(event);
|
||||
|
||||
if (!event.isPre())
|
||||
{
|
||||
Fqn fqn = event.getFqn();
|
||||
System.out.println("MyListener - Visiting node " + fqn.toString());
|
||||
String name = fqn.toString();
|
||||
String token = ".functional.";
|
||||
int index = name.indexOf(token);
|
||||
if (index > -1)
|
||||
{
|
||||
index += token.length();
|
||||
name = name.substring(index);
|
||||
System.out.println("MyListener - recording visit to " + name);
|
||||
visited.add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class IdContainer {
|
||||
Integer customerId;
|
||||
Set contactIds;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2007, Red Hat Middleware LLC 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 Middleware LLC.
|
||||
*
|
||||
* 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.jbc2.functional.util;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.hibernate.cache.CacheException;
|
||||
import org.hibernate.cache.jbc2.builder.MultiplexingCacheInstanceManager;
|
||||
import org.hibernate.cfg.Settings;
|
||||
import org.jboss.cache.CacheManager;
|
||||
|
||||
|
||||
/**
|
||||
* A {@link MultiplexingCacheInstanceManager} that exposes its
|
||||
* CacheManager via a static getter so the test fixture can get ahold
|
||||
* of it.
|
||||
*
|
||||
* @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
|
||||
*/
|
||||
public class TestCacheInstanceManager extends MultiplexingCacheInstanceManager {
|
||||
|
||||
private static final Hashtable cacheManagers = new Hashtable();
|
||||
|
||||
public static final String CACHE_MANAGER_NAME_PROP = "hibernate.test.cache.jbc2.cache.manager.name";
|
||||
|
||||
public static CacheManager getTestCacheManager(String name) {
|
||||
return (CacheManager) cacheManagers.get(name);
|
||||
}
|
||||
|
||||
private String cacheManagerName;
|
||||
|
||||
/**
|
||||
* Create a new TestCacheInstanceManager.
|
||||
*/
|
||||
public TestCacheInstanceManager() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Settings settings, Properties properties) throws CacheException {
|
||||
|
||||
super.start(settings, properties);
|
||||
|
||||
cacheManagerName = properties.getProperty(CACHE_MANAGER_NAME_PROP);
|
||||
cacheManagers.put(cacheManagerName, getCacheFactory());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop()
|
||||
{
|
||||
cacheManagers.remove(cacheManagerName);
|
||||
|
||||
super.stop();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2007, Red Hat Middleware LLC 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 Middleware LLC.
|
||||
*
|
||||
* 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.jbc2.functional.util;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.hibernate.cache.jbc2.JBossCacheRegionFactory;
|
||||
import org.jboss.cache.CacheManager;
|
||||
|
||||
/**
|
||||
* {@link JBossCacheRegionFactory} that uses
|
||||
* {@link TestCacheInstanceManager} as its
|
||||
* {@link #getCacheInstanceManager() CacheInstanceManager}.
|
||||
* <p>
|
||||
* This version lets a test fixture to access {@link CacheManager},
|
||||
* making it easy for the test fixture to get access to the caches being
|
||||
* used. Intended for FunctionalUnitTestCase subclasses where the creation
|
||||
* of the region factory is hidden inside the initialization of a SessionFactory.
|
||||
* </p>
|
||||
*
|
||||
* @author Brian Stansberry
|
||||
*/
|
||||
public class TestJBossCacheRegionFactory extends JBossCacheRegionFactory {
|
||||
|
||||
/**
|
||||
* FIXME Per the RegionFactory class Javadoc, this constructor version
|
||||
* should not be necessary.
|
||||
*
|
||||
* @param props The configuration properties
|
||||
*/
|
||||
public TestJBossCacheRegionFactory(Properties props) {
|
||||
this();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new TestJBossCacheRegionFactory.
|
||||
*
|
||||
*/
|
||||
public TestJBossCacheRegionFactory() {
|
||||
super(new TestCacheInstanceManager());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue