2009-11-20 Atsushi Enomoto <atsushi@ximian.com>
[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                 }
75
76                 // ConvertStringToValue
77
78                 [Test]
79                 [ExpectedException (typeof (InvalidCastException))]
80                 public void ConvertValueToStringInvalidCast ()
81                 {
82                         c.ConvertValueToString ("ABC", typeof (char));
83                 }
84
85                 [Test]
86                 [ExpectedException (typeof (InvalidCastException))]
87                 public void ConvertValueToStringInvalidCast2 ()
88                 {
89                         c.ConvertValueToString (123, typeof (string));
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (InvalidCastException))]
94                 public void ConvertValueToStringInvalidCast3 ()
95                 {
96                         c.ConvertValueToString ("123", typeof (int));
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (InvalidCastException))]
101                 public void ConvertValueToStringInvalidCast4 ()
102                 {
103                         c.ConvertValueToString (123.45, typeof (int));
104                 }
105
106                 [Test]
107                 [ExpectedException (typeof (InvalidCastException))]
108                 public void ConvertValueToStringInvalidCast5 ()
109                 {
110                         // umm...
111                         c.ConvertValueToString (123, typeof (double));
112                 }
113
114                 [Test]
115                 [ExpectedException (typeof (ArgumentNullException))]
116                 public void ConvertValueToStringNullToValueType ()
117                 {
118                         c.ConvertValueToString (null, typeof (char));
119                 }
120
121                 [Test]
122                 [ExpectedException (typeof (NotSupportedException))]
123                 public void ConvertValueToStringDbNull ()
124                 {
125                         c.ConvertValueToString (DBNull.Value, typeof (DBNull));
126                 }
127
128                 [Test]
129                 public void ConvertValueToStringNullToString ()
130                 {
131                         Assert.IsNull (c.ConvertValueToString (null, typeof (string)));
132                 }
133
134                 [Test]
135                 public void ConvertValueToString ()
136                 {
137                         Assert.AreEqual ("A", c.ConvertValueToString ('A', typeof (char)), "#1");
138                         Assert.AreEqual ("}}}", c.ConvertValueToString ("}}}", typeof (string)), "#2");
139                         Assert.AreEqual ("123", c.ConvertValueToString (123.0, typeof (double)), "#3");
140                 }
141
142                 // ConvertStringToValue
143
144                 [Test]
145                 [ExpectedException (typeof (FormatException))]
146                 public void ConvertStringToValueInvalidCast ()
147                 {
148                         c.ConvertStringToValue ("ABC", typeof (char));
149                 }
150
151                 [Test]
152                 [ExpectedException (typeof (FormatException))]
153                 [Category ("NotWorking")]
154                 public void ConvertStringToValueInvalidCast2 ()
155                 {
156                         c.ConvertStringToValue ("-123", typeof (uint));
157                 }
158
159                 [Test]
160                 [ExpectedException (typeof (FormatException))]
161                 public void ConvertStringToValueInvalidCast4 ()
162                 {
163                         c.ConvertStringToValue ("123.45", typeof (int));
164                 }
165
166                 [Test]
167                 public void ConvertStringToValueNullToValueType ()
168                 {
169                         // hmm, it passes.
170                         Assert.AreEqual (default (char), c.ConvertStringToValue (null, typeof (char)));
171                 }
172
173                 [Test]
174                 [ExpectedException (typeof (NotSupportedException))]
175                 public void ConvertStringToValueDbNull ()
176                 {
177                         c.ConvertStringToValue (null, typeof (DBNull));
178                 }
179
180                 [Test]
181                 public void ConvertStringToValueNullToString ()
182                 {
183                         Assert.IsNull (c.ConvertStringToValue (null, typeof (string)));
184                 }
185
186                 [Test]
187                 public void ConvertStringToValue ()
188                 {
189                         Assert.AreEqual ('A', c.ConvertStringToValue ("A", typeof (char)), "#1");
190                         Assert.AreEqual ("}}}", c.ConvertStringToValue ("}}}", typeof (string)), "#2");
191                         Assert.AreEqual (123.0, c.ConvertStringToValue ("123.0", typeof (double)), "#3");
192                         Assert.AreEqual (123.0, c.ConvertStringToValue ("123", typeof (double)), "#4");
193                 }
194
195                 // Types
196
197                 [TypeConverter (typeof (MyTypeConverter))]
198                 class MyConvertible
199                 {
200                 }
201
202                 class MyTypeConverter : TypeConverter
203                 {
204                         public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
205                         {
206                                 if (destinationType == typeof (string))
207                                         return "hogehoge";
208                                 throw new Exception ();
209                         }
210                 }
211         }
212 }