[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/transaction ...
Manik Surtani
msurtani at jboss.com
Tue Aug 29 07:54:10 EDT 2006
User: msurtani
Date: 06/08/29 07:54:10
Modified: tests/functional/org/jboss/cache/transaction
TransactionTest.java
Log:
Moved the getConfiguration() method from CacheSPI to Cache
Revision Changes Path
1.11 +278 -178 JBossCache/tests/functional/org/jboss/cache/transaction/TransactionTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: TransactionTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/transaction/TransactionTest.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- TransactionTest.java 20 Jul 2006 09:03:55 -0000 1.10
+++ TransactionTest.java 29 Aug 2006 11:54:10 -0000 1.11
@@ -27,66 +27,81 @@
* Tests transactional access to a local TreeCache.
* Note: we use DummpyTranasctionManager to replace jta
*
- * @version $Id: TransactionTest.java,v 1.10 2006/07/20 09:03:55 msurtani Exp $
+ * @version $Id: TransactionTest.java,v 1.11 2006/08/29 11:54:10 msurtani Exp $
*/
-public class TransactionTest extends TestCase {
- TreeCache cache=null;
- UserTransaction tx=null;
- Properties p=null;
- String old_factory=null;
- final String FACTORY="org.jboss.cache.transaction.DummyContextFactory";
+public class TransactionTest extends TestCase
+{
+ TreeCache cache = null;
+ UserTransaction tx = null;
+ Properties p = null;
+ String old_factory = null;
+ final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
Exception thread_ex;
- public TransactionTest(String name) {
+ public TransactionTest(String name)
+ {
super(name);
}
- public void setUp() throws Exception {
+ public void setUp() throws Exception
+ {
super.setUp();
- old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
+ old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
DummyTransactionManager.getInstance();
- if(p == null) {
- p=new Properties();
+ if (p == null)
+ {
+ p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
}
- tx=(UserTransaction)new InitialContext(p).lookup("UserTransaction");
+ tx = (UserTransaction) new InitialContext(p).lookup("UserTransaction");
+ cache = new TreeCache();
cache.getConfiguration().setClusterName("test");
cache.getConfiguration().setInitialStateRetrievalTimeout(10000);
cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
cache.createService();
cache.startService();
- thread_ex=null;
+ thread_ex = null;
}
- public void tearDown() throws Exception {
+ public void tearDown() throws Exception
+ {
super.tearDown();
- if(cache != null)
+ if (cache != null)
+ {
cache.stopService();
+ cache = null;
+ }
// BW. kind of a hack to destroy jndi binding and thread local tx before next run.
DummyTransactionManager.destroy();
- if(old_factory != null) {
+ if (old_factory != null)
+ {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
- old_factory=null;
+ old_factory = null;
}
- if(tx != null) {
- try {
+ if (tx != null)
+ {
+ try
+ {
tx.rollback();
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
}
- tx=null;
+ tx = null;
}
}
- public void testPutTx() {
- try {
+ public void testPutTx()
+ {
+ try
+ {
tx.begin();
cache.put("/a/b/c", "age", new Integer(38));
// the tx interceptor should know that we're in the same tx.
@@ -99,14 +114,17 @@
// modified "age". This works because we're the only TX running.
assertEquals(new Integer(39), cache.get("/a/b/c", "age"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
fail(t.toString());
}
}
- public void testRollbackTx1() {
- try {
+ public void testRollbackTx1()
+ {
+ try
+ {
tx.begin();
cache.put("/a/b/c", "age", new Integer(38));
cache.put("/a/b/c", "age", new Integer(39));
@@ -116,15 +134,18 @@
// modified "age". This works because we're the only TX running.
assertNull(cache.get("/a/b/c", "age"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testRollbackTx2() {
- try {
+ public void testRollbackTx2()
+ {
+ try
+ {
tx.begin();
cache.put("/a/b/c", "age", new Integer(38));
cache.remove("/a/b/c", "age");
@@ -134,14 +155,17 @@
// modified "age". This works because we're the only TX running.
assertNull(cache.get("/a/b/c", "age"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testRollbackTx2a() {
- try {
+ public void testRollbackTx2a()
+ {
+ try
+ {
System.out.println("locks " + cache.printLockInfo());
cache.put("/a/b/c", "age", new Integer(38));
System.out.println("locks " + cache.printLockInfo());
@@ -153,17 +177,20 @@
// modified "age". This works because we're the only TX running.
assertEquals(new Integer(38), cache.get("/a/b/c", "age"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testRollbackTx3() {
- try {
- java.util.Map map1=new java.util.HashMap();
+ public void testRollbackTx3()
+ {
+ try
+ {
+ java.util.Map map1 = new java.util.HashMap();
map1.put("age", new Integer(38));
- java.util.Map map2=new java.util.HashMap();
+ java.util.Map map2 = new java.util.HashMap();
map2.put("age", new Integer(39));
tx.begin();
cache.put("/a/b/c", map1);
@@ -174,16 +201,19 @@
// modified "age". This works because we're the only TX running.
assertNull(cache.get("/a/b/c", "age"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testRollbackTx4() {
- try {
- Map map=new HashMap();
+ public void testRollbackTx4()
+ {
+ try
+ {
+ Map map = new HashMap();
map.put("age", new Integer(38));
tx.begin();
cache.put("/a/b/c", map);
@@ -194,14 +224,17 @@
// modified "age". This works because we're the only TX running.
assertNull(cache.get("/a/b/c", "age"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testNodeCreationRollback() {
- try {
+ public void testNodeCreationRollback()
+ {
+ try
+ {
tx.begin();
System.out.println("initial state:\n" + cache);
cache.put("/bela/ban", null);
@@ -211,14 +244,17 @@
assertNull("node should be not existent", cache.get("/bela/ban"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testNodeCreationRollback2() {
- try {
+ public void testNodeCreationRollback2()
+ {
+ try
+ {
cache.put("/bela/ban", null);
tx.begin();
cache.put("/bela/ban/michelle", null);
@@ -226,14 +262,17 @@
assertNotNull("node should be not null", cache.get("/bela/ban"));
assertNull("node should be not existent", cache.get("/bela/ban/michelle"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testNodeDeletionRollback() {
- try {
+ public void testNodeDeletionRollback()
+ {
+ try
+ {
cache.put("/a/b/c", null);
tx.begin();
cache.remove("/a/b/c");
@@ -247,14 +286,17 @@
assertNotNull(cache.get("/a/b"));
assertNotNull(cache.get("/a"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testNodeDeletionRollback2() {
- try {
+ public void testNodeDeletionRollback2()
+ {
+ try
+ {
cache.put("/a/b/c", null);
cache.put("/a/b/c1", null);
cache.put("/a/b/c2", null);
@@ -265,9 +307,9 @@
assertNull(cache.get("/a/b/c2"));
assertNull(cache.get("/a/b"));
assertNull(cache.get("/a"));
- Set children=cache.getChildrenNames("/a/b");
+ Set children = cache.getChildrenNames("/a/b");
assertNull(children);
- children=cache.getChildrenNames("/a");
+ children = cache.getChildrenNames("/a");
assertNull(children);
tx.rollback();
assertNotNull(cache.get("/a"));
@@ -275,22 +317,24 @@
assertNotNull(cache.get("/a/b/c"));
assertNotNull(cache.get("/a/b/c1"));
assertNotNull(cache.get("/a/b/c2"));
- children=cache.getChildrenNames("/a/b");
+ children = cache.getChildrenNames("/a/b");
assertEquals(3, children.size());
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
-
- public void testNodeDeletionRollback3() {
+ public void testNodeDeletionRollback3()
+ {
GlobalTransaction gtx;
- try {
+ try
+ {
tx.begin();
- gtx=cache.getCurrentTransaction();
+ gtx = cache.getCurrentTransaction();
cache.put("/a/b/c1", null);
checkLock(gtx, "/a", false);
checkLock(gtx, "/a/b", false);
@@ -323,57 +367,75 @@
tx.rollback();
assertNull(cache.getChildrenNames("/a/b"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testDoubleLocks() {
- try {
+ public void testDoubleLocks()
+ {
+ try
+ {
tx.begin();
cache.put("/a/b/c", null);
cache.put("/a/b/c", null);
- DataNode n=cache.get("/a");
- IdentityLock lock=n.getLock();
- int num=lock.getReaderOwners().size();
+ DataNode n = cache.get("/a");
+ IdentityLock lock = n.getLock();
+ int num = lock.getReaderOwners().size();
assertEquals(1, num);
- n=cache.get("/a/b");
- lock=n.getLock();
- num=lock.getReaderOwners().size();
+ n = cache.get("/a/b");
+ lock = n.getLock();
+ num = lock.getReaderOwners().size();
assertEquals(1, num);
// added this here since we had stale gtxs lurking about. - Manik, 02Jan06
tx.rollback();
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- private void checkLock(Object owner, String fqn, boolean write_locked) throws Exception {
- DataNode n=cache.get(fqn);
- IdentityLock lock=n.getLock();
- if(owner == null)
- owner=Thread.currentThread();
- if(lock.isLocked() == false)
+ private void checkLock(Object owner, String fqn, boolean write_locked) throws Exception
+ {
+ DataNode n = cache.get(fqn);
+ IdentityLock lock = n.getLock();
+ if (owner == null)
+ {
+ owner = Thread.currentThread();
+ }
+ if (lock.isLocked() == false)
+ {
throw new Exception("node " + fqn + " is not locked");
- if(write_locked) {
- if(lock.isWriteLocked() == false)
+ }
+ if (write_locked)
+ {
+ if (lock.isWriteLocked() == false)
+ {
throw new Exception("node " + fqn + " is not write-locked");
}
- else {
- if(lock.isReadLocked() == false)
+ }
+ else
+ {
+ if (lock.isReadLocked() == false)
+ {
throw new Exception("node " + fqn + " is not read-locked");
}
- if(lock.isOwner(owner) == false)
+ }
+ if (lock.isOwner(owner) == false)
+ {
throw new Exception("owner " + owner + "is not owner");
}
+ }
- public void testRemove() throws CacheException, SystemException, NotSupportedException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
+ public void testRemove() throws CacheException, SystemException, NotSupportedException, HeuristicMixedException, HeuristicRollbackException, RollbackException
+ {
cache.put("/a/b/c", null);
cache.put("/a/b/c/1", null);
cache.put("/a/b/c/2", null);
@@ -393,7 +455,8 @@
}
- public void testRemoveAndRollback() throws CacheException, SystemException, NotSupportedException, HeuristicMixedException, HeuristicRollbackException, RollbackException {
+ public void testRemoveAndRollback() throws CacheException, SystemException, NotSupportedException, HeuristicMixedException, HeuristicRollbackException, RollbackException
+ {
cache.put("/a/b/c", null);
cache.put("/a/b/c/1", null);
cache.put("/a/b/c/2", null);
@@ -422,7 +485,8 @@
}
- public void testRemoveKeyRollback() throws CacheException, SystemException, NotSupportedException {
+ public void testRemoveKeyRollback() throws CacheException, SystemException, NotSupportedException
+ {
cache.put("/bela/ban", "name", "Bela");
tx.begin();
cache.remove("/bela/ban", "name");
@@ -432,9 +496,11 @@
}
- public void testRemoveKeyRollback2() {
- try {
- Map m=new HashMap();
+ public void testRemoveKeyRollback2()
+ {
+ try
+ {
+ Map m = new HashMap();
m.put("name", "Bela");
m.put("id", new Integer(322649));
cache.put("/bela/ban", m);
@@ -444,14 +510,17 @@
tx.rollback();
assertEquals("Bela", cache.get("/bela/ban", "name"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testRemoveKeyRollback3() {
- try {
+ public void testRemoveKeyRollback3()
+ {
+ try
+ {
cache.put("/bela/ban", "name", "Bela");
tx.begin();
cache.put("/bela/ban", "name", "Michelle");
@@ -460,17 +529,18 @@
tx.rollback();
assertEquals("Bela", cache.get("/bela/ban", "name"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
-
-
- public void testDoubleRemovalOfSameData() {
- try {
+ public void testDoubleRemovalOfSameData()
+ {
+ try
+ {
tx.begin();
cache.put("/foo/1", "item", new Integer(1));
assertEquals(cache.get("/foo/1", "item"), new Integer(1));
@@ -482,7 +552,8 @@
assertFalse(cache.exists("/foo/1"));
assertNull(cache.get("/foo/1", "item"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
@@ -491,21 +562,24 @@
/**
* put(Fqn, Map) with a previous null map
*/
- public void testPutDataRollback1() {
- try {
+ public void testPutDataRollback1()
+ {
+ try
+ {
cache.put("/bela/ban", null); // create a node /bela/ban with a null map
tx.begin();
- Map m=new HashMap();
+ Map m = new HashMap();
m.put("name", "Bela");
m.put("id", new Integer(322649));
cache.put("/bela/ban", m);
tx.rollback();
- DataNode n=cache.get("/bela/ban");
- if(n.getData() == null) return;
+ DataNode n = cache.get("/bela/ban");
+ if (n.getData() == null) return;
assertEquals("map should be empty", 0, n.getData().size());
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
@@ -514,41 +588,46 @@
/**
* put(Fqn, Map) with a previous non-null map
*/
- public void testputDataRollback2() {
+ public void testputDataRollback2()
+ {
Map m1, m2;
- m1=new HashMap();
+ m1 = new HashMap();
m1.put("name", "Bela");
m1.put("id", new Integer(322649));
- m2=new HashMap();
+ m2 = new HashMap();
m2.put("other", "bla");
m2.put("name", "Michelle");
- try {
+ try
+ {
cache.put("/bela/ban", m1);
tx.begin();
cache.put("/bela/ban", m2);
- Map tmp=cache.get("/bela/ban").getData();
+ Map tmp = cache.get("/bela/ban").getData();
assertEquals(3, tmp.size());
assertEquals("Michelle", tmp.get("name"));
assertEquals(tmp.get("id"), new Integer(322649));
assertEquals("bla", tmp.get("other"));
tx.rollback();
- tmp=cache.get("/bela/ban").getData();
+ tmp = cache.get("/bela/ban").getData();
assertEquals(2, tmp.size());
assertEquals("Bela", tmp.get("name"));
assertEquals(tmp.get("id"), new Integer(322649));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testPutRollback() {
- try {
+ public void testPutRollback()
+ {
+ try
+ {
cache.put("/bela/ban", null); // /bela/ban needs to exist
tx.begin();
cache.put("/bela/ban", "name", "Bela");
@@ -556,15 +635,18 @@
tx.rollback();
assertNull(cache.get("/bela/ban", "name"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testPutRollback2() {
- try {
+ public void testPutRollback2()
+ {
+ try
+ {
cache.put("/bela/ban", "name", "Bela"); // /bela/ban needs to exist
tx.begin();
cache.put("/bela/ban", "name", "Michelle");
@@ -572,15 +654,17 @@
tx.rollback();
assertEquals("Bela", cache.get("/bela/ban", "name"));
}
- catch(Throwable t) {
+ catch (Throwable t)
+ {
t.printStackTrace();
fail(t.toString());
}
}
- public void testSimpleRollbackTransactions() throws Exception {
- final Fqn FQN=Fqn.fromString("/a/b/c");
+ public void testSimpleRollbackTransactions() throws Exception
+ {
+ final Fqn FQN = Fqn.fromString("/a/b/c");
tx.begin();
cache.put(FQN, "entry", "commit");
tx.commit();
@@ -599,29 +683,35 @@
}
- private Transaction startTransaction() throws Exception {
- DummyTransactionManager mgr=DummyTransactionManager.getInstance();
+ private Transaction startTransaction() throws Exception
+ {
+ DummyTransactionManager mgr = DummyTransactionManager.getInstance();
mgr.begin();
return mgr.getTransaction();
}
- public void testConcurrentReadAndWriteAccess() throws Exception {
+ public void testConcurrentReadAndWriteAccess() throws Exception
+ {
cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
cache.put("/1/2/3/4", "foo", "bar"); // no TX, no locks held after put() returns
- class Reader extends Thread {
+ class Reader extends Thread
+ {
Transaction thread_tx;
- public Reader() {
+ public Reader()
+ {
super("Reader");
}
- public void run() {
- try {
- thread_tx=startTransaction();
+ public void run()
+ {
+ try
+ {
+ thread_tx = startTransaction();
log("acquiring RL");
cache.get("/1/2/3", "foo"); // acquires RLs on all 3 nodes
log("RL acquired successfully");
@@ -630,23 +720,28 @@
thread_tx.commit(); // releases RLs
log("committed TX");
}
- catch(Exception e) {
- thread_ex=e;
+ catch (Exception e)
+ {
+ thread_ex = e;
}
}
}
- class Writer extends Thread {
+ class Writer extends Thread
+ {
Transaction thread_tx;
- public Writer() {
+ public Writer()
+ {
super("Writer");
}
- public void run() {
- try {
+ public void run()
+ {
+ try
+ {
sleep(500); // give the Reader a chance to acquire the RLs
- thread_tx=startTransaction();
+ thread_tx = startTransaction();
log("acquiring WL");
cache.put("/1", "foo", "bar2"); // needs to acquired a WL on /1
log("acquired WL successfully");
@@ -654,21 +749,24 @@
thread_tx.commit();
log("committed TX");
}
- catch(Exception e) {
- thread_ex=e;
+ catch (Exception e)
+ {
+ thread_ex = e;
}
}
}
- Reader reader=new Reader();
- Writer writer=new Writer();
+ Reader reader = new Reader();
+ Writer writer = new Writer();
reader.start();
writer.start();
reader.join();
writer.join();
- if(thread_ex != null)
+ if (thread_ex != null)
+ {
throw thread_ex;
}
+ }
/* public void testConcurrentReadAccess() throws Exception {
@@ -713,8 +811,9 @@
throw thread_ex;
}
-*/
- private static void log(String msg) {
+ */
+ private static void log(String msg)
+ {
System.out.println(Thread.currentThread().getName() + ": " + msg);
}
@@ -777,7 +876,6 @@
// log(cache.get("/SecurityInfo", Integer.toString(23)).toString());
// }
-
// public void testConcurrentReadsWithSerializableIsolationLevel() throws CacheException, InterruptedException {
// final long TIMEOUT=5000;
// cache.setIsolationLevel(IsolationLevel.SERIALIZABLE);
@@ -841,11 +939,13 @@
// }
- public static Test suite() throws Exception {
+ public static Test suite() throws Exception
+ {
return new TestSuite(TransactionTest.class);
}
- public static void main(String[] args) throws Exception {
+ public static void main(String[] args) throws Exception
+ {
junit.textui.TestRunner.run(suite());
}
More information about the jboss-cvs-commits
mailing list