Fixing an issue with output parameters that are of type IntPtr
[castle.git] / ActiveRecord / Castle.ActiveRecord.Tests / Validation / ValidationTestCase.Generic.cs
blobaab484573bbf6a512019a36283a57a73f0a73544
1 // Copyright 2004-2008 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 Castle.ActiveRecord.Tests.Validation.Model.GenericModel;
23 using NUnit.Framework;
25 using Castle.ActiveRecord.Tests.Validation.GenericModel;
26 using Castle.Components.Validator;
28 [TestFixture]
29 public class ValidationTestCaseGeneric : AbstractActiveRecordTest
31 [Test]
32 public void IsValid()
34 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(User) );
36 User user = new User();
38 Assert.IsFalse(user.IsValid());
40 user.Name = "hammett";
41 user.Login = "hammett";
42 user.Password = "123";
43 user.ConfirmationPassword = "123";
44 user.Email = "hammett@gmail.com";
46 Assert.IsTrue(user.IsValid());
49 [Test]
50 public void ErrorMessages()
52 Thread.CurrentThread.CurrentCulture =
53 Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
55 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(User));
57 User user = new User();
58 Type type = user.GetType();
59 PropertyInfo info;
60 ArrayList propertyMessages;
62 Assert.IsFalse(user.IsValid());
63 Assert.AreEqual(5, user.ValidationErrorMessages.Length);
64 Assert.AreEqual("This is a required field", user.ValidationErrorMessages[0]);
65 Assert.AreEqual("This is a required field", user.ValidationErrorMessages[1]);
66 Assert.AreEqual("This is a required field", user.ValidationErrorMessages[2]);
67 Assert.AreEqual("This is a required field", user.ValidationErrorMessages[3]);
68 Assert.AreEqual("This is a required field", user.ValidationErrorMessages[4]);
70 Assert.AreEqual(5, user.PropertiesValidationErrorMessage.Count);
72 info = type.GetProperty("Login");
73 Assert.IsTrue(user.PropertiesValidationErrorMessage.Contains(info));
74 propertyMessages = (ArrayList)user.PropertiesValidationErrorMessage[info];
75 Assert.AreEqual(1, propertyMessages.Count);
76 Assert.AreEqual("This is a required field", propertyMessages[0]);
78 info = type.GetProperty("Name");
79 Assert.IsTrue(user.PropertiesValidationErrorMessage.Contains(info));
80 propertyMessages = (ArrayList)user.PropertiesValidationErrorMessage[info];
81 Assert.AreEqual(1, propertyMessages.Count);
82 Assert.AreEqual("This is a required field", propertyMessages[0]);
84 info = type.GetProperty("Email");
85 Assert.IsTrue(user.PropertiesValidationErrorMessage.Contains(info));
86 propertyMessages = (ArrayList)user.PropertiesValidationErrorMessage[info];
87 Assert.AreEqual(1, propertyMessages.Count);
88 Assert.AreEqual("This is a required field", propertyMessages[0]);
90 info = type.GetProperty("Password");
91 Assert.IsTrue(user.PropertiesValidationErrorMessage.Contains(info));
92 propertyMessages = (ArrayList)user.PropertiesValidationErrorMessage[info];
93 Assert.AreEqual(1, propertyMessages.Count);
94 Assert.AreEqual("This is a required field", propertyMessages[0]);
96 info = type.GetProperty("ConfirmationPassword");
97 Assert.IsTrue(user.PropertiesValidationErrorMessage.Contains(info));
98 propertyMessages = (ArrayList)user.PropertiesValidationErrorMessage[info];
99 Assert.AreEqual(1, propertyMessages.Count);
100 Assert.AreEqual("This is a required field", propertyMessages[0]);
102 user.Name = "hammett";
103 user.Login = "hammett";
104 user.Email = "hammett@gmail.com";
105 user.Password = "123x";
106 user.ConfirmationPassword = "123";
108 Assert.IsFalse(user.IsValid());
109 Assert.AreEqual(1, user.ValidationErrorMessages.Length);
110 Assert.AreEqual("Fields do not match", user.ValidationErrorMessages[0]);
112 info = type.GetProperty("Password");
113 Assert.IsTrue(user.PropertiesValidationErrorMessage.Contains(info));
114 propertyMessages = (ArrayList)user.PropertiesValidationErrorMessage[info];
115 Assert.AreEqual(1, propertyMessages.Count);
116 Assert.AreEqual("Fields do not match", propertyMessages[0]);
118 user.Password = "123";
120 Assert.IsTrue(user.IsValid());
121 Assert.AreEqual(0, user.ValidationErrorMessages.Length);
124 [Test, ExpectedException(typeof(ValidationException))]
125 public void InvalidClassIsNotPersisted()
127 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(User) );
128 Recreate();
130 int id;
132 using(new SessionScope())
134 User user = new User();
136 user.Name = "hammett";
137 user.Login = "hammett";
138 user.Email = "hammett@gmail.com";
139 user.ConfirmationPassword = "123";
140 user.Password = "123";
142 user.Save();
144 id = user.Id;
148 User user = (User)
149 ActiveRecordMediator.FindByPrimaryKey(typeof(User), id, true);
151 Assert.AreEqual("hammett@gmail.com", user.Email);
152 Assert.AreEqual("123", user.Password);
153 Assert.AreEqual("123", user.ConfirmationPassword);
156 using(new SessionScope())
158 User user = (User)
159 ActiveRecordMediator.FindByPrimaryKey(typeof(User), id, true);
161 user.Email = "wrong";
162 user.ConfirmationPassword = "123x";
163 user.Password = "123y";
167 [Test]
168 [ExpectedException(typeof(ValidationException), "Can't save or update as there is one (or more) field that has not passed the validation test")]
169 public void IsUnique()
171 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(Blog2) );
172 Recreate();
174 Blog2.DeleteAll();
176 Blog2 blog = new Blog2();
177 blog.Name = "hammett";
178 blog.Save();
180 blog = new Blog2();
181 blog.Name = "hammett";
183 String[] messages = blog.ValidationErrorMessages;
184 Assert.IsTrue(messages.Length == 1);
185 Assert.AreEqual("Name is currently in use. Please pick up a new Name.", messages[0]);
187 blog.Save();
190 [Test]
191 public void Hierarchy()
193 ActiveRecordStarter.Initialize( GetConfigSource(), typeof(Person), typeof(Customer) );
194 // Recreate();
196 Person p1 = new Person();
198 Assert.IsFalse( p1.IsValid() );
200 p1.Name = "hammett";
201 p1.Age = 25;
203 Assert.IsTrue( p1.IsValid() );
205 Customer c1 = new Customer();
207 Assert.IsFalse( c1.IsValid() );
209 c1.ContactName = "someone";
210 c1.Phone = "11";
212 Assert.IsFalse( c1.IsValid() );
214 c1.Name = "hammett";
216 Assert.IsTrue( c1.IsValid() );
219 [Test]
220 public void ValidateIsUnique()
222 ActiveRecordStarter.Initialize(GetConfigSource(), typeof(TimeSlotFixedDate));
223 Recreate();
225 TimeSlotFixedDate timeDate = new TimeSlotFixedDate();
226 timeDate.Hour = 1;
227 timeDate.Name = null;
229 Assert.IsFalse(timeDate.IsValid());