KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > hibernate > test > TimersUnitTestCase


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.hibernate.test;
23
24 import org.jboss.test.JBossTestCase;
25 import org.jboss.test.hibernate.timers.interfaces.ITimersHome;
26 import org.jboss.test.hibernate.timers.interfaces.ITimers;
27 import org.jboss.test.hibernate.timers.interfaces.Key;
28 import org.jboss.test.hibernate.timers.interfaces.Info;
29 import org.jboss.test.hibernate.timers.Timers;
30 import org.jboss.test.hibernate.timers.TimersID;
31
32 import java.util.List JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.io.ByteArrayOutputStream JavaDoc;
36 import java.io.ObjectOutputStream JavaDoc;
37 import java.io.Serializable JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.ByteArrayInputStream JavaDoc;
40 import java.io.ObjectInputStream JavaDoc;
41
42 import javax.naming.InitialContext JavaDoc;
43
44 import junit.framework.Test;
45
46 /**
47  Test of the ejb timers table mapping
48
49  @author Scott.Stark@jboss.org
50  @version $Revision: 58115 $
51  */

52 public class TimersUnitTestCase extends JBossTestCase
53 {
54    public TimersUnitTestCase(String JavaDoc name) throws Exception JavaDoc
55    {
56       super(name);
57    }
58
59    /**
60     Setup the test suite.
61     */

62    public static Test suite() throws Exception JavaDoc
63    {
64       return getDeploySetup(TimersUnitTestCase.class, "hib-timers.jar");
65    }
66
67    public void testCurrentSession() throws Throwable JavaDoc
68    {
69       InitialContext JavaDoc ctx = super.getInitialContext();
70       ITimersHome home = (ITimersHome) ctx.lookup("hib-timers/ITimersHome");
71       ITimers bean = null;
72
73       try
74       {
75          bean = home.create();
76
77          int initialCount = bean.listTimers().size();
78
79          TimersID id = new TimersID("testCurrentSession", "*:ejb=None");
80          Timers timer = new Timers(id);
81          Date JavaDoc now = new Date JavaDoc();
82          Long JavaDoc interval = new Long JavaDoc(5*1000);
83          Key key = new Key("key2", 123456789);
84          Info info = new Info(System.getProperties());
85          timer.setInitialDate(now);
86          timer.setInstancePK(serialize(key));
87          timer.setTimerInterval(interval);
88          timer.setInfo(serialize(info));
89
90          bean.persist(timer);
91          log.info("Timers created with id = " + id);
92
93          List JavaDoc timers = bean.listTimers();
94          assertNotNull(timers);
95          assertEquals("Incorrect result size", initialCount + 1, timers.size());
96
97          Timers found = null;
98          Iterator JavaDoc itr = timers.iterator();
99          while (itr.hasNext())
100          {
101             Timers t = (Timers) itr.next();
102             if (id.equals(t.getId()))
103             {
104                found = t;
105             }
106          }
107          assertNotNull("Saved timer found in list", found);
108          Date JavaDoc d = found.getInitialDate();
109          long t0 = (now.getTime() / 1000) * 1000;
110          long t1 = (d.getTime() / 1000) * 1000;
111          assertTrue("Timer.InitialDate("+t1+") == now("+t0+")", t0 == t1);
112          assertTrue("Timer.Id == id", found.getId().equals(id));
113          assertTrue("Timer.TimerInterval == interval", found.getTimerInterval().equals(interval));
114          Object JavaDoc tmp = deserialize(found.getInstancePK());
115          assertTrue("Timer.InstancePK == key", tmp.equals(key));
116          tmp = deserialize(found.getInfo());
117          log.info("Info: "+tmp);
118          assertTrue("Timer.Info == info", tmp.equals(info));
119       }
120       finally
121       {
122          if (bean != null)
123          {
124             try
125             {
126                bean.remove();
127             }
128             catch (Throwable JavaDoc t)
129             {
130                // ignore
131
}
132          }
133       }
134    }
135
136 }
Popular Tags