KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > sandesha > storage > TCClientStorageManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.sandesha.storage;
18
19 import junit.framework.TestCase;
20 import org.apache.sandesha.Constants;
21 import org.apache.sandesha.RMMessageContext;
22 import org.apache.sandesha.client.ClientStorageManager;
23 import org.apache.sandesha.storage.queue.QueueException;
24 import org.apache.sandesha.storage.queue.SandeshaQueue;
25
26 /**
27  * @author Chamikara Jayalath
28  * @author Jaliya Ekanayaka
29  */

30 public class TCClientStorageManager extends TestCase {
31
32     //For testing weather messages are re-transmitted correctly
33
public void testRetransmission() {
34
35         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.CLIENT);
36         ClientStorageManager csm = new ClientStorageManager();
37         RMMessageContext msg1;
38         
39         //approving the out sequence
40
csm.setTemporaryOutSequence("seqid1", "uuid:aaaa-bbbb-cccc");
41         csm.setApprovedOutSequence("uuid:aaaa-bbbb-cccc", "approved1");
42
43         //messages should be returned (since the out sequence is approved)
44
msg1 = csm.getNextMessageToSend();
45         assertNotNull(msg1);
46         msg1.setLocked(false);
47         msg1 = csm.getNextMessageToSend();
48         assertNotNull(msg1);
49         msg1.setLocked(false);
50         msg1 = csm.getNextMessageToSend();
51         assertNotNull(msg1);
52         msg1.setLocked(false);
53         msg1 = csm.getNextMessageToSend();
54         assertNotNull(msg1);
55         msg1.setLocked(false);
56         msg1 = csm.getNextMessageToSend();
57         assertNull(msg1);
58
59         //Waiting for little more than re-transmission interval
60
try {
61             Thread.sleep(Constants.RETRANSMISSION_INTERVAL + 100);
62         } catch (InterruptedException JavaDoc e) {
63             e.printStackTrace();
64         }
65
66
67         msg1 = csm.getNextMessageToSend();
68         assertNotNull(msg1);
69         msg1.setLocked(false);
70         msg1 = csm.getNextMessageToSend();
71         assertNotNull(msg1);
72         msg1.setLocked(false);
73         msg1 = csm.getNextMessageToSend();
74         assertNotNull(msg1);
75         msg1.setLocked(false);
76         msg1 = csm.getNextMessageToSend();
77         assertNotNull(msg1);
78         msg1.setLocked(false);
79         msg1 = csm.getNextMessageToSend();
80         assertNull(msg1);
81
82         //Again waiting for little more than re-transmission interval :)
83
try {
84             Thread.sleep(Constants.RETRANSMISSION_INTERVAL + 100);
85         } catch (InterruptedException JavaDoc e) {
86             e.printStackTrace();
87         }
88
89         //Messages should be returned once again
90
msg1 = csm.getNextMessageToSend();
91         assertNotNull(msg1);
92         msg1.setLocked(false);
93         msg1 = csm.getNextMessageToSend();
94         assertNotNull(msg1);
95         msg1.setLocked(false);
96         msg1 = csm.getNextMessageToSend();
97         assertNotNull(msg1);
98         msg1.setLocked(false);
99         msg1 = csm.getNextMessageToSend();
100         assertNotNull(msg1);
101         msg1.setLocked(false);
102         msg1 = csm.getNextMessageToSend();
103         assertNull(msg1);
104
105     }
106
107     //Testing weather the tr-transmission stops after a acknowledgement
108
public void testAcknowledgement() {
109         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.CLIENT);
110         ClientStorageManager csm = new ClientStorageManager();
111
112         csm.setTemporaryOutSequence("seqid1", "uuid:aaaa-bbbb-cccc");
113         csm.setApprovedOutSequence("uuid:aaaa-bbbb-cccc", "approved1");
114
115         RMMessageContext msg1;
116         msg1 = csm.getNextMessageToSend();
117         assertNotNull(msg1);
118         msg1.setLocked(false);
119         msg1 = csm.getNextMessageToSend();
120         assertNotNull(msg1);
121         msg1.setLocked(false);
122         msg1 = csm.getNextMessageToSend();
123         assertNotNull(msg1);
124         msg1.setLocked(false);
125         msg1 = csm.getNextMessageToSend();
126         assertNotNull(msg1);
127         msg1.setLocked(false);
128         msg1 = csm.getNextMessageToSend();
129         assertNull(msg1);
130         
131         //Acknowledging messages 1,2 and 4 (without 3)
132
csm.setAcknowledged("approved1", 1);
133         csm.setAcknowledged("approved1", 2);
134         csm.setAcknowledged("approved1", 4);
135         
136         //Waiting for little more than re-transmission interval
137
try {
138             Thread.sleep(Constants.RETRANSMISSION_INTERVAL + 100);
139         } catch (InterruptedException JavaDoc e) {
140             e.printStackTrace();
141         }
142
143         //Only message no. 3 should be re-transmitted
144
msg1 = csm.getNextMessageToSend();
145         assertNotNull(msg1);
146         assertEquals(msg1.getMessageID(), "rmsg3");
147         msg1 = csm.getNextMessageToSend();
148         assertNull(msg1);
149     }
150     
151     /*
152     //For testing weather getNextMsgToSend method works correctly
153     public void testNextMsgToSend(){
154         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.CLIENT);
155         ClientStorageManager csm = new ClientStorageManager();
156         RMMessageContext msg1;
157         
158         //Next message to sent should be null (before approving outsequence)
159         msg1 = csm.getNextMessageToSend();
160         assertNull(msg1);
161         msg1 = csm.getNextMessageToSend();
162         assertNull(msg1);
163         
164         //approving the out sequence
165         csm.setTemporaryOutSequence("seqid1","uuid:aaaa-bbbb-cccc");
166         csm.setApprovedOutSequence("uuid:aaaa-bbbb-cccc", "approved1");
167
168         //messages should be returned (since the out sequence is approved)
169         msg1 = csm.getNextMessageToSend();
170         assertNotNull(msg1);
171         msg1 = csm.getNextMessageToSend();
172         assertNotNull(msg1);
173         msg1 = csm.getNextMessageToSend();
174         assertNotNull(msg1);
175         msg1 = csm.getNextMessageToSend();
176         assertNotNull(msg1);
177         msg1 = csm.getNextMessageToSend();
178         assertNull(msg1);
179     }
180     
181     //Testing weather the out-sequence concept works correctly.
182     //Outgoing messages should be sent only when the out sequence is approved.
183     public void testOutSequence(){
184         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.CLIENT);
185         ClientStorageManager csm = new ClientStorageManager();
186         
187         //setting temporary out sequence
188         csm.setTemporaryOutSequence("seqid1","uuid:aaaa-bbbb-cccc");
189         
190         RMMessageContext msg1;
191         
192         //the message should be null since the out sequence has not been approved
193         msg1 = csm.getNextMessageToSend();
194         assertNull(msg1);
195         
196         //still the message should be null since the out sequence has not been approved
197         msg1 = csm.getNextMessageToSend();
198         assertNull(msg1);
199         
200         //approving the out sequence
201         csm.setApprovedOutSequence("uuid:aaaa-bbbb-cccc", "approved1");
202
203         //not the message should not be null (since out sequence was approved)
204         msg1 = csm.getNextMessageToSend();
205         assertNotNull(msg1);
206     }
207     
208     //Testing weather priority messages are sent correctly.
209     //They should be sent before application messages and should be sent
210     //even before the out-sequence get set.
211     public void testPriorityQueue(){
212         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.CLIENT);
213         ClientStorageManager csm = new ClientStorageManager();
214         
215         //Addign a create sequence request message (this will be added to the priority
216         //area of the storage)
217         RMMessageContext createSeqReq1 = new RMMessageContext();
218         createSeqReq1.setMessageType(Constants.MSG_TYPE_CREATE_SEQUENCE_REQUEST);
219         createSeqReq1.setMessageID("temp1");
220         
221         csm.addCreateSequenceRequest(createSeqReq1);
222         
223         RMMessageContext msg1;
224         ClientStorageManager csm1 = new ClientStorageManager();
225
226         //create sequence message should be returned
227         msg1 = csm1.getNextMessageToSend();
228         assertNotNull(msg1);
229         
230         //other messages should not be returned since the out sequence has not been approved.
231         msg1 = csm1.getNextMessageToSend();
232         assertNull(msg1);
233
234         //Addign a acknowledgement message (this will be added to the priority
235         //area of the storage)
236         RMMessageContext ack1 = new RMMessageContext();
237         ack1.setMessageID("msgack1");
238         csm.addAcknowledgement(ack1);
239
240         //ack message should be returned
241         msg1 = csm1.getNextMessageToSend();
242         assertNotNull(msg1);
243         
244         //other messages should not be returned since the out sequence has not been approved.
245         msg1 = csm1.getNextMessageToSend();
246         assertNull(msg1);
247     }*/

