1 package org.jacorb.test.notification; 2 3 23 24 import java.util.Date ; 25 26 import junit.framework.Test; 27 28 import org.jacorb.notification.engine.DefaultTaskProcessor; 29 import org.jacorb.notification.impl.DefaultMessageFactory; 30 import org.jacorb.notification.interfaces.FilterStage; 31 import org.jacorb.notification.interfaces.Message; 32 import org.jacorb.notification.servant.AbstractProxyConsumerI; 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.StartTime; 40 import org.omg.CosNotification.StructuredEvent; 41 import org.omg.TimeBase.UtcT; 42 import org.omg.TimeBase.UtcTHelper; 43 44 import EDU.oswego.cs.dl.util.concurrent.Latch; 45 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean; 46 47 50 51 public class StartTimeTest extends NotificationTestCase 52 { 53 DefaultMessageFactory messageFactory_; 54 55 StructuredEvent structuredEvent_; 56 57 AbstractProxyConsumerI proxyConsumerMock_ = new AbstractProxyConsumerI() 58 { 59 public boolean isStartTimeSupported() 60 { 61 return true; 62 } 63 64 public boolean isTimeOutSupported() 65 { 66 return true; 67 } 68 69 public FilterStage getFirstStage() 70 { 71 return null; 72 } 73 }; 74 75 77 public StartTimeTest(String name, NotificationTestCaseSetup setup) 78 { 79 super(name, setup); 80 } 81 82 public void setUpTest() throws Exception 83 { 84 messageFactory_ = new DefaultMessageFactory(getConfiguration()); 85 86 structuredEvent_ = new StructuredEvent(); 87 EventHeader _header = new EventHeader(); 88 FixedEventHeader _fixed = new FixedEventHeader(); 89 _fixed.event_name = "eventname"; 90 _fixed.event_type = new EventType("domain", "type"); 91 _header.fixed_header = _fixed; 92 _header.variable_header = new Property[0]; 93 94 structuredEvent_.header = _header; 95 96 structuredEvent_.filterable_data = new Property[0]; 97 98 structuredEvent_.remainder_of_body = getORB().create_any(); 99 } 100 101 public void tearDownTest() throws Exception 102 { 103 messageFactory_.dispose(); 104 } 105 106 public void testStructuredEventWithoutStartTimeProperty() throws Exception 107 { 108 Message _event = messageFactory_.newMessage(structuredEvent_); 109 110 assertTrue(!_event.hasStartTime()); 111 } 112 113 public void testAnyEventHasNoStartTime() throws Exception 114 { 115 Message _event = messageFactory_.newMessage(getORB().create_any()); 116 117 assertTrue(!_event.hasStartTime()); 118 } 119 120 public void testStructuredEventWithStartTimeProperty() throws Exception 121 { 122 structuredEvent_.header.variable_header = new Property[1]; 123 124 Date _now = new Date (); 125 126 Any _startTimeAny = getORB().create_any(); 127 UtcT _startTime = Time.corbaTime(_now); 128 UtcTHelper.insert(_startTimeAny, _startTime); 129 130 structuredEvent_.header.variable_header[0] = new Property(StartTime.value, _startTimeAny); 131 132 Message _event = messageFactory_.newMessage(structuredEvent_, proxyConsumerMock_); 133 134 assertTrue(_event.hasStartTime()); 135 assertEquals(_now.getTime(), _event.getStartTime()); 136 } 137 138 public void testProcessEventWithStartTime() throws Exception 139 { 140 processEventWithStartTime(0); 141 processEventWithStartTime(-1000); 142 processEventWithStartTime(-2000); 143 processEventWithStartTime(1000); 144 processEventWithStartTime(5000); 145 } 146 147 public void processEventWithStartTime(long offset) throws Exception 148 { 149 final SynchronizedBoolean failed = new SynchronizedBoolean(true); 150 151 structuredEvent_.header.variable_header = new Property[1]; 152 153 final Date _startTime = new Date (System.currentTimeMillis() + offset); 154 155 Any _startTimeAny = getORB().create_any(); 156 UtcTHelper.insert(_startTimeAny, Time.corbaTime(_startTime)); 157 158 structuredEvent_.header.variable_header[0] = new Property(StartTime.value, _startTimeAny); 159 160 final Message _event = messageFactory_.newMessage(structuredEvent_, proxyConsumerMock_); 161 162 final Latch _latch = new Latch(); 163 164 DefaultTaskProcessor _taskProcessor = new DefaultTaskProcessor(getConfiguration()) 166 { 167 public void processMessageInternal(Message event) 168 { 169 try 170 { 171 long _recvTime = System.currentTimeMillis(); 172 assertEquals(event, _event); 173 assertTrue(_recvTime >= _startTime.getTime()); 174 175 failed.set(false); 176 } finally 177 { 178 _latch.release(); 179 } 180 } 181 }; 182 183 _taskProcessor.processMessage(_event); 184 185 _latch.acquire(); 186 187 assertFalse(failed.get()); 188 189 _taskProcessor.dispose(); 190 } 191 192 public static Test suite() throws Exception 193 { 194 return NotificationTestCase.suite(StartTimeTest.class); 195 } 196 } | Popular Tags |