KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > messaging > MessageExchangeImplTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: MessageExchangeImplTest.java 10:58:30 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.messaging;
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.jbi.messaging.ExchangeStatus;
32 import javax.jbi.messaging.MessagingException;
33 import javax.jbi.messaging.NormalizedMessage;
34 import javax.jbi.messaging.MessageExchange.Role;
35 import javax.xml.namespace.QName JavaDoc;
36
37 import junit.framework.TestCase;
38
39 import org.easymock.classextension.EasyMock;
40
41 /**
42  * Test of the MessageExchangeImpl
43  *
44  * @author ddesjardins - eBMWebsourcing
45  */

46 public class MessageExchangeImplTest extends TestCase {
47
48     private MessageExchangeImpl messageExchangeImpl;
49
50     public void setUp() throws MessagingException {
51         messageExchangeImpl = new MessageExchangeImpl(null);
52         messageExchangeImpl.setPattern(MessageExchangeImpl.IN_OUT_PATTERN);
53         messageExchangeImpl.setRole(Role.PROVIDER);
54         NormalizedMessageImpl normalizedMessageImpl = new NormalizedMessageImpl();
55         messageExchangeImpl.setMessage(normalizedMessageImpl,
56             MessageExchangeImpl.OUT_MSG);
57     }
58
59     public void testSetFault() throws MessagingException {
60         FaultImpl faultImpl = new FaultImpl();
61         messageExchangeImpl.setFault(faultImpl);
62         FaultImpl faultImpl2 = (FaultImpl) messageExchangeImpl.getFault();
63         assertEquals(faultImpl.getProperty("test"), faultImpl2
64             .getProperty("test"));
65     }
66
67     public void testSetStatus() throws MessagingException {
68         messageExchangeImpl.setStatus(ExchangeStatus.ACTIVE);
69         assertEquals(messageExchangeImpl.getStatus(), ExchangeStatus.ACTIVE);
70     }
71
72     public void testSetStatus1() throws MessagingException {
73         messageExchangeImpl.setRole(Role.CONSUMER);
74         messageExchangeImpl.setStatus(ExchangeStatus.DONE);
75         assertEquals(messageExchangeImpl.getStatus(), ExchangeStatus.DONE);
76     }
77
78     public void testSetError() throws MessagingException {
79         messageExchangeImpl.setError(new RuntimeException JavaDoc("test"));
80         assertEquals(messageExchangeImpl.getStatus(), ExchangeStatus.ERROR);
81         assertEquals(messageExchangeImpl.getError().getMessage(), "test");
82     }
83
84     public void testSetProperty() {
85         messageExchangeImpl.setProperty("test", "value");
86         assertEquals(messageExchangeImpl.getProperty("test"), "value");
87         assertEquals(messageExchangeImpl.getPropertyNames().iterator().next(),
88             "test");
89     }
90
91     public void testGetMessage() {
92         assertNull(messageExchangeImpl.getMessage(null));
93     }
94
95     public void testSetOperation() {
96         messageExchangeImpl.setOperation(new QName JavaDoc("operation"));
97         assertEquals(messageExchangeImpl.getOperation(), new QName JavaDoc("operation"));
98     }
99
100     public void testSetExchangeId() {
101         messageExchangeImpl.setExchangeId("test01");
102         assertEquals(messageExchangeImpl.getExchangeId(), "test01");
103     }
104
105     public void testSetTransacted() {
106         messageExchangeImpl.setTransacted(true);
107         assertTrue(messageExchangeImpl.isTransacted());
108     }
109
110     public void testSetTerminated() {
111         messageExchangeImpl.setTerminated(true);
112         assertTrue(messageExchangeImpl.isTerminated());
113     }
114
115     public void testGetMessages() {
116         Map JavaDoc<String JavaDoc, NormalizedMessage> map = new HashMap JavaDoc<String JavaDoc, NormalizedMessage>();
117         messageExchangeImpl.messages = map;
118         assertEquals(messageExchangeImpl.getMessages(), map);
119     }
120
121     public void testSetFaultExceptionRoleNull() {
122         FaultImpl faultImpl = EasyMock.createMock(FaultImpl.class);
123         messageExchangeImpl.role = null;
124         try {
125             messageExchangeImpl.setFault(faultImpl);
126             fail();
127         } catch (MessagingException e) {
128             // do nothing
129
}
130     }
131
132     public void testSetFaultExceptionFaultNotNull() {
133         FaultImpl faultImpl = EasyMock.createMock(FaultImpl.class);
134         messageExchangeImpl.role = Role.CONSUMER;
135         messageExchangeImpl.fault = faultImpl;
136         try {
137             messageExchangeImpl.setFault(faultImpl);
138             fail();
139         } catch (MessagingException e) {
140             // do nothing
141
}
142     }
143
144     public void testSetFaultExceptionStatusDone() {
145         FaultImpl faultImpl = EasyMock.createMock(FaultImpl.class);
146         messageExchangeImpl.role = Role.CONSUMER;
147         messageExchangeImpl.fault = null;
148         messageExchangeImpl.status = ExchangeStatus.DONE;
149         try {
150             messageExchangeImpl.setFault(faultImpl);
151             fail();
152         } catch (MessagingException e) {
153             // do nothing
154
}
155     }
156
157     public void testSetFaultExceptionBadRole() {
158         FaultImpl faultImpl = EasyMock.createMock(FaultImpl.class);
159         messageExchangeImpl.role = Role.CONSUMER;
160         messageExchangeImpl.fault = null;
161         messageExchangeImpl.status = ExchangeStatus.ACTIVE;
162         messageExchangeImpl.messages.clear();
163         try {
164             messageExchangeImpl.setFault(faultImpl);
165             fail();
166         } catch (MessagingException e) {
167             // do nothing
168
}
169     }
170
171     public void testSetMessageExceptionMessageNull() {
172         try {
173             messageExchangeImpl.setMessage(null, "test");
174             fail();
175         } catch (MessagingException e) {
176             // do nothing
177
}
178     }
179
180     public void testSetMessageExceptionNameNull() {
181         NormalizedMessage normalizedMessage = EasyMock
182             .createMock(NormalizedMessage.class);
183         messageExchangeImpl.messages.put("out", normalizedMessage);
184         try {
185             messageExchangeImpl.setMessage(normalizedMessage, "out");
186             fail();
187         } catch (MessagingException e) {
188             // do nothing
189
}
190     }
191
192     public void testSetStatusException1() {
193         messageExchangeImpl.role = Role.CONSUMER;
194         messageExchangeImpl.pattern = MessageExchangeImpl.IN_ONLY_PATTERN;
195         try {
196             messageExchangeImpl.setStatus(ExchangeStatus.DONE);
197             fail();
198         } catch (MessagingException e) {
199             // do nothing
200
}
201     }
202
203     public void testSetStatusException2() {
204         messageExchangeImpl.role = Role.CONSUMER;
205         messageExchangeImpl.pattern = MessageExchangeImpl.IN_OUT_PATTERN;
206         messageExchangeImpl.fault = null;
207         messageExchangeImpl.messages.put("out", null);
208         try {
209             messageExchangeImpl.setStatus(ExchangeStatus.DONE);
210             fail();
211         } catch (MessagingException e) {
212             // do nothing
213
}
214     }
215
216     public void testSetStatusException3() {
217         messageExchangeImpl.role = Role.CONSUMER;
218         messageExchangeImpl.pattern = MessageExchangeImpl.IN_OPTIONAL_OUT_PATTERN;
219         messageExchangeImpl.fault = null;
220         messageExchangeImpl.messages.put("out", null);
221         try {
222             messageExchangeImpl.setStatus(ExchangeStatus.DONE);
223             fail();
224         } catch (MessagingException e) {
225             // do nothing
226
}
227     }
228
229     public void testSetStatusException4() {
230         messageExchangeImpl.role = Role.PROVIDER;
231         messageExchangeImpl.pattern = MessageExchangeImpl.ROBUST_IN_ONLY_PATTERN;
232         messageExchangeImpl.fault = EasyMock.createMock(FaultImpl.class);
233         try {
234             messageExchangeImpl.setStatus(ExchangeStatus.DONE);
235             fail();
236         } catch (MessagingException e) {
237             // do nothing
238
}
239     }
240
241     public void testSetStatusException5() {
242         messageExchangeImpl.role = Role.PROVIDER;
243         messageExchangeImpl.pattern = MessageExchangeImpl.IN_OUT_PATTERN;
244         try {
245             messageExchangeImpl.setStatus(ExchangeStatus.DONE);
246             fail();
247         } catch (MessagingException e) {
248             // do nothing
249
}
250     }
251
252     public void testSetStatusException6() {
253         messageExchangeImpl.role = Role.PROVIDER;
254         messageExchangeImpl.pattern = MessageExchangeImpl.IN_OPTIONAL_OUT_PATTERN;
255         messageExchangeImpl.fault = EasyMock.createMock(FaultImpl.class);
256         messageExchangeImpl.messages.put("out", null);
257         try {
258             messageExchangeImpl.setStatus(ExchangeStatus.DONE);
259             fail();
260         } catch (MessagingException e) {
261             // do nothing
262
}
263     }
264
265     public void testSetStatusException7() {
266         messageExchangeImpl.role = Role.PROVIDER;
267         messageExchangeImpl.pattern = MessageExchangeImpl.IN_OPTIONAL_OUT_PATTERN;
268         messageExchangeImpl.fault = null;
269         messageExchangeImpl.messages.put("out", EasyMock
270             .createMock(NormalizedMessage.class));
271         try {
272             messageExchangeImpl.setStatus(ExchangeStatus.DONE);
273             fail();
274         } catch (MessagingException e) {
275             // do nothing
276
}
277     }
278
279     public void testToString() {
280         messageExchangeImpl.exchangeId = "1";
281         assertEquals(messageExchangeImpl.toString(), "MessageExchange@1");
282     }
283
284     public void testCheckNotTerminated() throws SecurityException JavaDoc,
285         NoSuchMethodException JavaDoc {
286         MessageExchangeImpl messageExchangeImpl = EasyMock.createMock(
287             MessageExchangeImpl.class, new Method JavaDoc[] {MessageExchangeImpl.class
288                 .getDeclaredMethod("isTerminated", new Class JavaDoc[0])});
289
290         EasyMock.expect(messageExchangeImpl.isTerminated()).andReturn(true);
291
292         EasyMock.replay(messageExchangeImpl);
293
294         try {
295             messageExchangeImpl.checkNotTerminated();
296             fail();
297         } catch (MessagingException e) {
298             // do nothing
299
}
300     }
301
302     public void testCheckPatternMatchingExceptionPatternNull() {
303         messageExchangeImpl.pattern = null;
304         try {
305             messageExchangeImpl.checkPatternMatching(null);
306             fail();
307         } catch (MessagingException e) {
308             // do nothing
309
}
310     }
311
312     public void testCheckPatternMatchingNameNull() {
313         messageExchangeImpl.pattern = MessageExchangeImpl.IN_ONLY_PATTERN;
314         try {
315             messageExchangeImpl.checkPatternMatching(null);
316             fail();
317         } catch (MessagingException e) {
318             // do nothing
319
}
320     }
321
322     public void testCheckPatternMatchingNameTest() {
323         messageExchangeImpl.pattern = MessageExchangeImpl.IN_ONLY_PATTERN;
324         try {
325             messageExchangeImpl.checkPatternMatching("test");
326             fail();
327         } catch (MessagingException e) {
328             // do nothing
329
}
330     }
331
332     public void testCheckRoleMatchingExceptionRoleNull() {
333         messageExchangeImpl.role = null;
334         try {
335             messageExchangeImpl.checkRoleMatching(null);
336             fail();
337         } catch (MessagingException e) {
338             // do nothing
339
}
340     }
341
342     public void testCheckRoleMatchingExceptionNameNameNull() {
343         messageExchangeImpl.role = Role.CONSUMER;
344         try {
345             messageExchangeImpl.checkRoleMatching(null);
346             fail();
347         } catch (MessagingException e) {
348             // do nothing
349
}
350     }
351
352     public void testCheckRoleMatchingExceptionNameNameTest() {
353         messageExchangeImpl.role = Role.CONSUMER;
354         try {
355             messageExchangeImpl.checkRoleMatching("test");
356             fail();
357         } catch (MessagingException e) {
358             // do nothing
359
}
360     }
361
362     public void testReadObjectDelegate() throws IOException JavaDoc,
363         ClassNotFoundException JavaDoc {
364         ObjectInputStream JavaDoc objectInputStream = EasyMock
365             .createMock(ObjectInputStream JavaDoc.class);
366
367         objectInputStream.defaultReadObject();
368         EasyMock.expect(objectInputStream.readShort()).andReturn((short) 0);
369         EasyMock.expect(objectInputStream.readShort()).andReturn((short) 0);
370
371         EasyMock.replay(objectInputStream);
372
373         messageExchangeImpl.readObjectDelegate(objectInputStream);
374         assertEquals(messageExchangeImpl.role, Role.CONSUMER);
375         assertEquals(messageExchangeImpl.status, ExchangeStatus.ACTIVE);
376     }
377
378     public void testReadObjectDelegate1() throws IOException JavaDoc,
379         ClassNotFoundException JavaDoc {
380         ObjectInputStream JavaDoc objectInputStream = EasyMock
381             .createMock(ObjectInputStream JavaDoc.class);
382
383         objectInputStream.defaultReadObject();
384         EasyMock.expect(objectInputStream.readShort()).andReturn((short) 1);
385         EasyMock.expect(objectInputStream.readShort()).andReturn((short) 1);
386
387         EasyMock.replay(objectInputStream);
388
389         messageExchangeImpl.readObjectDelegate(objectInputStream);
390         assertEquals(messageExchangeImpl.role, Role.PROVIDER);
391         assertEquals(messageExchangeImpl.status, ExchangeStatus.DONE);
392     }
393
394     public void testReadObjectDelegate2() throws IOException JavaDoc,
395         ClassNotFoundException JavaDoc {
396         ObjectInputStream JavaDoc objectInputStream = EasyMock
397             .createMock(ObjectInputStream JavaDoc.class);
398
399         objectInputStream.defaultReadObject();
400         EasyMock.expect(objectInputStream.readShort()).andReturn((short) 1);
401         EasyMock.expect(objectInputStream.readShort()).andReturn((short) 2);
402
403         EasyMock.replay(objectInputStream);
404
405         messageExchangeImpl.readObjectDelegate(objectInputStream);
406         assertEquals(messageExchangeImpl.role, Role.PROVIDER);
407         assertEquals(messageExchangeImpl.status, ExchangeStatus.ERROR);
408     }
409
410     public void testWriteObjectDelegate() throws IOException JavaDoc {
411         messageExchangeImpl.role = Role.CONSUMER;
412         messageExchangeImpl.status = ExchangeStatus.ACTIVE;
413         ObjectOutputStream JavaDoc objectOutputStream = EasyMock
414             .createMock(ObjectOutputStream JavaDoc.class);
415
416         objectOutputStream.defaultWriteObject();
417         objectOutputStream.writeShort(0);
418         objectOutputStream.writeShort(0);
419
420         EasyMock.replay(objectOutputStream);
421
422         messageExchangeImpl.writeObjectDelegate(objectOutputStream);
423     }
424
425     public void testWriteObjectDelegate1() throws IOException JavaDoc {
426         messageExchangeImpl.role = Role.PROVIDER;
427         messageExchangeImpl.status = ExchangeStatus.DONE;
428         ObjectOutputStream JavaDoc objectOutputStream = EasyMock
429             .createMock(ObjectOutputStream JavaDoc.class);
430
431         objectOutputStream.defaultWriteObject();
432         objectOutputStream.writeShort(1);
433         objectOutputStream.writeShort(1);
434
435         EasyMock.replay(objectOutputStream);
436
437         messageExchangeImpl.writeObjectDelegate(objectOutputStream);
438     }
439
440     public void testWriteObjectDelegate2() throws IOException JavaDoc {
441         messageExchangeImpl.role = Role.PROVIDER;
442         messageExchangeImpl.status = ExchangeStatus.ERROR;
443         ObjectOutputStream JavaDoc objectOutputStream = EasyMock
444             .createMock(ObjectOutputStream JavaDoc.class);
445
446         objectOutputStream.defaultWriteObject();
447         objectOutputStream.writeShort(1);
448         objectOutputStream.writeShort(2);
449
450         EasyMock.replay(objectOutputStream);
451
452         messageExchangeImpl.writeObjectDelegate(objectOutputStream);
453     }
454 }
455
Popular Tags