248     
249     
250     public void setUp() throws QueueException {
251
252         SandeshaQueue sq = SandeshaQueue.getInstance(Constants.CLIENT);
253         ClientStorageManager csm = new ClientStorageManager();
254         RMMessageContext msg = new RMMessageContext();
255         
256         //Creating a new outgoing sequence.
257
sq.createNewOutgoingSequence("seqid1");
258         
259         //Adding messages to the outgoing sequence.
260

261         //Adding message 1
262
long nextMsgNo = csm.getNextMessageNumber("seqid1");
263         assertEquals(nextMsgNo, 1);
264         msg.setMessageID("rmsg1");
265         msg.setSequenceID("seqid1");
266         msg.setMsgNumber(nextMsgNo);
267         sq.addMessageToOutgoingSequence("seqid1", msg);
268
269         //Adding message 2
270
nextMsgNo = csm.getNextMessageNumber("seqid1");
271         assertEquals(nextMsgNo, 2);
272         msg = new RMMessageContext();
273         msg.setMessageID("rmsg2");
274         msg.setSequenceID("seqid1");
275         msg.setMsgNumber(nextMsgNo);
276         sq.addMessageToOutgoingSequence("seqid1", msg);
277
278         //Adding message 3
279
nextMsgNo = csm.getNextMessageNumber("seqid1");
280         assertEquals(nextMsgNo, 3);
281         msg = new RMMessageContext();
282         msg.setMessageID("rmsg3");
283         msg.setSequenceID("seqid1");
284         msg.setMsgNumber(nextMsgNo);
285
286         //Adding message 4
287
sq.addMessageToOutgoingSequence("seqid1", msg);
288         nextMsgNo = csm.getNextMessageNumber("seqid1");
289         assertEquals(nextMsgNo, 4);
290         msg = new RMMessageContext();
291         msg.setMessageID("rmsg4");
292         msg.setSequenceID("seqid1");
293         msg.setMsgNumber(nextMsgNo);
294         sq.addMessageToOutgoingSequence("seqid1", msg);
295     }
296
297     public void tearDown() {
298
299         //clearing the storage
300
ClientStorageManager csm = new ClientStorageManager();
301         csm.clearStorage();
302
303     }
304
305
306 }
Popular Tags