2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / Mono.Messaging.RabbitMQ / Test / Mono.Messaging.RabbitMQ / XmlMessageFormatterTest.cs
1 //
2 // XmlMessageFormatterTest.cs -
3 //      NUnit Test Cases for XmlMessageFormatterTest
4 //
5 // Author:
6 //      Michael Barker  <mike@middlesoft.co.uk>
7 //
8 // Copyright (C) 2008 Michael Barker
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.IO;
32 using System.Messaging;
33
34 using Mono.Messaging;
35
36 using NUnit.Framework;
37 using NUnit.Mocks;
38
39 namespace MonoTests.Mono.Messaging.RabbitMQ
40 {
41     [TestFixture]
42     public class XmlMessageFormatterTest
43     {
44
45         DynamicMock mock1;
46         IMessage msg1;
47         DynamicMock mock2;
48         IMessage msg2;
49         
50         [SetUp]
51         public void SetUp ()
52         {
53             mock1 = new DynamicMock (typeof (IMessage));
54             msg1 = (IMessage) mock1.MockInstance;
55             mock2 = new DynamicMock (typeof (IMessage));
56             msg2 = (IMessage) mock2.MockInstance;
57         }
58
59         [Test]
60         public void FormatString ()
61         {
62             Type[] t = { typeof (string) };
63             string s = "this is a test string";
64             
65             Stream ms = new MemoryStream ();
66             mock1.ExpectAndReturn ("get_BodyStream", ms);
67             mock1.ExpectAndReturn ("get_BodyStream", ms);
68             
69             mock2.ExpectAndReturn ("get_BodyStream", ms);
70             mock2.ExpectAndReturn ("get_BodyStream", ms);
71             
72             Message m = TestUtils.CreateMessage (msg1);
73             
74             XmlMessageFormatter xmlF = new XmlMessageFormatter ();
75             m.Formatter = xmlF;
76             m.Formatter.Write (m, s);
77             Stream stream = m.BodyStream;
78             
79             Assert.AreEqual (typeof (string), xmlF.TargetTypes[0]);
80             
81             Assert.IsTrue (stream.Length > 0);
82             
83             Message m2 = TestUtils.CreateMessage (msg2);
84             m2.Formatter = new XmlMessageFormatter (t);
85             
86             Assert.AreEqual (s, (string) m2.Formatter.Read (m2), "The string did not serialise/deserialise properly");
87             
88             mock1.Verify ();
89             mock2.Verify ();
90         }
91         
92         
93         [Test]
94         public void FormatComplexObject ()
95         {
96             Type[] ts = { typeof (Thingy2) };
97             Stream ms = new MemoryStream ();
98             mock1.ExpectAndReturn ("get_BodyStream", ms);
99             mock1.ExpectAndReturn ("get_BodyStream", ms);
100             
101             mock2.ExpectAndReturn ("get_BodyStream", ms);
102             mock2.ExpectAndReturn ("get_BodyStream", ms);
103             
104             Thingy2 t0 = new Thingy2();
105             t0.Iii = 42;
106             t0.Sss = "Some Text";
107             t0.Ttt = DateTime.Now;
108             
109             Message m = TestUtils.CreateMessage (msg1);
110             m.Formatter = new XmlMessageFormatter (ts);
111             m.Formatter.Write (m, t0);
112             Stream stream = m.BodyStream;
113             
114             Assert.IsTrue (stream.Length > 0);
115             
116             Message m2 = TestUtils.CreateMessage (msg2);
117             m2.Formatter = new XmlMessageFormatter (ts);
118             Thingy2 t1 = (Thingy2) m2.Formatter.Read (m2);
119             
120             Assert.AreEqual (t0.Iii, t1.Iii, "The int did not serialise/deserialise properly");
121             Assert.AreEqual (t0.Sss, t1.Sss, "The string did not serialise/deserialise properly");
122             Assert.AreEqual (t0.Ttt.ToString (), t1.Ttt.ToString (), "The date did not serialise/deserialise properly");
123                         
124                         mock1.Verify ();
125                         mock2.Verify ();
126         }
127         
128     }
129
130     [Serializable]
131     public class Thingy2
132     {
133         private int iii;
134         private string sss;
135         private DateTime ttt;
136         
137         public int Iii {
138             get { return iii; }
139             set { iii = value; }
140         }
141         
142         public string Sss {
143             get { return sss; }
144             set { sss = value; }
145         }
146         
147         public DateTime Ttt {
148             get { return ttt; }
149             set { ttt = value; }
150         }
151     }
152 }
153