KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > StartTimeTest


1 package org.jacorb.test.notification;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2003 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import java.util.Date JavaDoc;
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 JavaDoc;
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 /**
48  * @author Alphonse Bendt
49  */

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     ////////////////////////////////////////
76

77     public StartTimeTest(String JavaDoc name, NotificationTestCaseSetup setup)
78     {
79         super(name, setup);
80     }
81
82     public void setUpTest() throws Exception JavaDoc
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 JavaDoc
102     {
103         messageFactory_.dispose();
104     }
105
106     public void testStructuredEventWithoutStartTimeProperty() throws Exception JavaDoc
107     {
108         Message _event = messageFactory_.newMessage(structuredEvent_);
109
110         assertTrue(!_event.hasStartTime());
111     }
112
113     public void testAnyEventHasNoStartTime() throws Exception JavaDoc
114     {
115         Message _event = messageFactory_.newMessage(getORB().create_any());
116
117         assertTrue(!_event.hasStartTime());
118     }
119
120     public void testStructuredEventWithStartTimeProperty() throws Exception JavaDoc
121     {
122         structuredEvent_.header.variable_header = new Property[1];
123
124         Date JavaDoc _now = new Date JavaDoc();
125
126         Any JavaDoc _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 JavaDoc
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 JavaDoc
148     {
149         final SynchronizedBoolean failed = new SynchronizedBoolean(true);
150         
151         structuredEvent_.header.variable_header = new Property[1];
152
153         final Date JavaDoc _startTime = new Date JavaDoc(System.currentTimeMillis() + offset);
154
155         Any JavaDoc _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         // TODO check if MockTaskProcessor can be used here
165
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 JavaDoc
193     {
194         return NotificationTestCase.suite(StartTimeTest.class);
195     }
196 }
Popular Tags