KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > ageout > test > JDBC2PmAgeOutUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.ageout.test;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.sql.DataSource JavaDoc;
29 import javax.transaction.Transaction JavaDoc;
30 import javax.transaction.TransactionManager JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.ObjectName JavaDoc;
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 /**
44  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
45  * @version <tt>$Revision: 37406 $</tt>
46  */

47 public class JDBC2PmAgeOutUnitTestCase
48    extends EJBTestCase
49 {
50    private int maxAgeMs = 3000;
51    private int overagerPeriodMs = 1000;
52    private DataSource JavaDoc ds;
53    private TransactionManager JavaDoc tm;
54    private CacheListener cacheListener;
55
56    public JDBC2PmAgeOutUnitTestCase(String JavaDoc methodName)
57    {
58       super(methodName);
59    }
60
61    public static Test suite() throws Exception JavaDoc
62    {
63       return JBossTestCase.getDeploySetup(JDBC2PmAgeOutUnitTestCase.class, "cmp2-jdbc2pm-ageout.jar");
64    }
65
66    public void setUpEJB() throws Exception JavaDoc
67    {
68       MBeanServer JavaDoc server = MBeanServerLocator.locateJBoss();
69       cacheListener = new CacheListener();
70       server.invoke(new ObjectName JavaDoc("jboss.cmp:ejbname=A,service=tablecache,table=TEST_A"),
71          "registerListener", new Object JavaDoc[]{
72             cacheListener
73          }, new String JavaDoc[]{Cache.Listener.class.getName()});
74    }
75
76    public void testAgeOut() throws Exception JavaDoc
77    {
78       Transaction JavaDoc tx = suspendTx();
79       String JavaDoc 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          // test ejb update
104
try
105          {
106             Thread.sleep(1000);
107          }
108          catch(InterruptedException JavaDoc 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 JavaDoc 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 JavaDoc
152    {
153       if(System.currentTimeMillis() - lastUpdated > maxAgeMs)
154       {
155          throw new Exception JavaDoc("maxAgeMs should be increased for this test to work.");
156       }
157    }
158
159    private void assertValue(String JavaDoc id, int value) throws Exception JavaDoc
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 JavaDoc id, int value) throws Exception JavaDoc
175    {
176       DataSource JavaDoc ds = getDS();
177       Connection JavaDoc con = null;
178       Statement JavaDoc 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 JavaDoc("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 JavaDoc getDS() throws NamingException JavaDoc
199    {
200       if(ds == null)
201       {
202          ds = (DataSource JavaDoc)new InitialContext JavaDoc().lookup("java:/DefaultDS");
203       }
204       return ds;
205    }
206
207    private Transaction JavaDoc suspendTx() throws Exception JavaDoc
208    {
209       return getTM().suspend();
210    }
211
212    private void resumeTx(Transaction JavaDoc tx) throws Exception JavaDoc
213    {
214       getTM().resume(tx);
215    }
216
217    private Transaction JavaDoc beginTx() throws Exception JavaDoc
218    {
219       getTM().begin();
220       return getTM().getTransaction();
221    }
222
223    private void commitTx() throws Exception JavaDoc
224    {
225       getTM().commit();
226    }
227
228    private TransactionManager JavaDoc getTM()
229       throws NamingException JavaDoc
230    {
231       if(tm == null)
232       {
233          tm = (TransactionManager JavaDoc)new InitialContext JavaDoc().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 JavaDoc 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