1 package org.jboss.cache.tests.aop; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jboss.cache.transaction.DummyTransactionManager; 9 10 import javax.naming.Context ; 11 import javax.naming.InitialContext ; 12 import javax.naming.NamingException ; 13 import javax.transaction.*; 14 import java.util.Properties ; 15 16 18 19 25 26 public class ReplicatedTxAopTest extends TestCase 27 { 28 Log log=LogFactory.getLog(ReplicatedTxAopTest.class); 30 TreeCacheAopTester tester, tester1; 31 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 32 DummyTransactionManager tx_mgr; 33 Throwable t1_ex, t2_ex; 34 long start=0; 35 36 37 public ReplicatedTxAopTest(String name) 38 { 39 super(name); 40 } 41 42 protected void setUp() throws Exception 43 { 44 super.setUp(); 45 log.info("setUp() ...."); 46 String configFile = "META-INF/replSync-service.xml"; 47 tester = new TreeCacheAopTester(configFile); 48 tester1 = new TreeCacheAopTester(configFile); 49 50 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 51 52 tx_mgr = DummyTransactionManager.getInstance(); 53 t1_ex = t2_ex = null; 54 } 55 56 protected void tearDown() throws Exception 57 { 58 super.tearDown(); 59 tester.stop(); 60 tester1.stop(); 61 tester = null; 62 tester1 = null; 63 64 DummyTransactionManager.destroy(); 65 } 66 67 69 public void testSetup() 70 { 71 log.info("testSetup() ...."); 72 try { 73 tester.testSetup(); 74 tester1.testSetup(); 75 } catch (Exception ex) { 76 ex.printStackTrace(); 77 fail("testSetup(): fails. " + ex.toString()); 78 } 79 } 80 81 UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException 82 { 83 Properties prop = new Properties (); 84 prop.put(Context.INITIAL_CONTEXT_FACTORY, 85 "org.jboss.cache.transaction.DummyContextFactory"); 86 return (UserTransaction)new InitialContext (prop).lookup("UserTransaction"); 87 } 88 89 public void testSimple() throws Exception 90 { 91 log.info("testSimple() ...."); 92 UserTransaction tx = getTransaction(); 93 tx.begin(); 94 tester.createPerson("/person/test1", "Harald Gliebe", 32); 95 tx.commit(); 96 tx.begin(); 97 tester.setName("/person/test1", "Benoit"); 98 tx.commit(); 99 assertEquals((Object ) "Benoit", (Object ) tester.getName("/person/test1")); 100 assertEquals((Object ) "Benoit", (Object ) tester1.getName("/person/test1")); 101 tx.begin(); 102 tester.setAge("/person/test1", 61); 103 tx.commit(); 104 assertEquals(61, tester.getAge("/person/test1")); 105 assertEquals(61, tester1.getAge("/person/test1")); 106 } 107 108 public void testModification() throws Exception 109 { 110 UserTransaction tx = getTransaction(); 111 tx.begin(); 112 tester.createPerson("/person/test2", "Harald", 32); 113 tester.setName("/person/test2", "Harald Gliebe"); 114 tx.commit(); 115 tx.begin(); 116 tester1.setName("/person/test2", "Benoit"); 117 tx.commit(); 118 assertEquals((Object ) tester.getName("/person/test2"), (Object ) "Benoit"); 119 assertEquals((Object ) tester1.getName("/person/test2"), (Object ) "Benoit"); 120 tx.begin(); 121 tester1.setName("/person/test2", "Harald"); 122 tx.rollback(); 123 assertEquals((Object ) tester.getName("/person/test2"), (Object ) "Benoit"); 124 assertEquals((Object ) tester1.getName("/person/test2"), (Object ) "Benoit"); 125 tester.removePerson("/person/test2"); 126 } 127 128 129 130 public void testConcurrentPuts() throws Exception 131 { 132 tester.setSyncCommitPhase(true); 133 134 Thread t1 = new Thread () 135 { 136 Transaction tx; 137 138 public void run() 139 { 140 try { 141 tester.createPerson("/person/test6", "p6", 50); 142 UserTransaction tx = getTransaction(); 143 tx.begin(); 144 tester.addLanguage("/person/test6", "German"); 145 _pause(4000); 146 tx.commit(); 147 } 148 catch(RollbackException rollback) { 149 ; 150 } 151 catch (Exception ex) { 152 t1_ex = ex; 153 } 154 } 155 }; 156 157 Thread t2 = new Thread () 158 { 159 Transaction tx; 160 161 public void run() 162 { 163 try { 164 _pause(1000); UserTransaction tx = getTransaction(); 166 tx.begin(); 167 tester.addLanguage("/person/test6", "English"); 168 tx.commit(); 169 } 170 catch(RollbackException rollback) { 171 ; 172 } 173 catch (Exception ex) { 174 t2_ex = ex; 175 } 176 } 177 }; 178 179 t1.start(); 180 t2.start(); 181 182 t1.join(); 183 t2.join(); 184 185 if(t2_ex != null) 187 fail("Thread1 failed: " + t2_ex); 188 if(t1_ex != null) 189 fail("Thread2 failed: " + t1_ex); 190 191 int size = tester.getLanguagesSize("/person/test6"); 192 assertEquals("number of languages should be 2, but is " + size + " (" + 193 tester.getLanguages("/person/test6") + ")", 194 2, size); 195 System.out.println("tester: " + tester.getLanguages("/person/test6")); 196 size = tester1.getLanguagesSize("/person/test6"); 197 assertEquals("number of languages should be 2, but is " + size + " (" + 198 tester.getLanguages("/person/test6") + ")", 199 2, size); 200 System.out.println("tester1: " + tester1.getLanguages("/person/test6")); 201 } 202 203 static void _pause(long millis) 204 { 205 try { 206 Thread.sleep(millis); 207 } catch (Exception ex) { 208 } 209 } 210 211 void log(String s) { 212 long now; 213 if(start == 0) 214 start=System.currentTimeMillis(); 215 now=System.currentTimeMillis(); 216 217 System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s); 218 } 219 220 221 public static Test suite() throws Exception 222 { 223 return new TestSuite(ReplicatedTxAopTest.class); 224 } 225 226 227 public static void main(String [] args) throws Exception 228 { 229 junit.textui.TestRunner.run(suite()); 230 } 231 232 } 233 234 | Popular Tags |