1 22 package org.jboss.test.cache.bean; 23 24 import junit.framework.Test; 25 import org.jboss.test.JBossTestCase; 26 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 import javax.rmi.PortableRemoteObject ; 30 import javax.transaction.UserTransaction ; 31 import java.util.Properties ; 32 33 38 public class MBeanUnitTestCase extends JBossTestCase 39 { 40 TreeCacheMBeanTesterHome cache_home; 41 TreeCacheMBeanTester cache1 = null, cache2 = null; 42 Properties p_ = new Properties (); 43 44 45 public MBeanUnitTestCase(String name) 46 { 47 super(name); 48 } 49 50 public void setUp() throws Exception 51 { 52 super.setUp(); 53 mySetup(); 54 } 55 56 public void tearDown() throws Exception 57 { 58 super.tearDown(); 59 if (cache2 != null) 60 cache2.remove(); if (cache1 != null) 62 cache1.remove(); 63 } 64 65 public void mySetup() throws Exception 66 { 67 Object obj; 68 69 p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 70 p_.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces"); 71 p_.put(Context.PROVIDER_URL, "localhost:1099"); 72 obj = new InitialContext (p_).lookup(TreeCacheMBeanTesterHome.JNDI_NAME); 73 cache_home = (TreeCacheMBeanTesterHome) PortableRemoteObject.narrow(obj, TreeCacheMBeanTesterHome.class); 74 } 75 76 77 public void testSetup() 78 { 79 assertNotNull("TreeCacheMBeanTesterHome ", cache_home); 80 } 81 82 public void testPutTx() 83 { 84 UserTransaction tx = null; 85 86 try { 87 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 88 assertNotNull("UserTransaction should not be null ", tx); 89 cache1 = cache_home.create(); 92 93 tx.begin(); 94 cache1.put("/a/b/c", "age", new Integer (38)); 95 assertEquals(new Integer (38), cache1.get("/a/b/c", "age")); 96 97 cache1.put("/a/b/c", "age", new Integer (39)); 98 tx.commit(); 99 100 tx.begin(); 101 assertEquals(new Integer (39), cache1.get("/a/b/c", "age")); 102 tx.commit(); 103 104 tx.begin(); 106 cache1.remove("/a/b/c"); 107 cache1.remove("/a/b"); 108 cache1.remove("/a"); 109 tx.commit(); 110 111 } catch (Throwable t) { 112 fail(t.toString()); 113 try { 114 tx.rollback(); 115 } catch (Throwable t2) { 116 ; 117 } 118 fail(t.toString()); 119 } 120 } 121 122 123 public void testRollbackTx() 124 { 125 UserTransaction tx = null; 126 127 try { 128 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 129 assertNotNull("UserTransaction should not be null ", tx); 132 cache1 = cache_home.create(); 133 135 tx.begin(); 136 cache1.put("/a/b/c", "age", new Integer (38)); 137 cache1.put("/a/b/c", "age", new Integer (39)); 138 tx.rollback(); 139 140 tx.begin(); 141 Integer val = (Integer ) cache1.get("/a/b/c", "age"); 142 tx.commit(); 143 assertNull(val); 144 } catch (Throwable t) { 145 fail(t.toString()); 147 try { 148 tx.rollback(); 149 } catch (Throwable t2) { 150 ; 151 } 152 fail(t.toString()); 153 } 154 } 155 156 public void testReplicatedPutTx() 157 { 158 UserTransaction tx = null; 159 160 try { 161 tx = (UserTransaction ) new InitialContext (p_).lookup("UserTransaction"); 162 assertNotNull("UserTransaction should not be null ", tx); 163 cache1 = cache_home.create(); 166 cache2 = cache_home.create(); 167 168 tx.begin(); 169 cache1.put("/a/b/c", "age", new Integer (38)); 170 assertEquals(new Integer (38), cache1.get("/a/b/c", "age")); 171 172 cache1.put("/a/b/c", "age", new Integer (39)); 173 tx.commit(); 174 175 tx.begin(); 176 assertEquals(new Integer (39), cache2.get("/a/b/c", "age")); 177 tx.commit(); 178 179 tx.begin(); 181 cache1.remove("/a/b/c"); 182 cache1.remove("/a/b"); 183 cache1.remove("/a"); 184 tx.commit(); 185 186 } catch (Throwable t) { 187 fail(t.toString()); 188 try { 189 tx.rollback(); 190 } catch (Throwable t2) { 191 ; 192 } 193 fail(t.toString()); 194 } 195 } 196 197 void log(String msg) 198 { 199 getLog().info("-- [" + Thread.currentThread() + "]: " + msg); 200 } 201 202 203 public static Test suite() throws Exception 204 { 205 return getDeploySetup(getDeploySetup(MBeanUnitTestCase.class, "cachetest.jar"), 209 "cachetest.sar"); 210 } 212 213 public static void main(String [] args) throws Exception 214 { 215 junit.textui.TestRunner.run(suite()); 216 } 217 218 219 } 220 | Popular Tags |