Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / System.ServiceModel.Web / Test / System.ServiceModel.Dispatcher / QueryStringConverterTest.cs
1 //
2 // QueryStringConverterTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
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
36 using CategoryAttribute = NUnit.Framework.CategoryAttribute;
37
38 namespace MonoTests.System.ServiceModel.Description
39 {
40         [TestFixture]
41         public class QueryStringConverterTest
42         {
43                 QueryStringConverter c;
44
45                 [SetUp]
46                 public void Setup ()
47                 {
48                         c = new QueryStringConverter ();
49                 }
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.IsFalse (c.CanConvert (typeof (XmlQualifiedName)), "#16");
70                         Assert.IsTrue (c.CanConvert (typeof (object)), "#17");
71                         Assert.IsFalse (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 (DemoEnum)), "#20");
75                 }
76
77                 // ConvertStringToValue
78
79                 [Test]
80                 [ExpectedException (typeof (InvalidCastException))]
81                 public void ConvertValueToStringInvalidCast ()
82                 {
83                         c.ConvertValueToString ("ABC", typeof (char));
84                 }
85
86                 [Test]
87                 [ExpectedException (typeof (InvalidCastException))]
88                 public void ConvertValueToStringInvalidCast2 ()
89                 {
90                         c.ConvertValueToString (123, typeof (string));
91                 }
92
93                 [Test]
94                 [ExpectedException (typeof (InvalidCastException))]
95                 public void ConvertValueToStringInvalidCast3 ()
96                 {
97                         c.ConvertValueToString ("123", typeof (int));
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (InvalidCastException))]
102                 public void ConvertValueToStringInvalidCast4 ()
103                 {
104                         c.ConvertValueToString (123.45, typeof (int));
105                 }
106
107                 [Test]
108                 [ExpectedException (typeof (InvalidCastException))]
109                 public void ConvertValueToStringInvalidCast5 ()
110                 {
111                         // umm...
112                         c.ConvertValueToString (123, typeof (double));
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (ArgumentNullException))]
117                 public void ConvertValueToStringNullToValueType ()
118                 {
119                         c.ConvertValueToString (null, typeof (char));
120                 }
121
122                 [Test]
123                 [ExpectedException (typeof (NotSupportedException))]
124                 public void ConvertValueToStringDbNull ()
125                 {
126                         c.ConvertValueToString (DBNull.Value, typeof (DBNull));
127                 }
128
129                 [Test]
130                 public void ConvertValueToStringNullToString ()
131                 {
132                         Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
133                 }
134
135                 [Test]
136                 public void ConvertValueToString ()
137                 {
138                         Assert.AreEqual ("A", c.ConvertValueToString ('A', typeof (char)), "#1");
139                         Assert.AreEqual ("}}}", c.ConvertValueToString ("}}}", typeof (string)), "#2");
140                         Assert.AreEqual ("123", c.ConvertValueToString (123.0, typeof (double)), "#3");
141                 }
142
143                 [Test]
144                 public void ConvertValueToStringEnum ()
145                 {
146                         string stringValue = c.ConvertValueToString (DemoEnum.Value2, typeof (DemoEnum));
147                         Assert.AreEqual ("Value2", stringValue);
148                 }
149
150                 // ConvertStringToValue
151
152                 [Test]
153                 public void ConvertStringToValueEnum ()
154                 {
155                         Assert.AreEqual (DemoEnum.Value3, (DemoEnum)c.ConvertStringToValue ("Value3", typeof(DemoEnum)));
156                         Assert.AreEqual (DemoEnum.Value2, (DemoEnum)c.ConvertStringToValue ("value2", typeof(DemoEnum)));
157                 }
158
159                 [Test]
160                 [ExpectedException (typeof (FormatException))]
161                 public void ConvertStringToValueInvalidCast ()
162                 {
163                         c.ConvertStringToValue ("ABC", typeof (char));
164                 }
165
166                 [Test]
167                 [ExpectedException (typeof (FormatException))]
168                 [Category ("NotWorking")]
169                 public void ConvertStringToValueInvalidCast2 ()
170                 {
171                         c.ConvertStringToValue ("-123", typeof (uint));
172                 }
173
174                 [Test]
175                 [ExpectedException (typeof (FormatException))]
176                 public void ConvertStringToValueInvalidCast4 ()
177                 {
178                         c.ConvertStringToValue ("123.45", typeof (int));
179                 }
180
181                 [Test]
182                 public void ConvertStringToValueNullToValueType ()
183                 {
184                         // hmm, it passes.
185                         Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
186                 }
187
188                 [Test]
189                 [ExpectedException (typeof (NotSupportedException))]
190                 public void ConvertStringToValueDbNull ()
191                 {
192                         c.ConvertStringToValue (null, typeof (DBNull));
193                 }
194
195                 [Test]
196                 public void ConvertStringToValueNullToString ()
197                 {
198                         Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
199                 }
200
201                 [Test]
202                 public void ConvertStringToValue ()
203                 {
204                         Assert.AreEqual ('A', c.ConvertStringToValue ("A", typeof (char)), "#1");
205                         Assert.AreEqual ("}}}", c.ConvertStringToValue ("}}}", typeof (string)), "#2");
206                         Assert.AreEqual (123.0, c.ConvertStringToValue ("123.0", typeof (double)), "#3");
207                         Assert.AreEqual (123.0, c.ConvertStringToValue ("123", typeof (double)), "#4");
208                 }
209
210                 // Types
211
212                 public enum DemoEnum
213                 {
214                         Value1,
215                         Value2,
216                         Value3,
217                         Value4,
218                 }
219
220                 [TypeConverter (typeof (MyTypeConverter))]
221                 class MyConvertible
222                 {
223                 }
224
225                 class MyTypeConverter : TypeConverter
226                 {
227                         public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
228                         {
229                                 if (destinationType == typeof (string))
230                                         return "hogehoge";
231                                 throw new Exception ();
232                         }
233                 }
234         }
235 }