2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / Mono.Messaging.RabbitMQ / Test / Mono.Messaging.RabbitMQ / AdminTest.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 AdminTest {
43                 
44                 [Test]
45                 public void CreateNonTransactionalQueue ()
46                 {
47                         string qName = @".\private$\admin-queue-1";
48                         Assert.IsFalse (MessageQueue.Exists (qName), "Queue should not exist");
49                         MessageQueue q = MessageQueue.Create (qName);
50                         Assert.IsFalse (q.Transactional);
51                         Assert.IsTrue (MessageQueue.Exists (qName), "Queue should exist");
52                 }
53                 
54                 [Test]
55                 public void CreateTransactionalQueue ()
56                 {
57                         string qName = @".\private$\admin-queue-2";
58                         Assert.IsFalse (MessageQueue.Exists (qName), "Queue should not exist");
59                         MessageQueue q = MessageQueue.Create (qName, true);
60                         Assert.IsTrue (q.Transactional, "Queue should be transactional");
61                         Assert.IsTrue (MessageQueue.Exists (qName), "Queue should exist");
62                 }
63                 
64                 private bool Contains(MessageQueue[] qs, String qName)
65                 {
66                         foreach (MessageQueue q in qs)
67                         {
68                                 if (q.QueueName == qName)
69                                         return true;
70                         }
71                         return false;
72                 }
73
74                 [Test]
75                 public void GetPublicQueues ()
76                 {
77                         string qName1 = @".\admin-queue-3";
78                         string qName2 = @".\admin-queue-4";
79                         
80                         MessageQueue.Create (qName1);
81                         MessageQueue.Create (qName2);
82                         
83                         MessageQueue[] mq = MessageQueue.GetPublicQueues ();
84                         Assert.IsTrue (Contains (mq, "admin-queue-3"), qName1 + " not found");
85                         Assert.IsTrue (Contains (mq, "admin-queue-4"), qName2 + " not found");
86                 }
87                 
88                 [Test]
89                 public void GetQueue ()
90                 {
91                         MessageQueue q1 = MQUtil.GetQueue(@".\private$\admin-queue-5", true);
92                         Assert.IsTrue (q1.Transactional, "Queue should be transactional");
93                         MessageQueue q2 = MQUtil.GetQueue(@".\private$\admin-queue-5", true);
94                         Assert.IsTrue (q2.Transactional, "Queue should be transactional");
95                 }
96                 
97                 [Test]
98                 [ExpectedException (typeof (MessageQueueException))]
99                 public void PurgeQueue ()
100                 {
101                         MessageQueue q = MQUtil.GetQueue(@".\private$\purge-queue");
102                         Message m1 = new Message ("foobar1", new BinaryMessageFormatter ());
103                         Message m2 = new Message ("foobar2", new BinaryMessageFormatter ());
104                         Message m3 = new Message ("foobar3", new BinaryMessageFormatter ());
105                         Message m4 = new Message ("foobar4", new BinaryMessageFormatter ());
106                         
107                         q.Send (m1);
108                         q.Send (m2);
109                         q.Send (m3);
110                         q.Send (m4);
111                         
112                         q.Receive ();                   
113                         q.Purge ();
114                         q.Receive (new TimeSpan (0, 0, 2));
115                 }
116                                 
117                 [Test]
118                 public void DeleteQueue ()
119                 {
120                         MessageQueue q = MQUtil.GetQueue(@".\private$\delete-queue");
121                         Message m1 = new Message ("foobar1", new BinaryMessageFormatter ());
122                         
123                         q.Send (m1);
124                         
125                         q.Receive ();
126
127                         MessageQueue.Delete(@".\private$\delete-queue");
128                 }
129         }
130 }