2009-09-22 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Messaging / Test / System.Messaging / 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 NUnit.Framework;
35
36 namespace MonoTests.System.Messaging
37 {
38         [TestFixture]
39         public class MessageEnumeratorTest {
40                 
41                 string qName;
42                 
43                 [SetUp]
44                 public void SetUp()
45                 {
46                         qName = MQUtil.CreateQueueName ();
47                 }
48                 
49                 private void SendMessage (string s) {
50                         MessageQueue mq = MQUtil.GetQueue (qName);
51                         Message m = new Message (s, new BinaryMessageFormatter ());
52                         m.CorrelationId = Guid.NewGuid () + "\\0";
53                         mq.Send (m);
54                 }
55                 
56                 [Test]
57                 public void RemoveMessage ()
58                 {
59                         SendMessage ("message 1");
60                         SendMessage ("message 2");
61                         SendMessage ("message 3");
62                         SendMessage ("message 4");
63                         
64                         MessageQueue mq0 = MQUtil.GetQueue (qName);
65                         MessageEnumerator me0 = mq0.GetMessageEnumerator ();
66                         
67                         me0.MoveNext ();
68                         me0.MoveNext ();
69                         me0.MoveNext ();
70                         
71                         Message m0 = me0.RemoveCurrent ();
72                         
73                         me0.MoveNext ();
74                         
75                         me0.Dispose ();
76                         mq0.Dispose ();
77                         
78                         MessageQueue mq1 = MQUtil.GetQueue (qName);
79                         MessageEnumerator me1 = mq1.GetMessageEnumerator ();
80                         
81                         me1.MoveNext();
82                         me1.MoveNext();
83                         me1.MoveNext();
84                         
85                         Message m1 = me1.Current;
86                         m1.Formatter = new BinaryMessageFormatter ();
87                         Assert.AreEqual ("message 4", (String) m1.Body, "body incorrect");
88                         
89                         mq1.Purge ();
90                         MessageQueue.Delete (qName);
91                 }
92                 
93                 [Test]
94                 public void RemoveMessageWithTimeout ()
95                 {
96                         SendMessage ("message 1");
97                         SendMessage ("message 2");
98                         SendMessage ("message 3");
99                         SendMessage ("message 4");
100                         
101                         MessageQueue mq0 = MQUtil.GetQueue (qName);
102                         MessageEnumerator me0 = mq0.GetMessageEnumerator ();
103                         
104                         TimeSpan ts = new TimeSpan (0, 0, 2);
105                         
106                         me0.MoveNext (ts);
107                         me0.MoveNext (ts);
108                         me0.MoveNext (ts);
109                         
110                         Message m0 = me0.RemoveCurrent (ts);
111                         
112                         me0.MoveNext (ts);
113                         
114                         me0.Dispose ();
115                         mq0.Dispose ();
116                         
117                         MessageQueue mq1 = MQUtil.GetQueue (qName);
118                         MessageEnumerator me1 = mq1.GetMessageEnumerator ();
119                         
120                         me1.MoveNext (ts);
121                         me1.MoveNext (ts);
122                         me1.MoveNext (ts);
123                         
124                         Message m1 = me1.Current;
125                         m1.Formatter = new BinaryMessageFormatter ();
126                         Assert.AreEqual ("message 4", (String) m1.Body, "body incorrect");
127                         
128                         mq1.Purge ();
129                         MessageQueue.Delete (qName);
130                 }
131                 
132                 //[Test]
133                 // Not supported with AMQP
134                 public void RemoveMessageWithTx ()
135                 {
136                         MessageQueue q = MQUtil.GetQueue (qName);
137                         
138                         q.Formatter = new BinaryMessageFormatter ();
139                         q.Send ("foo1");
140                         q.Send ("foo2");
141                         
142                         MessageEnumerator me1 = q.GetMessageEnumerator ();
143                         MessageQueueTransaction tx = new MessageQueueTransaction ();
144                         me1.MoveNext ();
145                         Message m1 = me1.Current;
146                         me1.RemoveCurrent (tx);
147                         tx.Commit ();
148                         me1.Close ();
149                         
150                         MessageEnumerator me2 = q.GetMessageEnumerator ();
151                         Assert.IsTrue (me1.MoveNext ());
152                         me2.RemoveCurrent ();
153                         Assert.IsFalse (me2.MoveNext ());
154                 }
155         }
156 }