1 22 package org.jboss.test.cmp2.ageout.test; 23 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import javax.sql.DataSource ; 29 import javax.transaction.Transaction ; 30 import javax.transaction.TransactionManager ; 31 import javax.management.MBeanServer ; 32 import javax.management.ObjectName ; 33 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil; 34 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Cache; 35 import org.jboss.test.JBossTestCase; 36 import org.jboss.test.cmp2.ejbselect.ALocal; 37 import org.jboss.test.cmp2.ejbselect.ALocalHome; 38 import org.jboss.test.cmp2.ejbselect.AUtil; 39 import org.jboss.mx.util.MBeanServerLocator; 40 import junit.framework.Test; 41 import net.sourceforge.junitejb.EJBTestCase; 42 43 47 public class JDBC2PmAgeOutUnitTestCase 48 extends EJBTestCase 49 { 50 private int maxAgeMs = 3000; 51 private int overagerPeriodMs = 1000; 52 private DataSource ds; 53 private TransactionManager tm; 54 private CacheListener cacheListener; 55 56 public JDBC2PmAgeOutUnitTestCase(String methodName) 57 { 58 super(methodName); 59 } 60 61 public static Test suite() throws Exception 62 { 63 return JBossTestCase.getDeploySetup(JDBC2PmAgeOutUnitTestCase.class, "cmp2-jdbc2pm-ageout.jar"); 64 } 65 66 public void setUpEJB() throws Exception 67 { 68 MBeanServer server = MBeanServerLocator.locateJBoss(); 69 cacheListener = new CacheListener(); 70 server.invoke(new ObjectName ("jboss.cmp:ejbname=A,service=tablecache,table=TEST_A"), 71 "registerListener", new Object []{ 72 cacheListener 73 }, new String []{Cache.Listener.class.getName()}); 74 } 75 76 public void testAgeOut() throws Exception 77 { 78 Transaction tx = suspendTx(); 79 String id = "a1"; 80 81 try 82 { 83 beginTx(); 84 ALocalHome ah = AUtil.getLocalHome(); 85 ALocal a = ah.create(id); 86 a.setIntField(1); 87 commitTx(); 88 long lastUpdated = System.currentTimeMillis(); 89 90 checkAge(lastUpdated); 91 assertValue(id, 1); 92 93 checkAge(lastUpdated); 94 jdbcUpdate(id, 2); 95 96 checkAge(lastUpdated); 97 assertValue(id, 1); 98 99 sleepUntilEvicted(); 100 long lastEvicted = cacheListener.lastEvicted; 101 assertValue(id, 2); 102 103 try 105 { 106 Thread.sleep(1000); 107 } 108 catch(InterruptedException e) 109 { 110 e.printStackTrace(); 111 } 112 113 beginTx(); 114 a = ah.findByPrimaryKey(id); 115 a.setIntField(3); 116 commitTx(); 117 118 sleepUntilEvicted(); 119 assertTrue(cacheListener.lastEvicted - lastEvicted >= maxAgeMs + 1000); 120 } 121 finally 122 { 123 resumeTx(tx); 124 } 125 } 126 127 private void sleepUntilEvicted() 128 { 129 int sleepTime = 0; 130 while(!cacheListener.evicted) 131 { 132 try 133 { 134 Thread.sleep(1000); 135 sleepTime += 1000; 136 } 137 catch(InterruptedException e) 138 { 139 e.printStackTrace(); 140 } 141 142 if(!cacheListener.evicted && sleepTime > maxAgeMs + overagerPeriodMs) 143 { 144 fail("The instance must have been evicted!"); 145 } 146 } 147 cacheListener.evicted = false; 148 } 149 150 private void checkAge(long lastUpdated) 151 throws Exception 152 { 153 if(System.currentTimeMillis() - lastUpdated > maxAgeMs) 154 { 155 throw new Exception ("maxAgeMs should be increased for this test to work."); 156 } 157 } 158 159 private void assertValue(String id, int value) throws Exception 160 { 161 beginTx(); 162 try 163 { 164 ALocalHome ah = AUtil.getLocalHome(); 165 ALocal a = ah.findByPrimaryKey(id); 166 assertEquals(value, a.getIntField()); 167 } 168 finally 169 { 170 commitTx(); 171 } 172 } 173 174 private void jdbcUpdate(String id, int value) throws Exception 175 { 176 DataSource ds = getDS(); 177 Connection con = null; 178 Statement st = null; 179 try 180 { 181 con = ds.getConnection(); 182 st = con.createStatement(); 183 int rows = st.executeUpdate("update TEST_A set INT_FIELD=" + 184 value + " where ID='" + id + "'" 185 ); 186 if(rows != 1) 187 { 188 throw new Exception ("Expected one updated row but got " + rows); 189 } 190 } 191 finally 192 { 193 JDBCUtil.safeClose(st); 194 JDBCUtil.safeClose(con); 195 } 196 } 197 198 private DataSource getDS() throws NamingException 199 { 200 if(ds == null) 201 { 202 ds = (DataSource )new InitialContext ().lookup("java:/DefaultDS"); 203 } 204 return ds; 205 } 206 207 private Transaction suspendTx() throws Exception 208 { 209 return getTM().suspend(); 210 } 211 212 private void resumeTx(Transaction tx) throws Exception 213 { 214 getTM().resume(tx); 215 } 216 217 private Transaction beginTx() throws Exception 218 { 219 getTM().begin(); 220 return getTM().getTransaction(); 221 } 222 223 private void commitTx() throws Exception 224 { 225 getTM().commit(); 226 } 227 228 private TransactionManager getTM() 229 throws NamingException 230 { 231 if(tm == null) 232 { 233 tm = (TransactionManager )new InitialContext ().lookup("java:/TransactionManager"); 234 } 235 return tm; 236 } 237 238 private static class CacheListener implements Cache.Listener 239 { 240 public long lastEvicted; 241 public boolean evicted; 242 243 public void contention(int partitionIndex, long time) 244 { 245 } 246 247 public void eviction(int partitionIndex, Object pk, int size) 248 { 249 lastEvicted = System.currentTimeMillis(); 250 evicted = true; 251 } 252 253 public void hit(int partitionIndex) 254 { 255 } 256 257 public void miss(int partitionIndex) 258 { 259 } 260 } 261 } 262 | Popular Tags |