|
Hibernate example source code file (AbstractCollectionRegionAccessStrategyTestCase.java)
The Hibernate AbstractCollectionRegionAccessStrategyTestCase.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2007, Red Hat, Inc. and/or it's affiliates 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. and/or it's affiliates. * * 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.infinispan.collection; import javax.transaction.TransactionManager; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.infinispan.transaction.tm.BatchModeTransactionManager; import org.jboss.logging.Logger; import org.hibernate.cache.internal.CacheDataDescriptionImpl; import org.hibernate.cache.spi.CacheDataDescription; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; import org.hibernate.cache.infinispan.InfinispanRegionFactory; import org.hibernate.cache.infinispan.access.PutFromLoadValidator; import org.hibernate.cache.infinispan.access.TransactionalAccessDelegate; import org.hibernate.cache.infinispan.collection.CollectionRegionImpl; import org.hibernate.cfg.Configuration; import org.hibernate.internal.util.compare.ComparableComparator; import org.junit.After; import org.junit.Before; import org.junit.Test; import junit.framework.AssertionFailedError; import org.hibernate.test.cache.infinispan.AbstractNonFunctionalTestCase; import org.hibernate.test.cache.infinispan.NodeEnvironment; import org.hibernate.test.cache.infinispan.functional.cluster.DualNodeJtaTransactionManagerImpl; import org.hibernate.test.cache.infinispan.util.CacheTestUtil; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; /** * Base class for tests of CollectionRegionAccessStrategy impls. * * @author Galder Zamarreño * @since 3.5 */ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends AbstractNonFunctionalTestCase { private static final Logger log = Logger.getLogger( AbstractCollectionRegionAccessStrategyTestCase.class ); public static final String REGION_NAME = "test/com.foo.test"; public static final String KEY_BASE = "KEY"; public static final String VALUE1 = "VALUE1"; public static final String VALUE2 = "VALUE2"; protected static int testCount; protected NodeEnvironment localEnvironment; protected CollectionRegionImpl localCollectionRegion; protected CollectionRegionAccessStrategy localAccessStrategy; protected NodeEnvironment remoteEnvironment; protected CollectionRegionImpl remoteCollectionRegion; protected CollectionRegionAccessStrategy remoteAccessStrategy; protected boolean invalidation; protected boolean synchronous; protected Exception node1Exception; protected Exception node2Exception; protected AssertionFailedError node1Failure; protected AssertionFailedError node2Failure; protected abstract AccessType getAccessType(); @Before public void prepareResources() throws Exception { // to mimic exactly the old code results, both environments here are exactly the same... Configuration cfg = createConfiguration( getConfigurationName() ); localEnvironment = new NodeEnvironment( cfg ); localEnvironment.prepare(); localCollectionRegion = localEnvironment.getCollectionRegion( REGION_NAME, getCacheDataDescription() ); localAccessStrategy = localCollectionRegion.buildAccessStrategy( getAccessType() ); invalidation = localCollectionRegion.getCacheAdapter().isClusteredInvalidation(); synchronous = localCollectionRegion.getCacheAdapter().isSynchronous(); // Sleep a bit to avoid concurrent FLUSH problem avoidConcurrentFlush(); remoteEnvironment = new NodeEnvironment( cfg ); remoteEnvironment.prepare(); remoteCollectionRegion = remoteEnvironment.getCollectionRegion( REGION_NAME, getCacheDataDescription() ); remoteAccessStrategy = remoteCollectionRegion.buildAccessStrategy( getAccessType() ); } protected abstract String getConfigurationName(); protected static Configuration createConfiguration(String configName) { Configuration cfg = CacheTestUtil.buildConfiguration( REGION_PREFIX, InfinispanRegionFactory.class, true, false ); cfg.setProperty( InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, configName ); return cfg; } protected CacheDataDescription getCacheDataDescription() { return new CacheDataDescriptionImpl( true, true, ComparableComparator.INSTANCE ); } @After public void releaseResources() throws Exception { if ( localEnvironment != null ) { localEnvironment.release(); } if ( remoteEnvironment != null ) { remoteEnvironment.release(); } } protected boolean isUsingInvalidation() { return invalidation; } protected boolean isSynchronous() { return synchronous; } @Test public abstract void testCacheConfiguration(); @Test public void testGetRegion() { assertEquals( "Correct region", localCollectionRegion, localAccessStrategy.getRegion() ); } @Test public void testPutFromLoadRemoveDoesNotProduceStaleData() throws Exception { final CountDownLatch pferLatch = new CountDownLatch( 1 ); final CountDownLatch removeLatch = new CountDownLatch( 1 ); TransactionManager tm = DualNodeJtaTransactionManagerImpl.getInstance( "test1234" ); PutFromLoadValidator validator = new PutFromLoadValidator( tm ) { @Override public boolean acquirePutFromLoadLock(Object key) { boolean acquired = super.acquirePutFromLoadLock( key ); try { removeLatch.countDown(); pferLatch.await( 2, TimeUnit.SECONDS ); } catch (InterruptedException e) { log.debug( "Interrupted" ); Thread.currentThread().interrupt(); } catch (Exception e) { log.error( "Error", e ); throw new RuntimeException( "Error", e ); } return acquired; } }; final TransactionalAccessDelegate delegate = new TransactionalAccessDelegate( (CollectionRegionImpl) localCollectionRegion, validator ); Callable<Void> pferCallable = new Callable Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate AbstractCollectionRegionAccessStrategyTestCase.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.