How to use method of org.easymock.EasyMock class

Best Easymock code snippet using org.easymock.EasyMock.

Run Easymock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */
22package org.jboss.test.messaging.jms.message;
23
24import static org.easymock.EasyMock.expect;
25
26import java.io.Serializable;
27import java.nio.ByteBuffer;
28import java.util.Enumeration;
29import java.util.HashSet;
30
31import javax.jms.BytesMessage;
32import javax.jms.DeliveryMode;
33import javax.jms.Destination;
34import javax.jms.JMSException;
35import javax.jms.MapMessage;
36import javax.jms.Message;
37import javax.jms.MessageFormatException;
38import javax.jms.MessageNotWriteableException;
39import javax.jms.ObjectMessage;
40import javax.jms.StreamMessage;
41import javax.jms.TextMessage;
42
43import org.easymock.EasyMock;
44import org.jboss.messaging.core.client.ClientMessage;
45import org.jboss.messaging.core.client.ClientSession;
46import org.jboss.messaging.core.client.impl.ClientMessageImpl;
47import org.jboss.messaging.core.remoting.impl.ByteBufferWrapper;
48import org.jboss.messaging.jms.client.JBossBytesMessage;
49import org.jboss.messaging.jms.client.JBossMapMessage;
50import org.jboss.messaging.jms.client.JBossMessage;
51import org.jboss.messaging.jms.client.JBossObjectMessage;
52import org.jboss.messaging.jms.client.JBossStreamMessage;
53import org.jboss.messaging.jms.client.JBossTextMessage;
54
55/**
56 *
57 * @author <a href="mailto:ovidiu@feodorov.com">Ovidiu Feodorov</a>
58 * @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
59 * @version <tt>$Revision$</tt>
60 *
61 * $Id$
62 */
63public class MessageHeaderTest extends MessageHeaderTestBase
64{
65   // Constants -----------------------------------------------------
66
67   // Static --------------------------------------------------------
68
69   // Attributes ----------------------------------------------------
70
71   // Constructors --------------------------------------------------
72
73   public MessageHeaderTest(String name)
74   {
75      super(name);
76   }
77
78   // Public --------------------------------------------------------
79
80
81   public void testClearMessage() throws Exception
82   {
83      queueProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
84
85      Message message = queueProducerSession.createTextMessage("some message");
86
87      queueProducer.send(message);
88
89      message = queueConsumer.receive(1000);
90
91      assertNotNull(message);
92
93      message.clearProperties();
94
95      assertNotNull(message.getJMSDestination());
96
97   }
98
99   public void testMessageOrderQueue() throws Exception
100   {
101      final int NUM_MESSAGES = 10;
102      
103      queueProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
104      for (int i = 0; i < NUM_MESSAGES; i++)
105      {
106         Message m = queueProducerSession.createMessage();
107         m.setIntProperty("count", i);
108         queueProducer.send(m);
109      }
110      
111      for (int i = 0; i < NUM_MESSAGES; i++)
112      {
113         Message m = queueConsumer.receive(3000);
114         assertNotNull(m);
115         int count = m.getIntProperty("count");
116         assertEquals(i, count);
117      }
118      
119      queueProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
120      for (int i = 0; i < NUM_MESSAGES; i++)
121      {
122         Message m = queueProducerSession.createMessage();
123         m.setIntProperty("count2", i);
124         queueProducer.send(m);
125      }
126      
127      for (int i = 0; i < NUM_MESSAGES; i++)
128      {
129         Message m = queueConsumer.receive(3000);
130         assertNotNull(m);
131         int count = m.getIntProperty("count2");
132         assertEquals(i, count);
133      }
134   }
135   
136   public void testMessageOrderTopic() throws Exception
137   {
138      final int NUM_MESSAGES = 10;
139      
140      topicProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
141      for (int i = 0; i < NUM_MESSAGES; i++)
142      {
143         Message m = topicProducerSession.createMessage();
144         m.setIntProperty("count", i);
145         topicProducer.send(m);
146      }
147      
148      for (int i = 0; i < NUM_MESSAGES; i++)
149      {
150         Message m = topicConsumer.receive(3000);
151         assertNotNull(m);
152         int count = m.getIntProperty("count");
153         assertEquals(i, count);
154      }
155      
156      topicProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
157      for (int i = 0; i < NUM_MESSAGES; i++)
158      {
159         Message m = topicProducerSession.createMessage();
160         m.setIntProperty("count2", i);
161         topicProducer.send(m);
162      }
163      
164      for (int i = 0; i < NUM_MESSAGES; i++)
165      {
166         Message m = topicConsumer.receive(3000);
167         assertNotNull(m);
168         int count = m.getIntProperty("count2");
169         assertEquals(i, count);
170      }
171   }
172   
173
174   public void testProperties() throws Exception
175   {
176      Message m1 = queueProducerSession.createMessage();
177
178      //Some arbitrary values
179      boolean myBool = true;
180      byte myByte = 13;
181      short myShort = 15321;
182      int myInt = 0x71ab6c80;
183      long myLong = 0x20bf1e3fb6fa31dfL;
184      float myFloat = Float.MAX_VALUE - 23465;
185      double myDouble = Double.MAX_VALUE - 72387633;
186      String myString = "abcdef&^*&!^ghijkl";
187
188      m1.setBooleanProperty("myBool", myBool);
189      m1.setByteProperty("myByte", myByte);
190      m1.setShortProperty("myShort", myShort);
191      m1.setIntProperty("myInt", myInt);
192      m1.setLongProperty("myLong", myLong);
193      m1.setFloatProperty("myFloat", myFloat);
194      m1.setDoubleProperty("myDouble", myDouble);
195      m1.setStringProperty("myString", myString);
196
197      m1.setObjectProperty("myBool", new Boolean(myBool));
198      m1.setObjectProperty("myByte", new Byte(myByte));
199      m1.setObjectProperty("myShort", new Short(myShort));
200      m1.setObjectProperty("myInt", new Integer(myInt));
201      m1.setObjectProperty("myLong", new Long(myLong));
202      m1.setObjectProperty("myFloat", new Float(myFloat));
203      m1.setObjectProperty("myDouble", new Double(myDouble));
204      m1.setObjectProperty("myString", myString);
205
206      try
207      {
208         m1.setObjectProperty("myIllegal", new Object());
209         fail();
210      }
211      catch (javax.jms.MessageFormatException e)
212      {}
213
214
215      queueProducer.send(queue1, m1);
216
217      Message m2 = queueConsumer.receive(2000);
218
219      assertNotNull(m2);
220
221      assertEquals(myBool, m2.getBooleanProperty("myBool"));
222      assertEquals(myByte, m2.getByteProperty("myByte"));
223      assertEquals(myShort, m2.getShortProperty("myShort"));
224      assertEquals(myInt, m2.getIntProperty("myInt"));
225      assertEquals(myLong, m2.getLongProperty("myLong"));
226      assertEquals(myFloat, m2.getFloatProperty("myFloat"), 0);
227      assertEquals(myDouble, m2.getDoubleProperty("myDouble"), 0);
228      assertEquals(myString, m2.getStringProperty("myString"));
229
230
231      //Properties should now be read-only
232      try
233      {
234         m2.setBooleanProperty("myBool", myBool);
235         fail();
236      }
237      catch (MessageNotWriteableException e) {}
238
239      try
240      {
241         m2.setByteProperty("myByte", myByte);
242         fail();
243      }
244      catch (MessageNotWriteableException e) {}
245
246      try
247      {
248         m2.setShortProperty("myShort", myShort);
249         fail();
250      }
251      catch (MessageNotWriteableException e) {}
252
253      try
254      {
255         m2.setIntProperty("myInt", myInt);
256         fail();
257      }
258      catch (MessageNotWriteableException e) {}
259
260      try
261      {
262         m2.setLongProperty("myLong", myLong);
263         fail();
264      }
265      catch (MessageNotWriteableException e) {}
266
267      try
268      {
269         m2.setFloatProperty("myFloat", myFloat);
270         fail();
271      }
272      catch (MessageNotWriteableException e) {}
273
274      try
275      {
276         m2.setDoubleProperty("myDouble", myDouble);
277         fail();
278      }
279      catch (MessageNotWriteableException e) {}
280
281      try
282      {
283         m2.setStringProperty("myString", myString);
284         fail();
285      }
286      catch (MessageNotWriteableException e) {}
287
288      assertTrue(m2.propertyExists("myBool"));
289      assertTrue(m2.propertyExists("myByte"));
290      assertTrue(m2.propertyExists("myShort"));
291      assertTrue(m2.propertyExists("myInt"));
292      assertTrue(m2.propertyExists("myLong"));
293      assertTrue(m2.propertyExists("myFloat"));
294      assertTrue(m2.propertyExists("myDouble"));
295      assertTrue(m2.propertyExists("myString"));
296
297      assertFalse(m2.propertyExists("sausages"));
298
299      HashSet propNames = new HashSet();
300      Enumeration en = m2.getPropertyNames();
301      while (en.hasMoreElements())
302      {
303         String propName = (String)en.nextElement();
304         
305         log.info("Prop name is " + propName);
306         
307         propNames.add(propName);
308      }
309
310      assertEquals(9, propNames.size());
311
312      assertTrue(propNames.contains("myBool"));
313      assertTrue(propNames.contains("myByte"));
314      assertTrue(propNames.contains("myShort"));
315      assertTrue(propNames.contains("myInt"));
316      assertTrue(propNames.contains("myLong"));
317      assertTrue(propNames.contains("myFloat"));
318      assertTrue(propNames.contains("myDouble"));
319      assertTrue(propNames.contains("myString"));
320
321
322      // Check property conversions
323
324      //Boolean property can be read as String but not anything else
325
326      assertEquals(String.valueOf(myBool), m2.getStringProperty("myBool"));
327
328      try
329      {
330         m2.getByteProperty("myBool");
331         fail();
332      } catch (MessageFormatException e) {}
333
334      try
335      {
336         m2.getShortProperty("myBool");
337         fail();
338      } catch (MessageFormatException e) {}
339
340      try
341      {
342         m2.getIntProperty("myBool");
343         fail();
344      } catch (MessageFormatException e) {}
345
346      try
347      {
348         m2.getLongProperty("myBool");
349         fail();
350      } catch (MessageFormatException e) {}
351
352      try
353      {
354         m2.getFloatProperty("myBool");
355         fail();
356      } catch (MessageFormatException e) {}
357
358      try
359      {
360         m2.getDoubleProperty("myBool");
361         fail();
362      } catch (MessageFormatException e) {}
363
364
365      // byte property can be read as short, int, long or String
366
367      assertEquals((short)myByte, m2.getShortProperty("myByte"));
368      assertEquals((int)myByte, m2.getIntProperty("myByte"));
369      assertEquals((long)myByte, m2.getLongProperty("myByte"));
370      assertEquals(String.valueOf(myByte), m2.getStringProperty("myByte"));
371
372      try
373      {
374         m2.getBooleanProperty("myByte");
375         fail();
376      } catch (MessageFormatException e) {}
377
378      try
379      {
380         m2.getFloatProperty("myByte");
381         fail();
382      } catch (MessageFormatException e) {}
383
384      try
385      {
386         m2.getDoubleProperty("myByte");
387         fail();
388      } catch (MessageFormatException e) {}
389
390
391      // short property can be read as int, long or String
392
393      assertEquals((int)myShort, m2.getIntProperty("myShort"));
394      assertEquals((long)myShort, m2.getLongProperty("myShort"));
395      assertEquals(String.valueOf(myShort), m2.getStringProperty("myShort"));
396
397      try
398      {
399         m2.getByteProperty("myShort");
400         fail();
401      } catch (MessageFormatException e) {}
402
403      try
404      {
405         m2.getBooleanProperty("myShort");
406         fail();
407      } catch (MessageFormatException e) {}
408
409      try
410      {
411         m2.getFloatProperty("myShort");
412         fail();
413      } catch (MessageFormatException e) {}
414
415      try
416      {
417         m2.getDoubleProperty("myShort");
418         fail();
419      } catch (MessageFormatException e) {}
420
421      // int property can be read as long or String
422
423      assertEquals((long)myInt, m2.getLongProperty("myInt"));
424      assertEquals(String.valueOf(myInt), m2.getStringProperty("myInt"));
425
426      try
427      {
428         m2.getShortProperty("myInt");
429         fail();
430      } catch (MessageFormatException e) {}
431
432      try
433      {
434         m2.getByteProperty("myInt");
435         fail();
436      } catch (MessageFormatException e) {}
437
438      try
439      {
440         m2.getBooleanProperty("myInt");
441         fail();
442      } catch (MessageFormatException e) {}
443
444      try
445      {
446         m2.getFloatProperty("myInt");
447         fail();
448      } catch (MessageFormatException e) {}
449
450      try
451      {
452         m2.getDoubleProperty("myInt");
453         fail();
454      } catch (MessageFormatException e) {}
455
456
457      // long property can be read as String
458
459      assertEquals(String.valueOf(myLong), m2.getStringProperty("myLong"));
460
461      try
462      {
463         m2.getIntProperty("myLong");
464         fail();
465      } catch (MessageFormatException e) {}
466
467      try
468      {
469         m2.getShortProperty("myLong");
470         fail();
471      } catch (MessageFormatException e) {}
472
473      try
474      {
475         m2.getByteProperty("myLong");
476         fail();
477      } catch (MessageFormatException e) {}
478
479      try
480      {
481         m2.getBooleanProperty("myLong");
482         fail();
483      } catch (MessageFormatException e) {}
484
485      try
486      {
487         m2.getFloatProperty("myLong");
488         fail();
489      } catch (MessageFormatException e) {}
490
491      try
492      {
493         m2.getDoubleProperty("myLong");
494         fail();
495      } catch (MessageFormatException e) {}
496
497
498      // float property can be read as double or String
499
500      assertEquals(String.valueOf(myFloat), m2.getStringProperty("myFloat"));
501      assertEquals((double)myFloat, m2.getDoubleProperty("myFloat"), 0);
502
503      try
504      {
505         m2.getIntProperty("myFloat");
506         fail();
507      } catch (MessageFormatException e) {}
508
509      try
510      {
511         m2.getShortProperty("myFloat");
512         fail();
513      } catch (MessageFormatException e) {}
514
515      try
516      {
517         m2.getLongProperty("myFloat");
518         fail();
519      } catch (MessageFormatException e) {}
520
521      try
522      {
523         m2.getByteProperty("myFloat");
524         fail();
525      } catch (MessageFormatException e) {}
526
527      try
528      {
529         m2.getBooleanProperty("myFloat");
530         fail();
531      } catch (MessageFormatException e) {}
532
533
534
535      // double property can be read as String
536
537      assertEquals(String.valueOf(myDouble), m2.getStringProperty("myDouble"));
538
539
540      try
541      {
542         m2.getFloatProperty("myDouble");
543         fail();
544      } catch (MessageFormatException e) {}
545
546      try
547      {
548         m2.getIntProperty("myDouble");
549         fail();
550      } catch (MessageFormatException e) {}
551
552      try
553      {
554         m2.getShortProperty("myDouble");
555         fail();
556      } catch (MessageFormatException e) {}
557
558      try
559      {
560         m2.getByteProperty("myDouble");
561         fail();
562      } catch (MessageFormatException e) {}
563
564      try
565      {
566         m2.getBooleanProperty("myDouble");
567         fail();
568      } catch (MessageFormatException e) {}
569
570      try
571      {
572         m2.getFloatProperty("myDouble");
573         fail();
574      } catch (MessageFormatException e) {}
575
576      log.info("** clearing props");
577      m2.clearProperties();
578
579      log.info("** getting prop names");
580      Enumeration en2 = m2.getPropertyNames();
581      log.info("** got prop names");
582      assertTrue(en2.hasMoreElements());
583      en2.nextElement();
584      assertFalse(en2.hasMoreElements());
585
586
587
588
589      // Test String -> Numeric and bool conversions
590      Message m3 = queueProducerSession.createMessage();
591
592      m3.setStringProperty("myBool", String.valueOf(myBool));
593      m3.setStringProperty("myByte", String.valueOf(myByte));
594      m3.setStringProperty("myShort", String.valueOf(myShort));
595      m3.setStringProperty("myInt", String.valueOf(myInt));
596      m3.setStringProperty("myLong", String.valueOf(myLong));
597      m3.setStringProperty("myFloat", String.valueOf(myFloat));
598      m3.setStringProperty("myDouble", String.valueOf(myDouble));
599      m3.setStringProperty("myIllegal", "xyz123");
600
601      assertEquals(myBool, m3.getBooleanProperty("myBool"));
602      assertEquals(myByte, m3.getByteProperty("myByte"));
603      assertEquals(myShort, m3.getShortProperty("myShort"));
604      assertEquals(myInt, m3.getIntProperty("myInt"));
605      assertEquals(myLong, m3.getLongProperty("myLong"));
606      assertEquals(myFloat, m3.getFloatProperty("myFloat"), 0);
607      assertEquals(myDouble, m3.getDoubleProperty("myDouble"), 0);
608
609      m3.getBooleanProperty("myIllegal");
610
611      try
612      {
613         m3.getByteProperty("myIllegal");
614         fail();
615      }
616      catch (NumberFormatException e) {}
617      try
618      {
619         m3.getShortProperty("myIllegal");
620         fail();
621      }
622      catch (NumberFormatException e) {}
623      try
624      {
625         m3.getIntProperty("myIllegal");
626         fail();
627      }
628      catch (NumberFormatException e) {}
629      try
630      {
631         m3.getLongProperty("myIllegal");
632         fail();
633      }
634      catch (NumberFormatException e) {}
635      try
636      {
637         m3.getFloatProperty("myIllegal");
638         fail();
639      }
640      catch (NumberFormatException e) {}
641      try
642      {
643         m3.getDoubleProperty("myIllegal");
644         fail();
645      }
646      catch (NumberFormatException e) {}
647   }
648
649
650
651   public void testSendReceiveForeignMessage() throws JMSException
652   {
653      
654      log.trace("Starting da test");
655      
656      SimpleJMSMessage foreignMessage = new SimpleJMSMessage();
657      
658      foreignMessage.setStringProperty("animal", "aardvark");
659      
660      //foreign messages don't have to be serializable
661      assertFalse(foreignMessage instanceof Serializable);
662      
663      log.trace("Sending message");
664      
665      queueProducer.send(foreignMessage);
666      
667      log.trace("Sent message");
668
669      Message m2 = queueConsumer.receive(3000);
670      log.trace("The message is " + m2);
671      
672      assertNotNull(m2);
673      
674      assertEquals("aardvark", m2.getStringProperty("animal"));
675      
676      log.trace("Received message");
677
678      log.trace("Done that test");
679   }
680
681   public void testCopyOnJBossMessage() throws JMSException
682   {
683      ClientSession session = EasyMock.createNiceMock(ClientSession.class);
684      ByteBufferWrapper body = new ByteBufferWrapper(ByteBuffer.allocate(1024));
685      ClientMessage clientMessage = new ClientMessageImpl(JBossTextMessage.TYPE, true, 0, System.currentTimeMillis(), (byte)4, body);
686      expect(session.createClientMessage(EasyMock.anyByte(), EasyMock.anyBoolean(), EasyMock.anyInt(), EasyMock.anyLong(), EasyMock.anyByte())).andReturn(clientMessage);
687      EasyMock.replay(session);
688      JBossMessage jbossMessage = new JBossMessage();
689
690      configureMessage(jbossMessage);
691
692      JBossMessage copy = new JBossMessage(jbossMessage, session);
693
694      ensureEquivalent(jbossMessage, copy);
695      EasyMock.verify(session);
696   }
697
698
699   public void testCopyOnForeignMessage() throws JMSException
700   {
701      ClientSession session = EasyMock.createNiceMock(ClientSession.class);
702      ByteBufferWrapper body = new ByteBufferWrapper(ByteBuffer.allocate(1024));
703      ClientMessage clientMessage = new ClientMessageImpl(JBossMessage.TYPE, true, 0, System.currentTimeMillis(), (byte)4, body);
704      expect(session.createClientMessage(EasyMock.anyByte(), EasyMock.anyBoolean(), EasyMock.anyInt(), EasyMock.anyLong(), EasyMock.anyByte())).andReturn(clientMessage);
705      EasyMock.replay(session);
706
707      Message foreignMessage = new SimpleJMSMessage();
708
709      JBossMessage copy = new JBossMessage(foreignMessage, session);
710
711      ensureEquivalent(foreignMessage, copy);
712
713      EasyMock.verify(session);
714   }
715   
716   public void testCopyOnForeignBytesMessage() throws JMSException
717   {
718      ClientSession session = EasyMock.createNiceMock(ClientSession.class);
719      ByteBufferWrapper body = new ByteBufferWrapper(ByteBuffer.allocate(1024));
720      ClientMessage clientMessage = new ClientMessageImpl(JBossBytesMessage.TYPE, true, 0, System.currentTimeMillis(), (byte)4, body);
721      expect(session.createClientMessage(EasyMock.anyByte(), EasyMock.anyBoolean(), EasyMock.anyInt(), EasyMock.anyLong(), EasyMock.anyByte())).andReturn(clientMessage);
722      EasyMock.replay(session);
723
724      BytesMessage foreignBytesMessage = new SimpleJMSBytesMessage();
725      for(int i = 0; i < 20; i++)
726      {
727         foreignBytesMessage.writeByte((byte)i);
728      }
729
730      JBossBytesMessage copy = new JBossBytesMessage(foreignBytesMessage, session);
731
732      foreignBytesMessage.reset();
733      copy.reset();
734
735      ensureEquivalent(foreignBytesMessage, copy);
736      EasyMock.verify(session);
737   }
738  
739   public void testCopyOnForeignMapMessage() throws JMSException
740   {
741      ClientSession session = EasyMock.createNiceMock(ClientSession.class);
742      ByteBufferWrapper body = new ByteBufferWrapper(ByteBuffer.allocate(1024));
743      ClientMessage clientMessage = new ClientMessageImpl(JBossMapMessage.TYPE, true, 0, System.currentTimeMillis(), (byte)4, body);
744      expect(session.createClientMessage(EasyMock.anyByte(), EasyMock.anyBoolean(), EasyMock.anyInt(), EasyMock.anyLong(), EasyMock.anyByte())).andReturn(clientMessage);
745      EasyMock.replay(session);
746      MapMessage foreignMapMessage = new SimpleJMSMapMessage();
747      foreignMapMessage.setInt("int", 1);
748      foreignMapMessage.setString("string", "test");
749
750      JBossMapMessage copy = new JBossMapMessage(foreignMapMessage, session);
751
752      ensureEquivalent(foreignMapMessage, copy);
753      EasyMock.verify(session);
754   }
755
756
757   public void testCopyOnForeignObjectMessage() throws JMSException
758   {
759      ClientSession session = EasyMock.createNiceMock(ClientSession.class);
760      ByteBufferWrapper body = new ByteBufferWrapper(ByteBuffer.allocate(1024));
761      ClientMessage clientMessage = new ClientMessageImpl(JBossObjectMessage.TYPE, true, 0, System.currentTimeMillis(), (byte)4, body);
762      expect(session.createClientMessage(EasyMock.anyByte(), EasyMock.anyBoolean(), EasyMock.anyInt(), EasyMock.anyLong(), EasyMock.anyByte())).andReturn(clientMessage);
763      EasyMock.replay(session);
764
765      ObjectMessage foreignObjectMessage = new SimpleJMSObjectMessage();
766
767      JBossObjectMessage copy = new JBossObjectMessage(foreignObjectMessage, session);
768
769      ensureEquivalent(foreignObjectMessage, copy);
770   }
771
772
773   public void testCopyOnForeignStreamMessage() throws JMSException
774   {
775      ClientSession session = EasyMock.createNiceMock(ClientSession.class);
776      ByteBufferWrapper body = new ByteBufferWrapper(ByteBuffer.allocate(1024));
777      ClientMessage clientMessage = new ClientMessageImpl(JBossStreamMessage.TYPE, true, 0, System.currentTimeMillis(), (byte)4, body);
778      expect(session.createClientMessage(EasyMock.anyByte(), EasyMock.anyBoolean(), EasyMock.anyInt(), EasyMock.anyLong(), EasyMock.anyByte())).andReturn(clientMessage);
779      EasyMock.replay(session);
780
781      StreamMessage foreignStreamMessage = new SimpleJMSStreamMessage();
782      foreignStreamMessage.writeByte((byte)1);
783      foreignStreamMessage.writeByte((byte)2);
784      foreignStreamMessage.writeByte((byte)3);
785
786      JBossStreamMessage copy = new JBossStreamMessage(foreignStreamMessage, session);
787
788      ensureEquivalent(foreignStreamMessage, copy);
789      EasyMock.verify(session);
790   }
791
792
793   public void testCopyOnForeignTextMessage() throws JMSException
794   {
795      ClientSession session = EasyMock.createNiceMock(ClientSession.class);
796      ByteBufferWrapper body = new ByteBufferWrapper(ByteBuffer.allocate(1024));
797      ClientMessage clientMessage = new ClientMessageImpl(JBossTextMessage.TYPE, true, 0, System.currentTimeMillis(), (byte)4, body);
798      expect(session.createClientMessage(EasyMock.anyByte(), EasyMock.anyBoolean(), EasyMock.anyInt(), EasyMock.anyLong(), EasyMock.anyByte())).andReturn(clientMessage);
799      EasyMock.replay(session);
800
801      TextMessage foreignTextMessage = new SimpleJMSTextMessage();
802
803      JBossTextMessage copy = new JBossTextMessage(foreignTextMessage, session);
804
805      ensureEquivalent(foreignTextMessage, copy);
806      EasyMock.verify(session);
807   }
808   
809   public void testForeignJMSDestination() throws JMSException
810   {
811      Message message = queueProducerSession.createMessage();
812      
813      Destination foreignDestination = new ForeignDestination();
814      
815      message.setJMSDestination(foreignDestination);
816      
817      assertSame(foreignDestination, message.getJMSDestination());
818      
819      queueProducer.send(message);
820      
821      assertSame(queue1, message.getJMSDestination());
822      
823      Message receivedMessage = queueConsumer.receive(2000);
824      
825      ensureEquivalent(receivedMessage, (JBossMessage)message);
826   }
827   
828// Invalid!!
829//   public void testForeignJMSReplyTo() throws JMSException
830//   {
831//   	Message msg = queueProducerSession.createTextMessage();
832//   	
833//      JBossMessage jbossMessage = (JBossMessage)msg;
834//      
835//      Destination foreignDestination = new ForeignDestination();
836//      
837//      jbossMessage.setJMSReplyTo(foreignDestination);
838//      
839//      queueProducer.send(msg);
840//      
841//      Message receivedMessage = queueConsumer.receive(2000);
842//
843//      ensureEquivalent(receivedMessage, jbossMessage);
844//   }
845//   
846//   public void testCopyForeignDestinationAndReplyTo() throws JMSException
847//   {
848//      Message foreignMessage = new SimpleJMSMessage();
849//      foreignMessage.setJMSDestination(new ForeignDestination());
850//      foreignMessage.setJMSReplyTo(new ForeignDestination());
851//
852//      JBossMessage copy = new JBossMessage(foreignMessage);
853//
854//      ensureEquivalent(foreignMessage, copy);
855//   }
856
857   // Package protected ---------------------------------------------
858
859   // Protected -----------------------------------------------------
860
861   // Private -------------------------------------------------------
862
863   // Inner classes -------------------------------------------------
864
865   private static class ForeignDestination implements Destination, Serializable
866   {
867		private static final long serialVersionUID = 5545509674580823610L;
868
869		// A ForeignDestination equals any other ForeignDestination, for simplicity
870      public boolean equals(Object obj)
871      {
872         return obj instanceof ForeignDestination;
873      }
874      
875      public int hashCode()
876      {
877         return 157;
878      }
879   }
880   
881}
882
Full Screen
copy
1/*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */ 
22
23package org.jboss.messaging.tests.unit.jms.client;
24
25import static org.easymock.EasyMock.createNiceMock;
26import static org.easymock.EasyMock.expect;
27import static org.easymock.EasyMock.replay;
28import static org.easymock.EasyMock.verify;
29import static org.jboss.messaging.tests.util.RandomUtil.randomString;
30
31import java.nio.ByteBuffer;
32import java.util.Collections;
33
34import javax.jms.DeliveryMode;
35import javax.jms.TextMessage;
36
37import junit.framework.TestCase;
38
39import org.easymock.EasyMock;
40import org.jboss.messaging.core.client.ClientMessage;
41import org.jboss.messaging.core.client.ClientSession;
42import org.jboss.messaging.core.client.impl.ClientMessageImpl;
43import org.jboss.messaging.core.remoting.impl.ByteBufferWrapper;
44import org.jboss.messaging.core.remoting.spi.MessagingBuffer;
45import org.jboss.messaging.jms.client.JBossTextMessage;
46
47/**
48 * @author <a href="mailto:jmesnil@redhat.com">Jeff Mesnil</a>
49 * 
50 * @version <tt>$Revision$</tt>
51 * 
52 */
53public class JBossTextMessageTest extends TestCase
54{
55   // Constants -----------------------------------------------------
56
57   // Attributes ----------------------------------------------------
58
59   private String text;
60
61   @Override
62   protected void setUp() throws Exception
63   {
64      super.setUp();
65
66      text = randomString();
67   }
68
69   // Static --------------------------------------------------------
70
71   // Constructors --------------------------------------------------
72
73   // Public --------------------------------------------------------
74
75   public void testForeignTextMessage() throws Exception
76   {
77      TextMessage foreignMessage = createNiceMock(TextMessage.class);
78      ClientSession session = EasyMock.createNiceMock(ClientSession.class);
79      ByteBufferWrapper body = new ByteBufferWrapper(ByteBuffer.allocate(1024));
80      ClientMessage clientMessage = new ClientMessageImpl(JBossTextMessage.TYPE, true, 0, System.currentTimeMillis(), (byte)4, body);
81      expect(session.createClientMessage(EasyMock.anyByte(), EasyMock.anyBoolean(), EasyMock.anyInt(), EasyMock.anyLong(), EasyMock.anyByte())).andReturn(clientMessage);  
82      expect(foreignMessage.getJMSDeliveryMode()).andReturn(DeliveryMode.NON_PERSISTENT);
83      expect(foreignMessage.getPropertyNames()).andReturn(Collections.enumeration(Collections.EMPTY_LIST));
84      expect(foreignMessage.getText()).andReturn(text);
85      
86      replay(foreignMessage, session);
87      
88      JBossTextMessage msg = new JBossTextMessage(foreignMessage, session);
89      assertEquals(text, msg.getText());
90      
91      verify(foreignMessage, session);
92   }
93   
94   public void testGetText() throws Exception
95   {
96      JBossTextMessage msg = new JBossTextMessage();
97      msg.setText(text);
98      assertEquals(text, msg.getText());
99   }
100
101   public void testClearBody() throws Exception
102   {
103      JBossTextMessage msg = new JBossTextMessage();
104      msg.setText(text);
105      assertEquals(text, msg.getText());
106      msg.clearBody();
107      assertEquals(null, msg.getText());
108   }
109   
110   public void testGetType() throws Exception
111   {
112      JBossTextMessage msg = new JBossTextMessage();
113      assertEquals(JBossTextMessage.TYPE, msg.getType());
114   }
115   
116   public void testDoBeforeSend() throws Exception
117   {
118      JBossTextMessage msg = new JBossTextMessage();
119      msg.setText(text);
120      
121      msg.doBeforeSend();
122
123      MessagingBuffer body = msg.getCoreMessage().getBody();
124      String s = body.getNullableString();
125      assertEquals(text, s);
126   }
127   
128   public void testDoBeforeReceive() throws Exception
129   {
130      JBossTextMessage msg = new JBossTextMessage();
131      assertNull(msg.getText());
132      MessagingBuffer body = msg.getCoreMessage().getBody();
133      body.putNullableString(text);
134      body.flip();
135      
136      msg.doBeforeReceive();
137      
138      assertEquals(text, msg.getText());
139   }
140   
141   // Package protected ---------------------------------------------
142
143   // Protected -----------------------------------------------------
144
145   // Private -------------------------------------------------------
146
147   // Inner classes -------------------------------------------------
148}
149
Full Screen
copy
1package com.examples;
2
3import junit.framework.TestCase;
4import org.easymock.EasyMock;
5// import org.easymock.classextension.EasyMock;
6
7import java.io.IOException;
8
9public class CurrencyTest extends TestCase {
10
11    Currency testObject = new Currency(2.50, "USD");
12    Currency expected = new Currency(3.75, "EUR");
13
14    public void testToEuros() throws IOException {
15
16        ExchangeRate mock = EasyMock.createMock(ExchangeRate.class);
17
18        // EasyMock.anyInt()
19        // EasyMock.anyShort()
20        // EasyMock.anyByte()
21        // EasyMock.anyLong()
22        // EasyMock.anyFloat()
23        // EasyMock.anyDouble()
24        // EasyMock.anyBoolean()
25        // EasyMock.eq()
26
27        // EasyMock.expect(mock.getRate("USD", "EUR")).andReturn(1.5);
28        // EasyMock.expect(mock.getRate((String) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(1.5);
29        // EasyMock.expect(mock.getRate((String) EasyMock.notNull(), (String) EasyMock.notNull())).andReturn(1.5);
30        EasyMock.expect(mock.getRate(
31                (String) EasyMock.matches("[A-Z][A-Z][A-Z]"),
32                (String) EasyMock.matches("[A-Z][A-Z][A-Z]"))).andReturn(1.5);
33        EasyMock.replay(mock);
34
35        Currency actual = testObject.toEuros(mock);
36        assertEquals(expected, actual);
37        // 检查是否只调用 getRate() 一次
38        EasyMock.verify(mock);
39    }
40
41    public void testExchangeRateServerUnavailable() throws IOException {
42        ExchangeRate mock = EasyMock.createMock(ExchangeRate.class);
43        EasyMock.expect(mock.getRate("USD", "EUR")).andThrow(new IOException());
44        EasyMock.replay(mock);
45        Currency actual = testObject.toEuros(mock);
46        assertNull(actual);
47    }
48
49}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)