Mark tests as not working under TARGET_JVM
[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.ComponentModel;
35 using System.ComponentModel.Design.Serialization;
36 using System.Globalization;
37
38 namespace MonoTests.System {
39
40         class UriTypeDescriptorContext : ITypeDescriptorContext {
41
42                 public IContainer Container {
43                         get { throw new NotImplementedException (); }
44                 }
45
46                 public object Instance {
47                         get { throw new NotImplementedException (); }
48                 }
49
50                 public void OnComponentChanged ()
51                 {
52                         throw new NotImplementedException ();
53                 }
54
55                 public bool OnComponentChanging ()
56                 {
57                         throw new NotImplementedException ();
58                 }
59
60                 public PropertyDescriptor PropertyDescriptor {
61                         get { throw new NotImplementedException (); }
62                 }
63
64                 public object GetService (Type serviceType)
65                 {
66                         throw new NotImplementedException ();
67                 }
68         }
69
70         [TestFixture]
71         public class UriTypeConverterTest {
72
73                 private const string url = "http://www.mono-project.com/";
74                 private static Uri uri = new Uri (url);
75
76                 private UriTypeConverter converter;
77                 private UriTypeDescriptorContext context;
78
79                 [SetUp]
80                 public void SetUp ()
81                 {
82                         converter = new UriTypeConverter ();
83                         context = new UriTypeDescriptorContext ();
84                 }
85
86                 [Test]
87                 [ExpectedException (typeof (ArgumentNullException))]
88                 public void CanConvertFrom_Null ()
89                 {
90                         converter.CanConvertFrom (null);
91                 }
92
93                 [Test]
94                 public void CanConvertFrom ()
95                 {
96                         Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "string");
97                         Assert.IsTrue (converter.CanConvertFrom (typeof (Uri)), "Uri");
98                         Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "InstanceDescriptor");
99
100                         Assert.IsFalse (converter.CanConvertFrom (typeof (object)), "object");
101                         Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "int");
102                         Assert.IsFalse (converter.CanConvertFrom (typeof (char)), "char");
103                         Assert.IsFalse (converter.CanConvertFrom (typeof (DateTime)), "datetime");
104                 }
105
106                 [Test]
107                 [ExpectedException (typeof (ArgumentNullException))]
108                 public void CanConvertFrom_Null_Null ()
109                 {
110                         converter.CanConvertFrom (null, null);
111                 }
112
113                 [Test]
114                 public void CanConvertFrom_Null_Type ()
115                 {
116                         Assert.IsTrue (converter.CanConvertFrom (null, typeof (string)), "string");
117                         Assert.IsTrue (converter.CanConvertFrom (null, typeof (Uri)), "Uri");
118                         Assert.IsTrue (converter.CanConvertFrom (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
119
120                         Assert.IsFalse (converter.CanConvertFrom (null, typeof (object)), "object");
121                         Assert.IsFalse (converter.CanConvertFrom (null, typeof (int)), "int");
122                         Assert.IsFalse (converter.CanConvertFrom (null, typeof (char)), "char");
123                         Assert.IsFalse (converter.CanConvertFrom (null, typeof (DateTime)), "datetime");
124                 }
125
126                 [Test]
127                 public void CanConvertFrom_TypeDescriptorContext_Type ()
128                 {
129                         Assert.IsTrue (converter.CanConvertFrom (context, typeof (string)), "string");
130                         Assert.IsTrue (converter.CanConvertFrom (context, typeof (Uri)), "Uri");
131                         Assert.IsTrue (converter.CanConvertFrom (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
132
133                         Assert.IsFalse (converter.CanConvertFrom (context, typeof (object)), "object");
134                         Assert.IsFalse (converter.CanConvertFrom (context, typeof (int)), "int");
135                         Assert.IsFalse (converter.CanConvertFrom (context, typeof (char)), "char");
136                         Assert.IsFalse (converter.CanConvertFrom (context, typeof (DateTime)), "datetime");
137                 }
138
139                 [Test]
140                 public void CanConvertTo ()
141                 {
142                         Assert.IsFalse (converter.CanConvertTo (null), "null");
143
144                         Assert.IsTrue (converter.CanConvertTo (typeof (string)), "string");
145                         Assert.IsTrue (converter.CanConvertTo (typeof (Uri)), "Uri");
146                         Assert.IsTrue (converter.CanConvertTo (typeof (InstanceDescriptor)), "InstanceDescriptor");
147
148                         Assert.IsFalse (converter.CanConvertTo (typeof (object)), "object");
149                         Assert.IsFalse (converter.CanConvertTo (typeof (int)), "int");
150                         Assert.IsFalse (converter.CanConvertTo (typeof (char)), "char");
151                         Assert.IsFalse (converter.CanConvertTo (typeof (DateTime)), "datetime");
152                 }
153
154                 [Test]
155                 public void CanConvertTo_Null_Type ()
156                 {
157                         Assert.IsFalse (converter.CanConvertTo (null, null), "null");
158
159                         Assert.IsTrue (converter.CanConvertTo (null, typeof (string)), "string");
160                         Assert.IsTrue (converter.CanConvertTo (null, typeof (Uri)), "Uri");
161                         Assert.IsTrue (converter.CanConvertTo (null, typeof (InstanceDescriptor)), "InstanceDescriptor");
162
163                         Assert.IsFalse (converter.CanConvertTo (null, typeof (object)), "object");
164                         Assert.IsFalse (converter.CanConvertTo (null, typeof (int)), "int");
165                         Assert.IsFalse (converter.CanConvertTo (null, typeof (char)), "char");
166                         Assert.IsFalse (converter.CanConvertTo (null, typeof (DateTime)), "datetime");
167                 }
168
169                 [Test]
170                 public void CanConvertTo_TypeDescriptorContext_Type ()
171                 {
172                         Assert.IsTrue (converter.CanConvertTo (context, typeof (string)), "string");
173                         Assert.IsTrue (converter.CanConvertTo (context, typeof (Uri)), "Uri");
174                         Assert.IsTrue (converter.CanConvertTo (context, typeof (InstanceDescriptor)), "InstanceDescriptor");
175
176                         Assert.IsFalse (converter.CanConvertTo (context, typeof (object)), "object");
177                         Assert.IsFalse (converter.CanConvertTo (context, typeof (int)), "int");
178                         Assert.IsFalse (converter.CanConvertTo (context, typeof (char)), "char");
179                         Assert.IsFalse (converter.CanConvertTo (context, typeof (DateTime)), "datetime");
180                 }
181
182                 [Test]
183                 public void ConvertFrom ()
184                 {
185                         object o = converter.ConvertFrom (url);
186                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "url");
187
188                         o = converter.ConvertFrom (uri);
189                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
190
191                         o = converter.ConvertFrom (converter.ConvertTo (uri, typeof (InstanceDescriptor)));
192                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
193                 }
194
195                 [Test]
196                 [ExpectedException (typeof (NotSupportedException))]
197                 public void ConvertFrom_Bad ()
198                 {
199                         converter.ConvertFrom (new object ());
200                 }
201
202                 [Test]
203                 public void ConvertFrom_TypeDescriptorContext_Type ()
204                 {
205                         object o = converter.ConvertFrom (context, CultureInfo.CurrentCulture, url);
206                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "url");
207
208                         o = converter.ConvertFrom (context, CultureInfo.InvariantCulture, uri);
209                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
210
211                         o = converter.ConvertFrom (context, null, converter.ConvertTo (uri, typeof (InstanceDescriptor)));
212                         Assert.AreEqual (url, (o as Uri).AbsoluteUri, "uri");
213                 }
214
215                 [Test]
216                 [ExpectedException (typeof (NotSupportedException))]
217                 public void ConvertFrom_TypeDescriptorContext_Bad ()
218                 {
219                         converter.ConvertFrom (context, CultureInfo.InvariantCulture, new object ());
220                 }
221
222                 [Test]
223                 public void ConvertTo ()
224                 {
225                         object o = converter.ConvertTo (url, typeof (string));
226                         Assert.AreEqual (url, (o as String), "string-string");
227
228                         o = converter.ConvertTo (uri, typeof (string));
229                         Assert.AreEqual (url, (o as String), "uri-string");
230
231                         o = converter.ConvertTo (uri, typeof (Uri));
232                         Assert.AreEqual (uri, (o as Uri), "uri-uri");
233
234                         o = converter.ConvertTo (uri, typeof (InstanceDescriptor));
235                         Assert.IsTrue (o is InstanceDescriptor, "uri-InstanceDescriptor");
236
237                         InstanceDescriptor descriptor = (o as InstanceDescriptor);
238
239                         o = converter.ConvertTo (descriptor, typeof (string));
240                         Assert.AreEqual ("System.ComponentModel.Design.Serialization.InstanceDescriptor", (o as string), "InstanceDescriptor-string");
241
242                         // InstanceDescriptor to Uri or to InstanceDescriptor aren't supported either
243                 }
244
245                 [Test]
246                 [ExpectedException (typeof (NotSupportedException))]
247                 public void ConvertTo_String_to_Uri ()
248                 {
249                         converter.ConvertTo (url, typeof (Uri));
250                 }
251
252                 [Test]
253                 [ExpectedException (typeof (NotSupportedException))]
254                 public void ConvertTo_String_to_InstanceDescriptor ()
255                 {
256                         converter.ConvertTo (url, typeof (InstanceDescriptor));
257                 }
258
259                 [Test]
260                 public void ConvertTo_Bad ()
261                 {
262                         Assert.AreEqual ("System.Object", converter.ConvertTo (new object (), typeof (string)), "object");
263                         Assert.AreEqual ("4", converter.ConvertTo (4, typeof (string)), "int");
264                         Assert.AreEqual ("c", converter.ConvertTo ('c', typeof (string)), "char");
265                         Assert.AreEqual (String.Empty, converter.ConvertTo (null, typeof (string)), "null");
266                 }
267
268                 [Test]
269                 public void ConvertTo_TypeDescriptorContext ()
270                 {
271                         object o = converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (string));
272                         Assert.AreEqual (url, (o as String), "string-string");
273
274                         o = converter.ConvertTo (context, null, uri, typeof (string));
275                         Assert.AreEqual (url, (o as String), "uri-string");
276
277                         o = converter.ConvertTo (context, CultureInfo.InvariantCulture, uri, typeof (Uri));
278                         Assert.AreEqual (uri, (o as Uri), "uri-uri");
279
280                         o = converter.ConvertTo (context, CultureInfo.CurrentCulture, uri, typeof (InstanceDescriptor));
281                         Assert.IsTrue (o is InstanceDescriptor, "uri-InstanceDescriptor");
282
283                         InstanceDescriptor descriptor = (o as InstanceDescriptor);
284
285                         o = converter.ConvertTo (context, CultureInfo.InvariantCulture, descriptor, typeof (string));
286                         Assert.AreEqual ("System.ComponentModel.Design.Serialization.InstanceDescriptor", (o as string), "InstanceDescriptor-string");
287
288                         // InstanceDescriptor to Uri or to InstanceDescriptor aren't supported either
289                 }
290
291                 [Test]
292                 [ExpectedException (typeof (NotSupportedException))]
293                 public void ConvertTo_TypeDescriptorContext_String_to_Uri ()
294                 {
295                         converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (Uri));
296                 }
297
298                 [Test]
299                 [ExpectedException (typeof (NotSupportedException))]
300                 public void ConvertTo_TypeDescriptorContext_String_to_InstanceDescriptor ()
301                 {
302                         converter.ConvertTo (context, CultureInfo.InvariantCulture, url, typeof (InstanceDescriptor));
303                 }
304
305                 [Test]
306                 public void ConvertTo_TypeDescriptorContext_Bad ()
307                 {
308                         Assert.AreEqual ("System.Object", converter.ConvertTo (context, null, new object (), typeof (string)), "object");
309                         Assert.AreEqual ("4", converter.ConvertTo (context, CultureInfo.CurrentCulture, 4, typeof (string)), "int");
310                         Assert.AreEqual ("c", converter.ConvertTo (context, CultureInfo.InvariantCulture, 'c', typeof (string)), "char");
311                         Assert.AreEqual (String.Empty, converter.ConvertTo (null, null, null, typeof (string)), "null");
312                 }
313
314                 [Test]
315                 public void IsValid ()
316                 {
317                         Assert.IsFalse (converter.IsValid (null), "null");
318
319                         // LAMESPEC: all strings are accepted
320                         Assert.IsTrue (converter.IsValid (String.Empty), "empty");
321                         Assert.IsTrue (converter.IsValid ("\\"), "\\");
322                         Assert.IsTrue (converter.IsValid ("#%#%#"), "#%#%#");
323                         Assert.IsTrue (converter.IsValid (".."), "..");
324                         Assert.IsTrue (converter.IsValid (url), url);
325
326                         Assert.IsTrue (converter.IsValid (uri), "uri");
327
328                         Assert.IsFalse (converter.IsValid (new object ()), "object");
329                         Assert.IsFalse (converter.IsValid (4), "int");
330                         Assert.IsFalse (converter.IsValid ('c'), "char");
331                         Assert.IsFalse (converter.IsValid (DateTime.Now), "datetime");
332                 }
333         }
334 }
335
336 #endif