1 package org.jboss.cache.pojo; 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.pojo.test.Person; 9 import org.jboss.cache.pojo.test.Student; 10 import org.jboss.cache.transaction.DummyTransactionManager; 11 12 import javax.naming.Context ; 13 import javax.naming.InitialContext ; 14 import javax.naming.NamingException ; 15 import javax.transaction.NotSupportedException ; 16 import javax.transaction.RollbackException ; 17 import javax.transaction.SystemException ; 18 import javax.transaction.Transaction ; 19 import javax.transaction.UserTransaction ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.Properties ; 23 24 26 27 public class ReplicatedTxTest extends TestCase 28 { 29 Log log = LogFactory.getLog(ReplicatedTxTest.class); 30 PojoCache cache, cache1; 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 ReplicatedTxTest(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 boolean toStart = false; 48 cache = PojoCacheFactory.createCache(configFile, toStart); 49 cache.start(); 50 cache1 = PojoCacheFactory.createCache(configFile, toStart); 51 cache1.start(); 52 53 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 54 55 tx_mgr = DummyTransactionManager.getInstance(); 56 t1_ex = t2_ex = null; 57 } 58 59 protected void tearDown() throws Exception 60 { 61 super.tearDown(); 62 cache.stop(); 63 cache1.stop(); 64 65 DummyTransactionManager.destroy(); 66 } 67 68 70 UserTransaction getTransaction() throws SystemException , NotSupportedException , NamingException 71 { 72 Properties prop = new Properties (); 73 prop.put(Context.INITIAL_CONTEXT_FACTORY, 74 "org.jboss.cache.transaction.DummyContextFactory"); 75 return (UserTransaction ) new InitialContext (prop).lookup("UserTransaction"); 76 } 77 78 private Person createPerson(String id, String name, int age) 79 { 80 Person p = new Person(); 81 p.setName(name); 82 p.setAge(age); 83 cache.attach(id, p); 84 return p; 85 } 86 87 private Student createStudent(String id, String name, int age, String grade) 88 { 89 Student p = new Student(); 90 p.setName(name); 91 p.setAge(age); 92 p.setYear(grade); 93 cache.attach(id, p); 94 return p; 95 } 96 97 public void testSimple() throws Exception 98 { 99 log.info("testSimple() ...."); 100 UserTransaction tx = getTransaction(); 101 tx.begin(); 102 Person p = createPerson("/person/test1", "Harald Gliebe", 32); 103 tx.commit(); 104 tx.begin(); 105 p.setName("Benoit"); 106 tx.commit(); 107 Person p1 = (Person) cache1.find("/person/test1"); 108 assertEquals("Benoit", p.getName()); 109 assertEquals("Benoit", p1.getName()); 110 tx.begin(); 111 p1.setAge(61); 112 tx.commit(); 113 assertEquals(61, p.getAge()); 114 assertEquals(61, p1.getAge()); 115 } 116 117 public void testModification() throws Exception 118 { 119 UserTransaction tx = getTransaction(); 120 tx.begin(); 121 Person p = createPerson("/person/test2", "Harald", 32); 122 p.setName("Harald Gliebe"); 123 tx.commit(); 124 Person p1 = (Person) cache1.find("/person/test2"); 125 tx.begin(); 126 p1.setName("Benoit"); 127 tx.commit(); 128 assertEquals(p.getName(), "Benoit"); 129 assertEquals(p1.getName(), "Benoit"); 130 tx.begin(); 131 p1.setName("Harald"); 132 tx.rollback(); 133 assertEquals(p.getName(), "Benoit"); 134 assertEquals(p1.getName(), "Benoit"); 135 } 136 137 public void testConcurrentPuts() throws Exception 138 { 139 Thread t1 = new Thread () 140 { 141 Transaction tx; 142 143 public void run() 144 { 145 try 146 { 147 List <String > lang = ((Person) cache.find("/person/test6")).getLanguages(); 148 UserTransaction tx = getTransaction(); 149 tx.begin(); 150 lang.add("German"); 151 TestingUtil.sleepThread(17000); 152 tx.commit(); 153 } 154 catch (RollbackException rollback) 155 { 156 ; 157 } 158 catch (Exception ex) 159 { 160 t1_ex = ex; 161 } 162 } 163 }; 164 165 Thread t2 = new Thread () 166 { 167 Transaction tx; 168 169 public void run() 170 { 171 UserTransaction tx = null; 172 try 173 { 174 TestingUtil.sleepThread(1000); List <String > lang = ((Person) cache.find("/person/test6")).getLanguages(); 176 tx = getTransaction(); 177 tx.begin(); 178 lang.add("English"); 179 tx.commit(); 180 } 181 catch (RollbackException rollback) 182 { 183 ; 184 } 185 catch (Exception ex) 186 { 187 try 188 { 189 tx.rollback(); 190 } catch (SystemException e) 191 { 192 e.printStackTrace(); 193 t2_ex = e; 194 } 195 } 196 } 197 }; 198 199 Person p = createPerson("/person/test6", "p6", 50); 200 List <String > lang = new ArrayList <String >(); 201 lang.add("German"); 202 p.setLanguages(lang); 203 204 t1.start(); 205 t2.start(); 206 207 t1.join(); 208 t2.join(); 209 210 if (t2_ex != null) 212 fail("Thread1 failed: " + t2_ex); 213 if (t1_ex != null) 214 fail("Thread2 failed: " + t1_ex); 215 216 int size = ((Person) cache.find("/person/test6")).getLanguages().size(); 217 assertEquals("number of languages should be 2, but is " + size + " (" + 218 ((Person) cache.find("/person/test6")).getLanguages().size() + ")", 219 2, size); 220 size = ((Person) cache1.find("/person/test6")).getLanguages().size(); 221 assertEquals("number of languages should be 2, but is " + size + " (" + 222 ((Person) cache.find("/person/test6")).getLanguages().size() + ")", 223 2, size); 224 } 225 226 void log(String s) 227 { 228 long now; 229 if (start == 0) 230 start = System.currentTimeMillis(); 231 now = System.currentTimeMillis(); 232 233 System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s); 234 } 235 236 237 public static Test suite() throws Exception 238 { 239 return new TestSuite(ReplicatedTxTest.class); 240 } 241 242 243 public static void main(String [] args) throws Exception 244 { 245 junit.textui.TestRunner.run(suite()); 246 } 247 248 } 249 250 | Popular Tags |