Initial import!
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Context.Support;
|
||||
using Spring.Expressions;
|
||||
|
||||
namespace Spring.Validation.Actions
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the ErrorMessageAction class.
|
||||
/// </summary>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
/// <version>$Id: ErrorMessageActionTests.cs,v 1.4 2008/02/05 20:40:26 aseovic Exp $</version>
|
||||
[TestFixture]
|
||||
public class ErrorMessageActionTests
|
||||
{
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void WithNullMesageId()
|
||||
{
|
||||
new ErrorMessageAction(null, "errors");
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void WithEmptyMesageId()
|
||||
{
|
||||
new ErrorMessageAction("", "errors");
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void WithWhitespaceMesageId()
|
||||
{
|
||||
new ErrorMessageAction("\t ", "errors");
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void WithNullProviders()
|
||||
{
|
||||
new ErrorMessageAction("error", null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void WithEmptyProviders()
|
||||
{
|
||||
new ErrorMessageAction("error", new string[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenValid()
|
||||
{
|
||||
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
ErrorMessageAction action = new ErrorMessageAction("error", "errors");
|
||||
|
||||
action.Execute(true, context, null, errors);
|
||||
Assert.IsTrue(errors.IsEmpty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenInvalid()
|
||||
{
|
||||
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
|
||||
ErrorMessageAction action = new ErrorMessageAction("{0}, {1}", "errors");
|
||||
action.Parameters = new IExpression[] {Expression.Parse("Name"), Expression.Parse("Nationality")};
|
||||
|
||||
action.Execute(false, context, null, errors);
|
||||
Assert.IsFalse(errors.IsEmpty);
|
||||
Assert.AreEqual(1, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(context.Name + ", " + context.Nationality, errors.GetResolvedErrors("errors", new NullMessageSource())[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenActionIsNotExecutedBecauseWhenExpressionReturnsFalse()
|
||||
{
|
||||
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
|
||||
ErrorMessageAction action = new ErrorMessageAction("{0}, {1}", "errors");
|
||||
action.When = Expression.Parse("false");
|
||||
action.Execute(false, context, null, errors);
|
||||
Assert.IsTrue(errors.IsEmpty);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Expressions;
|
||||
|
||||
namespace Spring.Validation.Actions
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the ExpressionAction class.
|
||||
/// </summary>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
/// <version>$Id: ExpressionActionTests.cs,v 1.3 2006/04/09 07:24:52 markpollack Exp $</version>
|
||||
[TestFixture]
|
||||
public class ExpressionActionTests
|
||||
{
|
||||
[Test]
|
||||
public void WhenValid()
|
||||
{
|
||||
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
|
||||
IDictionary vars = new Hashtable();
|
||||
|
||||
ExpressionAction action = new ExpressionAction("#result = 'valid'", "#result = 'invalid'");
|
||||
action.Execute(true, context, vars, null);
|
||||
Assert.AreEqual("valid", vars["result"]);
|
||||
|
||||
action = new ExpressionAction(Expression.Parse("#result = Name"), Expression.Parse("#result = Nationality"));
|
||||
action.Execute(true, context, vars, null);
|
||||
Assert.AreEqual(context.Name, vars["result"]);
|
||||
|
||||
action = new ExpressionAction();
|
||||
action.Valid = Expression.Parse("#result = DOB.Year");
|
||||
action.Invalid = Expression.Parse("#result = DOB.Month");
|
||||
action.Execute(true, context, vars, null);
|
||||
Assert.AreEqual(context.DOB.Year, vars["result"]);
|
||||
|
||||
vars.Clear();
|
||||
action = new ExpressionAction(null, "#result = 'invalid'");
|
||||
action.Execute(true, context, vars, null);
|
||||
Assert.IsFalse(vars.Contains("result"), "Result should not exist when valid expression is null.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenInvalid()
|
||||
{
|
||||
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
|
||||
IDictionary vars = new Hashtable();
|
||||
|
||||
ExpressionAction action = new ExpressionAction("#result = 'valid'", "#result = 'invalid'");
|
||||
action.Execute(false, context, vars, null);
|
||||
Assert.AreEqual("invalid", vars["result"]);
|
||||
|
||||
action = new ExpressionAction(Expression.Parse("#result = Name"), Expression.Parse("#result = Nationality"));
|
||||
action.Execute(false, context, vars, null);
|
||||
Assert.AreEqual(context.Nationality, vars["result"]);
|
||||
|
||||
action = new ExpressionAction();
|
||||
action.Valid = Expression.Parse("#result = DOB.Year");
|
||||
action.Invalid = Expression.Parse("#result = DOB.Month");
|
||||
action.Execute(false, context, vars, null);
|
||||
Assert.AreEqual(context.DOB.Month, vars["result"]);
|
||||
|
||||
vars.Clear();
|
||||
action = new ExpressionAction("#result = 'valid'", null);
|
||||
action.Execute(false, context, vars, null);
|
||||
Assert.IsFalse(vars.Contains("result"), "Result should not exist when invalid expression is null.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenActionIsNotExecutedBecauseWhenExpressionReturnsFalse()
|
||||
{
|
||||
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
|
||||
IDictionary vars = new Hashtable();
|
||||
|
||||
ExpressionAction action = new ExpressionAction("#result = 'valid'", "#result = 'invalid'");
|
||||
action.When = Expression.Parse("false");
|
||||
action.Execute(true, context, vars, null);
|
||||
Assert.IsFalse(vars.Contains("result"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Expressions;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the AnyValidatorGroup class.
|
||||
/// </summary>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
/// <version>$Id: AnyValidatorGroupTests.cs,v 1.4 2008/02/05 20:40:26 aseovic Exp $</version>
|
||||
[TestFixture]
|
||||
public sealed class AnyValidatorGroupTests
|
||||
{
|
||||
[Test]
|
||||
public void WhenAllValidatorsReturnFalse()
|
||||
{
|
||||
AnyValidatorGroup vg = new AnyValidatorGroup();
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsFalse(valid, "Validation should fail when all inner validators return false.");
|
||||
Assert.AreEqual(3, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAllValidatorsReturnTrue()
|
||||
{
|
||||
AnyValidatorGroup vg = new AnyValidatorGroup("true");
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsTrue(valid, "Validation should succeed when all inner validators return true.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenSingleValidatorReturnsTrue()
|
||||
{
|
||||
AnyValidatorGroup vg = new AnyValidatorGroup(Expression.Parse("true"));
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsTrue(valid, "Validation should succeed when single inner validator returns true.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenGroupIsNotValidatedBecauseWhenExpressionReturnsFalse()
|
||||
{
|
||||
AnyValidatorGroup vg = new AnyValidatorGroup("false");
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsTrue(valid, "Validation should succeed when group validator is not evaluated.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using NUnit.Framework;
|
||||
using Spring.Collections;
|
||||
using Spring.Core.IO;
|
||||
using Spring.Expressions;
|
||||
using Spring.Objects;
|
||||
using Spring.Objects.Factory.Xml;
|
||||
using Spring.Validation.Actions;
|
||||
|
||||
namespace Spring.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the CollectionValidator class.
|
||||
/// </summary>
|
||||
/// <author>Damjan Tomic</author>
|
||||
[TestFixture]
|
||||
public class CollectionValidatorTests
|
||||
{
|
||||
[Test]
|
||||
public void TestCollection()
|
||||
{
|
||||
IList persons = new ArrayList();
|
||||
|
||||
persons.Add(new TestObject("Damjan Tomic", 24));
|
||||
persons.Add(new TestObject("Goran Milosavljevic", 24));
|
||||
persons.Add(new TestObject("Ivan Cikic", 28));
|
||||
|
||||
RequiredValidator req = new RequiredValidator("Name", "true");
|
||||
|
||||
RegularExpressionValidator reg = new RegularExpressionValidator("Name", "true", @"[a-z]*\s[a-z]*");
|
||||
reg.Options = RegexOptions.IgnoreCase;
|
||||
|
||||
CollectionValidator validator = new CollectionValidator();
|
||||
validator.Validators.Add(req);
|
||||
validator.Validators.Add(reg);
|
||||
|
||||
Assert.IsTrue(validator.Validate(persons, new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDifferentCollectionTypes()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<v:group id='validatePerson' when='T(Spring.Objects.TestObject) == #this.GetType()'>
|
||||
<v:required id ='req' when='true' test='Name'/>
|
||||
<v:regex id ='reg' test='Name'>
|
||||
<v:property name='Expression' value='[a-z]*\s[a-z]*'/>
|
||||
<v:property name='Options' value='IgnoreCase'/>
|
||||
<v:message id='reg1' providers='regularni' when='true'>
|
||||
<v:param value='#this.ToString()'/>
|
||||
</v:message>
|
||||
</v:regex>
|
||||
</v:group>
|
||||
<v:collection id='collectionValidator' validate-all='true'>
|
||||
<v:ref name='validatePerson'/>
|
||||
</v:collection>
|
||||
</objects>";
|
||||
|
||||
MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
|
||||
IResource resource = new InputStreamResource(stream, "collectionValidator");
|
||||
|
||||
XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
|
||||
CollectionValidator validator = (CollectionValidator) objectFactory.GetObject("collectionValidator");
|
||||
|
||||
IList listPersons = new ArrayList();
|
||||
IDictionary dictPersons = new Hashtable();
|
||||
ISet setPersons = new ListSet();
|
||||
|
||||
listPersons.Add(new TestObject("DAMJAN Tomic", 24));
|
||||
listPersons.Add(new TestObject("Goran Milosavljevic", 24));
|
||||
listPersons.Add(new TestObject("Ivan CIKIC", 28));
|
||||
|
||||
dictPersons.Add(1, listPersons[0]);
|
||||
dictPersons.Add(2, listPersons[1]);
|
||||
dictPersons.Add(3, listPersons[2]);
|
||||
|
||||
setPersons.AddAll(listPersons);
|
||||
IValidationErrors ve = new ValidationErrors();
|
||||
|
||||
Assert.IsTrue(validator.Validate(listPersons, ve));
|
||||
Assert.IsTrue(ve.IsEmpty);
|
||||
Assert.IsTrue(validator.Validate(dictPersons, ve));
|
||||
Assert.IsTrue(ve.IsEmpty);
|
||||
Assert.IsTrue(validator.Validate(setPersons, ve));
|
||||
Assert.IsTrue(ve.IsEmpty);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void TestWithWrongArgumentType()
|
||||
{
|
||||
RequiredValidator req = new RequiredValidator("Name", "true");
|
||||
CollectionValidator validator = new CollectionValidator();
|
||||
validator.Validators.Add(req);
|
||||
|
||||
TestObject tObj = new TestObject("Damjan Tomic", 24);
|
||||
|
||||
//This should cause the ArgumentException because tObj is not a Collection
|
||||
Assert.IsTrue(validator.Validate(tObj, new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestValidationErrorsAreCollected()
|
||||
{
|
||||
IList persons = new ArrayList();
|
||||
|
||||
persons.Add(new TestObject(null, 24));
|
||||
persons.Add(new TestObject("Goran Milosavljevic", 24));
|
||||
persons.Add(new TestObject("Ivan Cikic", 28));
|
||||
persons.Add(new TestObject(null, 20));
|
||||
|
||||
RequiredValidator req = new RequiredValidator("Name", "true");
|
||||
req.Actions.Add(new ErrorMessageAction("1", new string[] { "firstProvider", "secondProvider" }));
|
||||
|
||||
CollectionValidator validator = new CollectionValidator(true,true);
|
||||
|
||||
validator.Validators.Add(req);
|
||||
|
||||
IValidationErrors ve = new ValidationErrors();
|
||||
|
||||
Assert.IsFalse(validator.Validate(persons, ve));
|
||||
Assert.IsFalse(ve.IsEmpty);
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void TestWithNull()
|
||||
{
|
||||
CollectionValidator validator = new CollectionValidator();
|
||||
//This should cause the ArgumentException because we passed null into Validate method
|
||||
Assert.IsTrue(validator.Validate(null, new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestNestingCollectionValidator()
|
||||
{
|
||||
Society soc = new Society();
|
||||
soc.Members.Add(new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"));
|
||||
soc.Members.Add(new Inventor("Mihajlo Pupin", new DateTime(1854, 10, 9), "Serbian"));
|
||||
|
||||
|
||||
RequiredValidator req = new RequiredValidator("Name", "true");
|
||||
|
||||
RegularExpressionValidator reg = new RegularExpressionValidator("Name", "true", @"[a-z]*\s[a-z]*");
|
||||
reg.Options = RegexOptions.IgnoreCase;
|
||||
|
||||
CollectionValidator validator = new CollectionValidator();
|
||||
validator.Validators.Add(req);
|
||||
validator.Validators.Add(reg);
|
||||
|
||||
validator.Context = Expression.Parse("Members");
|
||||
|
||||
Assert.IsTrue(validator.Validate(soc, new ValidationErrors()));
|
||||
|
||||
validator.Context = null;
|
||||
Assert.IsTrue(validator.Validate(soc.Members, new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestNestingCollectionValidatorWithXMLDescription()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<v:group id='validatePerson' when='T(Spring.Objects.TestObject) == #this.GetType()'>
|
||||
<v:required id ='req' when='true' test='Name'/>
|
||||
<v:regex id ='reg' test='Name'>
|
||||
<v:property name='Expression' value='[a-z]*\s[a-z]*'/>
|
||||
<v:property name='Options' value='IgnoreCase'/>
|
||||
<v:message id='reg1' providers='regExpr' when='true'>
|
||||
<v:param value='#this.ToString()'/>
|
||||
</v:message>
|
||||
</v:regex>
|
||||
</v:group>
|
||||
|
||||
<v:group id='validator'>
|
||||
<v:collection id='collectionValidator' validate-all='true' context='Members' include-element-errors='true'>
|
||||
<v:ref name='validatePerson'/>
|
||||
<v:message id='coll1' providers='membersCollection' when='true'>
|
||||
<v:param value='#this.ToString()'/>
|
||||
</v:message>
|
||||
</v:collection>
|
||||
</v:group>
|
||||
</objects>";
|
||||
|
||||
MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
|
||||
IResource resource = new InputStreamResource(stream, "collection validator test");
|
||||
|
||||
XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
|
||||
|
||||
ValidatorGroup validator = (ValidatorGroup) objectFactory.GetObject("validator");
|
||||
Society soc = new Society();
|
||||
|
||||
soc.Members.Add(new TestObject("Damjan Tomic", 24));
|
||||
soc.Members.Add(new TestObject("Goran Milosavljevic", 24));
|
||||
soc.Members.Add(new TestObject("Ivan Cikic", 28));
|
||||
|
||||
IValidationErrors err1 = new ValidationErrors();
|
||||
|
||||
Assert.IsTrue(validator.Validate(soc, err1));
|
||||
|
||||
soc.Members.Add(new TestObject("foo", 30));
|
||||
soc.Members.Add(new TestObject("bar", 30));
|
||||
Assert.IsFalse(validator.Validate(soc, err1));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Expressions;
|
||||
using Spring.Validation.Actions;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the ExclusiveValidatorGroup class.
|
||||
/// </summary>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
/// <version>$Id: ExclusiveValidatorGroupTests.cs,v 1.4 2008/02/05 20:40:26 aseovic Exp $</version>
|
||||
[TestFixture]
|
||||
public sealed class ExclusiveValidatorGroupTests
|
||||
{
|
||||
[Test]
|
||||
public void WhenAllValidatorsReturnFalse()
|
||||
{
|
||||
ExclusiveValidatorGroup vg = new ExclusiveValidatorGroup();
|
||||
vg.Actions.Add(new ErrorMessageAction("exclusiveError", "exclusiveErrors"));
|
||||
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsFalse(valid, "Validation should fail when all inner validators return false.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("exclusiveErrors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAllValidatorsReturnTrue()
|
||||
{
|
||||
ExclusiveValidatorGroup vg = new ExclusiveValidatorGroup("true");
|
||||
vg.Actions.Add(new ErrorMessageAction("exclusiveError", "exclusiveErrors"));
|
||||
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsFalse(valid, "Validation should fail when all inner validators return true.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("exclusiveErrors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenSingleValidatorReturnsTrue()
|
||||
{
|
||||
ExclusiveValidatorGroup vg = new ExclusiveValidatorGroup(Expression.Parse("true"));
|
||||
vg.Actions.Add(new ErrorMessageAction("exclusiveError", "exclusiveErrors"));
|
||||
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsTrue(valid, "Validation should succeed when single inner validator returns true.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(0, errors.GetErrors("exclusiveErrors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenGroupIsNotValidatedBecauseWhenExpressionReturnsFalse()
|
||||
{
|
||||
ExclusiveValidatorGroup vg = new ExclusiveValidatorGroup("false");
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsTrue(valid, "Validation should succeed when group validator is not evaluated.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
114
test/Spring/Spring.Core.Tests/Validation/HelperClasses.cs
Normal file
114
test/Spring/Spring.Core.Tests/Validation/HelperClasses.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
using Spring.Objects.Factory.Config;
|
||||
using Spring.Objects.Factory.Support;
|
||||
using Spring.Validation.Actions;
|
||||
|
||||
namespace Spring.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper classes for validation tests.
|
||||
/// </summary>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
/// <version>$Id: HelperClasses.cs,v 1.2 2006/04/09 07:24:52 markpollack Exp $</version>
|
||||
public class TrueValidator : BaseValidator
|
||||
{
|
||||
public TrueValidator()
|
||||
{}
|
||||
|
||||
/// <summary>
|
||||
/// Validates test object.
|
||||
/// </summary>
|
||||
/// <param name="objectToValidate">Object to validate.</param>
|
||||
/// <returns><c>True</c> if specified object is valid, <c>False</c> otherwise.</returns>
|
||||
protected override bool Validate(object objectToValidate)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class FalseValidator : BaseValidator
|
||||
{
|
||||
public FalseValidator()
|
||||
{
|
||||
this.Actions.Add(new ErrorMessageAction("error", "errors"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates test object.
|
||||
/// </summary>
|
||||
/// <param name="objectToValidate">Object to validate.</param>
|
||||
/// <returns><c>True</c> if specified object is valid, <c>False</c> otherwise.</returns>
|
||||
protected override bool Validate(object objectToValidate)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MockObjectDefinitionRegistry : IObjectDefinitionRegistry
|
||||
{
|
||||
private IDictionary objects = new Hashtable();
|
||||
|
||||
public int ObjectDefinitionCount
|
||||
{
|
||||
get { return this.objects.Count; }
|
||||
}
|
||||
|
||||
public string[] GetObjectDefinitionNames()
|
||||
{
|
||||
return (string[]) new ArrayList(this.objects.Keys).ToArray(typeof(string));
|
||||
}
|
||||
|
||||
public IObjectDefinition[] GetObjectDefinitions()
|
||||
{
|
||||
return (IObjectDefinition[]) new ArrayList(this.objects.Values).ToArray(typeof(IObjectDefinition));
|
||||
}
|
||||
|
||||
public bool ContainsObjectDefinition(string name)
|
||||
{
|
||||
return objects.Contains(name);
|
||||
}
|
||||
|
||||
public IObjectDefinition GetObjectDefinition(string name)
|
||||
{
|
||||
return (IObjectDefinition) objects[name];
|
||||
}
|
||||
|
||||
public void RegisterObjectDefinition(string name, IObjectDefinition definition)
|
||||
{
|
||||
this.objects[name] = definition;
|
||||
}
|
||||
|
||||
public string[] GetAliases(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void RegisterAlias(string name, string theAlias)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using NUnit.Framework;
|
||||
using Spring.Core.IO;
|
||||
using Spring.Objects;
|
||||
using Spring.Objects.Factory;
|
||||
using Spring.Objects.Factory.Config;
|
||||
using Spring.Objects.Factory.Support;
|
||||
using Spring.Objects.Factory.Xml;
|
||||
using Spring.Validation.Actions;
|
||||
using Spring.Validation.Config;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the ValidationNamespaceParser class.
|
||||
/// </summary>
|
||||
/// <author>Rick Evans</author>
|
||||
/// <version>$Id: ValidationConfigParserTests.cs,v 1.14 2007/08/08 17:48:45 bbaia Exp $</version>
|
||||
[TestFixture]
|
||||
public sealed class ValidationConfigParserTests
|
||||
{
|
||||
[Test]
|
||||
public void WhenConfigFileIsValid()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<v:group id='destinationAirportValidator'>
|
||||
<v:ref name='airportCodeValidator' context='ReturningFrom.AirportCode'/>
|
||||
<v:ref name='airportCodeValidator'/>
|
||||
<v:condition test='ReturningFrom.AirportCode != StartingFrom.AirportCode'>
|
||||
<v:message id='error.destinationAirport.sameAsDeparture' providers='summary'/>
|
||||
</v:condition>
|
||||
<v:validator type='Spring.Validation.RegularExpressionValidator, Spring.Core' test='ReturningFrom.AirportCode' when='true'>
|
||||
<v:property name='Expression' value='[A-Z]*'/>
|
||||
<v:message id='error.destinationAirport.invalidFormat' providers='summary'/>
|
||||
</v:validator>
|
||||
</v:group>
|
||||
|
||||
<v:required id='airportCodeValidator' test='#this'>
|
||||
<v:message id='error.airportCode.dummy' providers='summary' when='false'/>
|
||||
<v:message id='error.airportCode.required' providers='summary'>
|
||||
<v:param value='#this.Abc'/>
|
||||
<v:param value='#this.Xyz'/>
|
||||
</v:message>
|
||||
<v:action type='Spring.Validation.Actions.ExpressionAction, Spring.Core' when='true'>
|
||||
<v:property name='Valid' value='#now = DateTime.Now'/>
|
||||
</v:action>
|
||||
<v:action type='Spring.Validation.Actions.ExpressionAction, Spring.Core'/>
|
||||
</v:required>
|
||||
|
||||
<object id='myObject' type='DateTime'/>
|
||||
|
||||
</objects>
|
||||
";
|
||||
XmlDocument doc = new XmlDocument();
|
||||
|
||||
AssemblyResource validationSchema = new AssemblyResource("assembly://Spring.Core/Spring.Validation.Config/spring-validation-1.1.xsd");
|
||||
AssemblyResource objectsSchema = new AssemblyResource("assembly://Spring.Core/Spring.Objects.Factory.Xml/spring-objects-1.1.xsd");
|
||||
|
||||
#if !NET_2_0
|
||||
XmlValidatingReader validatingReader = new XmlValidatingReader(xml, XmlNodeType.Document, null);
|
||||
validatingReader.ValidationType = ValidationType.Schema;
|
||||
validatingReader.Schemas.Add("http://www.springframework.net", new XmlTextReader(objectsSchema.InputStream));
|
||||
validatingReader.Schemas.Add("http://www.springframework.net/validation", new XmlTextReader(validationSchema.InputStream));
|
||||
#else
|
||||
XmlReaderSettings settings = new XmlReaderSettings();
|
||||
settings.Schemas.Add("http://www.springframework.net", new XmlTextReader(objectsSchema.InputStream));
|
||||
settings.Schemas.Add("http://www.springframework.net/validation", new XmlTextReader(validationSchema.InputStream));
|
||||
settings.ValidationType = ValidationType.Schema;
|
||||
XmlReader validatingReader = XmlReader.Create(new StringReader(xml), settings);
|
||||
#endif
|
||||
doc.Load(validatingReader);
|
||||
|
||||
MockObjectDefinitionRegistry registry = new MockObjectDefinitionRegistry();
|
||||
IObjectDefinitionDocumentReader reader = new DefaultObjectDefinitionDocumentReader();
|
||||
|
||||
XmlReaderContext readerContext = new XmlReaderContext(null, new XmlObjectDefinitionReader(registry));
|
||||
ObjectDefinitionParserHelper helper = new ObjectDefinitionParserHelper(readerContext);
|
||||
helper.InitDefaults(doc.DocumentElement);
|
||||
ParserContext parserContext = new ParserContext(helper.ReaderContext, helper);
|
||||
|
||||
ValidationNamespaceParser parser = new ValidationNamespaceParser();
|
||||
foreach (XmlElement element in doc.DocumentElement.ChildNodes)
|
||||
{
|
||||
if (element.NamespaceURI == "http://www.springframework.net/validation")
|
||||
{
|
||||
parser.ParseElement(element, parserContext);
|
||||
}
|
||||
}
|
||||
IObjectDefinition[] defs = registry.GetObjectDefinitions();
|
||||
Assert.AreEqual(2, defs.Length);
|
||||
|
||||
IObjectDefinition def = registry.GetObjectDefinition("destinationAirportValidator");
|
||||
Assert.IsTrue(def.IsSingleton);
|
||||
Assert.IsTrue(def.IsLazyInit);
|
||||
Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
|
||||
PropertyValue validatorsProperty = def.PropertyValues.GetPropertyValue("Validators");
|
||||
Assert.IsNotNull(validatorsProperty);
|
||||
object validatorsObject = validatorsProperty.Value;
|
||||
Assert.AreEqual(typeof(ManagedList), validatorsObject.GetType());
|
||||
ManagedList validators = (ManagedList) validatorsObject;
|
||||
Assert.AreEqual(4, validators.Count);
|
||||
|
||||
def = (IObjectDefinition) validators[3];
|
||||
Assert.IsTrue(def.IsSingleton);
|
||||
Assert.IsTrue(def.IsLazyInit);
|
||||
Assert.AreEqual(typeof(RegularExpressionValidator), def.ObjectType);
|
||||
Assert.AreEqual("[A-Z]*", def.PropertyValues.GetPropertyValue("Expression").Value);
|
||||
|
||||
def = registry.GetObjectDefinition("airportCodeValidator");
|
||||
Assert.IsTrue(def.IsSingleton);
|
||||
Assert.IsTrue(def.IsLazyInit);
|
||||
Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
|
||||
PropertyValue actionsProperty = def.PropertyValues.GetPropertyValue("Actions");
|
||||
Assert.IsNotNull(actionsProperty);
|
||||
object actionsObject = actionsProperty.Value;
|
||||
Assert.AreEqual(typeof(ManagedList), actionsObject.GetType());
|
||||
ManagedList actions = (ManagedList) actionsObject;
|
||||
Assert.AreEqual(4, actions.Count);
|
||||
|
||||
IObjectDefinition messageDefinition = (IObjectDefinition) actions[1];
|
||||
Assert.AreEqual(typeof(ErrorMessageAction), messageDefinition.ObjectType);
|
||||
|
||||
IObjectDefinition actionDefinition = (IObjectDefinition) actions[2];
|
||||
Assert.AreEqual(typeof(ExpressionAction), actionDefinition.ObjectType);
|
||||
Assert.AreEqual("#now = DateTime.Now", actionDefinition.PropertyValues.GetPropertyValue("Valid").Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ObjectDefinitionStoreException))]
|
||||
public void WhenConfigFileIsNotValid()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<v:required test='#this'>
|
||||
<v:message id='error.airportCode.required' providers='summary'/>
|
||||
<v:action type='Spring.Validation.Actions.ExpressionAction, Spring.Core' when='true'/>
|
||||
</v:required>
|
||||
</objects>
|
||||
";
|
||||
XmlDocument doc = new XmlDocument();
|
||||
|
||||
AssemblyResource validationSchema = new AssemblyResource("assembly://Spring.Core/Spring.Validation.Config/spring-validation-1.1.xsd");
|
||||
AssemblyResource objectsSchema = new AssemblyResource("assembly://Spring.Core/Spring.Objects.Factory.Xml/spring-objects-1.1.xsd");
|
||||
|
||||
#if !NET_2_0
|
||||
XmlValidatingReader validatingReader = new XmlValidatingReader(xml, XmlNodeType.Document, null);
|
||||
validatingReader.ValidationType = ValidationType.Schema;
|
||||
validatingReader.Schemas.Add("http://www.springframework.net", new XmlTextReader(objectsSchema.InputStream));
|
||||
validatingReader.Schemas.Add("http://www.springframework.net/validation", new XmlTextReader(validationSchema.InputStream));
|
||||
#else
|
||||
XmlReaderSettings settings = new XmlReaderSettings();
|
||||
settings.Schemas.Add("http://www.springframework.net", new XmlTextReader(objectsSchema.InputStream));
|
||||
settings.Schemas.Add("http://www.springframework.net/validation", new XmlTextReader(validationSchema.InputStream));
|
||||
settings.ValidationType = ValidationType.Schema;
|
||||
XmlReader validatingReader = XmlReader.Create(new StringReader(xml), settings);
|
||||
#endif
|
||||
doc.Load(validatingReader);
|
||||
|
||||
MockObjectDefinitionRegistry registry = new MockObjectDefinitionRegistry();
|
||||
IObjectDefinitionDocumentReader reader = new DefaultObjectDefinitionDocumentReader();
|
||||
|
||||
XmlReaderContext readerContext = new XmlReaderContext(null, new XmlObjectDefinitionReader(registry));
|
||||
ObjectDefinitionParserHelper helper = new ObjectDefinitionParserHelper(readerContext);
|
||||
helper.InitDefaults(doc.DocumentElement);
|
||||
ParserContext parserContext = new ParserContext(helper.ReaderContext, helper);
|
||||
|
||||
ValidationNamespaceParser parser = new ValidationNamespaceParser();
|
||||
foreach (XmlElement element in doc.DocumentElement.ChildNodes)
|
||||
{
|
||||
if (element.NamespaceURI == "http://www.springframework.net/validation")
|
||||
{
|
||||
parser.ParseElement(element, parserContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Xml.Serialization;
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Context.Support;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the ValidationErrors class.
|
||||
/// </summary>
|
||||
/// <author>Rick Evans</author>
|
||||
/// <author>Goran Milosavljevic</author>
|
||||
/// <version>$Id: ValidationErrorsTests.cs,v 1.7 2008/02/05 20:40:26 aseovic Exp $</version>
|
||||
[TestFixture]
|
||||
public sealed class ValidationErrorsTests
|
||||
{
|
||||
private const string GoodErrorKey = "key";
|
||||
private ErrorMessage ErrorMessageTwo = new ErrorMessage("This Is Eva Green", null);
|
||||
private ErrorMessage ErrorMessageOne = new ErrorMessage("Kissing Leads To Brain Disease", null);
|
||||
|
||||
[Test]
|
||||
public void ContainsNoErrorsDirectlyAfterInstantiation()
|
||||
{
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsTrue(errors.IsEmpty);
|
||||
Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof (ArgumentNullException))]
|
||||
public void AddErrorWithNullMessage()
|
||||
{
|
||||
new ValidationErrors().AddError(GoodErrorKey, null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void AddErrorWithNullKey()
|
||||
{
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError(null, ErrorMessageOne);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddErrorSunnyDay()
|
||||
{
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError(GoodErrorKey, ErrorMessageOne);
|
||||
Assert.IsFalse(errors.IsEmpty);
|
||||
Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
|
||||
Assert.AreEqual(1, errors.GetErrors(GoodErrorKey).Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddTwoErrorsSameKey()
|
||||
{
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError(GoodErrorKey, ErrorMessageOne);
|
||||
errors.AddError(GoodErrorKey, ErrorMessageTwo);
|
||||
Assert.IsFalse(errors.IsEmpty);
|
||||
Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
|
||||
Assert.AreEqual(2, errors.GetErrors(GoodErrorKey).Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EmptyErrorsReturnEmptyCollections()
|
||||
{
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
|
||||
IList typedErrors = errors.GetErrors("xyz");
|
||||
Assert.IsNotNull(typedErrors);
|
||||
Assert.AreEqual(0, typedErrors.Count);
|
||||
|
||||
IList resolvedErrors = errors.GetResolvedErrors("xyz", new NullMessageSource());
|
||||
Assert.IsNotNull(resolvedErrors);
|
||||
Assert.AreEqual(0, resolvedErrors.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MergeErrorsWithNull()
|
||||
{
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError(GoodErrorKey, ErrorMessageOne);
|
||||
errors.AddError(GoodErrorKey, ErrorMessageTwo);
|
||||
errors.MergeErrors(null);
|
||||
|
||||
// must be unchanged with no Exception thrown...
|
||||
Assert.IsFalse(errors.IsEmpty);
|
||||
Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
|
||||
Assert.AreEqual(2, errors.GetErrors(GoodErrorKey).Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MergeErrors()
|
||||
{
|
||||
ValidationErrors otherErrors = new ValidationErrors();
|
||||
const string anotherKey = "anotherKey";
|
||||
otherErrors.AddError(anotherKey, ErrorMessageTwo);
|
||||
otherErrors.AddError(GoodErrorKey, ErrorMessageTwo);
|
||||
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError(GoodErrorKey, ErrorMessageOne);
|
||||
errors.MergeErrors(otherErrors);
|
||||
|
||||
Assert.IsFalse(errors.IsEmpty);
|
||||
IList mergedErrors = errors.GetErrors(GoodErrorKey);
|
||||
Assert.IsNotNull(mergedErrors);
|
||||
Assert.AreEqual(2, mergedErrors.Count);
|
||||
Assert.AreEqual(ErrorMessageOne, mergedErrors[0]);
|
||||
Assert.AreEqual(ErrorMessageTwo, mergedErrors[1]);
|
||||
|
||||
IList otherErrorsForKey = errors.GetErrors(anotherKey);
|
||||
Assert.IsNotNull(otherErrorsForKey);
|
||||
Assert.AreEqual(1, otherErrorsForKey.Count);
|
||||
Assert.AreEqual(ErrorMessageTwo, otherErrorsForKey[0]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SerializeErrors()
|
||||
{
|
||||
ValidationErrors errors = new ValidationErrors();
|
||||
ErrorMessageOne = new ErrorMessage("Kissing Leads To Brain Disease", new object[] {"Param11", 5, "Param13"});
|
||||
ErrorMessageTwo = new ErrorMessage("This Is Eva Green", new object[] { "Param21", 'g' , new object[] {"Goran", "Milosavljevic"} });
|
||||
ErrorMessage ErrorMessageThree = new ErrorMessage("Third error message", null);
|
||||
ErrorMessage ErrorMessageFour = new ErrorMessage("Fourth error message", new object[]{});
|
||||
errors.AddError("key1", ErrorMessageOne);
|
||||
errors.AddError("key1", ErrorMessageTwo);
|
||||
errors.AddError("key2", ErrorMessageThree);
|
||||
errors.AddError("key3", ErrorMessageFour);
|
||||
|
||||
Stream streamBefore = new MemoryStream();
|
||||
Stream streamAfter = new MemoryStream();
|
||||
|
||||
// serialize ValidationErrors
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(ValidationErrors));
|
||||
serializer.Serialize(streamBefore, errors);
|
||||
streamBefore.Position = 0;
|
||||
|
||||
// deserialize ValidationErrors
|
||||
ValidationErrors result = (ValidationErrors) serializer.Deserialize(streamBefore);
|
||||
|
||||
// serialize ValidationErrors
|
||||
serializer.Serialize(streamAfter, result);
|
||||
|
||||
// compare ValidationErrors instances
|
||||
byte[] byteBefore = new byte[streamBefore.Length];
|
||||
byte[] byteAfter = new byte[streamAfter.Length];
|
||||
|
||||
Assert.AreEqual(byteAfter.Length, byteBefore.Length);
|
||||
|
||||
streamBefore.Position = 0;
|
||||
streamAfter.Position = 0;
|
||||
streamBefore.Read(byteBefore, 0, (int) streamBefore.Length);
|
||||
streamAfter.Read(byteAfter, 0, (int) streamAfter.Length);
|
||||
|
||||
Assert.AreEqual(byteBefore, byteAfter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Spring.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the ValidationException class.
|
||||
/// </summary>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
/// <version>$Id: ValidationExceptionTests.cs,v 1.1 2008/02/05 20:40:27 aseovic Exp $</version>
|
||||
[TestFixture]
|
||||
public sealed class ValidationExceptionTests
|
||||
{
|
||||
[Test]
|
||||
public void InstantiationUsingDefaultConstructor()
|
||||
{
|
||||
ValidationException ex = new ValidationException();
|
||||
Assert.IsNull(ex.ValidationErrors);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InstantiationSupplyingValidationErrors()
|
||||
{
|
||||
ValidationException ex = new ValidationException(new ValidationErrors());
|
||||
Assert.IsTrue(ex.ValidationErrors.IsEmpty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InstantiationSupplyingMessageAndValidationErrors()
|
||||
{
|
||||
ValidationException ex = new ValidationException("my message", new ValidationErrors());
|
||||
Assert.AreEqual("my message", ex.Message);
|
||||
Assert.IsTrue(ex.ValidationErrors.IsEmpty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InstantiationSupplyingMessageValidationErrorsAndRootCause()
|
||||
{
|
||||
Exception rootCause = new Exception("root cause");
|
||||
ValidationException ex = new ValidationException("my message", rootCause, new ValidationErrors());
|
||||
Assert.AreEqual("my message", ex.Message);
|
||||
Assert.IsTrue(ex.ValidationErrors.IsEmpty);
|
||||
Assert.AreEqual(rootCause, ex.InnerException);
|
||||
Assert.AreEqual("root cause", ex.InnerException.Message);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestExceptionSerialization()
|
||||
{
|
||||
MemoryStream buffer = new MemoryStream();
|
||||
BinaryFormatter serializer = new BinaryFormatter();
|
||||
|
||||
Exception rootCause = new Exception("root cause");
|
||||
ValidationException e1 = new ValidationException("my message", rootCause, new ValidationErrors());
|
||||
serializer.Serialize(buffer, e1);
|
||||
buffer.Position = 0;
|
||||
ValidationException e2 = (ValidationException)serializer.Deserialize(buffer);
|
||||
|
||||
Assert.AreEqual("my message", e2.Message);
|
||||
Assert.IsTrue(e2.ValidationErrors.IsEmpty);
|
||||
Assert.AreEqual("root cause", e2.InnerException.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
123
test/Spring/Spring.Core.Tests/Validation/ValidatorGroupTests.cs
Normal file
123
test/Spring/Spring.Core.Tests/Validation/ValidatorGroupTests.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Expressions;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the ValidatorGroup class.
|
||||
/// </summary>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
/// <version>$Id: ValidatorGroupTests.cs,v 1.4 2008/02/05 20:40:26 aseovic Exp $</version>
|
||||
[TestFixture]
|
||||
public sealed class ValidatorGroupTests : ValidatorGroup
|
||||
{
|
||||
[Test]
|
||||
public void WhenAllValidatorsReturnFalse()
|
||||
{
|
||||
ValidatorGroup vg = new ValidatorGroup();
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsFalse(valid, "Validation should fail when all inner validators return false.");
|
||||
Assert.AreEqual(3, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenAllValidatorsReturnTrue()
|
||||
{
|
||||
ValidatorGroup vg = new ValidatorGroup("true");
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsTrue(valid, "Validation should succeed when all inner validators return true.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenSingleValidatorReturnsTrue()
|
||||
{
|
||||
ValidatorGroup vg = new ValidatorGroup(Expression.Parse("true"));
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
vg.Validators.Add(new TrueValidator());
|
||||
vg.Validators.Add(new FalseValidator());
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsFalse(valid, "Validation should fail when single inner validator returns true.");
|
||||
Assert.AreEqual(2, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenGroupIsNotValidatedBecauseWhenExpressionReturnsFalse()
|
||||
{
|
||||
ValidatorGroup vg = new ValidatorGroup("false");
|
||||
IList validators = new ArrayList();
|
||||
validators.Add(new FalseValidator());
|
||||
validators.Add(new FalseValidator());
|
||||
vg.Validators = validators;
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
errors.AddError("existingErrors", new ErrorMessage("error", null));
|
||||
|
||||
bool valid = vg.Validate(new object(), errors);
|
||||
|
||||
Assert.IsTrue(valid, "Validation should succeed when group validator is not evaluated.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(NotSupportedException))]
|
||||
public void TestNonSupportedValidateMethod()
|
||||
{
|
||||
this.Validate("xyz");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Expressions;
|
||||
using Spring.Objects.Factory.Support;
|
||||
|
||||
namespace Spring.Validation
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for ValidatorReference class.
|
||||
/// </summary>
|
||||
/// <author>Aleksandar Seovic</author>
|
||||
/// <version>$Id: ValidatorReferenceTests.cs,v 1.3 2008/02/05 20:40:27 aseovic Exp $</version>
|
||||
[TestFixture]
|
||||
public class ValidatorReferenceTests
|
||||
{
|
||||
[Test]
|
||||
public void TrueValidatorReference()
|
||||
{
|
||||
StaticListableObjectFactory factory = new StaticListableObjectFactory();
|
||||
factory.AddObject("validator", new TrueValidator());
|
||||
|
||||
ValidatorReference v = new ValidatorReference();
|
||||
v.ObjectFactory = factory;
|
||||
v.Name = "validator";
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
|
||||
Assert.IsTrue(v.Validate(null, null, errors));
|
||||
Assert.IsTrue(v.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FalseValidatorReference()
|
||||
{
|
||||
StaticListableObjectFactory factory = new StaticListableObjectFactory();
|
||||
factory.AddObject("validator", new FalseValidator());
|
||||
|
||||
ValidatorReference v = new ValidatorReference();
|
||||
v.ObjectFactory = factory;
|
||||
v.Name = "validator";
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(v.Validate(null, null, errors));
|
||||
Assert.IsFalse(v.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ContextNarrowing()
|
||||
{
|
||||
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
|
||||
|
||||
ConditionValidator cv1 = new ConditionValidator("DOB.Year == 1856", null);
|
||||
ConditionValidator cv2 = new ConditionValidator("Year == 1856", null);
|
||||
|
||||
StaticListableObjectFactory factory = new StaticListableObjectFactory();
|
||||
factory.AddObject("cv1", cv1);
|
||||
factory.AddObject("cv2", cv2);
|
||||
|
||||
ValidatorReference v1 = new ValidatorReference();
|
||||
v1.ObjectFactory = factory;
|
||||
v1.Name = "cv1";
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
|
||||
Assert.IsTrue(v1.Validate(context, null, errors));
|
||||
Assert.IsTrue(v1.Validate(context, errors));
|
||||
|
||||
ValidatorReference v2 = new ValidatorReference();
|
||||
v2.ObjectFactory = factory;
|
||||
v2.Name = "cv2";
|
||||
v2.Context = Expression.Parse("DOB");
|
||||
|
||||
Assert.IsTrue(v2.Validate(context, null, errors));
|
||||
Assert.IsTrue(v2.Validate(context, errors));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System.Collections;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Context.Support;
|
||||
using Spring.Expressions;
|
||||
using Spring.Validation.Actions;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the ConditionValidator class.
|
||||
/// </summary>
|
||||
/// <author>Rick Evans</author>
|
||||
/// <version>$Id: ConditionValidatorTests.cs,v 1.9 2008/02/05 20:40:27 aseovic Exp $</version>
|
||||
[TestFixture]
|
||||
public sealed class ConditionValidatorTests
|
||||
{
|
||||
[Test]
|
||||
public void StraightTrue()
|
||||
{
|
||||
ConditionValidator validator = new ConditionValidator();
|
||||
validator.Test = Expression.Parse("true");
|
||||
Assert.IsTrue(validator.Validate(null, new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StraightFalse()
|
||||
{
|
||||
ConditionValidator validator = new ConditionValidator("false", null);
|
||||
Assert.IsFalse(validator.Validate(null, new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TrueScalarExpression()
|
||||
{
|
||||
Inventor tesla = new Inventor();
|
||||
tesla.Name = "Nikola Tesla";
|
||||
|
||||
ConditionValidator validator = new ConditionValidator(Expression.Parse("Name == 'Nikola Tesla'"), null);
|
||||
Assert.IsTrue(validator.Validate(tesla, new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FalseScalarExpression()
|
||||
{
|
||||
Inventor tesla = new Inventor();
|
||||
tesla.Name = "Soltan Gris";
|
||||
|
||||
ConditionValidator validator = new ConditionValidator(Expression.Parse("Name == 'Nikola Tesla'"), null);
|
||||
validator.Actions = new ErrorMessageAction[] {new ErrorMessageAction("Wrong name", "InventorValidator") };
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(tesla, errors));
|
||||
Assert.IsFalse(errors.IsEmpty);
|
||||
IList namedErrors = errors.GetResolvedErrors("InventorValidator", new NullMessageSource());
|
||||
Assert.AreEqual(1, namedErrors.Count);
|
||||
string error = (string) namedErrors[0];
|
||||
Assert.AreEqual("Wrong name", error);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenValidatorIsNotEvaluatedBecauseWhenExpressionReturnsFalse()
|
||||
{
|
||||
ConditionValidator validator = new ConditionValidator();
|
||||
validator.Test = Expression.Parse("false");
|
||||
validator.When = Expression.Parse("false");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
|
||||
bool valid = validator.Validate(new object(), null, errors);
|
||||
|
||||
Assert.IsTrue(valid, "Validation should succeed when condition validator is not evaluated.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the UrlValidator class.
|
||||
/// </summary>
|
||||
/// <author>Goran Milosavljevic</author>
|
||||
[TestFixture]
|
||||
public sealed class CreditCardValidatorTests
|
||||
{
|
||||
[Test]
|
||||
public void Validate()
|
||||
{
|
||||
CreditCardValidator validator = new CreditCardValidator();
|
||||
validator.CardType = new Amex();
|
||||
Assert.IsTrue(validator.Validate("378282246310005", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("444444444", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate(" ", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate(null, new ValidationErrors()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text.RegularExpressions;
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Context.Support;
|
||||
using Spring.Expressions;
|
||||
using Spring.Validation.Actions;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the EmailValidator class.
|
||||
/// </summary>
|
||||
/// <author>Goran Milosavljevic</author>
|
||||
[TestFixture]
|
||||
public sealed class EmailValidatorTests
|
||||
{
|
||||
[Test]
|
||||
public void Validate()
|
||||
{
|
||||
EmailValidator validator = new EmailValidator();
|
||||
Assert.IsTrue(validator.Validate("goran@eu.s4hc.com", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("goran.milosavljevic@s4hc.com", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("g.m.m@web_ask.com", new ValidationErrors()));
|
||||
|
||||
Assert.IsFalse(validator.Validate("@eu.s4hc.com", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("g @s4hc.com", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("g&@s", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("goran@s", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("goran@@", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("goran@eu s4hc.com", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate(" ", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate(null, new ValidationErrors()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright © 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the ISBNValidator class.
|
||||
/// </summary>
|
||||
/// <author>Goran Milosavljevic</author>
|
||||
[TestFixture]
|
||||
public sealed class ISBNValidatorTests
|
||||
{
|
||||
[Test]
|
||||
public void Validate()
|
||||
{
|
||||
ISBNValidator validator = new ISBNValidator();
|
||||
// validate ISBN10
|
||||
Assert.IsTrue(validator.Validate("90-70002-34-5", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("1575843013", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("81-7525-766-0", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("1905158793", new ValidationErrors()));
|
||||
|
||||
// validate ISBN13
|
||||
Assert.IsTrue(validator.Validate("978-1-905158-79-9", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("978-81-7525-766-5", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("978-90-70002-34-3", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("9789070002343", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("978907000234-3", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("9789070002-34-3", new ValidationErrors()));
|
||||
|
||||
Assert.IsFalse(validator.Validate("9789g70002-34-3", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("a789g70002343", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("978907000234x", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate(" ", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate(null, new ValidationErrors()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Spring.Core.IO;
|
||||
using Spring.Objects;
|
||||
using Spring.Objects.Factory.Xml;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// CreditCardValidator integration tests.
|
||||
/// </summary>
|
||||
/// <author>Goran Milosavljevic</author>
|
||||
[TestFixture]
|
||||
public class CreditCardValidatorIntegrationTests
|
||||
{
|
||||
[Test]
|
||||
public void CreditCardValidatorTests()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<object id='ccAmex' type='Spring.Validation.Validators.Amex, Spring.Core'/>
|
||||
<v:validator id='ccValidator' test='#this' type='Spring.Validation.Validators.CreditCardValidator, Spring.Core'>
|
||||
<v:property name='CardType' ref='ccAmex'/>
|
||||
<v:message id='error.airportCode.dummy' providers='summary' when='false'/>
|
||||
</v:validator>
|
||||
</objects>";
|
||||
|
||||
MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
|
||||
IResource resource = new InputStreamResource(stream, "ccValidator");
|
||||
|
||||
XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
|
||||
object obj = objectFactory.GetObject("ccValidator");
|
||||
|
||||
Assert.IsTrue(obj is CreditCardValidator);
|
||||
CreditCardValidator validator = obj as CreditCardValidator;
|
||||
Assert.IsNotNull(validator.CardType);
|
||||
Assert.IsTrue(validator.CardType is Amex);
|
||||
Assert.IsTrue(validator.Validate("378282246310005", new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void WithNullCardType()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<v:validator id='ccValidator' test='#this' type='Spring.Validation.Validators.CreditCardValidator, Spring.Core'>
|
||||
<v:message id='error.airportCode.dummy' providers='summary' when='false'/>
|
||||
</v:validator>
|
||||
</objects>";
|
||||
|
||||
MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
|
||||
IResource resource = new InputStreamResource(stream, "ccValidator");
|
||||
|
||||
XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
|
||||
object obj = objectFactory.GetObject("ccValidator");
|
||||
|
||||
Assert.IsTrue(obj is CreditCardValidator);
|
||||
CreditCardValidator validator = obj as CreditCardValidator;
|
||||
Assert.IsNull(validator.CardType);
|
||||
Assert.IsTrue(validator.Validate("378282246310005", new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ErrorTests()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<object id='ccAmex' type='Spring.Validation.Validators.Amex, Spring.Core'/>
|
||||
<v:validator id='ccValidator' test='Creditcard' type='Spring.Validation.Validators.CreditCardValidator, Spring.Core'>
|
||||
<v:property name='CardType' ref='ccAmex'/>
|
||||
<v:message id='errorKey' providers='validationSummary' />
|
||||
</v:validator>
|
||||
</objects>";
|
||||
|
||||
MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
|
||||
IResource resource = new InputStreamResource(stream, "ccValidator");
|
||||
|
||||
XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
|
||||
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Contact contact = new Contact();
|
||||
IValidator validator = (IValidator)objectFactory.GetObject("ccValidator");
|
||||
|
||||
contact.Creditcard = "378282246310";
|
||||
bool result = validator.Validate(contact, errors);
|
||||
|
||||
Assert.IsNotNull(errors.GetErrors("validationSummary"));
|
||||
Assert.IsFalse(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Spring.Core.IO;
|
||||
using Spring.Objects.Factory.Xml;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// EmailValidatorIntegration integration tests.
|
||||
/// </summary>
|
||||
/// <author>Goran Milosavljevic</author>
|
||||
[TestFixture]
|
||||
public class EmailValidatorIntegrationTests
|
||||
{
|
||||
[Test]
|
||||
public void EmailValidatorTests()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<v:validator id='emailValidator' test='#this' type='Spring.Validation.Validators.EmailValidator, Spring.Core'/>
|
||||
</objects>";
|
||||
|
||||
MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
|
||||
IResource resource = new InputStreamResource(stream, "emailValidator");
|
||||
|
||||
XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
|
||||
object obj = objectFactory.GetObject("emailValidator");
|
||||
|
||||
Assert.IsTrue(obj is IValidator);
|
||||
IValidator validator = obj as IValidator;
|
||||
Assert.IsTrue(validator.Validate("goran@goran.com", new ValidationErrors()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Spring.Core.IO;
|
||||
using Spring.Objects.Factory.Xml;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// ISBNValidatorIntegration integration tests.
|
||||
/// </summary>
|
||||
/// <author>Goran Milosavljevic</author>
|
||||
[TestFixture]
|
||||
public class ISBNValidatorIntegrationTests
|
||||
{
|
||||
[Test]
|
||||
public void ISBNValidatorTests()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<v:validator id='isbnValidator' test='#this' type='Spring.Validation.Validators.ISBNValidator, Spring.Core'>
|
||||
<v:message id='error.airportCode.dummy' providers='summary' when='false'/>
|
||||
</v:validator>
|
||||
</objects>";
|
||||
|
||||
MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
|
||||
IResource resource = new InputStreamResource(stream, "isbnValidator");
|
||||
|
||||
XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
|
||||
object obj = objectFactory.GetObject("isbnValidator");
|
||||
|
||||
Assert.IsTrue(obj is IValidator);
|
||||
IValidator validator = obj as IValidator;
|
||||
Assert.IsTrue(validator.Validate("978-1-905158-79-9", new ValidationErrors()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Spring.Core.IO;
|
||||
using Spring.Objects.Factory.Xml;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// UrlValidatorIntegration integration tests.
|
||||
/// </summary>
|
||||
/// <author>Goran Milosavljevic</author>
|
||||
[TestFixture]
|
||||
public class UrlValidatorIntegrationTests
|
||||
{
|
||||
[Test]
|
||||
public void ISBNValidatorTests()
|
||||
{
|
||||
const string xml = @"<?xml version='1.0' encoding='UTF-8' ?>
|
||||
<objects xmlns='http://www.springframework.net' xmlns:v='http://www.springframework.net/validation'>
|
||||
<v:validator id='urlValidator' test='#this' type='Spring.Validation.Validators.UrlValidator, Spring.Core'/>
|
||||
</objects>";
|
||||
|
||||
MemoryStream stream = new MemoryStream(new UTF8Encoding().GetBytes(xml));
|
||||
IResource resource = new InputStreamResource(stream, "urlValidator");
|
||||
|
||||
XmlObjectFactory objectFactory = new XmlObjectFactory(resource, null);
|
||||
object obj = objectFactory.GetObject("urlValidator");
|
||||
|
||||
Assert.IsTrue(obj is IValidator);
|
||||
IValidator validator = obj as IValidator;
|
||||
Assert.IsTrue(validator.Validate("http://www.springframework.net", new ValidationErrors()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Expressions;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the RegularExpressionValidator class.
|
||||
/// </summary>
|
||||
/// <author>Rick Evans</author>
|
||||
[TestFixture]
|
||||
public sealed class RegularExpressionValidatorTests
|
||||
{
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void WithNonString()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator();
|
||||
validator.Validate(this, new ValidationErrors());
|
||||
}
|
||||
|
||||
[Test]
|
||||
[ExpectedException(typeof(ArgumentException))]
|
||||
public void WithNull()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator();
|
||||
validator.Validate(null, new ValidationErrors());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EmptyStringValidatesToTrue()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator();
|
||||
Assert.IsTrue(validator.Validate(string.Empty, new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhitespaceStringDoesntEvaluateToTrueByDefault()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator();
|
||||
Assert.IsFalse(validator.Validate(" ", new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhitespaceStringOnlyValidatesToTrueWhenGivenMatchingRegex()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator();
|
||||
validator.Expression = @"\s*";
|
||||
Assert.IsTrue(validator.Validate(" ", new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CaseSensitiveStringMatching()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator("ToString()", "true", @"[A-Z][a-z]*");
|
||||
Assert.IsTrue(validator.Validate("Aleksandar", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("ALEKSANDAR", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("aleksandar", new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CaseInsensitiveStringMatching()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator("ToString()", "true", @"[A-Z][a-z]*");
|
||||
validator.Options = RegexOptions.IgnoreCase;
|
||||
Assert.IsTrue(validator.Validate("Aleksandar", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("ALEKSANDAR", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("aleksandar", new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SunnyDayFailure_Invalid()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator(Expression.Parse("'ljwdf87cwbh'"), Expression.Parse("true"), @"((\d{1,2}\.\d{1,3}\.\d{1,3}\.\d{1,3}))");
|
||||
Assert.IsFalse(validator.Validate("ljwdf87cwbh", new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SunnyDay_Valid()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator();
|
||||
validator.Expression = @"((\d{1,2}\.\d{1,3}\.\d{1,3}\.\d{1,3}))";
|
||||
Assert.IsTrue(validator.Validate("11.222.333.444", new ValidationErrors()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenValidatorIsNotEvaluatedBecauseWhenExpressionReturnsFalse()
|
||||
{
|
||||
RegularExpressionValidator validator = new RegularExpressionValidator("'ljwdf87cwbh'", "false", @"((\d{1,2}\.\d{1,3}\.\d{1,3}\.\d{1,3}))");
|
||||
bool valid = validator.Validate(null, new ValidationErrors());
|
||||
Assert.IsTrue(valid, "Validation should succeed when regex validator is not evaluated.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2004 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Expressions;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the RequiredValidator class.
|
||||
/// </summary>
|
||||
/// <author>Rick Evans</author>
|
||||
/// <version>$Id: RequiredValidatorTests.cs,v 1.7 2008/02/05 20:40:27 aseovic Exp $</version>
|
||||
[TestFixture]
|
||||
public sealed class RequiredValidatorTests
|
||||
{
|
||||
[Test]
|
||||
public void WithNull()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator();
|
||||
validator.Test = Expression.Parse("null");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithZeroNumber()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator("0", null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithPositiveNumber()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator(Expression.Parse("100"), null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsTrue(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithNegativeNumber()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator();
|
||||
validator.Test = Expression.Parse("-100");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsTrue(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithEmptyString()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator("''", null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithWhitespaceOnlyString()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator(Expression.Parse("' '"), null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithKosherString()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator();
|
||||
validator.Test = Expression.Parse("'some non-empty string'");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsTrue(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithKosherDate()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator("DateTime.Today", null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsTrue(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithMinDate()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator(Expression.Parse("DateTime.MinValue"), null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithMaxDate()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator();
|
||||
validator.Test = Expression.Parse("DateTime.MaxValue");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithZeroFloat()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator("0.00F", null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithKosherFloat()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator(Expression.Parse("5.25F"), null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsTrue(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithZeroDouble()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator("0.00D", null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithKosherDouble()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator("5.25D", null);
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsTrue(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithMinChar()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator();
|
||||
validator.Test = Expression.Parse("char.MinValue");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithWhitespaceChar()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator();
|
||||
validator.Test = Expression.Parse("' '.ToCharArray()[0]");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsFalse(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WithKosherChar()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator();
|
||||
validator.Test = Expression.Parse("'xyz'.ToCharArray()[1]");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
Assert.IsTrue(validator.Validate(null, errors));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WhenValidatorIsNotEvaluatedBecauseWhenExpressionReturnsFalse()
|
||||
{
|
||||
RequiredValidator validator = new RequiredValidator("DateTime.MinValue", "false");
|
||||
IValidationErrors errors = new ValidationErrors();
|
||||
|
||||
bool valid = validator.Validate(new object(), null, errors);
|
||||
|
||||
Assert.IsTrue(valid, "Validation should succeed when required validator is not evaluated.");
|
||||
Assert.AreEqual(0, errors.GetErrors("errors").Count);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#region License
|
||||
|
||||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
#region Imports
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text.RegularExpressions;
|
||||
using NUnit.Framework;
|
||||
|
||||
using Spring.Context.Support;
|
||||
using Spring.Expressions;
|
||||
using Spring.Validation.Actions;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Spring.Validation.Validators
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit tests for the UrlValidator class.
|
||||
/// </summary>
|
||||
/// <author>Goran Milosavljevic</author>
|
||||
[TestFixture]
|
||||
public sealed class UrlValidatorTests
|
||||
{
|
||||
[Test]
|
||||
public void Validate()
|
||||
{
|
||||
UrlValidator validator = new UrlValidator();
|
||||
Assert.IsTrue(validator.Validate("http://puzzle.com", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("http://www.com", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("www.1.org", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("ww.1.com", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("http://www.google-com.123.com", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("https://www.google-com.com", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("http://google-com.com", new ValidationErrors()));
|
||||
// just to make sure that we are ready for the Japanese market :)
|
||||
Assert.IsTrue(validator.Validate("www.amazon.co.jp/C-によるプログラミングWindows-上-Charles-Petzold/dp/4891002921", new ValidationErrors()));
|
||||
|
||||
Assert.IsFalse(validator.Validate("http://.com", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("http://www.google-com.123", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("http://1.1", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("ht:1.1", new ValidationErrors()));
|
||||
Assert.IsFalse(validator.Validate("/:www.1.com", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate("", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate(" ", new ValidationErrors()));
|
||||
Assert.IsTrue(validator.Validate(null, new ValidationErrors()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user