1 22 package org.objectweb.petals.jbi.messaging; 23 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.lang.reflect.Method ; 28 import java.util.HashMap ; 29 import java.util.Map ; 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 ; 36 37 import junit.framework.TestCase; 38 39 import org.easymock.classextension.EasyMock; 40 41 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 ("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 ("operation")); 97 assertEquals(messageExchangeImpl.getOperation(), new QName ("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 <String , NormalizedMessage> map = new HashMap <String , 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 } 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 } 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 } 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 } 169 } 170 171 public void testSetMessageExceptionMessageNull() { 172 try { 173 messageExchangeImpl.setMessage(null, "test"); 174 fail(); 175 } catch (MessagingException e) { 176 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 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 } 277 } 278 279 public void testToString() { 280 messageExchangeImpl.exchangeId = "1"; 281 assertEquals(messageExchangeImpl.toString(), "MessageExchange@1"); 282 } 283 284 public void testCheckNotTerminated() throws SecurityException , 285 NoSuchMethodException { 286 MessageExchangeImpl messageExchangeImpl = EasyMock.createMock( 287 MessageExchangeImpl.class, new Method [] {MessageExchangeImpl.class 288 .getDeclaredMethod("isTerminated", new Class [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 } 300 } 301 302 public void testCheckPatternMatchingExceptionPatternNull() { 303 messageExchangeImpl.pattern = null; 304 try { 305 messageExchangeImpl.checkPatternMatching(null); 306 fail(); 307 } catch (MessagingException e) { 308 } 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 } 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 } 330 } 331 332 public void testCheckRoleMatchingExceptionRoleNull() { 333 messageExchangeImpl.role = null; 334 try { 335 messageExchangeImpl.checkRoleMatching(null); 336 fail(); 337 } catch (MessagingException e) { 338 } 340 } 341 342 public void testCheckRoleMatchingExceptionNameNameNull() { 343 messageExchangeImpl.role = Role.CONSUMER; 344 try { 345 messageExchangeImpl.checkRoleMatching(null); 346 fail(); 347 } catch (MessagingException e) { 348 } 350 } 351 352 public void testCheckRoleMatchingExceptionNameNameTest() { 353 messageExchangeImpl.role = Role.CONSUMER; 354 try { 355 messageExchangeImpl.checkRoleMatching("test"); 356 fail(); 357 } catch (MessagingException e) { 358 } 360 } 361 362 public void testReadObjectDelegate() throws IOException , 363 ClassNotFoundException { 364 ObjectInputStream objectInputStream = EasyMock 365 .createMock(ObjectInputStream .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 , 379 ClassNotFoundException { 380 ObjectInputStream objectInputStream = EasyMock 381 .createMock(ObjectInputStream .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 , 395 ClassNotFoundException { 396 ObjectInputStream objectInputStream = EasyMock 397 .createMock(ObjectInputStream .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 { 411 messageExchangeImpl.role = Role.CONSUMER; 412 messageExchangeImpl.status = ExchangeStatus.ACTIVE; 413 ObjectOutputStream objectOutputStream = EasyMock 414 .createMock(ObjectOutputStream .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 { 426 messageExchangeImpl.role = Role.PROVIDER; 427 messageExchangeImpl.status = ExchangeStatus.DONE; 428 ObjectOutputStream objectOutputStream = EasyMock 429 .createMock(ObjectOutputStream .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 { 441 messageExchangeImpl.role = Role.PROVIDER; 442 messageExchangeImpl.status = ExchangeStatus.ERROR; 443 ObjectOutputStream objectOutputStream = EasyMock 444 .createMock(ObjectOutputStream .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 |