Merge pull request #260 from pcc/topmost
[mono.git] / mcs / class / System / Test / System / UriTypeConverterTest.cs
1 //
2 // UriTypeConverterTest.cs - Unit tests for System.UriTypeConverter
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 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
29 #if NET_2_0
30
31 using NUnit.Framework;
32
33 using System;
34 using System.IO;
35 using System.ComponentModel;
36 using System.ComponentModel.Design.Serialization;
37 using System.Globalization;
38
39 namespace MonoTests.System {
40
41         class UriTypeDescriptorContext : ITypeDescriptorContext {
42
43                 public IContainer Container {
44                         get { throw new NotImplementedException (); }
45                 }
46
47                 public object Instance {
48                         get { throw new NotImplementedException (); }
49                 }
50
51                 public void OnComponentChanged ()
52                 {
53                         throw new NotImplementedException ();
54                 }
55
56                 public bool OnComponentChanging ()
57                 {
58                         throw new NotImplementedException ();
59                 }
60
61                 public PropertyDescriptor PropertyDescriptor {
62                         get { throw new NotImplementedException (); }
63                 }
64
65                 public object GetService (Type serviceType)
66                 {
67                         throw new NotImplementedException ();
68                 }
69         }
70
71         [TestFixture]
72         public class UriTypeConverterTest {
73
74                 private const string url = "http://www.mono-project.com/";
75                 private static Uri uri = new Uri (url);
76
77                 private UriTypeConverter converter;
78                 private UriTypeDescriptorContext context;
79
80                 protected bool isWin32 = false;
81                 
82                 [SetUp]
83                 public void SetUp ()
84                 {
85                         converter = new UriTypeConverter ();
86                         context = new UriTypeDescriptorContext ();
87                         isWin32 = (Path.DirectorySeparatorChar == '\\');
88                 }
89
90                 [Test]
91                 [ExpectedException (typeof (ArgumentNullException))]
92                 public void CanConvertFrom_Null ()
93                 {
94                         converter.CanConvertFrom (null);
95                 }
96
97                 [Test]
98                 public void CanConvertFrom ()
99                 {
100                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "string");
101                         Assert.IsTrue (converter.CanConvertFrom (typeof (Uri)), "Uri");
102 #if MOBILE
103                         Assert.IsFalse (converter.CanConvertFrom (typeof (InstanceDescriptor)), "InstanceDescriptor");
104 #else
105                         Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "InstanceDescriptor");
106 #endif
107                         Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "object");
108                         Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "int");
109                         Assert.IsFalse (converter.CanConvertFrom (typeof (char)), "char");
110                         Assert.IsFalse (converter.CanConvertFrom (typeof (DateTime)), "datetime");
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (ArgumentNullException))]
115                 public void CanConvertFrom_Null_Null ()
116                 {
117                         converter.CanConvertFrom (null, null);
118                 }
119
120                 [Test]
121                 public void CanConvertFrom_Null_Type ()
122                 {
123                         Assert.IsTrue (converter.CanConvertFrom (null, typeof (string)), "string");
124                         Assert.IsTrue (converter.CanConvertFrom (null, typeof (Uri)), "Uri");
125 #if MOBILE
126                         Assert.IsFalse (converter.CanConvertFrom (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
127 #else
128                         Assert.IsTrue (converter.CanConvertFrom (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
129 #endif
130                         Assert.IsFalse (converter.CanConvertFrom (null, typeof (object)), "object");
131                         Assert.IsFalse (converter.CanConvertFrom (null, typeof (int)), "int");
132                         Assert.IsFalse (converter.CanConvertFrom (null, typeof (char)), "char");
133                         Assert.IsFalse (converter.CanConvertFrom (null, typeof (DateTime)), "datetime");
134                 }
135
136                 [Test]
137                 public void CanConvertFrom_TypeDescriptorContext_Type ()
138                 {
139                         Assert.IsTrue (converter.CanConvertFrom (context, typeof (string)), "string");
140                         Assert.IsTrue (converter.CanConvertFrom (context, typeof (Uri)), "Uri");
141 #if MOBILE
142                         Assert.IsFalse (converter.CanConvertFrom (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
143 #else
144                         Assert.IsTrue (converter.CanConvertFrom (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
145 #endif
146                         Assert.IsFalse (converter.CanConvertFrom (context, typeof (object)), "object");
147                         Assert.IsFalse (converter.CanConvertFrom (context, typeof (int)), "int");
148                         Assert.IsFalse (converter.CanConvertFrom (context, typeof (char)), "char");
149                         Assert.IsFalse (converter.CanConvertFrom (context, typeof (DateTime)), "datetime");
150                 }
151
152                 [Test]
153                 public void CanConvertTo ()
154                 {
155                         Assert.IsFalse (converter.CanConvertTo (null), "null");
156
157                         Assert.IsTrue (converter.CanConvertTo (typeof (string)), "string");
158                         Assert.IsTrue (converter.CanConvertTo (typeof (Uri)), "Uri");
159 #if MOBILE
160                         Assert.IsFalse (converter.CanConvertTo (typeof (InstanceDescriptor)), "InstanceDescriptor");
161 #else
162                         Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "InstanceDescriptor");
163 #endif
164                         Assert.IsFalse (converter.CanConvertTo (typeof (object)), "object");
165                         Assert.IsFalse (converter.CanConvertTo (typeof (int)), "int");
166                         Assert.IsFalse (converter.CanConvertTo (typeof (char)), "char");
167                         Assert.IsFalse (converter.CanConvertTo (typeof (DateTime)), "datetime");
168                 }
169
170                 [Test]
171                 public void CanConvertTo_Null_Type ()
172                 {
173                         Assert.IsFalse (converter.CanConvertTo (null, null), "null");
174
175                         Assert.IsTrue (converter.CanConvertTo (null, typeof (string)), "string");
176                         Assert.IsTrue (converter.CanConvertTo (null, typeof (Uri)), "Uri");
177 #if MOBILE
178                         Assert.IsFalse (converter.CanConvertTo (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
179 #else
180                         Assert.IsTrue (converter.CanConvertTo (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
181 #endif
182                         Assert.IsFalse (converter.CanConvertTo (null, typeof (object)), "object");
183                         Assert.IsFalse (converter.CanConvertTo (null, typeof (int)), "int");
184                         Assert.IsFalse (converter.CanConvertTo (null, typeof (char)), "char");
185                         Assert.IsFalse (converter.CanConvertTo (null, typeof (DateTime)), "datetime");
186                 }
187
188                 [Test]
189                 public void CanConvertTo_TypeDescriptorContext_Type ()
190                 {
191                         Assert.IsTrue (converter.CanConvertTo (context, typeof (string)), "string");
192                         Assert.IsTrue (converter.CanConvertTo (context, typeof (Uri)), "Uri");
193 #if MOBILE
194                         Assert.IsFalse (converter.CanConvertTo (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
195 #else
196                         Assert.IsTrue (converter.CanConvertTo (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
197 #endif
198                         Assert.IsFalse (converter.CanConvertTo (context, typeof (object)), "object");
199                         Assert.IsFalse (converter.CanConvertTo (context, typeof (int)), "int");
200                         Assert.IsFalse (converter.CanConvertTo (context, typeof (char)), "char");
201                         Assert.IsFalse (converter.CanConvertTo (context, typeof (DateTime)), "datetime");
202                 }
203
204                 [Test]
205                 public void ConvertFrom ()
206                 {
207                         object o = converter.ConvertFrom (url);
208                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "url");
209
210                         o = converter.ConvertFrom (uri);
211                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
212
213 #if MOBILE
214                         o = converter.ConvertFrom ("");
215                         Assert.IsNull (o, "null");
216 #else
217                         o = converter.ConvertFrom (converter.ConvertTo (uri, typeof (InstanceDescriptor)));
218                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
219
220                         o = converter.ConvertFrom ("");
221                         Assert.IsNotNull (o, "not null");
222 #endif
223                 }
224
225                 [Test]
226                 public void ConvertFromString ()
227                 {
228                         object o = converter.ConvertFrom ("~/SomeUri.txt");
229                         Assert.IsFalse ((o as Uri).IsAbsoluteUri, "CFS_01");
230                         Assert.AreEqual ("~/SomeUri.txt", (o as Uri).ToString (), "CFS_02");
231                         
232                         o = converter.ConvertFrom ("/SomeUri.txt");
233                         if (isWin32) {
234                                 Assert.IsFalse ((o as Uri).IsAbsoluteUri, "CFS_03_WIN");
235                                 Assert.AreEqual ("/SomeUri.txt", (o as Uri).ToString (), "CFS_04_WIN");
236                         } else {
237                                 Assert.IsTrue ((o as Uri).IsAbsoluteUri, "CFS_03_UNIX");
238                                 Assert.AreEqual ("file:///SomeUri.txt", (o as Uri).ToString (), "CFS_04_UNIX");
239                         }
240                 }
241                 
242                 [Test]
243                 [ExpectedException (typeof (NotSupportedException))]
244                 public void ConvertFrom_Bad ()
245                 {
246                         converter.ConvertFrom (new object ());
247                 }
248
249                 [Test]
250                 public void ConvertFrom_TypeDescriptorContext_Type ()
251                 {
252                         object o = converter.ConvertFrom (context, CultureInfo.CurrentCulture, url);
253                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "url");
254
255                         o = converter.ConvertFrom (context, CultureInfo.InvariantCulture, uri);
256                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
257
258 #if !MOBILE
259                         o = converter.ConvertFrom (context, null, converter.ConvertTo (uri, typeof (InstanceDescriptor)));
260                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
261 #endif
262                 }
263
264                 [Test]
265                 [ExpectedException (typeof (NotSupportedException))]
266                 public void ConvertFrom_TypeDescriptorContext_Bad ()
267                 {
268                         converter.ConvertFrom (context, CultureInfo.InvariantCulture, new object ());
269                 }
270
271                 [Test]
272                 public void ConvertTo ()
273                 {
274                         object o;
275                         o = converter.ConvertTo (uri, typeof (Uri));
276                         Assert.AreEqual (uri, (o as Uri), "uri-uri");
277
278                         o = converter.ConvertTo (uri, typeof (string));
279                         Assert.AreEqual (url, (o as String), "uri-string");
280
281 #if MOBILE
282                         try {
283                                 converter.ConvertTo (url, typeof (string));
284                                 Assert.Fail ("string-string");
285                         } catch (NotSupportedException) {
286                         }
287
288                         try {
289                                 converter.ConvertTo (uri, typeof (InstanceDescriptor));
290                                 Assert.Fail ("uri-InstanceDescriptor");
291                         } catch (NotSupportedException) {
292                         }
293 #else
294                         o = converter.ConvertTo (url, typeof (string));
295                         Assert.AreEqual (url, (o as String), "string-string");
296
297                         o = converter.ConvertTo (uri, typeof (InstanceDescriptor));
298                         Assert.IsTrue (o is InstanceDescriptor, "uri-InstanceDescriptor");
299
300                         InstanceDescriptor descriptor = (o as InstanceDescriptor);
301
302                         o = converter.ConvertTo (descriptor, typeof (string));
303                         Assert.AreEqual ("System.ComponentModel.Design.Serialization.InstanceDescriptor", (o as string), "InstanceDescriptor-string");
304 #endif
305                         // InstanceDescriptor to Uri or to InstanceDescriptor aren't supported either
306                 }
307
308                 [Test]
309                 [ExpectedException (typeof (NotSupportedException))]
310                 public void ConvertTo_String_to_Uri ()
311                 {
312                         converter.ConvertTo (url, typeof (Uri));
313                 }
314
315                 [Test]
316                 [ExpectedException (typeof (NotSupportedException))]
317                 public void ConvertTo_String_to_InstanceDescriptor ()
318                 {
319                         converter.ConvertTo (url, typeof (InstanceDescriptor));
320                 }
321
322                 [Test]
323                 public void ConvertTo_Bad ()
324                 {
325 #if MOBILE
326                         try {
327                                 converter.ConvertTo (new object (), typeof (string));
328                                 Assert.Fail ("object");
329                         } catch (NotSupportedException) {
330                         }
331 #else
332                         Assert.AreEqual ("System.Object", converter.ConvertTo (new object (), typeof (string)), "object");
333                         Assert.AreEqual ("4", converter.ConvertTo (4, typeof (string)), "int");
334                         Assert.AreEqual ("c", converter.ConvertTo ('c', typeof (string)), "char");
335                         Assert.AreEqual (String.Empty, converter.ConvertTo (null, typeof (string)), "null");
336 #endif
337                 }
338
339                 [Test]
340                 public void ConvertTo_TypeDescriptorContext ()
341                 {
342                         object o;
343
344                         o = converter.ConvertTo (context, null, uri, typeof (string));
345                         Assert.AreEqual (url, (o as String), "uri-string");
346
347                         o = converter.ConvertTo (context, CultureInfo.InvariantCulture, uri, typeof (Uri));
348                         Assert.AreEqual (uri, (o as Uri), "uri-uri");
349
350 #if MOBILE
351                         try {
352                                 o = converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (string));
353                                 Assert.AreEqual (url, (o as String), "string-string");
354                         } catch (NotSupportedException) {
355                         }
356 #else
357                         o = converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (string));
358                         Assert.AreEqual (url, (o as String), "string-string");
359
360                         o = converter.ConvertTo (context, CultureInfo.CurrentCulture, uri, typeof (InstanceDescriptor));
361                         Assert.IsTrue (o is InstanceDescriptor, "uri-InstanceDescriptor");
362
363                         InstanceDescriptor descriptor = (o as InstanceDescriptor);
364
365                         o = converter.ConvertTo (context, CultureInfo.InvariantCulture, descriptor, typeof (string));
366                         Assert.AreEqual ("System.ComponentModel.Design.Serialization.InstanceDescriptor", (o as string), "InstanceDescriptor-string");
367 #endif
368                         // InstanceDescriptor to Uri or to InstanceDescriptor aren't supported either
369                 }
370
371                 [Test]
372                 [ExpectedException (typeof (NotSupportedException))]
373                 public void ConvertTo_TypeDescriptorContext_String_to_Uri ()
374                 {
375                         converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (Uri));
376                 }
377
378                 [Test]
379                 [ExpectedException (typeof (NotSupportedException))]
380                 public void ConvertTo_TypeDescriptorContext_String_to_InstanceDescriptor ()
381                 {
382                         converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (InstanceDescriptor));
383                 }
384
385                 [Test]
386                 public void ConvertTo_TypeDescriptorContext_Bad ()
387                 {
388 #if MOBILE
389                         try {
390                                 converter.ConvertTo (context, null, new object (), typeof (string));
391                                 Assert.Fail ("object");
392                         } catch (NotSupportedException) {
393                         }
394 #else
395                         Assert.AreEqual ("System.Object", converter.ConvertTo (context, null, new object (), typeof (string)), "object");
396                         Assert.AreEqual ("4", converter.ConvertTo (context, CultureInfo.CurrentCulture, 4, typeof (string)), "int");
397                         Assert.AreEqual ("c", converter.ConvertTo (context, CultureInfo.InvariantCulture, 'c', typeof (string)), "char");
398                         Assert.AreEqual (String.Empty, converter.ConvertTo (null, null, null, typeof (string)), "null");
399 #endif
400                 }
401
402                 [Test]
403                 public void IsValid ()
404                 {
405                         Assert.IsFalse (converter.IsValid (null), "null");
406
407                         // LAMESPEC: all strings are accepted
408                         Assert.IsTrue (converter.IsValid (String.Empty), "empty");
409                         Assert.IsTrue (converter.IsValid ("\\"), "\\");
410                         Assert.IsTrue (converter.IsValid ("#%#%#"), "#%#%#");
411                         Assert.IsTrue (converter.IsValid (".."), "..");
412                         Assert.IsTrue (converter.IsValid (url), url);
413
414                         Assert.IsTrue (converter.IsValid (uri), "uri");
415
416                         Assert.IsFalse (converter.IsValid (new object ()), "object");
417                         Assert.IsFalse (converter.IsValid (4), "int");
418                         Assert.IsFalse (converter.IsValid ('c'), "char");
419                         Assert.IsFalse (converter.IsValid (DateTime.Now), "datetime");
420                 }
421         }
422 }
423
424 #endif