Facilitate the merge
[mono.git] / mcs / class / System.Web / Test / System.Web.UI / LosFormatterTest.cs
1 //
2 // LosFormatterTest.cs - Unit tests for System.Web.UI.LosFormatter
3 //
4 // Author:
5 //      Gert Driesen  <drieseng@users.sourceforge.net>
6 //
7 // Copyright (C) 2007 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.IO;
31 using System.Text;
32 using System.Web.UI;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Web.UI
37 {
38         [TestFixture]
39         public class LosFormatterTest
40         {
41                 [Test] // bug #411115
42                 public void Deserialize_Stream_NonSeekable ()
43                 {
44                         string s1 = "Hello world";
45                         NonSeekableStream ns = new NonSeekableStream ();
46                         LosFormatter lf = new LosFormatter ();
47                         lf.Serialize (ns, s1);
48                         ns.Reset ();
49                         string s2 = lf.Deserialize (ns) as string;
50                         Assert.AreEqual (s1, s2);
51                 }
52
53                 [Test] // bug #324526
54                 public void Serialize ()
55                 {
56                         string s = "Hello world";
57                         LosFormatter lf = new LosFormatter ();
58                         StringWriter sw = new StringWriter ();
59                         lf.Serialize (sw, s);
60                         string s1 = sw.ToString ();
61                         Assert.IsNotNull (s1, "#1");
62                         string s2 = lf.Deserialize (s1) as string;
63                         Assert.IsNotNull (s2, "#2");
64                         Assert.AreEqual (s, s2, "#3");
65                 }
66
67                 [Test]
68                 [Category ("NotWorking")]
69                 public void Serialize_Output ()
70                 {
71                         string s = "Hello world";
72                         LosFormatter lf = new LosFormatter ();
73                         StringWriter sw = new StringWriter ();
74                         lf.Serialize (sw, s);
75                         string s1 = sw.ToString ();
76 #if NET_2_0
77                         Assert.AreEqual ("/wEFC0hlbGxvIHdvcmxk", s1, "#1");
78 #else
79                         Assert.AreEqual ("SGVsbG8gd29ybGQ=", s1, "#1");
80 #endif
81                         string s2 = lf.Deserialize (s1) as string;
82                         Assert.IsNotNull (s2, "#2");
83                         Assert.AreEqual (s, s2, "#3");
84                 }
85
86                 [Test]
87                 [Category ("NotDotNet")] // MS throws NullReferenceException
88                 public void Serialize_Output_Null ()
89                 {
90                         LosFormatter lf = new LosFormatter ();
91                         try {
92                                 lf.Serialize ((TextWriter) null, "test");
93                                 Assert.Fail ("#1");
94                         } catch (ArgumentNullException ex) {
95                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
96                                 Assert.IsNull (ex.InnerException, "#3");
97                                 Assert.IsNotNull (ex.Message, "#4");
98                                 Assert.IsNotNull (ex.ParamName, "#5");
99                                 Assert.AreEqual ("output", ex.ParamName, "#6");
100                         }
101                 }
102
103                 [Test]
104                 [Category ("NotWorking")]
105                 public void Serialize_Stream ()
106                 {
107                         string s = "Hello world";
108                         LosFormatter lf = new LosFormatter ();
109                         MemoryStream ms = new MemoryStream ();
110                         lf.Serialize (ms, s);
111                         string s1 = Encoding.UTF8.GetString (ms.GetBuffer (), 0, (int) ms.Length);
112 #if NET_2_0
113                         Assert.AreEqual ("/wEFC0hlbGxvIHdvcmxk", s1, "#1");
114 #else
115                         Assert.AreEqual ("SGVsbG8gd29ybGQ=", s1, "#1");
116 #endif
117                         string s2 = lf.Deserialize (s1) as string;
118                         Assert.IsNotNull (s2, "#2");
119                         Assert.AreEqual (s, s2, "#3");
120                 }
121
122                 [Test]
123                 public void Serialize_Stream_Null ()
124                 {
125                         LosFormatter lf = new LosFormatter ();
126                         try {
127                                 lf.Serialize ((Stream) null, "test");
128                                 Assert.Fail ("#1");
129                         } catch (ArgumentNullException ex) {
130                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
131                                 Assert.IsNull (ex.InnerException, "#3");
132                                 Assert.IsNotNull (ex.Message, "#4");
133                                 Assert.IsNotNull (ex.ParamName, "#5");
134                                 Assert.AreEqual ("stream", ex.ParamName, "#6");
135                         }
136                 }
137
138                 [Test]
139                 [Category ("NotWorking")]
140                 public void Serialize_Value_Null ()
141                 {
142                         LosFormatter lf = new LosFormatter ();
143                         MemoryStream ms = new MemoryStream ();
144                         lf.Serialize (ms, null);
145                         string s1 = Encoding.UTF8.GetString (ms.GetBuffer (), 0, (int) ms.Length);
146 #if NET_2_0
147                         Assert.AreEqual ("/wFk", s1, "#1");
148 #else
149                         Assert.AreEqual (string.Empty, s1, "#1");
150 #endif
151
152                         StringWriter sw = new StringWriter ();
153                         lf.Serialize (sw, null);
154                         string s2 = sw.ToString ();
155 #if NET_2_0
156                         Assert.AreEqual ("/wFk", s1, "#2");
157 #else
158                         Assert.AreEqual (string.Empty, s1, "#2");
159 #endif
160                 }
161
162                 class NonSeekableStream : MemoryStream
163                 {
164                         private bool canSeek;
165
166                         public override bool CanSeek {
167                                 get { return canSeek; }
168                         }
169
170                         public override long Length {
171                                 get {
172                                         if (!CanSeek)
173                                                 throw new NotSupportedException ();
174                                         return base.Length;
175                                 }
176                         }
177
178                         public override long Position {
179                                 get{
180                                         if (!CanSeek)
181                                                 throw new NotSupportedException ();
182                                         return base.Position;
183                                 }
184                                 set {
185                                         base.Position = value;
186                                 }
187                         }
188
189                         public override long Seek (long offset, SeekOrigin origin)
190                         {
191                                 if (!CanSeek)
192                                         throw new NotSupportedException ();
193                                 return base.Seek (offset, origin);
194                         }
195
196                         public void Reset ()
197                         {
198                                 canSeek = true;
199                                 Position = 0;
200                                 canSeek = false;
201                         }
202                 }
203         }
204 }