1 package org.jacorb.test.notification; 2 3 23 24 import java.util.Date ; 25 import java.util.HashSet ; 26 27 import junit.framework.Test; 28 29 import org.jacorb.notification.MessageFactory; 30 import org.jacorb.notification.engine.DefaultTaskProcessor; 31 import org.jacorb.notification.impl.DefaultMessageFactory; 32 import org.jacorb.notification.interfaces.Message; 33 import org.jacorb.util.Time; 34 import org.omg.CORBA.Any ; 35 import org.omg.CosNotification.EventHeader; 36 import org.omg.CosNotification.EventType; 37 import org.omg.CosNotification.FixedEventHeader; 38 import org.omg.CosNotification.Property; 39 import org.omg.CosNotification.StopTime; 40 import org.omg.CosNotification.StructuredEvent; 41 import org.omg.TimeBase.UtcTHelper; 42 43 46 47 public class StopTimeTest extends NotificationTestCase 48 { 49 private StructuredEvent structuredEvent_; 50 private MessageFactory messageFactory_; 51 52 public StopTimeTest(String name, NotificationTestCaseSetup setup) 53 { 54 super(name, setup); 55 } 56 57 public void setUpTest() 58 { 59 messageFactory_ = new DefaultMessageFactory(getConfiguration()); 60 61 structuredEvent_ = new StructuredEvent(); 62 EventHeader _header = new EventHeader(); 63 FixedEventHeader _fixed = new FixedEventHeader(); 64 _fixed.event_name = "eventname"; 65 _fixed.event_type = new EventType("domain", "type"); 66 _header.fixed_header = _fixed; 67 _header.variable_header = new Property[0]; 68 69 structuredEvent_.header = _header; 70 71 structuredEvent_.filterable_data = new Property[0]; 72 73 structuredEvent_.remainder_of_body = getClientORB().create_any(); 74 } 75 76 public void testProcessEventWithStopTime() throws Exception 77 { 78 processEventWithStopTime(-10000, 5000, false); 79 processEventWithStopTime(-2000, 5000, false); 80 processEventWithStopTime(1000, 5000, true); 81 processEventWithStopTime(5000, 10000, true); 82 } 83 84 public void processEventWithStopTime(long offset, long timeout, boolean receive) 85 throws Exception 86 { 87 structuredEvent_.header.variable_header = new Property[1]; 88 89 final Date _time = new Date (System.currentTimeMillis() + offset); 90 91 Any _any = getORB().create_any(); 92 93 UtcTHelper.insert(_any, Time.corbaTime(_time)); 94 95 structuredEvent_.header.variable_header[0] = new Property(StopTime.value, _any); 96 97 final Message _event = messageFactory_.newMessage(structuredEvent_); 98 99 final HashSet _received = new HashSet (); 100 101 final Object lock = new Object (); 102 103 DefaultTaskProcessor _taskProcessor = new DefaultTaskProcessor(getConfiguration()) 105 { 106 public void processMessageInternal(Message event) 107 { 108 try 109 { 110 long _recvTime = System.currentTimeMillis(); 111 assertEquals("unexpected event", event, _event); 112 assertTrue("received too late", _recvTime <= _time.getTime()); 113 _received.add(event); 114 } finally 115 { 116 synchronized (lock) 117 { 118 lock.notifyAll(); 119 } 120 } 121 } 122 }; 123 124 _taskProcessor.processMessage(_event); 125 126 if (!_received.contains(_event)) 127 { 128 synchronized (lock) 129 { 130 lock.wait(timeout); 131 } 132 } 133 134 if (receive) 135 { 136 assertTrue("should have received something", _received.contains(_event)); 137 } 138 else 139 { 140 assertTrue("shouldn't", !_received.contains(_event)); 141 } 142 143 _taskProcessor.dispose(); 144 } 145 146 public static Test suite() throws Exception 147 { 148 return NotificationTestCase.suite(StopTimeTest.class); 149 } 150 } | Popular Tags |