2009-09-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel.Web / Test / System.ServiceModel.Dispatcher / JsonQueryStringConverterTest.cs
1 //
2 // JsonQueryStringConverterTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
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 using System;
29 using System.ComponentModel;
30 using System.Globalization;
31 using System.Runtime.Serialization;
32 using System.ServiceModel.Dispatcher;
33 using System.Text;
34 using System.Xml;
35 using NUnit.Framework;
36
37 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
38
39 namespace MonoTests.System.ServiceModel.Description
40 {
41         [TestFixture]
42         public class JsonQueryStringConverterTest
43         {
44                 JsonQueryStringConverter c;
45
46                 [SetUp]
47                 public void Setup ()
48                 {
49                         c = new JsonQueryStringConverter ();
50                 }
51
52                 [Test]
53                 public void CanConvert ()
54                 {
55                         Assert.IsTrue (c.CanConvert (typeof (bool)), "#1");
56                         Assert.IsTrue (c.CanConvert (typeof (char)), "#2");
57                         Assert.IsTrue (c.CanConvert (typeof (double)), "#3");
58                         Assert.IsTrue (c.CanConvert (typeof (decimal)), "#4");
59                         Assert.IsTrue (c.CanConvert (typeof (float)), "#5");
60                         Assert.IsTrue (c.CanConvert (typeof (string)), "#6");
61                         Assert.IsTrue (c.CanConvert (typeof (int)), "#7");
62                         Assert.IsTrue (c.CanConvert (typeof (byte)), "#8");
63                         Assert.IsTrue (c.CanConvert (typeof (sbyte)), "#9");
64                         Assert.IsTrue (c.CanConvert (typeof (long)), "#10");
65                         Assert.IsTrue (c.CanConvert (typeof (ulong)), "#11");
66                         Assert.IsTrue (c.CanConvert (typeof (DateTime)), "#12");
67                         Assert.IsTrue (c.CanConvert (typeof (DateTimeOffset)), "#13");
68                         Assert.IsTrue (c.CanConvert (typeof (TimeSpan)), "#14");
69                         Assert.IsTrue (c.CanConvert (typeof (Guid)), "#15");
70                         Assert.IsTrue (c.CanConvert (typeof (XmlQualifiedName)), "#16");
71                         Assert.IsTrue (c.CanConvert (typeof (object)), "#17");
72                         //Assert.IsFalse (c.CanConvert (typeof (QueryStringConverter)), "#18");
73                         // TypeConverterAttribute does not help it.
74                         Assert.IsFalse (c.CanConvert (typeof (MyConvertible)), "#19");
75                 }
76
77                 // ConvertValueToString
78
79                 [Test]
80                 public void ConvertValueToStringValidCast ()
81                 {
82                         Assert.AreEqual ("\"ABC\"", c.ConvertValueToString ("ABC", typeof (char)));
83                 }
84
85                 [Test]
86                 [Ignore ("huh? .NET converts to 123, not \"123\"")]
87                 public void ConvertValueToStringValidCast2 ()
88                 {
89                         Assert.AreEqual ("\"123\"", c.ConvertValueToString (123, typeof (string)));
90                 }
91
92                 [Test]
93                 [Ignore ("huh? .NET converts to \"123\", not 123")]
94                 public void ConvertValueToStringValidCast3 ()
95                 {
96                         Assert.AreEqual ("123", c.ConvertValueToString ("123", typeof (int)));
97                 }
98
99                 [Test]
100                 public void ConvertValueToStringValidCast4 ()
101                 {
102                         Assert.AreEqual ("123.45", c.ConvertValueToString (123.45, typeof (int)));
103                 }
104
105                 [Test]
106                 public void ConvertValueToStringValidCast5 ()
107                 {
108                         Assert.AreEqual ("123", c.ConvertValueToString (123, typeof (double)));
109                 }
110
111                 [Test]
112                 public void ConvertValueToStringValidCast6 ()
113                 {
114                         // umm... should be out of range
115                         Assert.AreEqual ("12345", c.ConvertValueToString (12345, typeof (byte)));
116                 }
117
118                 [Test]
119                 public void ConvertValueToStringNullToValueType ()
120                 {
121                         Assert.AreEqual (null, c.ConvertValueToString (null, typeof (char)));
122                 }
123
124                 [Test]
125                 public void ConvertValueToStringDbNull ()
126                 {
127                         Assert.AreEqual ("{}", c.ConvertValueToString (DBNull.Value, typeof (DBNull)));
128                 }
129
130                 [Test]
131                 public void ConvertValueToStringDbNull2 ()
132                 {
133                         Assert.AreEqual ("\"\"", c.ConvertValueToString ("", typeof (DBNull)));
134                 }
135
136                 [Test]
137                 public void ConvertValueToStringDbNull3 ()
138                 {
139                         Assert.AreEqual (null, c.ConvertValueToString (null, typeof (DBNull)));
140                 }
141
142                 [Test]
143                 public void ConvertValueToStringDbNull4 ()
144                 {
145                         // ... so, DBNull is just converted to String
146                         Assert.AreEqual ("\"ABC\"", c.ConvertValueToString ("ABC", typeof (DBNull)));
147                 }
148
149                 [Test]
150                 public void ConvertValueToStringQName ()
151                 {
152                         Assert.AreEqual ("\"foo:\"", c.ConvertValueToString (new XmlQualifiedName ("foo"), typeof (XmlQualifiedName)), "#1");
153                         Assert.AreEqual ("\"foo:urn:bar\"", c.ConvertValueToString (new XmlQualifiedName ("foo", "urn:bar"), typeof (XmlQualifiedName)), "#2");
154                 }
155
156                 [Test]
157                 public void ConvertValueToStringNullToString ()
158                 {
159                         Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
160                 }
161
162                 [Test]
163                 public void ConvertValueToString ()
164                 {
165                         Assert.AreEqual ("\"A\"", c.ConvertValueToString ('A', typeof (char)), "#1");
166                         Assert.AreEqual ("\"}}}\"", c.ConvertValueToString ("}}}", typeof (string)), "#2");
167                         Assert.AreEqual ("123", c.ConvertValueToString (123.0, typeof (double)), "#3");
168                 }
169
170                 // ConvertStringToValue
171
172                 [Test]
173                 [ExpectedException (typeof (FormatException))]
174                 public void ConvertStringToValueInvalidCast ()
175                 {
176                         c.ConvertStringToValue ("ABC", typeof (char));
177                 }
178
179                 [Test]
180                 [ExpectedException (typeof (FormatException))]
181                 [Category ("NotWorking")]
182                 public void ConvertStringToValueInvalidCast2 ()
183                 {
184                         c.ConvertStringToValue ("-123", typeof (uint));
185                 }
186
187                 [Test]
188                 [ExpectedException (typeof (FormatException))]
189                 public void ConvertStringToValueInvalidCast4 ()
190                 {
191                         c.ConvertStringToValue ("123.45", typeof (int));
192                 }
193
194                 [Test]
195                 public void ConvertStringToValueString1 ()
196                 {
197                         // hmm ...
198                         Assert.AreEqual ("-123.45", c.ConvertStringToValue ("-123.45", typeof (string)));
199                 }
200
201                 [Test]
202                 public void ConvertStringToValueString2 ()
203                 {
204                         Assert.AreEqual ("ABC", c.ConvertStringToValue ("\"ABC\"", typeof (string)));
205                 }
206
207                 [Test]
208                 [ExpectedException (typeof (SerializationException))]
209                 public void ConvertStringToValueString3 ()
210                 {
211                         // missing closing '"'
212                         Assert.AreEqual ("\"ABC", c.ConvertStringToValue ("\"ABC", typeof (string)));
213                         // this test exposes that .NET uses DataContractJsonSerializer internally.
214                 }
215
216                 [Test]
217                 public void ConvertStringToValueNullToValueType ()
218                 {
219                         // hmm, it passes.
220                         Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
221                 }
222
223                 [Test]
224                 public void ConvertStringToValueDbNull ()
225                 {
226                         Assert.AreEqual (null, c.ConvertStringToValue (null, typeof (DBNull)));
227                 }
228
229                 [Test]
230                 [ExpectedException (typeof (SerializationException))]
231                 public void ConvertStringToValueDbNull2 ()
232                 {
233                         c.ConvertStringToValue ("", typeof (DBNull));
234                 }
235
236                 [Test]
237                 [ExpectedException (typeof (SerializationException))]
238                 public void ConvertStringToValueDbNull3 ()
239                 {
240                         c.ConvertStringToValue ("ABC", typeof (DBNull));
241                 }
242
243                 [Test]
244                 public void ConvertStringToValueDbNull4 ()
245                 {
246                         Assert.AreEqual (DBNull.Value, c.ConvertStringToValue ("{}", typeof (DBNull)));
247                 }
248
249                 [Test]
250                 public void ConvertStringToValueNullToString ()
251                 {
252                         Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
253                 }
254
255                 [Test]
256                 public void ConvertStringToValue ()
257                 {
258                         Assert.AreEqual ('A', c.ConvertStringToValue ("A", typeof (char)), "#1");
259                         // likely .NET bug: it should fail to deserialize as it is not a valid JSON string.
260                         //Assert.AreEqual ("}}}", c.ConvertStringToValue ("}}}", typeof (string)), "#2");
261                         Assert.AreEqual (123.0, c.ConvertStringToValue ("123.0", typeof (double)), "#3");
262                         Assert.AreEqual (123.0, c.ConvertStringToValue ("123", typeof (double)), "#4");
263                 }
264
265                 // Types
266
267                 [TypeConverter (typeof (MyTypeConverter))]
268                 class MyConvertible
269                 {
270                 }
271
272                 class MyTypeConverter : TypeConverter
273                 {
274                         public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
275                         {
276                                 if (destinationType == typeof (string))
277                                         return "hogehoge";
278                                 throw new Exception ();
279                         }
280                 }
281         }
282 }