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
.ActiveRecord
.Tests
18 using Castle
.ActiveRecord
.Tests
.Model
.NestedValidation
;
19 using Castle
.Components
.Validator
;
20 using NUnit
.Framework
;
23 public class NestedModelValidation
: AbstractActiveRecordTest
26 public void DoesNestedPropertyValidateWithARVBGeneric()
28 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(UserWithNestedAddress
));
31 UserWithNestedAddress user
= new UserWithNestedAddress();
32 Assert
.IsFalse(user
.IsValid());
33 user
.Email
= "someemail";
35 Assert
.IsFalse(user
.IsValid(),"Nested class not validated");
36 Assert
.IsTrue(user
.ValidationErrorMessages
.Length
== 3,"Both nested props are required and should have error messages");
38 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Count
== 2,"Two properties should be invalid");
40 user
.PostalAddress
.AddressLine1
= "15st"; //to short
41 user
.PostalAddress
.Country
= "Brazil";
44 Assert
.IsFalse(user
.IsValid());
45 Assert
.IsTrue(user
.ValidationErrorMessages
.Length
== 1,"Should be a too short error message");
47 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Count
==1,"One property should be invalid") ;
49 user
.PostalAddress
.AddressLine1
= "12345";
50 Assert
.IsTrue(user
.IsValid());
52 //setup another nested prop
54 user
.BillingAddress
= new Address();
56 user
.BillingAddress
.AddressLine1
= "12"; // to short
57 user
.BillingAddress
.Country
= "New Zealand";
59 Assert
.IsFalse(user
.IsValid());
61 Assert
.IsTrue(user
.ValidationErrorMessages
.Length
== 1, "Should be one error message about required length");
63 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Count
== 1, "One property should be invalid");
65 user
.BillingAddress
.AddressLine1
= "12345";
67 Assert
.IsTrue(user
.IsValid());
71 public void DoesNestedPropertValidateWithARVBNonGeneric()
73 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(UserWithNestedAddressNonGeneric
));
77 UserWithNestedAddressNonGeneric user
= new UserWithNestedAddressNonGeneric();
78 Assert
.IsFalse(user
.IsValid());
79 user
.Email
= "someemail";
81 Assert
.IsFalse(user
.IsValid(), "Nested class not validated");
82 Assert
.IsTrue(user
.ValidationErrorMessages
.Length
== 3, "Both nested props are required and should have error messages");
84 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Count
== 2, "Two properties should be invalid");
86 user
.PostalAddress
.AddressLine1
= "15st"; //to short
87 user
.PostalAddress
.Country
= "Brazil";
90 Assert
.IsFalse(user
.IsValid());
91 Assert
.IsTrue(user
.ValidationErrorMessages
.Length
== 1, "Should be a too short error message");
93 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Count
== 1, "One property should be invalid");
95 user
.PostalAddress
.AddressLine1
= "12345";
96 Assert
.IsTrue(user
.IsValid());
98 //setup another nested prop
100 user
.BillingAddress
= new Address();
102 user
.BillingAddress
.AddressLine1
= "12"; // to short
103 user
.BillingAddress
.Country
= "New Zealand";
105 Assert
.IsFalse(user
.IsValid());
107 Assert
.IsTrue(user
.ValidationErrorMessages
.Length
== 1, "Should be one error message about required length");
109 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Count
== 1, "One property should be invalid");
111 user
.BillingAddress
.AddressLine1
= "12345";
113 Assert
.IsTrue(user
.IsValid());