JsonQueryStringConverterTest.cs 8.2 KB

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