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

@@ -565,6 +565,22 @@
</itemizedlist>
</section>
<section>
<title>Exception Action</title>
<para>If you would like an exception to be thrown when validation fails
use the exception action.</para>
<programlisting>&lt;v:exception/&gt;</programlisting>
<para>This will throw an exception of the type ValidationException and
you can access error information via its ValidationErrors property. To
throw your own custom exception, provide a SpEL fragment that
instantiates the custom exception.</para>
<programlisting>&lt;v:exception throw='new System.InvalidOperationException("invalid")'/&gt;</programlisting>
</section>
<section>
<title>Generic Actions</title>
@@ -888,7 +904,7 @@ bool userInfoIsValid = userInfoValidator.Validate(userInfo, errors);
<section>
<title>How Validate() and Validation Controls play together</title>
<para> Validation Controls (ValidationSummary, ValidationError) need to
<para>Validation Controls (ValidationSummary, ValidationError) need to
somehow get the list of errors collected during validation. Both,
Spring.Web.UI.Page and Spring.Web.UI.UserControl come with a
ValidationErrors property and implement IValidationContainer.

View File

@@ -1,226 +1,226 @@
#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 System.Globalization;
using Common.Logging;
using Spring.Objects.Factory;
using Spring.Objects.Factory.Support;
using Spring.Util;
#endregion
namespace Spring.Aop.Target
{
/// <summary>
/// Base class for dynamic <see cref="Spring.Aop.ITargetSource"/>
/// implementations that can create new prototype object instances to
/// support a pooling or new-instance-per-invocation strategy.
/// </summary>
/// <remarks>
/// <p>
/// All such <see cref="Spring.Aop.ITargetSource"/>s must run in an
/// <see cref="Spring.Objects.Factory.IObjectFactory"/>, as they need to
/// call the <see cref="Spring.Objects.Factory.IObjectFactory.GetObject(string)"/>
/// method to create a new prototype instance.
/// </p>
/// </remarks>
/// <author>Rod Johnson</author>
/// <author>Federico Spinazzi (.NET)</author>
public abstract class AbstractPrototypeTargetSource
: ITargetSource, IObjectFactoryAware, IInitializingObject
{
#region Constructor (s) / Destructor
/// <summary>
/// Creates a new instance of the
/// <see cref="Spring.Aop.Target.AbstractPrototypeTargetSource"/>
/// class.
/// </summary>
/// <remarks>
/// <p>
/// This is an <see langword="abstract"/> class, and as such exposes no
/// public constructors.
/// </p>
/// </remarks>
protected AbstractPrototypeTargetSource()
{
}
#endregion
#region Properties
/// <summary>
/// The name of the target object to be created on each invocation.
/// </summary>
/// <remarks>
/// <p>
/// This object should be a prototype, or the same instance will always
/// be obtained from the owning <see cref="ObjectFactory"/>.
/// </p>
/// </remarks>
public virtual string TargetObjectName
{
get { return _targetObjectName; }
set {
_targetObjectName = value;
}
}
/// <summary>
/// The <see cref="System.Type"/> of the target object.
/// </summary>
public virtual Type TargetType
{
get { return _targetType; }
}
/// <summary>
/// Is the target source static?
/// </summary>
/// <value>
/// <see langword="true"/> if the target source is static.
/// </value>
public virtual bool IsStatic
{
get { return false; }
}
/// <summary>
/// The target factory that will be used to perform the lookup
/// of the object referred to by the <see cref="TargetObjectName"/>
/// property.
/// </summary>
/// <remarks>
/// <p>
/// Needed so that prototype instances can be created as necessary.
/// </p>
/// </remarks>
/// <value>
/// The owning <see cref="Spring.Objects.Factory.IObjectFactory"/>
/// (will never be <see langword="null"/>).
/// </value>
/// <exception cref="Spring.Objects.ObjectsException">
/// In case of initialization errors.
/// </exception>
/// <seealso cref="Spring.Objects.Factory.IObjectFactoryAware.ObjectFactory"/>
public virtual IObjectFactory ObjectFactory
{
get { return _owningObjectFactory; }
set
{
_owningObjectFactory = value;
if (!value.IsPrototype(TargetObjectName))
{
throw new ObjectDefinitionStoreException(
"Cannot use PrototypeTargetSource against a " +
"Singleton object; instances would not be independent.");
}
#region Instrumentation
if (logger.IsDebugEnabled)
{
logger.Debug(string.Format(
"Getting object with name '{0}' to determine class.",
TargetObjectName));
}
#endregion
_targetType = _owningObjectFactory.GetType(TargetObjectName);
}
}
#endregion
#region Methods
/// <summary>
/// Subclasses should use this method to create a new prototype instance.
/// </summary>
protected virtual object NewPrototypeInstance()
{
#region Instrumentation
if (logger.IsDebugEnabled)
{
logger.Debug(string.Format(
"Creating new target from object '{0}'.",
TargetObjectName));
}
#endregion
return ObjectFactory.GetObject(TargetObjectName);
}
/// <summary>
/// Returns the target object.
/// </summary>
/// <returns>The target object.</returns>
/// <exception cref="System.Exception">
/// If unable to obtain the target object.
/// </exception>
public abstract object GetTarget();
/// <summary>
/// Releases the target object.
/// </summary>
/// <param name="target">The target object to release.</param>
public virtual void ReleaseTarget(object target)
{
}
/// <summary>
/// Invoked by an <see cref="Spring.Objects.Factory.IObjectFactory"/>
/// after it has set all object properties supplied
/// (and satisfied the
/// <see cref="Spring.Objects.Factory.IObjectFactoryAware"/>
/// and <see cref="Spring.Context.IApplicationContextAware"/>
/// interfaces).
/// </summary>
/// <remarks>
/// <p>
/// Ensures that the <see cref="TargetObjectName"/> property has been
/// set to a valid value (i.e. is not <see langword="null"/> or a string
/// that consists solely of whitespace).
/// </p>
/// </remarks>
/// <exception cref="System.Exception">
/// In the event of misconfiguration (such as failure to set an essential
/// property) or if initialization fails.
/// </exception>
/// <seealso cref="Spring.Objects.Factory.IInitializingObject.AfterPropertiesSet"/>
public virtual void AfterPropertiesSet()
{
AssertUtils.ArgumentHasText(
TargetObjectName, "TargetObjectName",
"The 'TargetObjectName' property must have a value.");
}
#endregion
#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 System.Globalization;
using Common.Logging;
using Spring.Objects.Factory;
using Spring.Objects.Factory.Support;
using Spring.Util;
#endregion
namespace Spring.Aop.Target
{
/// <summary>
/// Base class for dynamic <see cref="Spring.Aop.ITargetSource"/>
/// implementations that can create new prototype object instances to
/// support a pooling or new-instance-per-invocation strategy.
/// </summary>
/// <remarks>
/// <p>
/// All such <see cref="Spring.Aop.ITargetSource"/>s must run in an
/// <see cref="Spring.Objects.Factory.IObjectFactory"/>, as they need to
/// call the <see cref="Spring.Objects.Factory.IObjectFactory.GetObject(string)"/>
/// method to create a new prototype instance.
/// </p>
/// </remarks>
/// <author>Rod Johnson</author>
/// <author>Federico Spinazzi (.NET)</author>
public abstract class AbstractPrototypeTargetSource
: ITargetSource, IObjectFactoryAware, IInitializingObject
{
#region Constructor (s) / Destructor
/// <summary>
/// Creates a new instance of the
/// <see cref="Spring.Aop.Target.AbstractPrototypeTargetSource"/>
/// class.
/// </summary>
/// <remarks>
/// <p>
/// This is an <see langword="abstract"/> class, and as such exposes no
/// public constructors.
/// </p>
/// </remarks>
protected AbstractPrototypeTargetSource()
{
}
#endregion
#region Properties
/// <summary>
/// The name of the target object to be created on each invocation.
/// </summary>
/// <remarks>
/// <p>
/// This object should be a prototype, or the same instance will always
/// be obtained from the owning <see cref="ObjectFactory"/>.
/// </p>
/// </remarks>
public virtual string TargetObjectName
{
get { return _targetObjectName; }
set {
_targetObjectName = value;
}
}
/// <summary>
/// The <see cref="System.Type"/> of the target object.
/// </summary>
public virtual Type TargetType
{
get { return _targetType; }
}
/// <summary>
/// Is the target source static?
/// </summary>
/// <value>
/// <see langword="true"/> if the target source is static.
/// </value>
public virtual bool IsStatic
{
get { return false; }
}
/// <summary>
/// The target factory that will be used to perform the lookup
/// of the object referred to by the <see cref="TargetObjectName"/>
/// property.
/// </summary>
/// <remarks>
/// <p>
/// Needed so that prototype instances can be created as necessary.
/// </p>
/// </remarks>
/// <value>
/// The owning <see cref="Spring.Objects.Factory.IObjectFactory"/>
/// (will never be <see langword="null"/>).
/// </value>
/// <exception cref="Spring.Objects.ObjectsException">
/// In case of initialization errors.
/// </exception>
/// <seealso cref="Spring.Objects.Factory.IObjectFactoryAware.ObjectFactory"/>
public virtual IObjectFactory ObjectFactory
{
get { return _owningObjectFactory; }
set
{
_owningObjectFactory = value;
if (!value.IsPrototype(TargetObjectName))
{
throw new ObjectDefinitionStoreException(
"Cannot use PrototypeTargetSource against a " +
"Singleton object; instances would not be independent.");
}
#region Instrumentation
if (logger.IsDebugEnabled)
{
logger.Debug(string.Format(
"Getting object with name '{0}' to determine class.",
TargetObjectName));
}
#endregion
_targetType = _owningObjectFactory.GetType(TargetObjectName);
}
}
#endregion
#region Methods
/// <summary>
/// Subclasses should use this method to create a new prototype instance.
/// </summary>
protected virtual object NewPrototypeInstance()
{
#region Instrumentation
if (logger.IsDebugEnabled)
{
logger.Debug(string.Format(
"Creating new target from object '{0}'.",
TargetObjectName));
}
#endregion
return ObjectFactory.GetObject(TargetObjectName);
}
/// <summary>
/// Returns the target object.
/// </summary>
/// <returns>The target object.</returns>
/// <exception cref="System.Exception">
/// If unable to obtain the target object.
/// </exception>
public abstract object GetTarget();
/// <summary>
/// Releases the target object.
/// </summary>
/// <param name="target">The target object to release.</param>
public virtual void ReleaseTarget(object target)
{
}
/// <summary>
/// Invoked by an <see cref="Spring.Objects.Factory.IObjectFactory"/>
/// after it has set all object properties supplied
/// (and satisfied the
/// <see cref="Spring.Objects.Factory.IObjectFactoryAware"/>
/// and <see cref="Spring.Context.IApplicationContextAware"/>
/// interfaces).
/// </summary>
/// <remarks>
/// <p>
/// Ensures that the <see cref="TargetObjectName"/> property has been
/// set to a valid value (i.e. is not <see langword="null"/> or a string
/// that consists solely of whitespace).
/// </p>
/// </remarks>
/// <exception cref="System.Exception">
/// In the event of misconfiguration (such as failure to set an essential
/// property) or if initialization fails.
/// </exception>
/// <seealso cref="Spring.Objects.Factory.IInitializingObject.AfterPropertiesSet"/>
public virtual void AfterPropertiesSet()
{
AssertUtils.ArgumentHasText(
TargetObjectName, "TargetObjectName",
"The 'TargetObjectName' property must have a value.");
}
#endregion
#region Fields
/// <summary>
@@ -230,25 +230,25 @@ namespace Spring.Aop.Target
public override string ToString()
{
return GetDescription();
}
}
/// <summary>
/// Returns a textual representation of this target source instance
/// </summary>
protected virtual string GetDescription()
protected virtual string GetDescription()
{
return string.Format("[{0}:{1}]", this.GetType().Name, this.TargetObjectName);
}
/// <summary>
/// The shared <see cref="Common.Logging.ILog"/> instance for this class (and derived classes).
/// </summary>
protected readonly ILog logger = LogManager.GetLogger(typeof (AbstractPrototypeTargetSource));
private String _targetObjectName;
private IObjectFactory _owningObjectFactory;
private Type _targetType;
#endregion
}
}
/// <summary>
/// The shared <see cref="Common.Logging.ILog"/> instance for this class (and derived classes).
/// </summary>
protected readonly ILog logger = LogManager.GetLogger(typeof (AbstractPrototypeTargetSource));
private String _targetObjectName;
private IObjectFactory _owningObjectFactory;
private Type _targetType;
#endregion
}
}

