* Makefile: Fix $(the_lib) dependencies so that monodoc.dll is rebuilt
[mono.git] / mcs / class / System.Messaging / Test / System.Messaging / 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 NUnit.Framework;
35 using NUnit.Mocks;
36
37 namespace MonoTests.System.Messaging
38 {
39     [TestFixture]
40     public class XmlMessageFormatterTest
41     {
42
43         DynamicMock mock1;
44         Mono.Messaging.IMessage msg1;
45         DynamicMock mock2;
46         Mono.Messaging.IMessage msg2;
47         
48         [SetUp]
49         public void SetUp ()
50         {
51             mock1 = new DynamicMock (typeof (Mono.Messaging.IMessage));
52             msg1 = (Mono.Messaging.IMessage) mock1.MockInstance;
53             mock2 = new DynamicMock (typeof (Mono.Messaging.IMessage));
54             msg2 = (Mono.Messaging.IMessage) mock2.MockInstance;
55         }
56
57         [Test]
58         public void FormatString ()
59         {
60             Type[] t = { typeof (string) };
61             string s = "this is a test string";
62             
63             Stream ms = new MemoryStream ();
64             mock1.ExpectAndReturn ("get_BodyStream", ms);
65             mock1.ExpectAndReturn ("get_BodyStream", ms);
66             
67             mock2.Expect ("set_BodyStream", ms);
68             mock2.ExpectAndReturn ("get_BodyStream", ms);
69             mock2.ExpectAndReturn ("get_BodyStream", ms);
70             
71             Message m = TestUtils.CreateMessage (msg1);
72             
73             XmlMessageFormatter xmlF = new XmlMessageFormatter ();
74             m.Formatter = xmlF;
75             m.Formatter.Write (m, s);
76             Stream stream = m.BodyStream;
77             
78             Assert.AreEqual (typeof (string), xmlF.TargetTypes[0]);
79             
80             Assert.IsTrue (stream.Length > 0);
81             
82             Message m2 = TestUtils.CreateMessage (msg2);
83             m2.Formatter = new XmlMessageFormatter (t);
84             m2.BodyStream = stream;
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 (Thingy) };
97             Stream ms = new MemoryStream ();
98             mock1.ExpectAndReturn ("get_BodyStream", ms);
99             mock1.ExpectAndReturn ("get_BodyStream", ms);
100             
101             mock2.Expect ("set_BodyStream", ms);
102             mock2.ExpectAndReturn ("get_BodyStream", ms);
103             mock2.ExpectAndReturn ("get_BodyStream", ms);
104             
105             Thingy t0 = new Thingy();
106             t0.Iii = 42;
107             t0.Sss = "Some Text";
108             t0.Ttt = DateTime.Now;
109             
110             Message m = TestUtils.CreateMessage (msg1);
111             m.Formatter = new XmlMessageFormatter (ts);
112             m.Formatter.Write (m, t0);
113             Stream stream = m.BodyStream;
114             
115             Assert.IsTrue (stream.Length > 0);
116             
117             Message m2 = TestUtils.CreateMessage (msg2);
118             m2.Formatter = new XmlMessageFormatter (ts);
119             m2.BodyStream = stream;
120             Thingy t1 = (Thingy) m2.Formatter.Read (m2);
121             
122             Assert.AreEqual (t0.Iii, t1.Iii, "The int did not serialise/deserialise properly");
123             Assert.AreEqual (t0.Sss, t1.Sss, "The string did not serialise/deserialise properly");
124             Assert.AreEqual (t0.Ttt, t1.Ttt, "The date did not serialise/deserialise properly");
125         }
126         
127     }
128
129     [Serializable]
130     public class Thingy
131     {
132         private int iii;
133         private string sss;
134         private DateTime ttt;
135         
136         public int Iii {
137             get { return iii; }
138             set { iii = value; }
139         }
140         
141         public string Sss {
142             get { return sss; }
143             set { sss = value; }
144         }
145         
146         public DateTime Ttt {
147             get { return ttt; }
148             set { ttt = value; }
149         }
150     }
151
152 }
153