KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.HashSet JavaDoc;
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 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.StopTime;
40 import org.omg.CosNotification.StructuredEvent;
41 import org.omg.TimeBase.UtcTHelper;
42
43 /**
44  * @author Alphonse Bendt
45  */

46
47 public class StopTimeTest extends NotificationTestCase
48 {
49     private StructuredEvent structuredEvent_;
50     private MessageFactory messageFactory_;
51
52     public StopTimeTest(String JavaDoc 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 JavaDoc
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 JavaDoc
86     {
87         structuredEvent_.header.variable_header = new Property[1];
88
89         final Date JavaDoc _time = new Date JavaDoc(System.currentTimeMillis() + offset);
90
91         Any JavaDoc _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 JavaDoc _received = new HashSet JavaDoc();
100
101         final Object JavaDoc lock = new Object JavaDoc();
102
103         // TODO check if MockTaskProcessor can be used here
104
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 JavaDoc
147     {
148         return NotificationTestCase.suite(StopTimeTest.class);
149     }
150 }
Popular Tags