New test.
[mono.git] / mcs / class / System.Configuration / Test / System.Configuration / TimeSpanSecondsOrInfiniteConverterTest.cs
1 //
2 // System.Configuration.TimeSpanSecondsOrInfiniteConverterTest.cs - Unit tests
3 // for System.Configuration.TimeSpanSecondsOrInfiniteConverter.
4 //
5 // Author:
6 //      Chris Toshok  <toshok@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.Configuration;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Configuration {
37         [TestFixture]
38         public class TimeSpanSecondsOrInfiniteConverterTest
39         {
40                 [Test]
41                 public void CanConvertFrom ()
42                 {
43                         TimeSpanSecondsOrInfiniteConverter cv = new TimeSpanSecondsOrInfiniteConverter ();
44
45                         Assert.IsTrue (cv.CanConvertFrom (null, typeof (string)), "A1");
46                         Assert.IsFalse (cv.CanConvertFrom (null, typeof (TimeSpan)), "A2");
47                         Assert.IsFalse (cv.CanConvertFrom (null, typeof (int)), "A3");
48                         Assert.IsFalse (cv.CanConvertFrom (null, typeof (object)), "A4");
49                 }
50
51                 [Test]
52                 public void CanConvertTo ()
53                 {
54                         TimeSpanSecondsOrInfiniteConverter cv = new TimeSpanSecondsOrInfiniteConverter ();
55
56                         Assert.IsTrue (cv.CanConvertTo (null, typeof (string)), "A1");
57                         Assert.IsFalse (cv.CanConvertTo (null, typeof (TimeSpan)), "A2");
58                         Assert.IsFalse (cv.CanConvertTo (null, typeof (int)), "A3");
59                         Assert.IsFalse (cv.CanConvertTo (null, typeof (object)), "A4");
60                 }
61
62                 [Test]
63                 public void ConvertFrom ()
64                 {
65                         TimeSpanSecondsOrInfiniteConverter cv = new TimeSpanSecondsOrInfiniteConverter ();
66                         object o;
67
68                         /* make sure the TimeSpanSecondsConverter tests work here too */
69                         o = cv.ConvertFrom (null, null, "59");
70                         Assert.AreEqual (typeof (TimeSpan), o.GetType(), "A1");
71                         Assert.AreEqual ("00:00:59", o.ToString(), "A2");
72                         o = cv.ConvertFrom (null, null, "104");
73                         Assert.AreEqual ("00:01:44", o.ToString(), "A3");
74
75                         /* and now test infinity */
76                         o = cv.ConvertFrom (null, null, "Infinite");
77                         Assert.AreEqual (TimeSpan.MaxValue.ToString(), o.ToString(), "A3");
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (ArgumentException))]
82                 public void ConvertFrom_FormatError ()
83                 {
84                         TimeSpanSecondsOrInfiniteConverter cv = new TimeSpanSecondsOrInfiniteConverter ();
85                         object o;
86
87                         o = cv.ConvertFrom (null, null, "100.5");
88                         Assert.IsNull (o, "A1");
89                 }
90
91                 [Test]
92                 [ExpectedException (typeof (InvalidCastException))]
93                 public void ConvertFrom_TypeError ()
94                 {
95                         TimeSpanSecondsOrInfiniteConverter cv = new TimeSpanSecondsOrInfiniteConverter ();
96                         object o;
97
98                         o = cv.ConvertFrom (null, null, 59);
99                         Assert.IsNull (o, "A1");
100                 }
101
102                 [Test]
103                 public void ConvertTo ()
104                 {
105                         TimeSpanSecondsOrInfiniteConverter cv = new TimeSpanSecondsOrInfiniteConverter ();
106                         TimeSpan ts;
107
108                         ts = TimeSpan.FromSeconds (59);
109                         Assert.AreEqual ("59", cv.ConvertTo (null, null, ts, typeof (string)), "A1");
110
111                         ts = TimeSpan.FromSeconds (144);
112                         Assert.AreEqual ("144", cv.ConvertTo (null, null, ts, typeof (string)), "A2");
113                         
114                         /* infinity tests */
115                         Assert.AreEqual ("Infinite", cv.ConvertTo (null, null, TimeSpan.MaxValue, typeof (string)), "A4");
116                         Assert.AreEqual ("Infinite", cv.ConvertTo (null, null, new TimeSpan (Int64.MaxValue), typeof (string)), "A5");
117                         Assert.AreEqual ("922337203685", cv.ConvertTo (null, null, new TimeSpan (Int64.MaxValue - 1), typeof (string)), "A6");
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof (NullReferenceException))]\r
122 #if TARGET_JVM\r
123                 [Category ("NotWorking")]\r
124 #endif
125                 public void ConvertTo_NullError ()
126                 {
127                         TimeSpanSecondsOrInfiniteConverter cv = new TimeSpanSecondsOrInfiniteConverter ();
128
129                         Assert.AreEqual ("", cv.ConvertTo (null, null, null, typeof (string)), "A1");
130                 }
131
132                 [Test]
133                 [ExpectedException (typeof (ArgumentException))]
134                 public void ConvertTo_TypeError1 ()
135                 {
136                         TimeSpanSecondsOrInfiniteConverter cv = new TimeSpanSecondsOrInfiniteConverter ();
137
138                         Assert.AreEqual ("59", cv.ConvertTo (null, null, 59, typeof (string)), "A1");
139                 }
140
141                 [Test]
142                 public void ConvertTo_TypeError2 ()
143                 {
144                         TimeSpanSecondsOrInfiniteConverter cv = new TimeSpanSecondsOrInfiniteConverter ();
145                         TimeSpan ts;
146
147                         ts = TimeSpan.FromSeconds (59);
148
149                         Assert.AreEqual ("59", cv.ConvertTo (null, null, ts, typeof (int)), "A1");
150                         Assert.AreEqual ("59", cv.ConvertTo (null, null, ts, null), "A2");
151                 }
152
153         }
154 }
155
156 #endif