📄 pessimisticisolatedclassloadertest.java
字号:
/*
* 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.classloader;
import javax.transaction.TransactionManager;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.hibernate.SessionFactory;
import org.hibernate.cache.StandardQueryCache;
import org.hibernate.cache.jbc2.BasicRegionAdapter;
import org.hibernate.cache.jbc2.builder.MultiplexingCacheInstanceManager;
import org.hibernate.cache.jbc2.query.QueryResultsRegionImpl;
import org.hibernate.cfg.Configuration;
import org.hibernate.test.cache.jbc2.functional.DualNodeTestCaseBase;
import org.hibernate.test.cache.jbc2.functional.util.DualNodeJtaTransactionManagerImpl;
import org.hibernate.test.cache.jbc2.functional.util.DualNodeTestUtil;
import org.hibernate.test.cache.jbc2.functional.util.IsolatedCacheTestSetup;
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.Region;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Tests entity and query caching when class of objects being cached are not
* visible to JBoss Cache's classloader. Also serves as a general integration
* test.
* <p/>
* This test stores an object (AccountHolder) that isn't visible to the JBC
* classloader in the cache in two places:
*
* 1) As part of the value tuple in an Account entity
* 2) As part of the FQN in a query cache entry (see query in
* ClassLoaderTestDAO.getBranch())
*/
public class PessimisticIsolatedClassLoaderTest
extends DualNodeTestCaseBase
{
public static final String OUR_PACKAGE = PessimisticIsolatedClassLoaderTest.class.getPackage().getName();
private static final String CACHE_CONFIG = "pessimistic-shared";
protected static final long SLEEP_TIME = 300L;
protected final Logger log = LoggerFactory.getLogger(getClass());
static int test = 0;
private Cache localCache;
private CacheAccessListener localListener;
private Cache remoteCache;
private CacheAccessListener remoteListener;
public PessimisticIsolatedClassLoaderTest(String name)
{
super(name);
}
public static Test suite() throws Exception {
TestSuite suite = new TestSuite(PessimisticIsolatedClassLoaderTest.class);
String[] acctClasses = { OUR_PACKAGE + ".Account", OUR_PACKAGE + ".AccountHolder" };
return new IsolatedCacheTestSetup(suite, acctClasses, CACHE_CONFIG);
}
@Override
protected Class getCacheRegionFactory()
{
return TestJBossCacheRegionFactory.class;
}
@Override
protected boolean getUseQueryCache()
{
return true;
}
@Override
public String[] getMappings()
{
return new String[] { "cache/jbc2/functional/classloader/Account.hbm.xml" };
}
@Override
protected void configureCacheFactory(Configuration cfg)
{
cfg.setProperty(MultiplexingCacheInstanceManager.ENTITY_CACHE_RESOURCE_PROP,
getEntityCacheConfigName());
cfg.setProperty(MultiplexingCacheInstanceManager.TIMESTAMP_CACHE_RESOURCE_PROP,
getEntityCacheConfigName());
}
protected String getEntityCacheConfigName() {
return CACHE_CONFIG;
}
@Override
protected void cleanupTransactionManagement() {
// Don't clean up the managers, just the transactions
// Managers are still needed by the long-lived caches
DualNodeJtaTransactionManagerImpl.cleanupTransactions();
}
@Override
protected void cleanupTest() throws Exception
{
try
{
if (localCache != null && localListener != null)
localCache.removeCacheListener(localListener);
if (remoteCache != null && remoteListener != null)
remoteCache.removeCacheListener(remoteListener);
}
finally
{
super.cleanupTest();
}
}
/**
* Simply confirms that the test fixture's classloader isolation setup
* is functioning as expected.
*
* @throws Exception
*/
public void testIsolatedSetup() throws Exception
{
// Bind a listener to the "local" cache
// Our region factory makes its CacheManager available to us
org.jboss.cache.CacheManager localManager = TestCacheInstanceManager.getTestCacheManager(DualNodeTestUtil.LOCAL);
org.jboss.cache.Cache localCache = localManager.getCache(getEntityCacheConfigName(), true);
// Bind a listener to the "remote" cache
org.jboss.cache.CacheManager remoteManager = TestCacheInstanceManager.getTestCacheManager(DualNodeTestUtil.REMOTE);
org.jboss.cache.Cache remoteCache = remoteManager.getCache(getEntityCacheConfigName(), true);
ClassLoader cl = Thread.currentThread().getContextClassLoader();
log.info("TCCL is " + cl);
Thread.currentThread().setContextClassLoader(cl.getParent());
org.jboss.cache.Fqn fqn = org.jboss.cache.Fqn.fromString("/isolated1");
org.jboss.cache.Region r = localCache.getRegion(fqn, true);
r.registerContextClassLoader(cl.getParent());
r.activate();
r = remoteCache.getRegion(fqn, true);
r.registerContextClassLoader(cl.getParent());
r.activate();
Thread.currentThread().setContextClassLoader(cl);
Account acct = new Account();
acct.setAccountHolder(new AccountHolder());
try
{
localCache.put(fqn, "key", acct);
fail("Should not have succeeded in putting acct -- classloader not isolated");
}
catch (Exception e) {
log.info("Caught exception as desired", e);
}
localCache.getRegion(fqn, false).registerContextClassLoader(Thread.currentThread().getContextClassLoader());
remoteCache.getRegion(fqn, false).registerContextClassLoader(Thread.currentThread().getContextClassLoader());
localCache.put(fqn, "key", acct);
assertEquals(acct.getClass().getName(), remoteCache.get(fqn, "key").getClass().getName());
}
public void testClassLoaderHandlingNamedQueryRegion() throws Exception {
queryTest(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -