[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/options ...
Manik Surtani
msurtani at jboss.com
Wed Jan 10 12:47:22 EST 2007
User: msurtani
Date: 07/01/10 12:47:22
Added: tests/functional/org/jboss/cache/options
ForceWriteLockTest.java
Log:
JBCACHE-629
Revision Changes Path
1.1 date: 2007/01/10 17:47:22; author: msurtani; state: Exp;JBossCache/tests/functional/org/jboss/cache/options/ForceWriteLockTest.java
Index: ForceWriteLockTest.java
===================================================================
package org.jboss.cache.options;
import junit.framework.TestCase;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.Fqn;
import org.jboss.cache.NodeSPI;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.factories.DefaultCacheFactory;
import org.jboss.cache.lock.NodeLock;
import javax.transaction.TransactionManager;
/**
* Tests forcing a write lock to be obtained on a node
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
* @since 2.0.0
*/
public class ForceWriteLockTest extends TestCase
{
private CacheSPI cache;
private Fqn fqn = Fqn.fromString("/a/b");
private TransactionManager tm;
protected void setUp()
{
Configuration c = new Configuration();
c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(c);
tm = cache.getTransactionManager();
}
protected void tearDown()
{
try
{
tm.getTransaction().rollback();
}
catch (Exception e)
{
// ignore
}
cache.stop();
}
public void testControl() throws Exception
{
cache.put(fqn, "k", "v");
tm.begin();
cache.get(fqn, "k");
assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, false);
tm.commit();
assertNotLocked(fqn);
}
public void testForceWithGetTx() throws Exception
{
cache.put(fqn, "k", "v");
tm.begin();
cache.getInvocationContext().getOptionOverrides().setForceWriteLock(true);
cache.get(fqn, "k");
assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, true);
tm.commit();
assertNotLocked(fqn);
// now test normal operation
testControl();
}
public void testForceWithPutTx() throws Exception
{
cache.put(fqn, "k", "v");
tm.begin();
cache.getInvocationContext().getOptionOverrides().setForceWriteLock(true);
cache.put(fqn, "k", "v2");
assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, true);
tm.commit();
assertNotLocked(fqn);
// now test normal operation
testControl();
}
public void testForceWithRemoveTx() throws Exception
{
cache.put(fqn, "k", "v");
tm.begin();
cache.getInvocationContext().getOptionOverrides().setForceWriteLock(true);
cache.remove(fqn, "k");
assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, true);
tm.commit();
assertNotLocked(fqn);
// now test normal operation
testControl();
}
private void assertNotLocked(Fqn fqn) throws Exception
{
NodeSPI n = cache.peek(fqn);
NodeLock lock = n.getLock();
assertFalse("node " + fqn + " is locked!", lock.isLocked());
}
private void assertLocked(Object owner, Fqn fqn, boolean write_locked) throws Exception
{
NodeSPI n = cache.peek(fqn);
if (owner == null) owner = Thread.currentThread();
NodeLock lock = n.getLock();
assertTrue("node " + fqn + " is not locked", lock.isLocked());
if (write_locked)
{
assertTrue("node " + fqn + " is not write-locked" + (lock.isReadLocked() ? " but is read-locked instead!" : "!"), lock.isWriteLocked());
}
else
{
assertTrue("node " + fqn + " is not read-locked" + (lock.isWriteLocked() ? " but is write-locked instead!" : "!"), lock.isReadLocked());
}
assertTrue("owner " + owner + "is not owner for lock " + lock, lock.isOwner(owner));
}
}
More information about the jboss-cvs-commits
mailing list