KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > txtimer > test > TimerSerializationTestCase


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.txtimer.test;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.NotSerializableException JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29
30 import javax.ejb.Timer JavaDoc;
31 import javax.ejb.TimerHandle JavaDoc;
32 import javax.ejb.TimerService JavaDoc;
33
34 /**
35  * @author Thomas.Diesler@jboss.org
36  * @since 07-Apr-2004
37  */

38 public class TimerSerializationTestCase extends TimerTestBase
39 {
40    public TimerSerializationTestCase(String JavaDoc name)
41    {
42       super(name);
43    }
44
45    public void testTimerSerialization() throws Exception JavaDoc
46    {
47       TimedMockObject to = new TimedMockObject();
48       TimerService JavaDoc service = createTimerService(to);
49       Timer JavaDoc timer = service.createTimer(500, null);
50       timer.cancel();
51
52       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
53       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
54       try
55       {
56          oos.writeObject(timer);
57          fail("NotSerializableException expected");
58       }
59       catch (NotSerializableException JavaDoc expected)
60       {
61       }
62    }
63
64    public void testTimerHandleSerialization() throws Exception JavaDoc
65    {
66       TimedMockObject to = new TimedMockObject();
67       TimerService JavaDoc service = createTimerService(to);
68       Timer JavaDoc timer1 = service.createTimer(500, null);
69       TimerHandle JavaDoc handle1 = timer1.getHandle();
70
71       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
72       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
73       oos.writeObject(handle1);
74       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
75       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
76       TimerHandle JavaDoc handle2 = (TimerHandle JavaDoc)ois.readObject();
77
78       Timer JavaDoc timer2 = handle2.getTimer();
79       assertEquals("Timers are not equal", timer1, timer2);
80
81       sleep(1000);
82       assertTrue("TimedObject not called", 1 == to.getCallCount());
83       assertEquals("Expected no txtimer", 0, service.getTimers().size());
84    }
85 }
86
Popular Tags