View File

@@ -1,7 +1,7 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<ProjectType>Local</ProjectType>
<ProductVersion>9.0.30729</ProductVersion>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{710961A3-0DF4-49E4-A26E-F5B9C044AC84}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -1145,6 +1145,7 @@
<Compile Include="Util\StringUtils.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Validation\Actions\ExceptionAction.cs" />
<Compile Include="Validation\BaseSimpleValidator.cs" />
<Compile Include="Validation\BaseValidatorGroup.cs" />
<Compile Include="Validation\Actions\ErrorMessageAction.cs" />
@@ -1200,6 +1201,7 @@
</EmbeddedResource>
<EmbeddedResource Include="Objects\Factory\Xml\spring-objects-1.3.xsd" />
<None Include="Spring.Core.build" />
<EmbeddedResource Include="Validation\Config\spring-validation-1.3.xsd" />
<EmbeddedResource Include="Validation\Config\spring-validation-1.1.xsd" />
<EmbeddedResource Include="Resources\Strings.resx">
<SubType>Designer</SubType>

View File

@@ -0,0 +1,97 @@
#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 Common.Logging;
using Spring.Expressions;
using Spring.Util;
namespace Spring.Validation.Actions
{
public class ExceptionAction : BaseValidationAction
{
private ILog log = LogManager.GetLogger(typeof(ExceptionAction));
private IExpression throwsExpression;
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionAction"/> class.
/// </summary>
public ExceptionAction()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionAction"/> class.
/// </summary>
/// <param name="exceptionExpression">Expression that defines the exception to throw when the validator is not valid.</param>
public ExceptionAction(string exceptionExpression)
: this((exceptionExpression != null ? Expression.Parse(exceptionExpression) : null))
{}
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionAction"/> class with an expression
/// that defines the exception to throw.
/// </summary>
public ExceptionAction(IExpression throwsExpression)
{
this.throwsExpression = throwsExpression;
}
/// <summary>
/// Gets or sets the exception to throw
/// </summary>
/// <value>The throws.</value>
public IExpression ThrowsExpression
{
get { return throwsExpression; }
set { throwsExpression = value; }
}
/// <summary>
/// Called when associated validator is invalid.
/// </summary>
/// <param name="validationContext">Validation context.</param>
/// <param name="contextParams">Additional context parameters.</param>
/// <param name="errors">Validation errors container.</param>
protected override void OnInvalid(object validationContext, IDictionary contextParams, IValidationErrors errors)
{
if (throwsExpression != null)
{
object o = null;
try
{
o = throwsExpression.GetValue(null, contextParams);
}
catch (Exception e)
{
log.Error("Was not able to evaluate action expression [" + throwsExpression + "]", e);
}
Exception exception = o as Exception;
if (exception != null)
{
throw exception;
}
}
throw new ValidationException(errors);
}
}
}

