JsonQueryStringConverterTest.cs 9.7 KB

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