Move tests that do not reference RabbitMQ to System.Messaging.
[mono.git] /
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.Mono.Messaging.RabbitMQ
37 {
38         [TestFixture]
39         public class PeekTest {
40                 
41                 [Test]
42                 public void PeekMessage ()
43                 {
44                         String body = "foo-" + DateTime.Now.ToString ();
45                         Message s1 = new Message(body, new BinaryMessageFormatter());
46                         MessageQueue mq = MQUtil.GetQueue (@".\private$\peek-queue-1");
47                         mq.Send (s1);
48                         
49                         Message r1 = mq.Peek ();
50                         Assert.AreEqual (body, r1.Body);
51                         
52                         Message r2 = mq.Receive ();
53                         Assert.AreEqual (body, r2.Body);
54                 }
55                 
56                 [Test]
57                 public void PeekMessageWithTimeout ()
58                 {
59                         String body = "foo-" + DateTime.Now.ToString();
60                         Message s1 = new Message(body, new BinaryMessageFormatter());
61                         MessageQueue mq = MQUtil.GetQueue(@".\private$\peek-queue-2");
62                         mq.Send (s1);
63                         
64                         Message r1 = mq.Peek (new TimeSpan (0, 0, 2));
65                         Assert.AreEqual (body, r1.Body);
66                         
67                         Message r2 = mq.Receive ();
68                         Assert.AreEqual (body, r2.Body);
69                 }
70                 
71                 [Test]
72                 [ExpectedException (typeof (MessageQueueException))]
73                 public void PeekNoMessageWithTimeout ()
74                 {
75                         MessageQueue mq = MQUtil.GetQueue(@".\private$\peek-queue-3");
76                         Message r1 = mq.Peek (new TimeSpan (0, 0, 2));
77                 }
78                 
79                 [Test]
80                 public void PeekById ()
81                 {
82                         String body = "Foo-" + DateTime.Now.ToString ();
83                         Message s1 = new Message (body, new BinaryMessageFormatter());
84                         MessageQueue q = MQUtil.GetQueue (@".\private$\peek-queue-4");
85                         q.Send (s1);
86                         
87                         String id = s1.Id;
88                         try {
89                                 Message r1 = q.PeekById (id);
90                                 Assert.AreEqual (body, r1.Body, "Unable to PeekById correctly");
91                         } finally {
92                                 q.Purge ();
93                         }
94                 }
95                 
96                 [Test]
97                 public void PeekByIdWithTimeout ()
98                 {
99                         String body = "Foo-" + DateTime.Now.ToString ();
100                         Message s1 = new Message (body, new BinaryMessageFormatter());
101                         MessageQueue q = MQUtil.GetQueue (@".\private$\peek-queue-5");
102                         q.Send (s1);
103                         
104                         String id = s1.Id;
105                         try {
106                                 Message r1 = q.PeekById (id, new TimeSpan (0, 0, 2));
107                                 Assert.AreEqual (body, r1.Body, "Unable to PeekById correctly");
108                         } finally {
109                                 q.Purge ();
110                         }
111                 }
112                 
113                 [Test]
114                 [ExpectedException (typeof (InvalidOperationException))]
115                 public void PeekByIdNotFound ()
116                 {
117                         String body = "Foo-" + DateTime.Now.ToString ();
118                         Message s1 = new Message (body, new BinaryMessageFormatter());
119                         MessageQueue q = MQUtil.GetQueue (@".\private$\peek-queue-6");
120                         q.Send (s1);
121                         
122                         String id = "fail!";
123                         
124                         try {
125                                 Message r1 = q.PeekById (id);
126                         } finally {
127                                 q.Purge ();
128                         }
129                 }
130                 
131                 [Test]
132                 public void PeekByCorrelationId ()
133                 {
134                         String correlationId = Guid.NewGuid () + "\\0";
135                         String body = "Foo-" + DateTime.Now.ToString ();
136                         Message s1 = new Message (body, new BinaryMessageFormatter());
137                         s1.CorrelationId = correlationId;
138                         MessageQueue q = MQUtil.GetQueue (@".\private$\peek-queue-7");
139                         q.Formatter = new BinaryMessageFormatter ();
140                         q.Send (s1);
141                         
142                         try {
143                                 Message r1 = q.PeekByCorrelationId (correlationId);
144                                 Assert.AreEqual (body, r1.Body, "Unable to PeekByCorrelationId correctly");
145                         } finally {
146                                 q.Purge ();
147                         }
148                 }
149                 
150                 [Test]
151                 [ExpectedException (typeof (InvalidOperationException))]
152                 public void PeekByCorrelationIdNotFound ()
153                 {
154                         String body = "Foo-" + DateTime.Now.ToString ();
155                         Message s1 = new Message (body);
156                         String correlationId = Guid.NewGuid() + "\\0";
157                         MessageQueue q = MQUtil.GetQueue(@".\private$\peek-queue-8");
158                         q.Formatter = new BinaryMessageFormatter ();
159                         q.Send (s1);
160                         
161                         try {
162                                 Message r1 = q.PeekByCorrelationId ("fail!");
163                         } finally {
164                                 q.Purge ();
165                         }
166                 }
167                 
168                 [Test]
169                 public void PeekByCorrelationIdWithTimeout ()
170                 {
171                         String correlationId = Guid.NewGuid () + "\\0";
172                         String body = "Foo-" + DateTime.Now.ToString ();
173                         Message s1 = new Message (body, new BinaryMessageFormatter());
174                         s1.CorrelationId = correlationId;
175                         MessageQueue q = MQUtil.GetQueue (@".\private$\peek-queue-9");
176                         q.Formatter = new BinaryMessageFormatter ();
177                         q.Send (s1);
178                         
179                         try {
180                                 Message r1 = q.PeekByCorrelationId (correlationId, new TimeSpan (0, 0, 2));
181                                 Assert.AreEqual (body, r1.Body, "Unable to PeekByCorrelationId correctly");
182                         } finally {
183                                 q.Purge ();
184                         }
185                 }               
186         }
187 }