View File

@@ -47,7 +47,7 @@ namespace Spring.Validation.Config
NamespaceParser(
Namespace = "http://www.springframework.net/validation",
SchemaLocationAssemblyHint = typeof(ValidationNamespaceParser),
SchemaLocation = "/Spring.Validation.Config/spring-validation-1.1.xsd")
SchemaLocation = "/Spring.Validation.Config/spring-validation-1.3.xsd")
]
public sealed class ValidationNamespaceParser : ObjectsNamespaceParser
{
@@ -167,6 +167,9 @@ namespace Spring.Validation.Config
case ValidatorDefinitionConstants.ActionElement:
actions.Add(ParseGenericAction(child, childParserContext));
break;
case ValidatorDefinitionConstants.ExceptionElement:
actions.Add(ParseExceptionAction(child, childParserContext));
break;
case ValidatorDefinitionConstants.ReferenceElement:
nestedValidators.Add(ParseValidatorReference(child, childParserContext));
break;
@@ -280,6 +283,30 @@ namespace Spring.Validation.Config
return action;
}
private IObjectDefinition ParseExceptionAction(XmlElement element, ParserContext parserContext)
{
string typeName = "Spring.Validation.Actions.ExceptionAction, Spring.Core";
string throwExpression = GetAttributeValue(element, ValidatorDefinitionConstants.ThrowAttribute);
ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues();
ctorArgs.AddGenericArgumentValue(throwExpression);
string when = GetAttributeValue(element, ValidatorDefinitionConstants.WhenAttribute);
MutablePropertyValues properties = new MutablePropertyValues();
if (StringUtils.HasText(when))
{
properties.Add("When", when);
}
IConfigurableObjectDefinition action =
parserContext.ReaderContext.ObjectDefinitionFactory.CreateObjectDefinition(typeName, null, parserContext.ReaderContext.Reader.Domain);
action.ConstructorArgumentValues = ctorArgs;
action.PropertyValues = properties;
return action;
}
/// <summary>
/// Creates a generic action based on the specified element.
/// </summary>
@@ -335,12 +362,14 @@ namespace Spring.Validation.Config
public const string PropertyElement = "property";
public const string MessageElement = "message";
public const string ActionElement = "action";
public const string ExceptionElement = "exception";
public const string ReferenceElement = "ref";
public const string TypeAttribute = "type";
public const string TestAttribute = "test";
public const string NameAttribute = "name";
public const string WhenAttribute = "when";
public const string ThrowAttribute = "throw";
public const string PropertyNameAttribute = "name";

