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.System.Messaging
42 public class BasicMessageTest {
45 public void SendReceiveBinaryMessage ()
47 MessageQueue mq = MQUtil.GetQueue ();
48 String s = "Test: " + DateTime.Now;
49 Message m = new Message (s, new BinaryMessageFormatter ());
50 m.CorrelationId = Guid.NewGuid () + "\\0";
51 mq.MessageReadPropertyFilter.SetAll ();
55 Message m2 = mq.Receive ();
56 m2.Formatter = new BinaryMessageFormatter ();
57 Assert.AreEqual (s, m2.Body);
59 //Assert.IsTrue (DateTime.MinValue == m.ArrivedTime);
60 Assert.IsNotNull(m2.Id, "Id is null");
61 Assert.IsTrue (Guid.Empty.ToString () != m2.Id, "Id is Empty");
62 Assert.IsTrue (DateTime.MinValue != m2.ArrivedTime, "Arrived Time is not set");
63 Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
64 Assert.AreEqual (m.CorrelationId, m2.CorrelationId, "CorrelationId not set properly");
65 Assert.IsTrue (0 != m2.SenderVersion);
66 // TODO: This is not supported on a workgroup installation.
67 //Assert.IsNotNull (m2.SourceMachine, "SourceMachine is null");
68 Assert.AreEqual (mq.QueueName, m2.DestinationQueue.QueueName, "Destination Queue not set");
74 public void SendMessageWithLabel ()
76 MessageQueue mq = MQUtil.GetQueue ();
77 String label = "mylabel";
78 String s = "Test: " + DateTime.Now;
79 Message m = new Message (s, new BinaryMessageFormatter ());
80 m.CorrelationId = Guid.NewGuid () + "\\0" ;
84 Message m2 = mq.Receive ();
85 m2.Formatter = new BinaryMessageFormatter ();
86 Assert.AreEqual (s, m2.Body, "Message not passed correctly");
87 Assert.AreEqual (label, m2.Label, "Label not passed correctly");
92 public void CheckDefaults ()
94 Message m = new Message ("Test", new BinaryMessageFormatter ());
95 Assert.AreEqual (true, m.AttachSenderId, "AttachSenderId has incorrect default");
96 Assert.AreEqual (Guid.Empty.ToString () + "\\0", m.Id, "Id has incorrect default");
97 Assert.AreEqual ("Microsoft Base Cryptographic Provider, Ver. 1.0",
98 m.AuthenticationProviderName,
99 "AuthenticationProviderName has incorrect default");
100 Assert.AreEqual (0, m.Extension.Length, "Extension has incorrect default");
101 Assert.AreEqual ("", m.Label, "Label has incorrect default");
102 Assert.IsFalse (m.Recoverable, "Recoverable has incorrect default");
103 Assert.IsFalse (m.IsFirstInTransaction, "IsFirstInTransaction has incorrect default");
104 Assert.IsFalse (m.IsLastInTransaction, "IsLastInTransaction has incorrect default");
105 Assert.AreEqual ("", m.TransactionId, "TransactionId has incorrect default");
106 Assert.AreEqual (MessagePriority.Normal, m.Priority, "MessagePriority has incorrect default");
109 private static void CheckInvalidOperation (Message m, String property)
111 PropertyInfo pi = m.GetType ().GetProperty (property);
113 Assert.IsNotNull (pi, "Property not defined: " + property);
114 object o = pi.GetValue (m, null);
115 Assert.Fail (property + ": " + o);
116 } catch (InvalidOperationException) {
117 } catch (TargetInvocationException e) {
118 Assert.AreEqual (typeof (InvalidOperationException),
119 e.InnerException.GetType ());
124 public void CheckInvalidPropertyOperations ()
126 Message m = new Message ("Test", new BinaryMessageFormatter ());
127 CheckInvalidOperation (m, "Acknowledgment");
128 CheckInvalidOperation (m, "ArrivedTime");
129 CheckInvalidOperation (m, "Authenticated");
130 CheckInvalidOperation (m, "DestinationQueue");
131 //CheckInvalidOperation (m, "Id");
132 //CheckInvalidOperation (m, "IsFirstInTransaction");
133 //CheckInvalidOperation (m, "IsLastInTransaction");
134 // TODO: Support 2.0 features.
135 //CheckInvalidOperation (m, "LookupId");
136 CheckInvalidOperation (m, "MessageType");
137 CheckInvalidOperation (m, "SenderId");
138 CheckInvalidOperation (m, "SenderVersion");
139 CheckInvalidOperation (m, "SentTime");
140 CheckInvalidOperation (m, "SourceMachine");
141 //CheckInvalidOperation (m, "TransactionId");
144 private static void CheckArgumentInvalid(Message m, String property, Type exceptionType)
146 PropertyInfo pi = m.GetType().GetProperty(property);
148 Assert.IsNotNull(pi, "Property not defined: " + property);
149 pi.SetValue(m, null, null);
150 Assert.Fail(property);
151 } catch (InvalidOperationException) {
152 } catch (TargetInvocationException e) {
153 Assert.AreEqual(exceptionType,
154 e.InnerException.GetType(),
160 public void CheckArgumentInvalidForProperties ()
162 Message m = new Message ("Stuff");
163 CheckArgumentInvalid (m, "DestinationSymmetricKey", typeof (ArgumentNullException));
164 CheckArgumentInvalid (m, "DigitalSignature", typeof(ArgumentNullException));
165 CheckArgumentInvalid (m, "Extension", typeof(ArgumentNullException));
169 public void SendReceiveBinaryMessageWithAllPropertiesSet ()
171 MessageQueue mq = MQUtil.GetQueue ();
172 mq.MessageReadPropertyFilter.SetAll ();
174 MessageQueue adminQ = MQUtil.GetQueue (@".\private$\myadmin");
175 MessageQueue responseQ = MQUtil.GetQueue (@".\private$\myresponse");
176 Guid connectorType = Guid.NewGuid ();
177 String s = "Test: " + DateTime.Now;
179 Message m = new Message (s, new BinaryMessageFormatter ());
180 m.CorrelationId = Guid.NewGuid () + "\\0";
181 m.AcknowledgeType = AcknowledgeTypes.PositiveArrival;
182 m.AdministrationQueue = adminQ;
184 //m.AuthenticationProviderName = "Test Provider Name";
185 //m.AuthenticationProviderType = CryptographicProviderType.None;
186 //m.ConnectorType = connectorType;
187 //m.DestinationSymmetricKey = new byte[] { 0x0A, 0x0B, 0x0C };
188 //m.DigitalSignature = new byte[] { 0x0C, 0x0D, 0x0E };
189 //m.EncryptionAlgorithm = EncryptionAlgorithm.Rc4;
190 m.Extension = new byte[] { 0x01, 0x02, 0x03 };
191 //m.HashAlgorithm = HashAlgorithm.Sha;
193 m.Priority = MessagePriority.AboveNormal;
194 m.Recoverable = true;
195 m.ResponseQueue = responseQ;
196 m.SenderCertificate = new byte[] { 0x04, 0x05, 0x06 };
197 m.TimeToBeReceived = new TimeSpan(0, 0, 10);
198 m.TimeToReachQueue = new TimeSpan(0, 0, 5);
199 //m.UseAuthentication = true;
200 m.UseDeadLetterQueue = true;
201 //m.UseEncryption = true;
205 Message m2 = mq.Receive ();
207 m2.Formatter = new BinaryMessageFormatter ();
208 Assert.AreEqual (s, m2.Body);
210 Assert.AreEqual (AcknowledgeTypes.PositiveArrival, m2.AcknowledgeType,
211 "AcknowledgeType not passed correctly");
212 Assert.AreEqual (adminQ.QueueName, m2.AdministrationQueue.QueueName,
213 "AdministrationQueue not passed correctly");
214 Assert.AreEqual (5, m2.AppSpecific, "AppSpecific not passed correctly");
215 //Assert.AreEqual (m.AuthenticationProviderName, m2.AuthenticationProviderName,
216 // "AuthenticationProviderName not passed correctly");
217 //Assert.AreEqual (m.AuthenticationProviderType, m2.AuthenticationProviderType,
218 // "AuthenticationProviderType not passed correctly");
219 //Assert.AreEqual (connectorType, m2.ConnectorType,
220 // "ConnectorType not passed correctly");
221 Assert.AreEqual (m.CorrelationId, m2.CorrelationId,
222 "CorrelationId not passed correctly");
223 //AreEqual (m.DestinationSymmetricKey, m2.DestinationSymmetricKey,
224 // "DestinationSymmetricKey not passed correctly");
225 //AreEqual (m.DigitalSignature, m2.DigitalSignature,
226 // "DigitalSignature not passed properly");
227 //Assert.AreEqual (EncryptionAlgorithm.Rc4, m2.EncryptionAlgorithm,
228 // "EncryptionAlgorithm not passed properly");
229 AreEqual (m.Extension, m2.Extension, "Extension not passed properly");
230 //Assert.AreEqual (m.HashAlgorithm, m2.HashAlgorithm,
231 // "HashAlgorithm not passed properly");
232 Assert.AreEqual (m.Label, m2.Label, "Label not passed correctly");
233 Assert.AreEqual (MessagePriority.AboveNormal, m2.Priority,
234 "Priority not passed properly");
235 Assert.AreEqual (true, m2.Recoverable, "Recoverable not passed properly");
236 Assert.AreEqual (responseQ.QueueName, m2.ResponseQueue.QueueName,
237 "ResponseQueue not passed properly");
238 AreEqual (m.SenderCertificate, m2.SenderCertificate,
239 "SenderCertificate not passed properly");
240 Assert.AreEqual (m.TimeToBeReceived, m2.TimeToBeReceived,
241 "TimeToBeReceived not passed properly");
242 Assert.AreEqual (m.TimeToReachQueue, m2.TimeToReachQueue,
243 "TimeToReachQueue not passed properly");
244 //Assert.IsTrue (m2.UseAuthentication,
245 // "UseAuthentication not passed properly");
246 Assert.IsTrue (m2.UseDeadLetterQueue,
247 "UseDeadLetterQueue not passed properly");
248 //Assert.IsTrue (m2.UseEncryption, "UseEncryption not pass properly");
249 //Assert.AreEqual ();
251 Assert.IsNotNull (m2.Id, "Id is null");
252 Assert.IsTrue (Guid.Empty.ToString () != m2.Id, "Id is Empty");
253 Assert.IsTrue (DateTime.MinValue != m2.ArrivedTime, "Arrived Time is not set");
254 Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
255 Assert.IsTrue (0 != m2.SenderVersion);
257 //Assert.IsNotNull (m2.SourceMachine, "SourceMachine is null");
261 private static void AreEqual(byte[] expected, byte[] actual, string message)
263 Assert.AreEqual (expected.Length, actual.Length, message);
264 for (int i = 0; i < expected.Length; i++)
265 Assert.AreEqual (expected[i], actual[i], message);
269 // No supported by Rabbit
270 public void SendPriorityMessages ()
272 MessageQueue mq = MQUtil.GetQueue ();
273 Message sent1 = new Message ("Highest", new BinaryMessageFormatter ());
274 sent1.Priority = MessagePriority.Highest;
275 Message sent2 = new Message ("Lowest", new BinaryMessageFormatter ());
276 sent2.Priority = MessagePriority.Lowest;
281 Message received1 = mq.Receive ();
282 Message received2 = mq.Receive ();
284 Assert.AreEqual (MessagePriority.Highest, received2.Priority,
285 "Priority delivery incorrect");
286 Assert.AreEqual (MessagePriority.Lowest, received1.Priority,
287 "Priority delivery incorrect");
291 public void SendReceiveXmlMessage ()
293 MessageQueue mq = MQUtil.GetQueue ();
294 String s = "Test: " + DateTime.Now;
295 Message m = new Message (s, new XmlMessageFormatter (new Type[] { typeof (string) }));
296 mq.MessageReadPropertyFilter.SetAll();
299 Message m2 = mq.Receive ();
300 m2.Formatter = new XmlMessageFormatter (new Type[] { typeof (string) });
301 Assert.AreEqual (s, m2.Body);
303 Assert.AreEqual (Acknowledgment.None, m2.Acknowledgment, "Acknowledgment");
304 Assert.IsNotNull (m2.ArrivedTime, "Acknowledgment");
305 Assert.IsNotNull (m2.Id, "Id");
309 public void SendBinaryText ()
311 string body = "This is a test";
313 MessageQueue q = MQUtil.GetQueue ();
315 q.Formatter = new BinaryMessageFormatter ();
319 Message m2 = q.Receive ();
321 Assert.IsNotNull (m2.Formatter, "Formatter is null");
322 Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
323 Assert.AreEqual (body, m2.Body);
327 public void SendDefaultText ()
329 string body = "This is a test";
331 MessageQueue q = MQUtil.GetQueue (MQUtil.CreateQueueName (), new XmlMessageFormatter ());
335 Message m2 = q.Receive ();
336 XmlMessageFormatter xmlf = (XmlMessageFormatter) q.Formatter;
337 Assert.AreEqual (typeof (XmlMessageFormatter), m2.Formatter.GetType ());
338 Assert.AreEqual (body, m2.Body);
339 Assert.AreEqual (0, m2.BodyType);
343 public void SendBinaryObject ()
345 Thingy body = new Thingy ();
346 body.MyProperty1 = 42;
347 body.MyProperty2 = "Something";
348 body.MyProperty3 = "Something else";
350 MessageQueue q = MQUtil.GetQueue ();
352 q.Formatter = new BinaryMessageFormatter ();
356 Message m2 = q.Receive ();
357 Thingy body2 = (Thingy) m2.Body;
359 Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
360 Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
361 Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
362 Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
363 Assert.AreEqual (768, m2.BodyType);
367 public void SendDefaultObject ()
369 string path = MQUtil.CreateQueueName ();
370 Thingy body = new Thingy();
371 body.MyProperty1 = 42;
372 body.MyProperty2 = "Something";
373 body.MyProperty3 = "Something else";
375 MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
379 MessageQueue q2 = MQUtil.GetQueue (path);
380 q2.Formatter = new XmlMessageFormatter (new Type[] { typeof(Thingy) });
382 Message m2 = q2.Receive ();
383 Thingy body2 = (Thingy) m2.Body;
385 Assert.AreEqual (typeof (XmlMessageFormatter), m2.Formatter.GetType ());
386 Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
387 Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
388 Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
392 public void SendBinaryMessage ()
394 Thingy body = new Thingy ();
395 body.MyProperty1 = 42;
396 body.MyProperty2 = "Something";
397 body.MyProperty3 = "Something else";
398 Message m1 = new Message (body);
399 m1.Formatter = new BinaryMessageFormatter ();
401 MessageQueue q = MQUtil.GetQueue ();
405 Message m2 = q.Receive ();
406 m2.Formatter = new BinaryMessageFormatter ();
407 Assert.IsNotNull (m2.Formatter);
408 Assert.AreEqual (typeof (BinaryMessageFormatter), m2.Formatter.GetType ());
409 Thingy body2 = (Thingy) m2.Formatter.Read (m2);
411 Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
412 Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
413 Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
417 public void SendDefaultMessage ()
419 string path = MQUtil.CreateQueueName ();
420 Thingy body = new Thingy ();
421 body.MyProperty1 = 42;
422 body.MyProperty2 = "Something";
423 body.MyProperty3 = "Something else";
424 Message m1 = new Message (body);
425 Assert.IsNull (m1.Formatter);
427 MessageQueue q = MQUtil.GetQueue (path, new XmlMessageFormatter ());
431 Message m2 = q.Receive ();
432 m2.Formatter = new XmlMessageFormatter (new Type[] { typeof (Thingy) });
433 Assert.IsNotNull (m2.Formatter);
434 Assert.AreEqual (typeof (XmlMessageFormatter), m2.Formatter.GetType ());
435 Thingy body2 = (Thingy) m2.Formatter.Read (m2);
437 Assert.AreEqual (body.MyProperty1, body2.MyProperty1);
438 Assert.AreEqual (body.MyProperty2, body2.MyProperty2);
439 Assert.AreEqual (body.MyProperty3, body2.MyProperty3);
448 public int MyProperty1
450 get { return myVar1; }
451 set { myVar1 = value; }
454 private string myVar2;
456 public string MyProperty2
458 get { return myVar2; }
459 set { myVar2 = value; }
462 private string myVar3;
464 public string MyProperty3
466 get { return myVar3; }
467 set { myVar3 = value; }