private void CheckTranslation(IAdoExceptionTranslator translator, string errorCode, Type exType) { TestSqlException sex = new TestSqlException("", errorCode); DataAccessException ex = translator.Translate("", "", sex); Assert.IsTrue(exType.IsAssignableFrom(ex.GetType())); Assert.IsTrue(ex.InnerException == sex); }
public void ErrorCodeTransation() { IAdoExceptionTranslator exceptionTranslator = new ErrorCodeExceptionTranslator(dbProvider, ERROR_CODES); TestSqlException badSqlEx = new TestSqlException("", "1"); BadSqlGrammarException bsgex = (BadSqlGrammarException)exceptionTranslator.Translate("task","SQL", badSqlEx); Assert.AreEqual("SQL", bsgex.Sql); Assert.AreEqual(badSqlEx, bsgex.InnerException); TestSqlException invResEx = new TestSqlException("", "4"); InvalidResultSetAccessException irsex = (InvalidResultSetAccessException) exceptionTranslator.Translate("task", "SQL", invResEx); Assert.AreEqual("SQL", irsex.Sql); Assert.AreEqual(invResEx, irsex.InnerException); CheckTranslation(exceptionTranslator, "5", typeof(DataAccessResourceFailureException)); CheckTranslation(exceptionTranslator, "544", typeof(DataIntegrityViolationException)); CheckTranslation(exceptionTranslator, "7", typeof(CannotAcquireLockException)); CheckTranslation(exceptionTranslator, "8", typeof(DeadlockLoserDataAccessException)); CheckTranslation(exceptionTranslator, "9", typeof(CannotSerializeTransactionException)); }
public void ErrorCodeTransation() { IAdoExceptionTranslator exceptionTranslator = new ErrorCodeExceptionTranslator(dbProvider, ERROR_CODES); TestSqlException badSqlEx = new TestSqlException("", "1"); BadSqlGrammarException bsgex = (BadSqlGrammarException)exceptionTranslator.Translate("task", "SQL", badSqlEx); Assert.AreEqual("SQL", bsgex.Sql); Assert.AreEqual(badSqlEx, bsgex.InnerException); TestSqlException invResEx = new TestSqlException("", "4"); InvalidResultSetAccessException irsex = (InvalidResultSetAccessException)exceptionTranslator.Translate("task", "SQL", invResEx); Assert.AreEqual("SQL", irsex.Sql); Assert.AreEqual(invResEx, irsex.InnerException); CheckTranslation(exceptionTranslator, "5", typeof(DataAccessResourceFailureException)); CheckTranslation(exceptionTranslator, "544", typeof(DataIntegrityViolationException)); CheckTranslation(exceptionTranslator, "7", typeof(CannotAcquireLockException)); CheckTranslation(exceptionTranslator, "8", typeof(DeadlockLoserDataAccessException)); CheckTranslation(exceptionTranslator, "9", typeof(CannotSerializeTransactionException)); }
/// <summary> /// Does the test transaction commit with flush failure. /// </summary> /// <param name="fallbackTranslation">if set to <c>true</c> if the exception throw /// is of the type NHibernate.ADOException, in which case HibernateTransactionManager /// will 'fallback' to using the error codes in the underlying exception thrown by /// the provider, ie. a SqlException, MySqlException. Otherwise, if it is /// another subclass of HibernateException, then perform a direct maping as /// found in SessionFactoryUtils.ConvertHibernateAccessException.</param> private void DoTransactionCommitWithFlushFailure(bool fallbackTranslation) { #region Mock Setup IDbProvider provider = new TestDbProvider(); IDbConnection connection = mocks.StrictMock<IDbConnection>(); ISessionFactory sessionFactory = mocks.StrictMock<ISessionFactory>(); ISession session = mocks.StrictMock<ISession>(); ITransaction transaction = mocks.StrictMock<ITransaction>(); IDbTransaction adoTransaction = mocks.StrictMock<IDbTransaction>(); Exception rootCause = null; using (mocks.Ordered()) { Expect.Call(sessionFactory.OpenSession()).Return(session); Expect.Call(session.Connection).Return(connection); Expect.Call(session.BeginTransaction(IsolationLevel.ReadCommitted)).Return(transaction); Expect.Call(session.IsOpen).Return(true); transaction.Commit(); Exception sqlException = new TestSqlException("mymsg", "2627"); if (fallbackTranslation) { //error code 2627 will map to a DataAccessIntegrity exception in sqlserver, which is the metadata //used by TestDbProvider. rootCause = sqlException; LastCall.On(transaction).Throw(new ADOException("mymsg", sqlException)); } else { rootCause = new PropertyValueException("mymsg", typeof(string).Name, "Name"); LastCall.On(transaction).Throw(rootCause); } Expect.Call(adoTransaction.Connection).Return(connection); LastCall.On(adoTransaction).Repeat.Once(); transaction.Rollback(); LastCall.On(transaction).Repeat.Once(); Expect.Call(session.Close()).Return(null); } #endregion mocks.ReplayAll(); TestableHibernateTransactionManager tm = new TestableHibernateTransactionManager(sessionFactory); tm.TransactionSynchronization = TransactionSynchronizationState.Always; tm.DbProvider = provider; tm.StubbedTransactionThatReturnsExpectedConnection = adoTransaction; TransactionTemplate tt = new TransactionTemplate(tm); Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active"); IList list = new ArrayList(); list.Add("test"); try { tt.Execute(new TransactionCommitWithFlushFailureCallback(sessionFactory, list)); Assert.Fail("Should have thrown DataIntegrityViolationException"); } catch (DataIntegrityViolationException ex) { Assert.AreEqual(rootCause, ex.InnerException); Assert.IsTrue(ex.Message.IndexOf("mymsg") != -1); } Assert.IsTrue(!TransactionSynchronizationManager.HasResource(sessionFactory), "Hasn't thread session"); Assert.IsTrue(!TransactionSynchronizationManager.SynchronizationActive, "Synchronizations not active"); mocks.VerifyAll(); }
private void CheckTranslation(IAdoExceptionTranslator translator, string errorCode, Type exType) { TestSqlException sex = new TestSqlException("", errorCode); DataAccessException ex = translator.Translate("", "", sex); Assert.IsTrue(exType.IsAssignableFrom(ex.GetType())); Assert.IsTrue(ex.InnerException == sex); }
public string ExtractError(Exception e) { TestSqlException testSqlException = (TestSqlException)e; return(testSqlException.ErrorNumber); }