2
0

JsonQueryStringConverterTest.cs 9.3 KB

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