1 // Copyright 2004-2007 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
.Validation
18 using System
.Collections
;
19 using System
.Globalization
;
20 using System
.Reflection
;
21 using System
.Threading
;
22 using NUnit
.Framework
;
24 using Castle
.ActiveRecord
.Tests
.Validation
.Model
;
25 using Castle
.Components
.Validator
;
28 public class ValidationTestCase
: AbstractActiveRecordTest
33 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(User
) );
35 User user
= new User();
37 Assert
.IsFalse(user
.IsValid());
39 user
.Name
= "hammett";
40 user
.Login
= "hammett";
41 user
.Password
= "123";
42 user
.ConfirmationPassword
= "123";
43 user
.Email
= "hammett@gmail.com";
45 Assert
.IsTrue(user
.IsValid());
49 public void ErrorMessages()
51 Thread
.CurrentThread
.CurrentCulture
=
52 Thread
.CurrentThread
.CurrentUICulture
= new CultureInfo("en-us");
54 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(User
) );
56 User user
= new User();
57 Type type
= user
.GetType();
59 ArrayList propertyMessages
;
61 Assert
.IsFalse(user
.IsValid());
62 Assert
.AreEqual(5, user
.ValidationErrorMessages
.Length
);
63 Assert
.AreEqual("This is a required field", user
.ValidationErrorMessages
[0]);
64 Assert
.AreEqual("This is a required field", user
.ValidationErrorMessages
[1]);
65 Assert
.AreEqual("This is a required field", user
.ValidationErrorMessages
[2]);
66 Assert
.AreEqual("This is a required field", user
.ValidationErrorMessages
[3]);
67 Assert
.AreEqual("This is a required field", user
.ValidationErrorMessages
[4]);
69 Assert
.AreEqual(5, user
.PropertiesValidationErrorMessage
.Count
);
71 info
= type
.GetProperty("Login");
72 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Contains(info
));
73 propertyMessages
= (ArrayList
)user
.PropertiesValidationErrorMessage
[info
];
74 Assert
.AreEqual(1, propertyMessages
.Count
);
75 Assert
.AreEqual("This is a required field", propertyMessages
[0]);
77 info
= type
.GetProperty("Name");
78 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Contains(info
));
79 propertyMessages
= (ArrayList
)user
.PropertiesValidationErrorMessage
[info
];
80 Assert
.AreEqual(1, propertyMessages
.Count
);
81 Assert
.AreEqual("This is a required field", propertyMessages
[0]);
83 info
= type
.GetProperty("Email");
84 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Contains(info
));
85 propertyMessages
= (ArrayList
)user
.PropertiesValidationErrorMessage
[info
];
86 Assert
.AreEqual(1, propertyMessages
.Count
);
87 Assert
.AreEqual("This is a required field", propertyMessages
[0]);
89 info
= type
.GetProperty("Password");
90 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Contains(info
));
91 propertyMessages
= (ArrayList
)user
.PropertiesValidationErrorMessage
[info
];
92 Assert
.AreEqual(1, propertyMessages
.Count
);
93 Assert
.AreEqual("This is a required field", propertyMessages
[0]);
95 info
= type
.GetProperty("ConfirmationPassword");
96 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Contains(info
));
97 propertyMessages
= (ArrayList
)user
.PropertiesValidationErrorMessage
[info
];
98 Assert
.AreEqual(1, propertyMessages
.Count
);
99 Assert
.AreEqual("This is a required field", propertyMessages
[0]);
101 user
.Name
= "hammett";
102 user
.Login
= "hammett";
103 user
.Email
= "hammett@gmail.com";
104 user
.Password
= "123x";
105 user
.ConfirmationPassword
= "123";
107 Assert
.IsFalse(user
.IsValid());
108 Assert
.AreEqual(1, user
.ValidationErrorMessages
.Length
);
109 Assert
.AreEqual("Fields do not match", user
.ValidationErrorMessages
[0]);
111 info
= type
.GetProperty("Password");
112 Assert
.IsTrue(user
.PropertiesValidationErrorMessage
.Contains(info
));
113 propertyMessages
= (ArrayList
)user
.PropertiesValidationErrorMessage
[info
];
114 Assert
.AreEqual(1, propertyMessages
.Count
);
115 Assert
.AreEqual("Fields do not match", propertyMessages
[0]);
117 user
.Password
= "123";
119 Assert
.IsTrue(user
.IsValid());
120 Assert
.AreEqual(0, user
.ValidationErrorMessages
.Length
);
123 [Test
, ExpectedException(typeof(ValidationException
))]
124 public void CreateFail1()
126 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
129 User user
= new User();
134 [Test
, ExpectedException(typeof(ValidationException
))]
135 public void CreateFail2()
137 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
140 User user
= new User();
145 [Test
, ExpectedException(typeof(ValidationException
))]
146 public void CreateFail3()
148 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
151 using (new SessionScope())
153 User user
= new User();
159 [Test
, ExpectedException(typeof(ValidationException
))]
160 public void CreateFail4()
162 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
165 using (new TransactionScope())
167 User user
= new User();
173 [Test
, ExpectedException(typeof(ValidationException
))]
174 public void UpdateFail1()
176 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
179 int id
= CreateNewUser();
182 ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id
, true);
188 [Test
, ExpectedException(typeof(ValidationException
))]
189 public void UpdateFail2()
191 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
194 int id
= CreateNewUser();
197 ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id
, true);
203 [Test
, ExpectedException(typeof(ValidationException
))]
204 public void UpdateFail3()
206 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
209 int id
= CreateNewUser();
212 ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id
, true);
215 user
.UpdateAndFlush();
218 [Test
, ExpectedException(typeof(ValidationException
))]
219 public void UpdateFail4()
221 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
224 int id
= CreateNewUser();
227 ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id
, true);
233 [Test
, ExpectedException(typeof(ValidationException
))]
234 public void DeleteFail1()
236 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
239 int id1
= CreateNewUser();
240 int id2
= CreateNewUser();
242 using (new SessionScope())
244 User user1
= (User
) ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id1
, true);
245 User user2
= (User
) ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id2
, true);
248 user2
.DeleteAndFlush();
253 public void CheckScope()
255 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(User
));
258 int id1
= CreateNewUser();
259 int id2
= CreateNewUser();
263 using (new SessionScope())
265 User user1
= (User
)ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id1
, true);
266 User user2
= (User
)ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id2
, true);
272 catch (ValidationException
)
276 User user
= (User
)ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id1
, true);
277 Assert
.AreNotEqual("dng", user
.Name
);
280 [Test
, ExpectedException(typeof(ValidationException
))]
281 public void InvalidClassIsNotPersisted()
283 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(User
) );
286 int id
= CreateNewUser();
290 ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id
, true);
292 Assert
.AreEqual("hammett@gmail.com", user
.Email
);
293 Assert
.AreEqual("123", user
.Password
);
294 Assert
.AreEqual("123", user
.ConfirmationPassword
);
297 using(new SessionScope())
300 ActiveRecordMediator
.FindByPrimaryKey(typeof(User
), id
, true);
302 user
.Email
= "wrong";
303 user
.ConfirmationPassword
= "123x";
304 user
.Password
= "123y";
309 [ExpectedException(typeof(ValidationException
), "Can't save or update as there is one (or more) field that has not passed the validation test")]
310 public void IsUnique()
312 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(Blog2
) );
317 Blog2 blog
= new Blog2();
318 blog
.Name
= "hammett";
322 blog
.Name
= "hammett";
324 String
[] messages
= blog
.ValidationErrorMessages
;
325 Assert
.IsTrue(messages
.Length
== 1);
326 Assert
.AreEqual("Name is currently in use. Please pick up a new Name.", messages
[0]);
331 [Test(Description
= "Reproduces AR-136")]
332 public void IsUniqueWithInvalidData()
334 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(Blog2
), typeof(Blog2B
) );
339 Blog2B blog
= new Blog2B();
340 blog
.Name
= "hammett";
344 blog
.Name
= "hammett";
347 Blog2 blog2
= new Blog2();
348 blog2
.Name
= blog
.Name
;
350 Assert
.IsFalse(blog2
.IsValid());
355 [ExpectedException(typeof(ValidationException
), "Can't save or update as there is one (or more) field that has not passed the validation test")]
356 public void IsUniqueWithNullKey()
358 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Blog4
));
363 Blog4 blog
= new Blog4();
364 blog
.Name
= "hammett";
368 blog
.Name
= "hammett";
370 String
[] messages
= blog
.ValidationErrorMessages
;
371 Assert
.IsTrue(messages
.Length
== 1);
372 Assert
.AreEqual("Name is currently in use. Please pick up a new Name.", messages
[0]);
378 public void IsUniqueWithSessionScope()
380 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof (Blog2
));
385 Blog2 blog
= new Blog2();
386 blog
.Name
= "hammett";
389 using (new SessionScope())
391 Blog2 fromDb
= Blog2
.Find(blog
.Id
);
398 [ExpectedException(typeof(ValidationException
), "Can't save or update as there is one (or more) field that has not passed the validation test")]
399 public void IsUnique2()
401 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(Blog3
) );
406 Blog3 blog
= new Blog3();
407 blog
.Id
= "assignedKey";
408 blog
.Name
= "First Blog";
412 blog
.Id
= "assignedKey";
413 blog
.Name
= "Second Blog";
415 String
[] messages
= blog
.ValidationErrorMessages
;
416 Assert
.IsTrue(messages
.Length
== 1);
417 Assert
.AreEqual("The ID you specified already exists.", messages
[0]);
423 public void Hierarchy()
425 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(Person
), typeof(Customer
) );
428 Person p1
= new Person();
430 Assert
.IsFalse( p1
.IsValid() );
435 Assert
.IsTrue( p1
.IsValid() );
437 Customer c1
= new Customer();
439 Assert
.IsFalse( c1
.IsValid() );
441 c1
.ContactName
= "someone";
444 Assert
.IsFalse( c1
.IsValid() );
448 Assert
.IsTrue( c1
.IsValid() );
452 public void ValidateIsUniqueWithinTransactionScope()
454 ActiveRecordStarter
.Initialize( GetConfigSource(), typeof(Blog2
) );
457 // The IsUniqueValidator was created a new SessionScope and causing an
458 // error when used inside TransactionScope
460 using (TransactionScope scope
= new TransactionScope())
462 Blog2 blog
= new Blog2();
463 blog
.Name
= "A cool blog";
467 blog
.Name
= "Another cool blog";
474 [Test
, ExpectedException(typeof(ValidationException
))]
475 public void IsUniqueTimeoutExpiredBug()
477 ActiveRecordStarter
.Initialize(GetConfigSource(), typeof(Blog2
), typeof(Blog5
));
480 for (int i
= 0; i
< 5; i
++)
482 Blog5 blog
= new Blog5();
483 blog
.Name
= "A cool blog";
487 Blog5 theBlog
= new Blog5();
488 theBlog
.Name
= "A cool blog";
491 Blog5 anotherBlog
= new Blog5();
492 anotherBlog
.Name
= "A cool blog";
493 anotherBlog
.Create();
495 anotherBlog
.Name
= "A very cool blog";
496 anotherBlog
.Update();
498 Assert
.IsFalse(Blog2
.Find(theBlog
.Id
).IsValid());
500 Blog2 weblog
= new Blog2();
501 weblog
.Name
= theBlog
.Name
;
503 Assert
.IsFalse(weblog
.IsValid());
508 private int CreateNewUser()
512 using (new SessionScope())
514 User user
= new User();
516 user
.Name
= "hammett";
517 user
.Login
= "hammett";
518 user
.Email
= "hammett@gmail.com";
519 user
.ConfirmationPassword
= "123";
520 user
.Password
= "123";