1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Components
.Validator
.Tests
.ValidatorTests
18 using System
.Globalization
;
19 using System
.Threading
;
20 using NUnit
.Framework
;
23 public class RangeValidatorTestCase
25 private RangeValidator validatorIntLow
,
27 validatorIntLowOrHigh
,
30 validatorDecimalLowOrHigh
,
32 validatorDateTimeHigh
,
33 validatorDateTimeLowOrHigh
,
36 validatorStringLowOrHigh
;
38 private TestTargetInt intTarget
;
39 private TestTargetDecimal decimalTarget
;
40 private TestTargetDateTime dateTimeTarget
;
41 private TestTargetString stringTarget
;
46 Thread
.CurrentThread
.CurrentCulture
= Thread
.CurrentThread
.CurrentUICulture
= new CultureInfo("en-us");
49 validatorIntLow
= new RangeValidator(0, int.MaxValue
);
50 validatorIntLow
.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt
).GetProperty("TargetField"));
52 validatorIntHigh
= new RangeValidator(int.MinValue
, 0);
53 validatorIntHigh
.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt
).GetProperty("TargetField"));
55 validatorIntLowOrHigh
= new RangeValidator(RangeValidationType
.Integer
, "-1", "1");
56 validatorIntLowOrHigh
.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt
).GetProperty("TargetField"));
58 intTarget
= new TestTargetInt();
61 validatorDecimalLow
= new RangeValidator(0, decimal.MaxValue
);
62 validatorDecimalLow
.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt
).GetProperty("TargetField"));
64 validatorDecimalHigh
= new RangeValidator(decimal.MinValue
, 0);
65 validatorDecimalHigh
.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt
).GetProperty("TargetField"));
67 validatorDecimalLowOrHigh
= new RangeValidator(RangeValidationType
.Decimal
, "-1", "1");
68 validatorDecimalLowOrHigh
.Initialize(new CachedValidationRegistry(), typeof(TestTargetInt
).GetProperty("TargetField"));
70 decimalTarget
= new TestTargetDecimal();
72 // DateTime validation
73 validatorDateTimeLow
= new RangeValidator(DateTime
.Now
, DateTime
.MaxValue
);
74 validatorDateTimeLow
.Initialize(new CachedValidationRegistry(), typeof(TestTargetDateTime
).GetProperty("TargetField"));
76 validatorDateTimeHigh
= new RangeValidator(DateTime
.MinValue
, DateTime
.Now
);
77 validatorDateTimeHigh
.Initialize(new CachedValidationRegistry(), typeof(TestTargetDateTime
).GetProperty("TargetField"));
79 validatorDateTimeLowOrHigh
= new RangeValidator(RangeValidationType
.DateTime
, "2000-01-01", "2099-12-31");
80 validatorDateTimeLowOrHigh
.Initialize(new CachedValidationRegistry(), typeof(TestTargetDateTime
).GetProperty("TargetField"));
82 dateTimeTarget
= new TestTargetDateTime();
85 validatorStringLow
= new RangeValidator("bbb", String
.Empty
);
86 validatorStringLow
.Initialize(new CachedValidationRegistry(), typeof(TestTargetString
).GetProperty("TargetField"));
88 validatorStringHigh
= new RangeValidator(String
.Empty
, "yyy");
89 validatorStringHigh
.Initialize(new CachedValidationRegistry(), typeof(TestTargetString
).GetProperty("TargetField"));
91 validatorStringLowOrHigh
= new RangeValidator(RangeValidationType
.String
, 'b'.ToString(), 'y'.ToString());
92 validatorStringLowOrHigh
.Initialize(new CachedValidationRegistry(), typeof(TestTargetString
).GetProperty("TargetField"));
94 stringTarget
= new TestTargetString();
97 public class TestTargetInt
99 private int targetField
;
101 public int TargetField
103 get { return targetField; }
104 set { targetField = value; }
108 public class TestTargetDecimal
110 private decimal targetField
;
112 public decimal TargetField
114 get { return targetField; }
115 set { targetField = value; }
120 public class TestTargetDateTime
122 private DateTime targetField
;
124 public DateTime TargetField
126 get { return targetField; }
127 set { targetField = value; }
131 public class TestTargetString
133 private string targetField
;
135 public string TargetField
137 get { return targetField; }
138 set { targetField = value; }
142 #region Integer range tests
145 public void RangeIntTooLowValidator()
147 //fail when compare to non-number
148 Assert
.IsFalse(validatorIntLow
.IsValid(intTarget
, "abc"));
149 //fail when compare to number too low
150 Assert
.IsFalse(validatorIntLow
.IsValid(intTarget
, -1));
151 //pass when compare to number not too low
152 Assert
.IsTrue(validatorIntLow
.IsValid(intTarget
, 1));
156 public void RangeIntTooHighValidator()
158 //fail when compare to non-number
159 Assert
.IsFalse(validatorIntHigh
.IsValid(intTarget
, "abc"));
160 //fail when compare to number too high
161 Assert
.IsFalse(validatorIntHigh
.IsValid(intTarget
, 1));
162 //pass when compare to number not too high
163 Assert
.IsTrue(validatorIntHigh
.IsValid(intTarget
, -1));
167 public void RangeIntTooLowOrHighValidator()
169 //fail when compare to non-number
170 Assert
.IsFalse(validatorIntLowOrHigh
.IsValid(intTarget
, "abc"));
171 //fail when compare to number too low
172 Assert
.IsFalse(validatorIntLowOrHigh
.IsValid(intTarget
, -2));
173 //fail when compare to number too high
174 Assert
.IsFalse(validatorIntLowOrHigh
.IsValid(intTarget
, 2));
175 //pass when compare to number not too high
176 Assert
.IsTrue(validatorIntLowOrHigh
.IsValid(intTarget
, 0));
181 #region Decimal range tests
184 public void RangeDecimalTooLowValidator()
186 //fail when compare to non-number
187 Assert
.IsFalse(validatorDecimalLow
.IsValid(decimalTarget
, "abc"));
188 //fail when compare to number too low
189 Assert
.IsFalse(validatorDecimalLow
.IsValid(decimalTarget
, -1));
190 //pass when compare to number not too low
191 Assert
.IsTrue(validatorDecimalLow
.IsValid(decimalTarget
, 1));
195 public void RangeDecimalTooHighValidator()
197 //fail when compare to non-number
198 Assert
.IsFalse(validatorDecimalHigh
.IsValid(decimalTarget
, "abc"));
199 //fail when compare to number too high
200 Assert
.IsFalse(validatorDecimalHigh
.IsValid(decimalTarget
, 1));
201 //pass when compare to number not too high
202 Assert
.IsTrue(validatorDecimalHigh
.IsValid(decimalTarget
, -1));
206 public void RangeDecimalTooLowOrHighValidator()
208 //fail when compare to non-number
209 Assert
.IsFalse(validatorDecimalLowOrHigh
.IsValid(decimalTarget
, "abc"));
210 //fail when compare to number too low
211 Assert
.IsFalse(validatorDecimalLowOrHigh
.IsValid(decimalTarget
, -2));
212 //fail when compare to number too high
213 Assert
.IsFalse(validatorDecimalLowOrHigh
.IsValid(decimalTarget
, 2));
214 //pass when compare to number not too high
215 Assert
.IsTrue(validatorDecimalLowOrHigh
.IsValid(decimalTarget
, 0));
220 #region DateTime range tests
223 public void RangeDateTimeTooLowValidator()
225 //fail when compare to non-date
226 Assert
.IsFalse(validatorDateTimeLow
.IsValid(dateTimeTarget
, "abc"));
227 //fail when compare to date too low
228 Assert
.IsFalse(validatorDateTimeLow
.IsValid(dateTimeTarget
, DateTime
.Now
.AddDays(-1)));
229 //pass when compare to date not too low
230 Assert
.IsTrue(validatorDateTimeLow
.IsValid(dateTimeTarget
, DateTime
.Now
.AddDays(1)));
234 public void RangeDateTimeTooHighValidator()
236 //fail when compare to non-date
237 Assert
.IsFalse(validatorDateTimeHigh
.IsValid(dateTimeTarget
, "abc"));
238 //fail when compare to number too high
239 Assert
.IsFalse(validatorDateTimeHigh
.IsValid(dateTimeTarget
, DateTime
.Now
.AddDays(1)));
240 //pass when compare to date not too high
241 Assert
.IsTrue(validatorDateTimeHigh
.IsValid(dateTimeTarget
, DateTime
.Now
.AddDays(-1)));
245 public void RangeDateTimeTooLowOrHighValidator()
247 //fail when compare to non-date
248 Assert
.IsFalse(validatorDateTimeLowOrHigh
.IsValid(dateTimeTarget
, "abc"));
249 //fail when compare to date too low
250 Assert
.IsFalse(validatorDateTimeLowOrHigh
.IsValid(dateTimeTarget
, new DateTime(1999, 01, 01)));
251 //fail when compare to date too high
252 Assert
.IsFalse(validatorDateTimeLowOrHigh
.IsValid(dateTimeTarget
, new DateTime(2100, 01, 01)));
253 //pass when compare to date not too low or high
254 Assert
.IsTrue(validatorDateTimeLowOrHigh
.IsValid(dateTimeTarget
, new DateTime(2007, 01, 01)));
259 #region String range tests
262 public void RangeStringTooLowValidator()
264 //fail when compare to string too low
265 Assert
.IsFalse(validatorStringLow
.IsValid(stringTarget
, "aaa"));
266 //pass when compare to string not too low
267 Assert
.IsTrue(validatorStringLow
.IsValid(stringTarget
, "ccc"));
271 public void RangeStringTooHighValidator()
273 //fail when compare to string too high
274 Assert
.IsFalse(validatorStringHigh
.IsValid(stringTarget
, "zzz"));
275 //pass when compare to string not too high
276 Assert
.IsTrue(validatorStringHigh
.IsValid(stringTarget
, "xxx"));
280 public void RangeStringTooLowOrHighValidator()
282 //fail when compare to string too low
283 Assert
.IsFalse(validatorStringLowOrHigh
.IsValid(stringTarget
, "a"));
284 //fail when compare to string too high
285 Assert
.IsFalse(validatorStringLowOrHigh
.IsValid(stringTarget
, "z"));
286 //pass when compare to string not too low or high
287 Assert
.IsTrue(validatorStringLowOrHigh
.IsValid(stringTarget
, "m"));