From 7b8ea2a7eab7e81d2add6e37f0c3003af1fa206c Mon Sep 17 00:00:00 2001 From: markpollack Date: Tue, 28 Jul 2009 20:15:08 +0000 Subject: [PATCH] SPRNET-904 - RollbackRuleAttribute throws AopConfiguration exception instead of ArgumentException --- .../Interceptor/RollbackRuleAttribute.cs | 409 +++++++++--------- .../NoRollbackRuleAttributeTests.cs | 2 +- .../Interceptor/RollbackRuleAttributeTests.cs | 2 +- 3 files changed, 210 insertions(+), 203 deletions(-) diff --git a/src/Spring/Spring.Data/Transaction/Interceptor/RollbackRuleAttribute.cs b/src/Spring/Spring.Data/Transaction/Interceptor/RollbackRuleAttribute.cs index 15565bbb..9bde262d 100644 --- a/src/Spring/Spring.Data/Transaction/Interceptor/RollbackRuleAttribute.cs +++ b/src/Spring/Spring.Data/Transaction/Interceptor/RollbackRuleAttribute.cs @@ -1,201 +1,208 @@ -#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 Spring.Aop.Framework; -using Spring.Util; - -namespace Spring.Transaction.Interceptor -{ - /// - /// Rule determining whether or not a given exception (and any subclasses) should - /// cause a rollback. - /// - /// - ///

- /// Multiple such rules can be applied to determine whether a transaction should commit - /// or rollback after an exception has been thrown. - ///

- ///
- /// Griffin Caprio (.NET) - [Serializable] - public class RollbackRuleAttribute : Attribute - { - private string _exceptionName; - - /// - /// Canonical instance representing default behavior for rolling back on - /// all s. - /// - public static RollbackRuleAttribute RollbackOnSystemExceptions - = new RollbackRuleAttribute(typeof (Exception).Name); - - /// - /// Creates a new instance of the - /// class - /// for the named . - /// - /// The exception name. - /// - ///

- /// As always, the should be the full - /// assembly qualified version. - ///

- ///
- public RollbackRuleAttribute( string exceptionName ) - { - AssertUtils.ArgumentHasText(exceptionName, "exceptionName"); - _exceptionName = exceptionName; - } - - /// - /// Creates a new instance of the - /// class - /// for the suplied . - /// - /// - ///

- /// The exception class must be or a subclass. - ///

- ///

- /// This is the preferred way to construct a - /// , - /// matching the exception class and subclasses. - ///

- ///
- /// - /// The class that will trigger a rollback. - /// - public RollbackRuleAttribute( Type exceptionType ) - { - AssertUtils.ArgumentNotNull(exceptionType, "exceptionType"); - if ( ! typeof(Exception).IsAssignableFrom( exceptionType ) ) - { - throw new AopConfigException("Cannot construct rollback rule from " + exceptionType + "; " + "It's not an Exception"); - } - _exceptionName = exceptionType.Name; - } - - /// - /// Returns the name of the exception. - /// - public string ExceptionName - { - get { return _exceptionName; } - } - - /// - /// Return the depth to the matching superclass execption . - /// - /// - /// A return value of 0 means that the matches. - /// - /// - /// The of exception to find. - /// - /// - /// Return -1 if there's no match. Otherwise, return depth. Lowest depth wins. - /// - public int GetDepth( Type exceptionType ) - { - return getDepth( exceptionType, 0 ); - } - - /// - /// Return the depth to the matching superclass execption . - /// - /// - /// A return value of 0 means that the s - /// matches. - /// - /// - /// The exception object to find. - /// - /// - /// Return -1 if there's no match. Otherwise, return depth. Lowest depth wins. - /// - public int GetDepth( object exception ) - { - return GetDepth( exception.GetType() ); - } - - /// - /// Returns a representation of this instance. - /// - /// - /// A containing the exception covered by this instance. - /// - public override string ToString() - { - return "RollbackRule with pattern '" + ExceptionName + "'"; - } - - /// - /// Override of . - /// - /// The hashcode of the exception name covered by this instance. - public override int GetHashCode() - { - return ExceptionName.GetHashCode(); - } - - /// - /// Override of . - /// - /// The object to compare. - /// True if the input object is equal to this instance. - public override bool Equals(object obj) - { - RollbackRuleAttribute rollbackRuleAttribute = obj as RollbackRuleAttribute; - if ( rollbackRuleAttribute == null ) - { - return false; - } - return Equals( rollbackRuleAttribute ); - } - - /// - /// Strongly typed Equals() implementation. - /// - /// - /// The to compare. - /// - /// - /// True if the input object is equal to the supplied . - /// - public bool Equals( RollbackRuleAttribute rollbackRuleAttribute ) - { - return ExceptionName.Equals(rollbackRuleAttribute.ExceptionName); - } - - private int getDepth( Type exceptionType, int depth ) - { - if ( ( exceptionType.Name.IndexOf( ExceptionName ) != -1 ) || ( exceptionType.FullName.IndexOf( ExceptionName ) != -1 ) ) - { - return depth; - } - if ( exceptionType == typeof(Exception)) - { - return -1; - } - return getDepth( exceptionType.BaseType, depth + 1 ); - } - } -} +#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 Spring.Aop.Framework; +using Spring.Util; + +namespace Spring.Transaction.Interceptor +{ + /// + /// Rule determining whether or not a given exception (and any subclasses) should + /// cause a rollback. + /// + /// + ///

+ /// Multiple such rules can be applied to determine whether a transaction should commit + /// or rollback after an exception has been thrown. + ///

