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 using System
.Globalization
;
16 using System
.Resources
;
17 using System
.Threading
;
19 namespace Castle
.Components
.Validator
.Tests
21 using Castle
.Components
.Validator
.Tests
.Models
;
22 using NUnit
.Framework
;
25 public class CachedValidationRegistryTestCase
27 private CachedValidationRegistry registry
;
32 registry
= new CachedValidationRegistry();
36 public void RunWhenEverytimeTest()
38 IValidator
[] validators
= registry
.GetValidators(new ValidatorRunner(registry
), typeof(Client
), RunWhen
.Everytime
);
40 Assert
.IsNotNull(validators
);
41 Assert
.AreEqual(8, validators
.Length
);
43 foreach (IValidator val
in validators
)
45 Assert
.IsTrue((val
.RunWhen
& RunWhen
.Everytime
) != 0);
50 public void RunWhenCustomTest()
52 IValidator
[] validators
= registry
.GetValidators(new ValidatorRunner(registry
), typeof(Client
), RunWhen
.Custom
);
54 Assert
.IsNotNull(validators
);
55 Assert
.AreEqual(9, validators
.Length
); // RunWhen.Everytime is returned too
57 foreach (IValidator val
in validators
)
59 Assert
.IsTrue((val
.RunWhen
& RunWhen
.Custom
) != 0 || (val
.RunWhen
& RunWhen
.Everytime
) != 0);
64 public void WithCustomResource()
66 CultureInfo prev
= Thread
.CurrentThread
.CurrentCulture
;
67 CultureInfo prevUI
= Thread
.CurrentThread
.CurrentUICulture
;
70 Thread
.CurrentThread
.CurrentCulture
= CultureInfo
.InvariantCulture
;
71 Thread
.CurrentThread
.CurrentUICulture
= CultureInfo
.InvariantCulture
;
73 ResourceManager resourceManager
= new ResourceManager("Castle.Components.Validator.Tests.Messages", typeof(CachedValidationRegistryTestCase
).Assembly
);
74 registry
= new CachedValidationRegistry(resourceManager
);
75 string fromResource
= registry
.GetStringFromResource("time_invalid");
76 Assert
.AreEqual("This is a test value", fromResource
);
80 Thread
.CurrentThread
.CurrentCulture
= prev
;
81 Thread
.CurrentThread
.CurrentUICulture
= prevUI
;
87 public void WillFallbackToDefaultResourceIfNotFoundOnCustomOne()
89 CultureInfo prev
= Thread
.CurrentThread
.CurrentCulture
;
90 CultureInfo prevUI
= Thread
.CurrentThread
.CurrentUICulture
;
93 Thread
.CurrentThread
.CurrentCulture
= CultureInfo
.InvariantCulture
;
94 Thread
.CurrentThread
.CurrentUICulture
= CultureInfo
.InvariantCulture
;
96 ResourceManager resourceManager
= new ResourceManager("Castle.Components.Validator.Tests.Messages", typeof(CachedValidationRegistryTestCase
).Assembly
);
97 registry
= new CachedValidationRegistry(resourceManager
);
98 string fromResource
= registry
.GetStringFromResource("collection_not_empty");
99 Assert
.AreEqual("Collection must not be empty", fromResource
);
103 Thread
.CurrentThread
.CurrentCulture
= prev
;
104 Thread
.CurrentThread
.CurrentUICulture
= prevUI
;