SPRNET-1081 - Add an ExceptionThrowingAction to Spring.Validation framework

This commit is contained in:
markpollack
2009-07-30 03:32:45 +00:00
parent bba99bbbaf
commit e842673936
12 changed files with 900 additions and 447 deletions

View File

@@ -22,10 +22,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
#if !NET_1_1
using System.Collections.Specialized;
using System.Collections.Generic;
#endif
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
@@ -408,7 +408,9 @@ namespace Spring.Objects
private IDictionary sharedState;
private NameValueCollection someNameValueCollection;
#if !NET_1_1
private List<string> someGenericStringList;
#endif
#endregion

View File

@@ -722,6 +722,7 @@
<Compile Include="Core\TypeConversion\UniqueKeyConverterTests.cs" />
<Compile Include="Util\SystemUtilsTests.cs" />
<Compile Include="Util\UniqueKeyTests.cs" />
<Compile Include="Validation\Actions\ExceptionActionTests.cs" />
<Compile Include="Validation\Actions\ErrorMessageActionTests.cs" />
<Compile Include="Validation\Actions\ExpressionActionTests.cs" />
<Compile Include="Validation\AnyValidatorGroupTests.cs" />
@@ -811,8 +812,8 @@
<Content Include="Data\Spring\Objects\Factory\Xml\collections.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\constructor-arg.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\array-autowire.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\collectionMergingGen.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\collectionMerging.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\collectionMergingGeneric.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\objectNameGeneration.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\simple-constructor-arg.xml" />
<Content Include="Data\Spring\Objects\Factory\Xml\expressions.xml" />
@@ -853,7 +854,6 @@
<EmbeddedResource Include="Objects\Factory\Attributes\RequiredWithAllRequiredPropertiesProvided.xml" />
<EmbeddedResource Include="Objects\Factory\Attributes\RequiredWithCustomAttribute.xml" />
<Content Include="Spring.Core.Tests.dll.config" />
<Content Include="Validation\dummy.xml" />
<EmbeddedResource Include="Validation\ValidationNamespaceParserTests_WhenConfigFileIsNotValid.xml" />
<EmbeddedResource Include="Validation\ValidationNamespaceParserTests_WhenConfigFileIsValid.xml" />
<EmbeddedResource Include="Util\ConfigXmlDocumentTests_SampleConfig.xml" />

View File

@@ -0,0 +1,109 @@
#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>
/// Tests for the ExceptionAction class.
/// </summary>
/// <author>Mark Pollack</author>
[TestFixture]
public class ExceptionActionTests
{
[Test]
public void WhenInvalidThrowDefaultException()
{
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
IDictionary vars = new Hashtable();
ExceptionAction action = new ExceptionAction();
try
{
action.Execute(false, context, vars, null);
Assert.Fail("Should have thrown exception");
}
catch (ValidationException)
{
}
}
[Test]
public void WhenInvalidThrowCustomException()
{
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
IDictionary vars = new Hashtable();
ExceptionAction action = new ExceptionAction("new System.InvalidOperationException('invalid')");
try
{
action.Execute(false, context, vars, null);
Assert.Fail("Should have thrown exception");
}
catch (InvalidOperationException e)
{
Assert.AreEqual("invalid", e.Message);
}
}
[Test]
public void WhenInvalidThrowCustomExceptionUsingSetter()
{
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
IDictionary vars = new Hashtable();
ExceptionAction action = new ExceptionAction();
IExpression expression = Expression.Parse("new System.InvalidOperationException('invalid')");
action.ThrowsExpression = expression;
try
{
action.Execute(false, context, vars, null);
Assert.Fail("Should have thrown exception");
}
catch (InvalidOperationException e)
{
Assert.AreEqual("invalid", e.Message);
}
}
[Test]
public void WhenValid()
{
Inventor context = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian");
IDictionary vars = new Hashtable();
ExceptionAction action = new ExceptionAction();
try
{
action.Execute(true, context, vars, null);
}
catch (Exception)
{
Assert.Fail("Should not have thrown exception");
}
}
}
}

View File

@@ -65,7 +65,7 @@ namespace Spring.Validation
}
}
IObjectDefinition[] defs = registry.GetObjectDefinitions();
Assert.AreEqual(8, defs.Length);
Assert.AreEqual(9, defs.Length);
IObjectDefinition def = registry.GetObjectDefinition("destinationAirportValidator");
Assert.IsTrue(def.IsSingleton);
@@ -89,7 +89,8 @@ namespace Spring.Validation
Assert.AreEqual(typeof(RegularExpressionValidator), def.ObjectType);
Assert.AreEqual("[A-Z]*", def.PropertyValues.GetPropertyValue("Expression").Value);
def = registry.GetObjectDefinition("airportCodeValidator");
def = registry.GetObjectDefinition("simpleAirportValidator");
Assert.IsTrue(def.IsSingleton);
Assert.IsTrue(def.IsLazyInit);
Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
@@ -98,6 +99,22 @@ namespace Spring.Validation
object actionsObject = actionsProperty.Value;
Assert.AreEqual(typeof(ManagedList), actionsObject.GetType());
ManagedList actions = (ManagedList)actionsObject;
Assert.AreEqual(1, actions.Count);
IObjectDefinition exceptionActionDefinition = (IObjectDefinition)actions[0];
Assert.AreEqual(typeof(ExceptionAction), exceptionActionDefinition.ObjectType);
Assert.AreEqual("'new System.InvalidOperationException(\"invalid\")' []", exceptionActionDefinition.ConstructorArgumentValues.GenericArgumentValues[0].ToString());
//
def = registry.GetObjectDefinition("airportCodeValidator");
Assert.IsTrue(def.IsSingleton);
Assert.IsTrue(def.IsLazyInit);
Assert.IsTrue(typeof(IValidator).IsAssignableFrom(def.ObjectType));
actionsProperty = def.PropertyValues.GetPropertyValue("Actions");
Assert.IsNotNull(actionsProperty);
actionsObject = actionsProperty.Value;
Assert.AreEqual(typeof(ManagedList), actionsObject.GetType());
actions = (ManagedList)actionsObject;
Assert.AreEqual(4, actions.Count);
IObjectDefinition messageDefinition = (IObjectDefinition)actions[1];
@@ -110,7 +127,7 @@ namespace Spring.Validation
private XmlDocument GetValidatedXmlResource(string resourceExtension)
{
AssemblyResource validationSchema = new AssemblyResource("assembly://Spring.Core/Spring.Validation.Config/spring-validation-1.1.xsd");
AssemblyResource validationSchema = new AssemblyResource("assembly://Spring.Core/Spring.Validation.Config/spring-validation-1.3.xsd");
AssemblyResource objectsSchema = new AssemblyResource("assembly://Spring.Core/Spring.Objects.Factory.Xml/spring-objects-1.3.xsd");
return TestResourceLoader.GetXmlValidated(this, resourceExtension, objectsSchema, validationSchema);

View File

@@ -18,13 +18,18 @@
<v:message id='error.airportCode.required' providers='summary'>
<v:param value='#this.Abc'/>
<v:param value='#this.Xyz'/>
</v:message>
</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>
<v:required id='simpleAirportValidator' test='false'>
<v:exception throw='new System.InvalidOperationException("invalid")'/>
</v:required>
<object id='myObject' type='DateTime'>
<property name='' value='' />
</object>