+ ///
+ /// Griffin Caprio (.NET) + [Serializable] + public class RollbackRuleAttribute : Attribute + { + /// + /// Could hold exception, resolving class name but would always require FQN. + /// This way does multiple string comparisons, but how often do we decide + /// whether to roll back a transaction following an exception? + /// + private string _exceptionName; + + /// + /// Canonical instance representing default behavior for rolling back on + /// all s. + /// + public static RollbackRuleAttribute RollbackOnSystemExceptions + = new RollbackRuleAttribute(typeof (Exception).Name); + + /// + /// Creates a new instance of the + /// class + /// for the named . + /// + /// The exception name. + /// + ///

+ /// As always, the should be the full + /// assembly qualified version. + ///

+ ///
+ public RollbackRuleAttribute( string exceptionName ) + { + AssertUtils.ArgumentHasText(exceptionName, "exceptionName"); + _exceptionName = exceptionName; + } + + /// + /// Creates a new instance of the + /// class + /// for the suplied . + /// + /// + ///

+ /// The exception class must be or a subclass. + ///

+ ///

+ /// This is the preferred way to construct a + /// , + /// matching the exception class and subclasses. + ///

+ ///
+ /// + /// The class that will trigger a rollback. + /// + public RollbackRuleAttribute( Type exceptionType ) + { + AssertUtils.ArgumentNotNull(exceptionType, "exceptionType"); + if ( ! typeof(Exception).IsAssignableFrom( exceptionType ) ) + { + throw new ArgumentException("Cannot construct rollback rule from " + exceptionType + "; " + "It's not an Exception"); + } + _exceptionName = exceptionType.Name; + } + + /// + /// Returns the name of the exception. + /// + public string ExceptionName + { + get { return _exceptionName; } + } + + /// + /// Return the depth to the matching superclass execption . + /// + /// + /// A return value of 0 means that the matches. + /// + /// + /// The of exception to find. + /// + /// + /// Return -1 if there's no match. Otherwise, return depth. Lowest depth wins. + /// + public int GetDepth( Type exceptionType ) + { + return getDepth( exceptionType, 0 ); + } + + /// + /// Return the depth to the matching superclass execption . + /// + /// + /// A return value of 0 means that the s + /// matches. + /// + /// + /// The exception object to find. + /// + /// + /// Return -1 if there's no match. Otherwise, return depth. Lowest depth wins. + /// + public int GetDepth( object exception ) + { + return GetDepth( exception.GetType() ); + } + + /// + /// Returns a representation of this instance. + /// + /// + /// A containing the exception covered by this instance. + /// + public override string ToString() + { + return "RollbackRule with pattern '" + ExceptionName + "'"; + } + + /// + /// Override of . + /// + /// The hashcode of the exception name covered by this instance. + public override int GetHashCode() + { + return base.GetHashCode(); + } + + + /// + /// Override of . + /// + /// The object to compare. + /// True if the input object is equal to this instance. + public override bool Equals(object obj) + { + if (ReferenceEquals(this, obj)) return true; + RollbackRuleAttribute rollbackRuleAttribute = obj as RollbackRuleAttribute; + if ( rollbackRuleAttribute == null ) + { + return false; + } + return Equals( rollbackRuleAttribute ); + } + + /// + /// Strongly typed Equals() implementation. + /// + /// + /// The to compare. + /// + /// + /// True if the input object is equal to the supplied . + /// + public bool Equals( RollbackRuleAttribute rollbackRuleAttribute ) + { + return base.Equals(rollbackRuleAttribute); + } + + private int getDepth( Type exceptionType, int depth ) + { + if ( ( exceptionType.Name.IndexOf( ExceptionName ) != -1 ) || ( exceptionType.FullName.IndexOf( ExceptionName ) != -1 ) ) + { + return depth; + } + if ( exceptionType == typeof(Exception)) + { + return -1; + } + return getDepth( exceptionType.BaseType, depth + 1 ); + } + } +} diff --git a/test/Spring/Spring.Data.Tests/Transaction/Interceptor/NoRollbackRuleAttributeTests.cs b/test/Spring/Spring.Data.Tests/Transaction/Interceptor/NoRollbackRuleAttributeTests.cs index 40999406..3e8294c4 100644 --- a/test/Spring/Spring.Data.Tests/Transaction/Interceptor/NoRollbackRuleAttributeTests.cs +++ b/test/Spring/Spring.Data.Tests/Transaction/Interceptor/NoRollbackRuleAttributeTests.cs @@ -43,7 +43,7 @@ namespace Spring.Transaction.Interceptor Assert.IsTrue(rr.GetDepth(typeof(TransactionSystemException)) > 0); } [Test] - [ExpectedException(typeof(AopConfigException))] + [ExpectedException(typeof(ArgumentException))] public void ConstructorArgMustBeAExceptionClass() { new NoRollbackRuleAttribute( typeof( StringBuilder ) ); diff --git a/test/Spring/Spring.Data.Tests/Transaction/Interceptor/RollbackRuleAttributeTests.cs b/test/Spring/Spring.Data.Tests/Transaction/Interceptor/RollbackRuleAttributeTests.cs index 1bf95286..b99601bf 100644 --- a/test/Spring/Spring.Data.Tests/Transaction/Interceptor/RollbackRuleAttributeTests.cs +++ b/test/Spring/Spring.Data.Tests/Transaction/Interceptor/RollbackRuleAttributeTests.cs @@ -76,7 +76,7 @@ namespace Spring.Transaction.Interceptor } [Test] - [ExpectedException(typeof (AopConfigException))] + [ExpectedException(typeof (ArgumentException))] public void ConstructorArgMustBeAExceptionClass() { new RollbackRuleAttribute(typeof (StringBuilder));