2009-05-23 Michael Barker <mike@middlesoft.co.uk>
[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                         Assert.AreEqual ("message 4", (String) m1.Body, "body incorrect");
85                         
86                         mq1.Purge ();
87                         MessageQueue.Delete (qName);
88                 }
89                 
90                 [Test]
91                 public void RemoveMessageWithTimeout ()
92                 {
93                         SendMessage ("message 1");
94                         SendMessage ("message 2");
95                         SendMessage ("message 3");
96                         SendMessage ("message 4");
97                         
98                         MessageQueue mq0 = MQUtil.GetQueue (qName);
99                         MessageEnumerator me0 = mq0.GetMessageEnumerator ();
100                         
101                         TimeSpan ts = new TimeSpan (0, 0, 2);
102                         
103                         me0.MoveNext (ts);
104                         me0.MoveNext (ts);
105                         me0.MoveNext (ts);
106                         
107                         Message m0 = me0.RemoveCurrent (ts);
108                         
109                         me0.MoveNext (ts);
110                         
111                         me0.Dispose ();
112                         mq0.Dispose ();
113                         
114                         MessageQueue mq1 = MQUtil.GetQueue (qName);
115                         MessageEnumerator me1 = mq1.GetMessageEnumerator ();
116                         
117                         me1.MoveNext (ts);
118                         me1.MoveNext (ts);
119                         me1.MoveNext (ts);
120                         
121                         Message m1 = me1.Current;
122                         m1.Formatter = new BinaryMessageFormatter ();
123                         Assert.AreEqual ("message 4", (String) m1.Body, "body incorrect");
124                         
125                         mq1.Purge ();
126                         MessageQueue.Delete (qName);
127                 }
128                 
129                 //[Test]
130                 // Not supported with AMQP
131                 public void RemoveMessageWithTx ()
132                 {
133                         MessageQueue q = MQUtil.GetQueue ("testq3");
134                         
135                         q.Formatter = new BinaryMessageFormatter ();
136                         q.Send ("foo1");
137                         q.Send ("foo2");
138                         
139                         MessageEnumerator me1 = q.GetMessageEnumerator ();
140                         MessageQueueTransaction tx = new MessageQueueTransaction ();
141                         me1.MoveNext ();
142                         Message m1 = me1.Current;
143                         me1.RemoveCurrent (tx);
144                         tx.Commit ();
145                         me1.Close ();
146                         
147                         MessageEnumerator me2 = q.GetMessageEnumerator ();
148                         Assert.IsTrue (me1.MoveNext ());
149                         me2.RemoveCurrent ();
150                         Assert.IsFalse (me2.MoveNext ());
151                 }
152         }
153 }