Merge pull request #2619 from directhex/dont_remap_npgsql
[mono.git] / mcs / class / System.Configuration / Test / System.Configuration / IntegerValidatorTest.cs
1 //
2 // System.Configuration.IntegerValidatorTest.cs - Unit tests
3 // for System.Configuration.IntegerValidator.
4 //
5 // Author:
6 //      Chris Toshok  <toshok@ximian.com>
7 //      Andres G. Aragoneses  <andres@7digital.com>
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2012 7digital Media, Ltd (http://www.7digital.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32
33 using System;
34 using System.Xml;
35 using System.IO;
36 using System.Configuration;
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Configuration {
40         [TestFixture]
41         public class IntegerValidatorTest
42         {
43                 [Test]
44                 public void CanValidate ()
45                 {
46                         IntegerValidator v = new IntegerValidator (1, 1);
47
48                         Assert.IsFalse (v.CanValidate (typeof (TimeSpan)), "A1");
49                         Assert.IsTrue (v.CanValidate (typeof (int)), "A2");
50                         Assert.IsFalse (v.CanValidate (typeof (long)), "A3");
51                 }
52
53                 [Test]
54                 public void Validate_inRange ()
55                 {
56                         IntegerValidator v = new IntegerValidator (5000, 10000);
57                         v.Validate (7000);
58                 }
59
60                 [Test]
61                 public void Validate_Inclusive ()
62                 {
63                         IntegerValidator v = new IntegerValidator (5000, 10000, false);
64                         v.Validate (5000);
65                         v.Validate (10000);
66                 }
67
68                 [Test]
69                 public void Validate_Exclusive ()
70                 {
71                         IntegerValidator v = new IntegerValidator (5000, 10000, true);
72                         v.Validate (1000);
73                         v.Validate (15000);
74                 }
75
76                 [Test]
77                 [ExpectedException (typeof (ArgumentException))]
78                 public void Validate_Exclusive_fail1 ()
79                 {
80                         IntegerValidator v = new IntegerValidator (5000, 10000, true);
81                         v.Validate (5000);
82                 }
83
84                 [Test]
85                 [ExpectedException (typeof (ArgumentException))]
86                 public void Validate_Exclusive_fail2 ()
87                 {
88                         IntegerValidator v = new IntegerValidator (5000, 10000, true);
89                         v.Validate (10000);
90                 }
91
92                 [Test]
93                 [ExpectedException (typeof (ArgumentException))]
94                 public void Validate_Exclusive_fail3 ()
95                 {
96                         IntegerValidator v = new IntegerValidator (5000, 10000, true);
97                         v.Validate (7000);
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentException))]
102                 public void Validate_Resolution ()
103                 {
104                         IntegerValidator v = new IntegerValidator (20000,
105                                                                    50000,
106                                                                    false,
107                                                                    3000);
108
109                         v.Validate (40000);
110                 }
111
112                 #region BNC654721 https://bugzilla.novell.com/show_bug.cgi?id=654721
113                 public sealed class TestSection : ConfigurationSection
114                 {
115                         public void Load (string xml)
116                         {
117                                 Init ();
118                                 using (var sr = new StringReader (xml))
119                                 using (var reader = new XmlTextReader (sr))
120                                 {
121                                         DeserializeSection (reader);
122                                 }
123                         }
124
125                         [ConfigurationProperty ("integerValidatorMinValue")]
126                         public IntegerValidatorMinValueChildElement IntegerValidatorMinValue
127                         {
128                                 get { return (IntegerValidatorMinValueChildElement)base["integerValidatorMinValue"]; }
129                                 set { base["integerValidatorMinValue"] = value; }
130                         }
131
132                         [ConfigurationProperty ("integerValidatorMaxValue")]
133                         public IntegerValidatorMaxValueChildElement IntegerValidatorMaxValue
134                         {
135                                 get { return (IntegerValidatorMaxValueChildElement)base["integerValidatorMaxValue"]; }
136                                 set { base["integerValidatorMaxValue"] = value; }
137                         }
138                 }
139
140                 public sealed class IntegerValidatorMaxValueChildElement : ConfigurationElement
141                 {
142                         [ConfigurationProperty ("theProperty"), IntegerValidator (MaxValue = 100)]
143                         public int TheProperty
144                         {
145                                 get { return (int)base["theProperty"]; }
146                                 set { base ["theProperty"] = value; }
147                         }
148                 }
149
150                 public sealed class IntegerValidatorMinValueChildElement : ConfigurationElement
151                 {
152                         [ConfigurationProperty ("theProperty"), IntegerValidator (MinValue = 0)]
153                         public int TheProperty
154                         {
155                                 get { return (int)base["theProperty"]; }
156                                 set { base ["theProperty"] = value; }
157                         }
158                 }
159
160                 [Test]
161                 public void IntegerValidatorMinValueTest ()
162                 {
163                         var section = new TestSection ();
164                         section.Load (@"<someSection><integerValidatorMinValue theProperty=""15"" /></someSection>");
165                         Assert.AreEqual (15, section.IntegerValidatorMinValue.TheProperty);
166                 }
167
168                 [Test]
169                 public void IntegerValidatorMaxValueTest ()
170                 {
171                         var section = new TestSection ();
172                         section.Load (@"<someSection><integerValidatorMaxValue theProperty=""25"" /></someSection>");
173                         Assert.AreEqual (25, section.IntegerValidatorMaxValue.TheProperty);
174                 }
175                 #endregion
176         }
177 }
178