2 // Test.Mono.Messaging.RabbitMQ
5 // Michael Barker (mike@middlesoft.co.uk)
7 // (C) 2008 Michael Barker
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Messaging;
33 using System.Reflection;
34 using System.Threading;
35 using System.Text.RegularExpressions;
37 using NUnit.Framework;
39 namespace MonoTests.Mono.Messaging.RabbitMQ
42 public class BasicMessageTest {
45 public void SendReceiveBinaryMessage ()
47 String qName = @"private$\testq";
48 String qPath = @".\" + qName;
49 MessageQueue mq = MQUtil.GetQueue (qPath);
50 Assert.AreEqual(qName, mq.QueueName, "Queue name not set properly");
51 String s = "Test: " + DateTime.Now;
52 Message m = new Message (s, new BinaryMessageFormatter ());
53 m.CorrelationId = Guid.NewGuid () + "\\0";
54 mq.MessageReadPropertyFilter.SetAll ();
58 Message m2 = mq.Receive ();
59 m2.Formatter = new BinaryMessageFormatter ();
60 Assert.AreEqual (s, m2.Body);
62 //Assert.IsTrue (DateTime.MinValue == m.ArrivedTime);
63 Assert.IsNotNull(m2.Id, "Id is null");
64 Assert.IsTrue (Guid.Empty.ToString () != m2.Id, "Id is Empty");
65 Assert.IsTrue (DateTime.MinValue != m2.ArrivedTime, "Arrived Time is not set");
66 Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
67 Assert.AreEqual (m.CorrelationId, m2.CorrelationId, "CorrelationId not set properly");
68 Assert.IsTrue (0 != m2.SenderVersion);
69 // TODO: This is not support on a workgroup installation.
70 //Assert.IsNotNull (m2.SourceMachine, "SourceMachine is null");
71 Assert.AreEqual (qName, m2.DestinationQueue.QueueName, "Destination Queue not set");
77 public void SendMessageWithLabel ()
79 String qName = @".\private$\testq";
80 String label = "mylabel";
81 MessageQueue mq = MQUtil.GetQueue (qName);
82 Assert.AreEqual (@"private$\testq", mq.QueueName, "Queue name not set properly");
83 String s = "Test: " + DateTime.Now;
84 Message m = new Message (s, new BinaryMessageFormatter ());
85 m.CorrelationId = Guid.NewGuid () + "\\0" ;
89 Message m2 = mq.Receive ();
90 m2.Formatter = new BinaryMessageFormatter ();
91 Assert.AreEqual (s, m2.Body, "Message not passed correctly");
92 Assert.AreEqual (label, m2.Label, "Label not passed correctly");
97 public void CheckDefaults ()
99 Message m = new Message ("Test", new BinaryMessageFormatter ());
100 Assert.AreEqual (true, m.AttachSenderId, "AttachSenderId has incorrect default");
101 Assert.AreEqual (Guid.Empty.ToString () + "\\0", m.Id, "Id has incorrect default");
102 Assert.AreEqual ("Microsoft Base Cryptographic Provider, Ver. 1.0",
103 m.AuthenticationProviderName,
104 "AuthenticationProviderName has incorrect default");
105 Assert.AreEqual (0, m.Extension.Length, "Extension has incorrect default");
106 Assert.AreEqual ("", m.Label, "Label has incorrect default");
107 Assert.IsFalse (m.Recoverable, "Recoverable has incorrect default");
108 Assert.IsFalse (m.IsFirstInTransaction, "IsFirstInTransaction has incorrect default");
109 Assert.IsFalse (m.IsLastInTransaction, "IsLastInTransaction has incorrect default");
110 Assert.AreEqual ("", m.TransactionId, "TransactionId has incorrect default");
111 Assert.AreEqual (MessagePriority.Normal, m.Priority, "MessagePriority has incorrect default");
114 private static void CheckInvalidOperation (Message m, String property)
116 PropertyInfo pi = m.GetType ().GetProperty (property);
118 Assert.IsNotNull (pi, "Property not defined: " + property);
119 object o = pi.GetValue (m, null);
120 Assert.Fail (property + ": " + o);
121 } catch (InvalidOperationException e) {
122 } catch (TargetInvocationException e) {
123 Assert.AreEqual (typeof (InvalidOperationException),
124 e.InnerException.GetType ());
129 public void CheckInvalidPropertyOperations ()
131 Message m = new Message ("Test", new BinaryMessageFormatter ());
132 CheckInvalidOperation (m, "Acknowledgment");
133 CheckInvalidOperation (m, "ArrivedTime");
134 CheckInvalidOperation (m, "Authenticated");
135 CheckInvalidOperation (m, "DestinationQueue");
136 //CheckInvalidOperation (m, "Id");
137 //CheckInvalidOperation (m, "IsFirstInTransaction");
138 //CheckInvalidOperation (m, "IsLastInTransaction");
139 // TODO: Support 2.0 features.
140 //CheckInvalidOperation (m, "LookupId");
141 CheckInvalidOperation (m, "MessageType");
142 CheckInvalidOperation (m, "SenderId");
143 CheckInvalidOperation (m, "SenderVersion");
144 CheckInvalidOperation (m, "SentTime");
145 CheckInvalidOperation (m, "SourceMachine");
146 //CheckInvalidOperation (m, "TransactionId");
149 private static void CheckArgumentInvalid(Message m, String property, Type exceptionType)
151 PropertyInfo pi = m.GetType().GetProperty(property);
153 Assert.IsNotNull(pi, "Property not defined: " + property);
154 pi.SetValue(m, null, null);
155 Assert.Fail(property);
156 } catch (InvalidOperationException e) {
157 } catch (TargetInvocationException e) {
158 Assert.AreEqual(exceptionType,
159 e.InnerException.GetType(),
165 public void CheckArgumentInvalidForProperties ()
167 Message m = new Message ("Stuff");
168 CheckArgumentInvalid (m, "DestinationSymmetricKey", typeof (ArgumentNullException));
169 CheckArgumentInvalid (m, "DigitalSignature", typeof(ArgumentNullException));
170 CheckArgumentInvalid (m, "Extension", typeof(ArgumentNullException));
174 public void SendReceiveBinaryMessageWithAllPropertiesSet ()
176 String qName = @"private$\testq";
177 String qPath = @".\" + qName;
178 MessageQueue mq = MQUtil.GetQueue (qPath);
179 mq.MessageReadPropertyFilter.SetAll ();
180 Assert.AreEqual(qName, mq.QueueName, "Queue name not set properly");
182 MessageQueue adminQ = MQUtil.GetQueue (@".\private$\myadmin");
183 MessageQueue responseQ = MQUtil.GetQueue (@".\private$\myresponse");
184 Guid connectorType = Guid.NewGuid ();
185 String s = "Test: " + DateTime.Now;
187 Message m = new Message (s, new BinaryMessageFormatter ());
188 m.CorrelationId = Guid.NewGuid () + "\\0";
189 m.AcknowledgeType = AcknowledgeTypes.PositiveArrival;
190 m.AdministrationQueue = adminQ;
192 //m.AuthenticationProviderName = "Test Provider Name";
193 //m.AuthenticationProviderType = CryptographicProviderType.None;
194 //m.ConnectorType = connectorType;
195 //m.DestinationSymmetricKey = new byte[] { 0x0A, 0x0B, 0x0C };
196 //m.DigitalSignature = new byte[] { 0x0C, 0x0D, 0x0E };
197 //m.EncryptionAlgorithm = EncryptionAlgorithm.Rc4;
198 m.Extension = new byte[] { 0x01, 0x02, 0x03 };
199 //m.HashAlgorithm = HashAlgorithm.Sha;
201 m.Priority = MessagePriority.AboveNormal;
202 m.Recoverable = true;
203 m.ResponseQueue = responseQ;
204 m.SenderCertificate = new byte[] { 0x04, 0x05, 0x06 };
205 m.TimeToBeReceived = new TimeSpan(0, 0, 10);
206 m.TimeToReachQueue = new TimeSpan(0, 0, 5);
207 //m.UseAuthentication = true;
208 m.UseDeadLetterQueue = true;
209 //m.UseEncryption = true;
213 Message m2 = mq.Receive ();
215 m2.Formatter = new BinaryMessageFormatter ();
216 Assert.AreEqual (s, m2.Body);
218 Assert.AreEqual (AcknowledgeTypes.PositiveArrival, m2.AcknowledgeType,
219 "AcknowledgeType not passed correctly");
220 Assert.AreEqual (adminQ.QueueName, m2.AdministrationQueue.QueueName,
221 "AdministrationQueue not passed correctly");
222 Assert.AreEqual (5, m2.AppSpecific, "AppSpecific not passed correctly");
223 //Assert.AreEqual (m.AuthenticationProviderName, m2.AuthenticationProviderName,
224 // "AuthenticationProviderName not passed correctly");
225 //Assert.AreEqual (m.AuthenticationProviderType, m2.AuthenticationProviderType,
226 // "AuthenticationProviderType not passed correctly");
227 //Assert.AreEqual (connectorType, m2.ConnectorType,
228 // "ConnectorType not passed correctly");
229 Assert.AreEqual (m.CorrelationId, m2.CorrelationId,
230 "CorrelationId not passed correctly");
231 //AreEqual (m.DestinationSymmetricKey, m2.DestinationSymmetricKey,
232 // "DestinationSymmetricKey not passed correctly");
233 //AreEqual (m.DigitalSignature, m2.DigitalSignature,
234 // "DigitalSignature not passed properly");
235 //Assert.AreEqual (EncryptionAlgorithm.Rc4, m2.EncryptionAlgorithm,
236 // "EncryptionAlgorithm not passed properly");
237 AreEqual (m.Extension, m2.Extension, "Extension not passed properly");
238 //Assert.AreEqual (m.HashAlgorithm, m2.HashAlgorithm,
239 // "HashAlgorithm not passed properly");
240 Assert.AreEqual (m.Label, m2.Label, "Label not passed correctly");
241 Assert.AreEqual (MessagePriority.AboveNormal, m2.Priority,
242 "Priority not passed properly");
243 Assert.AreEqual (true, m2.Recoverable, "Recoverable not passed properly");
244 Assert.AreEqual (responseQ.QueueName, m2.ResponseQueue.QueueName,
245 "ResponseQueue not passed properly");
246 AreEqual (m.SenderCertificate, m2.SenderCertificate,
247 "SenderCertificate not passed properly");
248 Assert.AreEqual (m.TimeToBeReceived, m2.TimeToBeReceived,
249 "TimeToBeReceived not passed properly");
250 Assert.AreEqual (m.TimeToReachQueue, m2.TimeToReachQueue,
251 "TimeToReachQueue not passed properly");
252 //Assert.IsTrue (m2.UseAuthentication,
253 // "UseAuthentication not passed properly");
254 Assert.IsTrue (m2.UseDeadLetterQueue,
255 "UseDeadLetterQueue not passed properly");
256 //Assert.IsTrue (m2.UseEncryption, "UseEncryption not pass properly");
257 //Assert.AreEqual ();
259 Assert.IsNotNull (m2.Id, "Id is null");
260 Assert.IsTrue (Guid.Empty.ToString () != m2.Id, "Id is Empty");
261 Assert.IsTrue (DateTime.MinValue != m2.ArrivedTime, "Arrived Time is not set");
262 Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
263 Assert.IsTrue (0 != m2.SenderVersion);
265 //Assert.IsNotNull (m2.SourceMachine, "SourceMachine is null");
269 private static void AreEqual(byte[] expected, byte[] actual, string message)
271 Assert.AreEqual (expected.Length, actual.Length, message);
272 for (int i = 0; i < expected.Length; i++)
273 Assert.AreEqual (expected[i], actual[i], message);
277 // No supported by Rabbit
278 public void SendPriorityMessages ()
280 MessageQueue mq = MQUtil.GetQueue ("testpriority");
281 Message sent1 = new Message ("Highest", new BinaryMessageFormatter ());
282 sent1.Priority = MessagePriority.Highest;
283 Message sent2 = new Message ("Lowest", new BinaryMessageFormatter ());
284 sent2.Priority = MessagePriority.Lowest;
289 Message received1 = mq.Receive ();
290 Message received2 = mq.Receive ();
292 Assert.AreEqual (MessagePriority.Highest, received2.Priority,
293 "Priority delivery incorrect");
294 Assert.AreEqual (MessagePriority.Lowest, received1.Priority,
295 "Priority delivery incorrect");
299 public void SendReceiveXmlMessage ()
301 MessageQueue mq = MQUtil.GetQueue (@".\private$\testq");
302 String s = "Test: " + DateTime.Now;
303 Message m = new Message (s, new XmlMessageFormatter (new Type[] { typeof (string) }));
304 mq.MessageReadPropertyFilter.SetAll();
307 Message m2 = mq.Receive ();
308 m2.Formatter = new XmlMessageFormatter (new Type[] { typeof (string) });
309 Assert.AreEqual (s, m2.Body);
311 Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
312 Assert.IsNotNull (m2.ArrivedTime, "Acknowledgment");
313 Assert.IsNotNull (m2.Id, "Id");
317 public void SendBinaryText ()
319 string path = @".\private$\MyQ";
320 string body = "This is a test";
322 MessageQueue q = MQUtil.GetQueue (path);
324 q.Formatter = new BinaryMessageFormatter ();
328 Message m2 = q.Receive ();
330 Assert.IsNotNull (m2.Formatter, "Formatter is null");
331 Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
332 Assert.AreEqual (body, m2.Body);
336 public void SendDefaultText ()
338 string path = @".\private$\MyQ";
339 string body = "This is a test";
341 MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
345 Message m2 = q.Receive ();
346 XmlMessageFormatter xmlf = (XmlMessageFormatter) q.Formatter;
347 //Assert.AreEqual (typeof (string), xmlf.TargetTypes[0]);
348 Assert.AreEqual (typeof (XmlMessageFormatter), m2.Formatter.GetType ());
349 Assert.AreEqual (body, m2.Body);
350 Assert.AreEqual (0, m2.BodyType);
354 public void SendBinaryObject ()
356 string path = @".\private$\MyQ";
357 Thingy body = new Thingy ();
358 body.MyProperty1 = 42;
359 body.MyProperty2 = "Something";
360 body.MyProperty3 = "Something else";
362 MessageQueue q = MQUtil.GetQueue (path);
364 q.Formatter = new BinaryMessageFormatter ();
368 Message m2 = q.Receive ();
369 Thingy body2 = (Thingy) m2.Body;
371 Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
372 Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
373 Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
374 Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
375 Assert.AreEqual (768, m2.BodyType);
379 public void SendDefaultObject ()
381 string path = @".\private$\MyQ";
382 Thingy body = new Thingy();
383 body.MyProperty1 = 42;
384 body.MyProperty2 = "Something";
385 body.MyProperty3 = "Something else";
387 MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
391 MessageQueue q2 = MQUtil.GetQueue (path);
392 q2.Formatter = new XmlMessageFormatter (new Type[] { typeof(Thingy) });
394 Message m2 = q2.Receive ();
395 Thingy body2 = (Thingy) m2.Body;
397 Assert.AreEqual (typeof (XmlMessageFormatter), m2.Formatter.GetType ());
398 Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
399 Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
400 Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
404 public void SendBinaryMessage ()
406 string path = @".\private$\MyQ";
407 Thingy body = new Thingy ();
408 body.MyProperty1 = 42;
409 body.MyProperty2 = "Something";
410 body.MyProperty3 = "Something else";
411 Message m1 = new Message (body);
412 m1.Formatter = new BinaryMessageFormatter ();
414 MessageQueue q = MQUtil.GetQueue (path);
418 Message m2 = q.Receive ();
419 m2.Formatter = new BinaryMessageFormatter ();
420 Assert.IsNotNull (m2.Formatter);
421 Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
422 Thingy body2 = (Thingy) m2.Formatter.Read (m2);
424 Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
425 Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
426 Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
430 public void SendDefaultMessage ()
432 string path = @".\private$\MyQ";
433 Thingy body = new Thingy ();
434 body.MyProperty1 = 42;
435 body.MyProperty2 = "Something";
436 body.MyProperty3 = "Something else";
437 Message m1 = new Message (body);
438 Assert.IsNull (m1.Formatter);
440 MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
444 Message m2 = q.Receive ();
445 m2.Formatter = new XmlMessageFormatter (new Type[] { typeof (Thingy) });
446 Assert.IsNotNull (m2.Formatter);
447 Assert.AreEqual (typeof (XmlMessageFormatter), m2.Formatter.GetType ());
448 Thingy body2 = (Thingy) m2.Formatter.Read (m2);
450 Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
451 Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
452 Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
461 public int MyProperty1
463 get { return myVar1; }
464 set { myVar1 = value; }
467 private string myVar2;
469 public string MyProperty2
471 get { return myVar2; }
472 set { myVar2 = value; }
475 private string myVar3;
477 public string MyProperty3
479 get { return myVar3; }
480 set { myVar3 = value; }