View File

@@ -5,7 +5,7 @@
xmlns:vs="http://schemas.microsoft.com/Visual-Studio-Intellisense"
targetNamespace="http://www.springframework.net/validation"
elementFormDefault="qualified" attributeFormDefault="unqualified"
vs:friendlyname="Spring.NET Validation Framework Configuration" vs:ishtmlschema="false" vs:iscasesensitive="true" vs:requireattributequotes="true" vs:defaultnamespacequalifier="v" vs:defaultnsprefix="v">
vs:friendlyname="Spring.NET Validation Framework Configuration" vs:ishtmlschema="false" vs:iscasesensitive="true" vs:requireattributequotes="true" vs:defaultnamespacequalifier="" vs:defaultnsprefix="">
<xs:import namespace="http://www.springframework.net"/>
@@ -19,259 +19,141 @@
This file defines a configuration schema for the validation framework
object definitions. Using elements from this schema instead of the
standard object definitions can greatly simplify validator configuration.
]]>
]]>
</xs:documentation>
</xs:annotation>
<xs:complexType name="messageType">
<xs:annotation>
<xs:documentation><![CDATA[Define the message action to be executed by the enclosing validator.]]></xs:documentation>
<xs:documentation>Defines a message type.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="param" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:annotation>
<xs:documentation><![CDATA[Define a parameter to the enclosing message action]]></xs:documentation>
</xs:annotation>
<xs:attribute name="value" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[the SpEL expression that evaluates to a parameter value to be passed into the message.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="param" type="messageParamType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[The identifier for the message.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="providers" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[A comma separated list of providers to send the message to.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="when" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[A boolean SpEL expression that determines if this message action shall be executed.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="id" type="objects:nonNullString" use="required"/>
<xs:attribute name="providers" type="objects:nonNullString" use="required"/>
<xs:attribute name="when" type="objects:nonNullString" use="optional"/>
</xs:complexType>
<xs:complexType name="messageParamType">
<xs:annotation>
<xs:documentation>Defines a message parameter type.</xs:documentation>
</xs:annotation>
<xs:attribute name="value" type="objects:nonNullString" use="optional"/>
</xs:complexType>
<xs:complexType name="actionType">
<xs:annotation>
<xs:documentation><![CDATA[Define an action to be executed by the enclosing validator]]></xs:documentation>
<xs:documentation>Defines an action type.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="property" type="objects:property" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="type" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[The qualified CLR typename of this action]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="when" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[A boolean SpEL expression that determines if this action shall be executed.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="type" type="objects:nonNullString" use="required"/>
<xs:attribute name="when" type="objects:nonNullString" use="optional"/>
</xs:complexType>
<xs:complexType name="validatorReferenceType">
<xs:complexType name="referenceType">
<xs:annotation>
<xs:documentation>Defines a validator reference type.</xs:documentation>
</xs:annotation>
<xs:attribute name="name" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[The name of the referenced validator definition]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="context" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[A SpEL expression evaluating to the context to switch to when evaluating the referenced validator.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="name" type="objects:nonNullString" use="required"/>
<xs:attribute name="context" type="objects:nonNullString" use="optional"/>
</xs:complexType>
<xs:complexType name="baseValidatorType">
<xs:annotation>
<xs:documentation>Defines common validator attributes and elements.</xs:documentation>
<xs:documentation>Defines base validator type.</xs:documentation>
</xs:annotation>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="property" type="objects:property" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="message" type="messageType" minOccurs="0" maxOccurs="unbounded" />
<xs:sequence>
<xs:element name="message" type="messageType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="action" type="actionType" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute name="id" use="optional">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[The unique identifier for the validator. The scope of the identifier is the enclosing object factory.]]></xs:documentation>
</xs:annotation>
<xs:restriction base="xs:ID"/>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="parent" use="optional">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[The name of the parent object definition to inherit from.]]></xs:documentation>
</xs:annotation>
<xs:restriction base="objects:nonNullString"/>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="when" use="optional">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[A boolean SpEL expression that determines if this validator shall be executed.]]></xs:documentation>
</xs:annotation>
<xs:restriction base="objects:nonNullString"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:complexType name="simpleValidatorType">
<xs:annotation>
<xs:documentation>Defines simple validator type.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="baseValidatorType">
<xs:attribute name="test" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[An SpEL expression evaluating to the object to be validated by this validator.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:complexContent>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="optional"/>
<xs:attribute name="parent" type="objects:nonNullString" use="optional"/>
<xs:attribute name="test" type="objects:nonNullString" use="required"/>
<xs:attribute name="when" type="objects:nonNullString" use="optional"/>
</xs:complexType>
<xs:complexType name="genericValidatorType">
<xs:annotation>
<xs:documentation>Defines a generic validator type.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="simpleValidatorType">
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[The qualified CLR typename of this generic validator]]></xs:documentation>
</xs:annotation>
<xs:restriction base="objects:nonNullString"/>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:complexContent>
<xs:sequence>
<xs:element name="property" type="objects:property" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="message" type="messageType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="action" type="actionType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="optional"/>
<xs:attribute name="type" type="objects:nonNullString" use="required"/>
<xs:attribute name="parent" type="objects:nonNullString" use="optional"/>
<xs:attribute name="test" type="objects:nonNullString" use="optional"/>
<xs:attribute name="when" type="objects:nonNullString" use="optional"/>
</xs:complexType>
<xs:complexType name="regexValidatorType">
<xs:annotation>
<xs:documentation>Defines a regex validator type.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="simpleValidatorType">
<xs:attribute name="expression" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation>The regular expression to match the result of evaluation the 'test' expression against.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:complexContent>
<xs:sequence>
<xs:element name="property" type="objects:property" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="message" type="messageType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="action" type="actionType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="optional"/>
<xs:attribute name="parent" type="objects:nonNullString" use="optional"/>
<xs:attribute name="test" type="objects:nonNullString" use="required"/>
<xs:attribute name="when" type="objects:nonNullString" use="optional"/>
</xs:complexType>
<xs:complexType name="validatorGroupType">
<xs:annotation>
<xs:documentation>Defines a validator group type.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="baseValidatorType">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element ref="validator" />
<xs:element ref="required" />
<xs:element ref="condition" />
<xs:element ref="regex" />
<xs:element name="ref" type="validatorReferenceType"/>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:choice>
<xs:element name="message" type="messageType"/>
<xs:element name="action" type="actionType"/>
<xs:element ref="collection" />
<xs:element ref="group" />
<xs:element ref="any" />
<xs:element ref="exclusive" />
</xs:choice>
<xs:attribute name="fast-validate" use="optional">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[Determines, whether to short-circuit validation. If true, once the result of the validation is determined, the validation process stops evaluating further validators and returns.]]></xs:documentation>
</xs:annotation>
<xs:restriction base="xs:boolean"/>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:complexContent>
<xs:element name="group" type="validatorGroupType"/>
<xs:element name="any" type="validatorGroupType"/>
<xs:element name="exclusive" type="validatorGroupType"/>
<xs:element name="validator" type="genericValidatorType"/>
<xs:element name="required" type="baseValidatorType"/>
<xs:element name="condition" type="baseValidatorType"/>
<xs:element name="regex" type="regexValidatorType"/>
<xs:element name="ref" type="referenceType"/>
<xs:element name="collection" type="collectionValidatorGroupType"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="optional"/>
<xs:attribute name="parent" type="objects:nonNullString" use="optional"/>
<xs:attribute name="when" type="objects:nonNullString" use="optional"/>
</xs:complexType>
<xs:complexType name="collectionValidatorGroupType">
<xs:annotation>
<xs:documentation>Defines a CollectionValidator.</xs:documentation>
<xs:documentation>Defines a collection validator group type.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="validatorGroupType">
<xs:attribute name="validate-all" type="xs:boolean" use="optional" default="false">
<xs:annotation>
<xs:documentation>Whether to force validating all collection elements, regardless of errors. This attribute is just an alias for 'fast-validate' and kept for backwards compatibility.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="include-element-errors" type="xs:boolean" use="optional" default="false">
<xs:annotation>
<xs:documentation>Whether to include individual element's error messages in the resulting list of messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="context" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation>an SpEL expression to narrow the validation context for each element</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="validate-all" type="xs:boolean" use="optional" default="false"/>
<xs:attribute name="include-element-errors" type="xs:boolean" use="optional" default="false"/>
<xs:attribute name="context" type="objects:nonNullString" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="validator" type="genericValidatorType">
<xs:annotation>
<xs:documentation>Defines a custom validator, allowing to specify the CLR typename of the validator class.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="required" type="simpleValidatorType">
<xs:annotation>
<xs:documentation>Defines a RequiredValidator.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="condition" type="simpleValidatorType">
<xs:annotation>
<xs:documentation>Defines a ConditionValidator.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="regex" type="regexValidatorType">
<xs:annotation>
<xs:documentation>Defines a RegularExpressionValidator.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="group" type="validatorGroupType">
<xs:annotation>
<xs:documentation>Defines a ValidatorGroup.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="any" type="validatorGroupType">
<xs:annotation>
<xs:documentation>Defines an AnyValidatorGroup.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="exclusive" type="validatorGroupType">
<xs:annotation>
<xs:documentation>Defines an ExclusiveValidatorGroup.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="validator" type="genericValidatorType"/>
<xs:element name="required" type="baseValidatorType"/>
<xs:element name="condition" type="baseValidatorType"/>
<xs:element name="regex" type="regexValidatorType"/>
<xs:element name="collection" type="collectionValidatorGroupType">
<xs:annotation>
<xs:documentation>Defines an CollectionValidator.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="group" type="validatorGroupType"/>
<xs:element name="any" type="validatorGroupType"/>
<xs:element name="exclusive" type="validatorGroupType"/>
<xs:element name="collection" type="collectionValidatorGroupType"/>
</xs:schema>

