1 9 package test.tx; 10 11 import junit.framework.Test; 12 import junit.framework.TestSuite; 13 import org.apache.log4j.Logger; 14 import org.ozoneDB.ExternalTransaction; 15 import org.ozoneDB.OzoneInterface; 16 import org.ozoneDB.TransactionException; 17 import test.OzoneTestCase; 18 19 import java.io.IOException ; 20 21 25 public class TxTest extends OzoneTestCase { 26 27 30 private static Logger log = Logger.getLogger(TxTest.class); 31 private ExternalTransaction tx; 32 private Group group; 33 34 public static void addSuite(TestSuite suite) { 35 suite.addTest(new TxTest("testAll")); 36 } 37 38 39 public static Test suite() { 40 TestSuite suite = new TestSuite(); 41 suite.addTestSuite(TxTest.class); 42 return suite; 43 } 44 45 46 public TxTest(String name) { 47 super(name); 48 } 49 50 protected void setUp() throws Exception { 51 db().reloadClasses(); 52 53 tx = db().newTransaction(); 54 tx.begin(); 55 group = (Group) db().objectForName("group1"); 56 if (group != null) { 57 db().deleteObject(group); 58 } 59 group = (Group) db().createObject(GroupImpl.class.getName(), OzoneInterface.Public, "group1"); 60 tx.commit(); 61 } 62 63 public void testMultipleThreadsPerTransaction() throws TransactionException, IOException , InterruptedException { 64 log.info("testMultipleThreadsPerTransaction(): 1: group.name()=\"" + group.name() + "\"."); 66 tx.begin(); 67 group.setName("Gruppe2"); 68 Thread t1 = new AccessThread(this, tx, "group1", "Gruppe2"); 69 t1.start(); 70 Thread t2 = new AccessThread(this, null, "group1", null); 71 t2.start(); 72 73 Thread.sleep(3000); 74 log.info("testMultipleThreadsPerTransaction(): 2: group.name()=\"" + group.name() + "\"."); 75 tx.rollback(); 76 log.info("testMultipleThreadsPerTransaction(): 3: group.name()=\"" + group.name() + "\"."); 77 assertTrue("After rollback, group name should not be Gruppe2", !group.name().equals("Gruppe2")); 78 } 79 80 public void testExternalCommit() throws TransactionException, IOException { 81 tx.begin(); 83 group.setName("Gruppe"); 84 assertTrue("Group name should be Gruppe", group.name().equals("Gruppe")); 85 tx.commit(); 86 assertTrue("After commit, group name should be Gruppe", group.name().equals("Gruppe")); 87 } 88 89 public void testExternalAbortAfterCrash() throws TransactionException, IOException { 90 tx.begin(); 92 try { 93 group.setName("Gruppe"); 94 group.crash(); 95 tx.commit(); 96 } catch (Exception e) { 97 tx.rollback(); 98 } 99 assertTrue("After crash and rollback, group name should not be Gruppe", !group.name().equals("Gruppe")); 100 } 101 102 public void testExternalAbort() throws TransactionException, IOException { 103 tx.begin(); 105 group.setName("Gruppe"); 106 assertTrue("Group name should be Gruppe", group.name().equals("Gruppe")); 107 tx.rollback(); 108 assertTrue("After rollback, group name should not be Gruppe", !group.name().equals("Gruppe")); 109 } 110 111 112 protected static void print(Group g) throws Exception { 113 log.debug(Thread.currentThread().getName() + " - Group:"); 114 User[] users = g.getAll(); 115 for (int i = 0; i < users.length; i++) { 116 log.debug(" " + Thread.currentThread().getName() + ": " + users[i]); 117 } 118 } 119 120 } 121 122 123 class AccessThread extends Thread { 124 125 128 private static Logger log = Logger.getLogger(AccessThread.class); 129 TxTest test; 130 ExternalTransaction tx; 131 String dbName; 132 String name; 133 134 public AccessThread(TxTest _test, ExternalTransaction _tx, String _dbName, String _name) { 135 test = _test; 136 tx = _tx; 137 dbName = _dbName; 138 name = _name; 139 } 140 141 142 public void run() { 143 try { 144 if (tx != null) { 145 log.debug("thread(" + getName() + "): joining transaction..."); 146 tx.join(); 147 } else { 148 log.debug("thread(" + getName() + "): creating new transaction..."); 149 } 152 153 Group group = (Group) test.db().objectForName(dbName); 154 test.assertTrue("thread(" + getName() + "): group != null", group != null); 155 156 log.debug("thread(" + getName() + "): " + group.toString()); 157 if (name != null) { 158 test.assertTrue("group.name().equals (name)", group.name().equals(name)); 159 } 160 } catch (Throwable e) { 161 log.debug("Assertion failed in thread!", e); 162 } 163 } 164 } 165 | Popular Tags |