Changed to use Authenticate asp.net event instead of Authorize
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / Validation / ValidationTestCase.cs
blob5f97cec5e7876ccdc37c648cd12082037d60f1b1
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
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;
27 [TestFixture]
28 public class ValidationTestCase : AbstractActiveRecordTest
30 [Test]
31 public void IsValid()
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());
48 [Test]
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();
58 PropertyInfo info;
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));
127 Recreate();
129 User user = new User();
131 user.Create();
134 [Test, ExpectedException(typeof(ValidationException))]
135 public void CreateFail2()
137 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
138 Recreate();
140 User user = new User();
142 user.Save();
145 [Test, ExpectedException(typeof(ValidationException))]
146 public void CreateFail3()
148 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
149 Recreate();
151 using (new SessionScope())
153 User user = new User();
155 user.Save();
159 [Test, ExpectedException(typeof(ValidationException))]
160 public void CreateFail4()
162 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
163 Recreate();
165 using (new TransactionScope())
167 User user = new User();
169 user.Save();
173 [Test, ExpectedException(typeof(ValidationException))]
174 public void UpdateFail1()
176 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
177 Recreate();
179 int id = CreateNewUser();
181 User user = (User)
182 ActiveRecordMediator.FindByPrimaryKey(typeof(User), id, true);
184 user.Name = "";
185 user.Update();
188 [Test, ExpectedException(typeof(ValidationException))]
189 public void UpdateFail2()
191 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
192 Recreate();
194 int id = CreateNewUser();
196 User user = (User)
197 ActiveRecordMediator.FindByPrimaryKey(typeof(User), id, true);
199 user.Name = "";
200 user.Save();
203 [Test, ExpectedException(typeof(ValidationException))]
204 public void UpdateFail3()
206 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
207 Recreate();
209 int id = CreateNewUser();
211 User user = (User)
212 ActiveRecordMediator.FindByPrimaryKey(typeof(User), id, true);
214 user.Name = "";
215 user.UpdateAndFlush();
218 [Test, ExpectedException(typeof(ValidationException))]
219 public void UpdateFail4()
221 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
222 Recreate();
224 int id = CreateNewUser();
226 User user = (User)
227 ActiveRecordMediator.FindByPrimaryKey(typeof(User), id, true);
229 user.Name = "";
230 user.SaveAndFlush();
233 [Test, ExpectedException(typeof(ValidationException))]
234 public void DeleteFail1()
236 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
237 Recreate();
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);
247 user1.Name = "";
248 user2.DeleteAndFlush();
252 [Test]
253 public void CheckScope()
255 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
256 Recreate();
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);
268 user1.Name = "dng";
269 user2.Name = "";
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) );
284 Recreate();
286 int id = CreateNewUser();
289 User user = (User)
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())
299 User user = (User)
300 ActiveRecordMediator.FindByPrimaryKey(typeof(User), id, true);
302 user.Email = "wrong";
303 user.ConfirmationPassword = "123x";
304 user.Password = "123y";
308 [Test]
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) );
313 Recreate();
315 Blog2.DeleteAll();
317 Blog2 blog = new Blog2();
318 blog.Name = "hammett";
319 blog.Create();
321 blog = new Blog2();
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]);
328 blog.Create();
331 [Test(Description = "Reproduces AR-136")]
332 public void IsUniqueWithInvalidData()
334 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(Blog2), typeof(Blog2B) );
335 Recreate();
337 Blog2B.DeleteAll();
339 Blog2B blog = new Blog2B();
340 blog.Name = "hammett";
341 blog.Create();
343 blog = new Blog2B();
344 blog.Name = "hammett";
345 blog.Create();
347 Blog2 blog2 = new Blog2();
348 blog2.Name = blog.Name;
350 Assert.IsFalse(blog2.IsValid());
354 [Test]
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));
359 Recreate();
361 Blog4.DeleteAll();
363 Blog4 blog = new Blog4();
364 blog.Name = "hammett";
365 blog.Create();
367 blog = new Blog4();
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]);
374 blog.Create();
377 [Test]
378 public void IsUniqueWithSessionScope()
380 ActiveRecordStarter.Initialize(GetConfigSource(), typeof (Blog2));
381 Recreate();
383 Blog2.DeleteAll();
385 Blog2 blog = new Blog2();
386 blog.Name = "hammett";
387 blog.Create();
389 using (new SessionScope())
391 Blog2 fromDb = Blog2.Find(blog.Id);
392 fromDb.Name = "foo";
393 fromDb.Save();
397 [Test]
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) );
402 Recreate();
404 Blog3.DeleteAll();
406 Blog3 blog = new Blog3();
407 blog.Id = "assignedKey";
408 blog.Name = "First Blog";
409 blog.Create();
411 blog = new Blog3();
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]);
419 blog.Create();
422 [Test]
423 public void Hierarchy()
425 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(Person), typeof(Customer) );
426 // Recreate();
428 Person p1 = new Person();
430 Assert.IsFalse( p1.IsValid() );
432 p1.Name = "hammett";
433 p1.Age = 25;
435 Assert.IsTrue( p1.IsValid() );
437 Customer c1 = new Customer();
439 Assert.IsFalse( c1.IsValid() );
441 c1.ContactName = "someone";
442 c1.Phone = "11";
444 Assert.IsFalse( c1.IsValid() );
446 c1.Name = "hammett";
448 Assert.IsTrue( c1.IsValid() );
451 [Test]
452 public void ValidateIsUniqueWithinTransactionScope()
454 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(Blog2) );
455 Recreate();
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";
464 blog.Create();
466 blog = new Blog2();
467 blog.Name = "Another cool blog";
468 blog.Create();
470 scope.VoteCommit();
474 [Test, ExpectedException(typeof(ValidationException))]
475 public void IsUniqueTimeoutExpiredBug()
477 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Blog2), typeof(Blog5));
478 Recreate();
480 for (int i = 0; i < 5; i++)
482 Blog5 blog = new Blog5();
483 blog.Name = "A cool blog";
484 blog.Create();
487 Blog5 theBlog = new Blog5();
488 theBlog.Name = "A cool blog";
489 theBlog.Create();
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());
505 weblog.Create();
508 private int CreateNewUser()
510 int id;
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";
522 user.Save();
524 id = user.Id;
527 return id;