5c4c2bf358411a3392b88ab90fa98e115543fce1
[mono.git] / mcs / class / Mono.Messaging.RabbitMQ / Test / Mono.Messaging.RabbitMQ / MessageEnumeratorTest.cs
1 //
2 // Test.Mono.Messaging.RabbitMQ
3 //
4 // Authors:
5 //        Michael Barker (mike@middlesoft.co.uk)
6 //
7 // (C) 2008 Michael Barker
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Messaging;
33
34 //using Mono.Messaging;
35 //using Mono.Messaging.RabbitMQ;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.Mono.Messaging.RabbitMQ
40 {
41         [TestFixture]
42         public class MessageEnumeratorTest {
43                 
44                 private readonly String qName = @".\private$\testq2";
45                 
46                 private void SendMessage (string s) {
47                         MessageQueue mq = MQUtil.GetQueue (qName);
48                         Message m = new Message (s, new BinaryMessageFormatter ());
49                         m.CorrelationId = Guid.NewGuid () + "\\0";
50                         mq.Send (m);
51                 }
52                 
53                 [Test]
54                 public void RemoveMessage ()
55                 {
56                         SendMessage ("message 1");
57                         SendMessage ("message 2");
58                         SendMessage ("message 3");
59                         SendMessage ("message 4");
60                         
61                         MessageQueue mq0 = MQUtil.GetQueue (qName);
62                         MessageEnumerator me0 = mq0.GetMessageEnumerator ();
63                         
64                         me0.MoveNext ();
65                         me0.MoveNext ();
66                         me0.MoveNext ();
67                         
68                         Message m0 = me0.RemoveCurrent ();
69                         
70                         me0.MoveNext ();
71                         
72                         me0.Dispose ();
73                         mq0.Dispose ();
74                         
75                         MessageQueue mq1 = MQUtil.GetQueue (qName);
76                         MessageEnumerator me1 = mq1.GetMessageEnumerator ();
77                         
78                         me1.MoveNext();
79                         me1.MoveNext();
80                         me1.MoveNext();
81                         
82                         Message m1 = me1.Current;
83                         m1.Formatter = new BinaryMessageFormatter ();
84                         Console.WriteLine ("{0}", m1.Body);
85                         Assert.AreEqual ("message 4", (String) m1.Body, "body incorrect");
86                         
87                         mq1.Purge ();
88                         MessageQueue.Delete (qName);
89                 }
90                 
91                 //[Test]
92                 // Not supported with AMQP
93                 public void RemoveMessageWithTx ()
94                 {
95                         MessageQueue q = MQUtil.GetQueue ("testq3");
96                         
97                         q.Formatter = new BinaryMessageFormatter ();
98                         q.Send ("foo1");
99                         q.Send ("foo2");
100                         
101                         MessageEnumerator me1 = q.GetMessageEnumerator ();
102                         MessageQueueTransaction tx = new MessageQueueTransaction ();
103                         me1.MoveNext ();
104                         Message m1 = me1.Current;
105                         me1.RemoveCurrent (tx);
106                         tx.Commit ();
107                         me1.Close ();
108                         
109                         MessageEnumerator me2 = q.GetMessageEnumerator ();
110                         Assert.IsTrue (me1.MoveNext ());
111                         me2.RemoveCurrent ();
112                         Assert.IsFalse (me2.MoveNext ());
113                 }
114         }
115 }