View File

@@ -0,0 +1,294 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns="http://www.springframework.net/validation"
xmlns:objects="http://www.springframework.net"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vs="http://schemas.microsoft.com/Visual-Studio-Intellisense"
targetNamespace="http://www.springframework.net/validation"
elementFormDefault="qualified" attributeFormDefault="unqualified"
vs:friendlyname="Spring.NET Validation Framework Configuration v" vs:ishtmlschema="false" vs:iscasesensitive="true" vs:requireattributequotes="true" vs:defaultnamespacequalifier="v" vs:defaultnsprefix="v">
<xs:import namespace="http://www.springframework.net"/>
<xs:annotation>
<xs:documentation>
<![CDATA[
Spring.NET Validation Framework Config Schema Definition
Author: Aleksandar Seovic
This file defines a configuration schema for the validation framework
object definitions. Using elements from this schema instead of the
standard object definitions can greatly simplify validator configuration.
]]>
</xs:documentation>
</xs:annotation>
<xs:complexType name="messageType">
<xs:annotation>
<xs:documentation><![CDATA[Define the message action to be executed by the enclosing validator.]]></xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="param" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:annotation>
<xs:documentation><![CDATA[Define a parameter to the enclosing message action]]></xs:documentation>
</xs:annotation>
<xs:attribute name="value" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[the SpEL expression that evaluates to a parameter value to be passed into the message.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[The identifier for the message.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="providers" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[A comma separated list of providers to send the message to.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="when" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[A boolean SpEL expression that determines if this message action shall be executed.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="actionType">
<xs:annotation>
<xs:documentation><![CDATA[Define an action to be executed by the enclosing validator]]></xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="property" type="objects:property" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="type" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[The qualified CLR typename of this action]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="when" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[A boolean SpEL expression that determines if this action shall be executed.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="exceptionActionType">
<xs:annotation>
<xs:documentation><![CDATA[Define an exception throws to be executed by the enclosing validator]]></xs:documentation>
</xs:annotation>
<xs:attribute name="throw" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[A boolean SpEL expression that return the exceptions to be thrown.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="when" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[A boolean SpEL expression that determines if this action shall be executed.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="validatorReferenceType">
<xs:annotation>
<xs:documentation>Defines a validator reference type.</xs:documentation>
</xs:annotation>
<xs:attribute name="name" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[The name of the referenced validator definition]]></xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="context" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation><![CDATA[A SpEL expression evaluating to the context to switch to when evaluating the referenced validator.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="baseValidatorType">
<xs:annotation>
<xs:documentation>Defines common validator attributes and elements.</xs:documentation>
</xs:annotation>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="property" type="objects:property" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="message" type="messageType" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="action" type="actionType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="exception" type="exceptionActionType" minOccurs="0" maxOccurs="1"/>
</xs:choice>
<xs:attribute name="id" use="optional">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[The unique identifier for the validator. The scope of the identifier is the enclosing object factory.]]></xs:documentation>
</xs:annotation>
<xs:restriction base="xs:ID"/>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="parent" use="optional">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[The name of the parent object definition to inherit from.]]></xs:documentation>
</xs:annotation>
<xs:restriction base="objects:nonNullString"/>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="when" use="optional">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[A boolean SpEL expression that determines if this validator shall be executed.]]></xs:documentation>
</xs:annotation>
<xs:restriction base="objects:nonNullString"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:complexType name="simpleValidatorType">
<xs:annotation>
<xs:documentation>Defines simple validator type.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="baseValidatorType">
<xs:attribute name="test" type="objects:nonNullString" use="required">
<xs:annotation>
<xs:documentation><![CDATA[An SpEL expression evaluating to the object to be validated by this validator.]]></xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="genericValidatorType">
<xs:annotation>
<xs:documentation>Defines a generic validator type.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="simpleValidatorType">
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[The qualified CLR typename of this generic validator]]></xs:documentation>
</xs:annotation>
<xs:restriction base="objects:nonNullString"/>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="regexValidatorType">
<xs:annotation>
<xs:documentation>Defines a regex validator type.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="simpleValidatorType">
<xs:attribute name="expression" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation>The regular expression to match the result of evaluation the 'test' expression against.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="validatorGroupType">
<xs:annotation>
<xs:documentation>Defines a validator group type.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="baseValidatorType">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element ref="validator" />
<xs:element ref="required" />
<xs:element ref="condition" />
<xs:element ref="regex" />
<xs:element name="ref" type="validatorReferenceType"/>
<xs:element ref="collection" />
<xs:element ref="group" />
<xs:element ref="any" />
<xs:element ref="exclusive" />
</xs:choice>
<xs:attribute name="fast-validate" use="optional">
<xs:simpleType>
<xs:annotation>
<xs:documentation><![CDATA[Determines, whether to short-circuit validation. If true, once the result of the validation is determined, the validation process stops evaluating further validators and returns.]]></xs:documentation>
</xs:annotation>
<xs:restriction base="xs:boolean"/>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="collectionValidatorGroupType">
<xs:annotation>
<xs:documentation>Defines a CollectionValidator.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="validatorGroupType">
<xs:attribute name="validate-all" type="xs:boolean" use="optional" default="false">
<xs:annotation>
<xs:documentation>Whether to force validating all collection elements, regardless of errors. This attribute is just an alias for 'fast-validate' and kept for backwards compatibility.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="include-element-errors" type="xs:boolean" use="optional" default="false">
<xs:annotation>
<xs:documentation>Whether to include individual element's error messages in the resulting list of messages.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="context" type="objects:nonNullString" use="optional">
<xs:annotation>
<xs:documentation>an SpEL expression to narrow the validation context for each element</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="validator" type="genericValidatorType">
<xs:annotation>
<xs:documentation>Defines a custom validator, allowing to specify the CLR typename of the validator class.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="required" type="simpleValidatorType">
<xs:annotation>
<xs:documentation>Defines a RequiredValidator.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="condition" type="simpleValidatorType">
<xs:annotation>
<xs:documentation>Defines a ConditionValidator.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="regex" type="regexValidatorType">
<xs:annotation>
<xs:documentation>Defines a RegularExpressionValidator.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="group" type="validatorGroupType">
<xs:annotation>
<xs:documentation>Defines a ValidatorGroup.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="any" type="validatorGroupType">
<xs:annotation>
<xs:documentation>Defines an AnyValidatorGroup.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="exclusive" type="validatorGroupType">
<xs:annotation>
<xs:documentation>Defines an ExclusiveValidatorGroup.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="collection" type="collectionValidatorGroupType">
<xs:annotation>
<xs:documentation>Defines an CollectionValidator.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:schema>

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>