2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / Mono.Messaging.RabbitMQ / Test / Mono.Messaging.RabbitMQ / BasicMessagingTest.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 using System.Reflection;
34 using System.Threading;
35 using System.Text.RegularExpressions;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.Mono.Messaging.RabbitMQ
40 {
41         [TestFixture]
42         public class BasicMessageTest {
43
44                 [Test]
45                 public void SendReceiveBinaryMessage ()
46                 {
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 ();
55
56                         mq.Send (m);
57
58                         Message m2 = mq.Receive ();
59                         m2.Formatter = new BinaryMessageFormatter ();
60                         Assert.AreEqual (s, m2.Body);
61
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");
72                         
73                         mq.Close ();
74                 }
75                 
76                 [Test]
77                 public void SendMessageWithLabel ()
78                 {
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" ;
86
87                         mq.Send (m, label);
88
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");
93                 }
94                 
95                 
96                 [Test]
97                 public void CheckDefaults ()
98                 {
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");
112                 }
113                 
114                 private static void CheckInvalidOperation (Message m, String property)
115                 {
116                         PropertyInfo pi = m.GetType ().GetProperty (property);
117                         try {
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 ());
125                         }
126                 }
127                 
128                 [Test]
129                 public void CheckInvalidPropertyOperations ()
130                 {
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");
147                 }
148                 
149                 private static void CheckArgumentInvalid(Message m, String property, Type exceptionType)
150                 {
151                         PropertyInfo pi = m.GetType().GetProperty(property);
152                         try {
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(),
160                                                 property);
161                         }
162                 }
163
164                 [Test]
165                 public void CheckArgumentInvalidForProperties ()
166                 {
167                         Message m = new Message ("Stuff");
168                         CheckArgumentInvalid (m, "DestinationSymmetricKey", typeof (ArgumentNullException));
169                         CheckArgumentInvalid (m, "DigitalSignature", typeof(ArgumentNullException));
170                         CheckArgumentInvalid (m, "Extension", typeof(ArgumentNullException));
171                 }
172
173                 [Test]
174                 public void SendReceiveBinaryMessageWithAllPropertiesSet ()
175                 {
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");
181                         
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;
186                         
187                         Message m = new Message (s, new BinaryMessageFormatter ());
188                         m.CorrelationId = Guid.NewGuid () + "\\0";
189                         m.AcknowledgeType = AcknowledgeTypes.PositiveArrival;
190                         m.AdministrationQueue = adminQ;
191                         m.AppSpecific = 5;
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;
200                         m.Label = "MyLabel";
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;
210                         
211                         mq.Send (m);
212                         
213                         Message m2 = mq.Receive ();
214                         
215                         m2.Formatter = new BinaryMessageFormatter ();
216                         Assert.AreEqual (s, m2.Body);
217                         
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 ();
258                         
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);
264                         
265                         //Assert.IsNotNull (m2.SourceMachine, "SourceMachine is null");
266                         
267                 }
268                 
269                 private static void AreEqual(byte[] expected, byte[] actual, string message)
270                 {
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);
274                 }
275                 
276                 //[Test]
277                 // No supported by Rabbit
278                 public void SendPriorityMessages ()
279                 {
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;
285                         
286                         mq.Send (sent1);
287                         mq.Send (sent2);
288                         
289                         Message received1 = mq.Receive ();
290                         Message received2 = mq.Receive ();
291                         
292                         Assert.AreEqual (MessagePriority.Highest, received2.Priority,
293                                          "Priority delivery incorrect");
294                         Assert.AreEqual (MessagePriority.Lowest, received1.Priority,
295                                          "Priority delivery incorrect");
296                 }
297                 
298                 [Test]
299                 public void SendReceiveXmlMessage ()
300                 {
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();
305                    
306                         mq.Send (m);
307                         Message m2 = mq.Receive ();
308                         m2.Formatter = new XmlMessageFormatter (new Type[] { typeof (string) });
309                         Assert.AreEqual (s, m2.Body);
310                    
311                         Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
312                         Assert.IsNotNull (m2.ArrivedTime, "Acknowledgment");
313                         Assert.IsNotNull (m2.Id, "Id");
314                 }
315            
316                 [Test]
317                 public void SendBinaryText ()
318                 {
319                         string path = @".\private$\MyQ";
320                         string body = "This is a test";
321                    
322                         MessageQueue q = MQUtil.GetQueue (path);
323
324                         q.Formatter = new BinaryMessageFormatter ();
325
326                         q.Send (body);
327
328                         Message m2 = q.Receive ();
329                    
330                         Assert.IsNotNull (m2.Formatter, "Formatter is null");
331                         Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
332                         Assert.AreEqual (body, m2.Body);
333                 }
334
335                 [Test]
336                 public void SendDefaultText ()
337                 {
338                         string path = @".\private$\MyQ";
339                         string body = "This is a test";
340
341                         MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
342
343                         q.Send (body);
344
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);
351                 }
352
353                 [Test]
354                 public void SendBinaryObject ()
355                 {
356                         string path = @".\private$\MyQ";
357                         Thingy body = new Thingy ();
358                         body.MyProperty1 = 42;
359                         body.MyProperty2 = "Something";
360                         body.MyProperty3 = "Something else";
361
362                         MessageQueue q = MQUtil.GetQueue (path);
363
364                         q.Formatter = new BinaryMessageFormatter ();
365
366                         q.Send (body);
367                    
368                         Message m2 = q.Receive ();
369                         Thingy body2 = (Thingy) m2.Body;
370
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);
376                 }
377
378                 [Test]
379                 public void SendDefaultObject ()
380                 {
381                         string path = @".\private$\MyQ";
382                         Thingy body = new Thingy();
383                         body.MyProperty1 = 42;
384                         body.MyProperty2 = "Something";
385                         body.MyProperty3 = "Something else";
386
387                         MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
388
389                         q.Send (body);
390
391                         MessageQueue q2 = MQUtil.GetQueue (path);
392                         q2.Formatter = new XmlMessageFormatter (new Type[] { typeof(Thingy) });
393
394                         Message m2 = q2.Receive ();
395                         Thingy body2 = (Thingy) m2.Body;
396
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);
401                 }
402
403                 [Test]
404                 public void SendBinaryMessage ()
405                 {
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 ();
413
414                         MessageQueue q = MQUtil.GetQueue (path);
415
416                         q.Send (m1);
417
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);
423
424                         Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
425                         Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
426                         Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
427                 }
428
429                 [Test]
430                 public void SendDefaultMessage ()
431                 {
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);
439
440                         MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
441
442                         q.Send (m1);
443
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);
449
450                         Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
451                         Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
452                         Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
453                 }                         
454         }
455
456         [Serializable]
457         public class Thingy
458         {
459                 private int myVar1;
460
461                 public int MyProperty1
462                 {
463                         get { return myVar1; }
464                         set { myVar1 = value; }
465                 }
466
467                 private string myVar2;
468
469                 public string MyProperty2
470                 {
471                         get { return myVar2; }
472                         set { myVar2 = value; }
473                 }
474
475                 private string myVar3;
476
477                 public string MyProperty3
478                 {
479                         get { return myVar3; }
480                         set { myVar3 = value; }
481                 }
482         }
483 }