1 22 package org.jboss.test.txtimer.test; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.NotSerializableException ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 30 import javax.ejb.Timer ; 31 import javax.ejb.TimerHandle ; 32 import javax.ejb.TimerService ; 33 34 38 public class TimerSerializationTestCase extends TimerTestBase 39 { 40 public TimerSerializationTestCase(String name) 41 { 42 super(name); 43 } 44 45 public void testTimerSerialization() throws Exception 46 { 47 TimedMockObject to = new TimedMockObject(); 48 TimerService service = createTimerService(to); 49 Timer timer = service.createTimer(500, null); 50 timer.cancel(); 51 52 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 53 ObjectOutputStream oos = new ObjectOutputStream (baos); 54 try 55 { 56 oos.writeObject(timer); 57 fail("NotSerializableException expected"); 58 } 59 catch (NotSerializableException expected) 60 { 61 } 62 } 63 64 public void testTimerHandleSerialization() throws Exception 65 { 66 TimedMockObject to = new TimedMockObject(); 67 TimerService service = createTimerService(to); 68 Timer timer1 = service.createTimer(500, null); 69 TimerHandle handle1 = timer1.getHandle(); 70 71 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 72 ObjectOutputStream oos = new ObjectOutputStream (baos); 73 oos.writeObject(handle1); 74 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 75 ObjectInputStream ois = new ObjectInputStream (bais); 76 TimerHandle handle2 = (TimerHandle )ois.readObject(); 77 78 Timer 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 |