2 // JsonQueryStringConverterTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Globalization;
31 using System.Runtime.Serialization;
32 using System.ServiceModel.Dispatcher;
35 using NUnit.Framework;
37 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
39 namespace MonoTests.System.ServiceModel.Description
42 public class JsonQueryStringConverterTest
44 JsonQueryStringConverter c;
49 c = new JsonQueryStringConverter ();
53 public void CanConvert ()
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");
77 // ConvertValueToString
80 public void ConvertValueToStringValidCast ()
82 Assert.AreEqual ("\"ABC\"", c.ConvertValueToString ("ABC", typeof (char)));
86 [Ignore ("huh? .NET converts to 123, not \"123\"")]
87 public void ConvertValueToStringValidCast2 ()
89 Assert.AreEqual ("\"123\"", c.ConvertValueToString (123, typeof (string)));
93 [Ignore ("huh? .NET converts to \"123\", not 123")]
94 public void ConvertValueToStringValidCast3 ()
96 Assert.AreEqual ("123", c.ConvertValueToString ("123", typeof (int)));
100 public void ConvertValueToStringValidCast4 ()
102 Assert.AreEqual ("123.45", c.ConvertValueToString (123.45, typeof (int)));
106 public void ConvertValueToStringValidCast5 ()
108 Assert.AreEqual ("123", c.ConvertValueToString (123, typeof (double)));
112 public void ConvertValueToStringValidCast6 ()
114 // umm... should be out of range
115 Assert.AreEqual ("12345", c.ConvertValueToString (12345, typeof (byte)));
119 public void ConvertValueToStringNullToValueType ()
121 Assert.AreEqual (null, c.ConvertValueToString (null, typeof (char)));
125 public void ConvertValueToStringDbNull ()
127 Assert.AreEqual ("{}", c.ConvertValueToString (DBNull.Value, typeof (DBNull)));
131 public void ConvertValueToStringDbNull2 ()
133 Assert.AreEqual ("\"\"", c.ConvertValueToString ("", typeof (DBNull)));
137 public void ConvertValueToStringDbNull3 ()
139 Assert.AreEqual (null, c.ConvertValueToString (null, typeof (DBNull)));
143 public void ConvertValueToStringDbNull4 ()
145 // ... so, DBNull is just converted to String
146 Assert.AreEqual ("\"ABC\"", c.ConvertValueToString ("ABC", typeof (DBNull)));
150 public void ConvertValueToStringQName ()
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");
157 public void ConvertValueToStringNullToString ()
159 Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
163 public void ConvertValueToString ()
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");
170 // ConvertStringToValue
173 [ExpectedException (typeof (FormatException))]
174 public void ConvertStringToValueInvalidCast ()
176 c.ConvertStringToValue ("ABC", typeof (char));
180 [ExpectedException (typeof (FormatException))]
181 [Category ("NotWorking")]
182 public void ConvertStringToValueInvalidCast2 ()
184 c.ConvertStringToValue ("-123", typeof (uint));
188 [ExpectedException (typeof (FormatException))]
189 public void ConvertStringToValueInvalidCast4 ()
191 c.ConvertStringToValue ("123.45", typeof (int));
195 public void ConvertStringToValueString1 ()
198 Assert.AreEqual ("-123.45", c.ConvertStringToValue ("-123.45", typeof (string)));
202 public void ConvertStringToValueString2 ()
204 Assert.AreEqual ("ABC", c.ConvertStringToValue ("\"ABC\"", typeof (string)));
208 [ExpectedException (typeof (SerializationException))]
209 public void ConvertStringToValueString3 ()
211 // missing closing '"'
212 Assert.AreEqual ("\"ABC", c.ConvertStringToValue ("\"ABC", typeof (string)));
213 // this test exposes that .NET uses DataContractJsonSerializer internally.
217 public void ConvertStringToValueNullToValueType ()
220 Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
224 public void ConvertStringToValueDbNull ()
226 Assert.AreEqual (null, c.ConvertStringToValue (null, typeof (DBNull)));
230 [ExpectedException (typeof (SerializationException))]
231 public void ConvertStringToValueDbNull2 ()
233 c.ConvertStringToValue ("", typeof (DBNull));
237 [ExpectedException (typeof (SerializationException))]
238 public void ConvertStringToValueDbNull3 ()
240 c.ConvertStringToValue ("ABC", typeof (DBNull));
244 public void ConvertStringToValueDbNull4 ()
246 Assert.AreEqual (DBNull.Value, c.ConvertStringToValue ("{}", typeof (DBNull)));
250 public void ConvertStringToValueNullToString ()
252 Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
256 public void ConvertStringToValue ()
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");
267 [TypeConverter (typeof (MyTypeConverter))]
272 class MyTypeConverter : TypeConverter
274 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
276 if (destinationType == typeof (string))
278 throw new Exception ();