QueryStringConverterTest.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // QueryStringConverterTest.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.ServiceModel.Dispatcher;
  32. using System.Text;
  33. using System.Xml;
  34. using NUnit.Framework;
  35. using CategoryAttribute = NUnit.Framework.CategoryAttribute;
  36. namespace MonoTests.System.ServiceModel.Description
  37. {
  38. [TestFixture]
  39. public class QueryStringConverterTest
  40. {
  41. QueryStringConverter c;
  42. [SetUp]
  43. public void Setup ()
  44. {
  45. c = new QueryStringConverter ();
  46. }
  47. [Test]
  48. public void CanConvert ()
  49. {
  50. Assert.IsTrue (c.CanConvert (typeof (bool)), "#1");
  51. Assert.IsTrue (c.CanConvert (typeof (char)), "#2");
  52. Assert.IsTrue (c.CanConvert (typeof (double)), "#3");
  53. Assert.IsTrue (c.CanConvert (typeof (decimal)), "#4");
  54. Assert.IsTrue (c.CanConvert (typeof (float)), "#5");
  55. Assert.IsTrue (c.CanConvert (typeof (string)), "#6");
  56. Assert.IsTrue (c.CanConvert (typeof (int)), "#7");
  57. Assert.IsTrue (c.CanConvert (typeof (byte)), "#8");
  58. Assert.IsTrue (c.CanConvert (typeof (sbyte)), "#9");
  59. Assert.IsTrue (c.CanConvert (typeof (long)), "#10");
  60. Assert.IsTrue (c.CanConvert (typeof (ulong)), "#11");
  61. Assert.IsTrue (c.CanConvert (typeof (DateTime)), "#12");
  62. Assert.IsTrue (c.CanConvert (typeof (DateTimeOffset)), "#13");
  63. Assert.IsTrue (c.CanConvert (typeof (TimeSpan)), "#14");
  64. Assert.IsTrue (c.CanConvert (typeof (Guid)), "#15");
  65. Assert.IsFalse (c.CanConvert (typeof (XmlQualifiedName)), "#16");
  66. Assert.IsTrue (c.CanConvert (typeof (object)), "#17");
  67. Assert.IsFalse (c.CanConvert (typeof (QueryStringConverter)), "#18");
  68. // TypeConverterAttribute does not help it.
  69. Assert.IsFalse (c.CanConvert (typeof (MyConvertible)), "#19");
  70. Assert.IsTrue (c.CanConvert (typeof (DemoEnum)), "#20");
  71. }
  72. // ConvertStringToValue
  73. [Test]
  74. [ExpectedException (typeof (InvalidCastException))]
  75. public void ConvertValueToStringInvalidCast ()
  76. {
  77. c.ConvertValueToString ("ABC", typeof (char));
  78. }
  79. [Test]
  80. [ExpectedException (typeof (InvalidCastException))]
  81. public void ConvertValueToStringInvalidCast2 ()
  82. {
  83. c.ConvertValueToString (123, typeof (string));
  84. }
  85. [Test]
  86. [ExpectedException (typeof (InvalidCastException))]
  87. public void ConvertValueToStringInvalidCast3 ()
  88. {
  89. c.ConvertValueToString ("123", typeof (int));
  90. }
  91. [Test]
  92. [ExpectedException (typeof (InvalidCastException))]
  93. public void ConvertValueToStringInvalidCast4 ()
  94. {
  95. c.ConvertValueToString (123.45, typeof (int));
  96. }
  97. [Test]
  98. [ExpectedException (typeof (InvalidCastException))]
  99. public void ConvertValueToStringInvalidCast5 ()
  100. {
  101. // umm...
  102. c.ConvertValueToString (123, typeof (double));
  103. }
  104. [Test]
  105. [ExpectedException (typeof (ArgumentNullException))]
  106. public void ConvertValueToStringNullToValueType ()
  107. {
  108. c.ConvertValueToString (null, typeof (char));
  109. }
  110. [Test]
  111. [ExpectedException (typeof (NotSupportedException))]
  112. public void ConvertValueToStringDbNull ()
  113. {
  114. c.ConvertValueToString (DBNull.Value, typeof (DBNull));
  115. }
  116. [Test]
  117. public void ConvertValueToStringNullToString ()
  118. {
  119. Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
  120. }
  121. [Test]
  122. public void ConvertValueToString ()
  123. {
  124. Assert.AreEqual ("A", c.ConvertValueToString ('A', typeof (char)), "#1");
  125. Assert.AreEqual ("}}}", c.ConvertValueToString ("}}}", typeof (string)), "#2");
  126. Assert.AreEqual ("123", c.ConvertValueToString (123.0, typeof (double)), "#3");
  127. }
  128. [Test]
  129. public void ConvertValueToStringEnum ()
  130. {
  131. string stringValue = c.ConvertValueToString (DemoEnum.Value2, typeof (DemoEnum));
  132. Assert.AreEqual ("Value2", stringValue);
  133. }
  134. // ConvertStringToValue
  135. [Test]
  136. public void ConvertStringToValueEnum ()
  137. {
  138. Assert.AreEqual (DemoEnum.Value3, (DemoEnum)c.ConvertStringToValue ("Value3", typeof(DemoEnum)));
  139. Assert.AreEqual (DemoEnum.Value2, (DemoEnum)c.ConvertStringToValue ("value2", typeof(DemoEnum)));
  140. }
  141. [Test]
  142. [ExpectedException (typeof (FormatException))]
  143. public void ConvertStringToValueInvalidCast ()
  144. {
  145. c.ConvertStringToValue ("ABC", typeof (char));
  146. }
  147. [Test]
  148. [ExpectedException (typeof (FormatException))]
  149. [Category ("NotWorking")]
  150. public void ConvertStringToValueInvalidCast2 ()
  151. {
  152. c.ConvertStringToValue ("-123", typeof (uint));
  153. }
  154. [Test]
  155. [ExpectedException (typeof (FormatException))]
  156. public void ConvertStringToValueInvalidCast4 ()
  157. {
  158. c.ConvertStringToValue ("123.45", typeof (int));
  159. }
  160. [Test]
  161. public void ConvertStringToValueNullToValueType ()
  162. {
  163. // hmm, it passes.
  164. Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
  165. }
  166. [Test]
  167. [ExpectedException (typeof (NotSupportedException))]
  168. public void ConvertStringToValueDbNull ()
  169. {
  170. c.ConvertStringToValue (null, typeof (DBNull));
  171. }
  172. [Test]
  173. public void ConvertStringToValueNullToString ()
  174. {
  175. Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
  176. }
  177. [Test]
  178. public void ConvertStringToValue ()
  179. {
  180. Assert.AreEqual ('A', c.ConvertStringToValue ("A", typeof (char)), "#1");
  181. Assert.AreEqual ("}}}", c.ConvertStringToValue ("}}}", typeof (string)), "#2");
  182. Assert.AreEqual (123.0, c.ConvertStringToValue ("123.0", typeof (double)), "#3");
  183. Assert.AreEqual (123.0, c.ConvertStringToValue ("123", typeof (double)), "#4");
  184. }
  185. // Types
  186. public enum DemoEnum
  187. {
  188. Value1,
  189. Value2,
  190. Value3,
  191. Value4,
  192. }
  193. [TypeConverter (typeof (MyTypeConverter))]
  194. class MyConvertible
  195. {
  196. }
  197. class MyTypeConverter : TypeConverter
  198. {
  199. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  200. {
  201. if (destinationType == typeof (string))
  202. return "hogehoge";
  203. throw new Exception ();
  204. }
  205. }
  206. }
  207. }