Exemplo n.º 1
0
        public void CheckInnerExceptionsOnSave()
        {
            Csla.ApplicationContext.Clear();

            Csla.Test.DataPortal.TransactionalRoot root = Csla.Test.DataPortal.TransactionalRoot.NewTransactionalRoot();
            root.FirstName   = "Billy";
            root.LastName    = "lastname";
            root.SmallColumn = "too long for the database"; //normally would be prevented through validation

            string baseException           = string.Empty;
            string baseInnerException      = string.Empty;
            string baseInnerInnerException = string.Empty;
            string exceptionSource         = string.Empty;

            try
            {
                root = root.Save();
            }
            catch (Csla.DataPortalException ex)
            {
                baseException           = ex.Message;
                baseInnerException      = ex.InnerException.Message;
                baseInnerInnerException = ex.InnerException.InnerException.Message;
                exceptionSource         = ex.InnerException.InnerException.Source;
                Assert.IsNull(ex.BusinessObject, "Business object shouldn't be returned");
            }

            //check base exception
            Assert.IsTrue(baseException.StartsWith("DataPortal.Update failed"), "Exception should start with 'DataPortal.Update failed'");
            Assert.IsTrue(baseException.Contains("String or binary data would be truncated."),
                          "Exception should contain 'String or binary data would be truncated.'");
            //check inner exception
            Assert.AreEqual("TransactionalRoot.DataPortal_Insert method call failed", baseInnerException);
            //check inner exception of inner exception
            Assert.AreEqual("String or binary data would be truncated.\r\nThe statement has been terminated.", baseInnerInnerException);

            //check what caused inner exception's inner exception (i.e. the root exception)
            Assert.AreEqual(".Net SqlClient Data Provider", exceptionSource);

            //verify that the implemented method, DataPortal_OnDataPortal
            //was called for the business object that threw the exception
            Assert.AreEqual("Called", Csla.ApplicationContext.GlobalContext["OnDataPortalException"]);
        }
Exemplo n.º 2
0
        public void TestTransactionScopeUpdate()
        {
            Csla.Test.DataPortal.TransactionalRoot tr = Csla.Test.DataPortal.TransactionalRoot.NewTransactionalRoot();
            tr.FirstName = "Bill";
            tr.LastName  = "Johnson";
            //setting smallColumn to a string less than or equal to 5 characters will
            //not cause the transaction to rollback
            tr.SmallColumn = "abc";

            tr = tr.Save();

            SqlConnection cn = new SqlConnection(CONNECTION_STRING);
            SqlCommand    cm = new SqlCommand("SELECT * FROM Table2", cn);

            try
            {
                cn.Open();
                SqlDataReader dr = cm.ExecuteReader();

                //will have rows since no sqlexception was thrown on the insert
                Assert.AreEqual(true, dr.HasRows);
                dr.Close();
            }
            catch (Exception ex)
            {
                //do nothing
            }
            finally
            {
                cn.Close();
            }

            ClearDataBase();

            Csla.Test.DataPortal.TransactionalRoot tr2 = Csla.Test.DataPortal.TransactionalRoot.NewTransactionalRoot();
            tr2.FirstName = "Jimmy";
            tr2.LastName  = "Smith";
            //intentionally input a string longer than varchar(5) to
            //cause a sql exception and rollback the transaction
            tr2.SmallColumn = "this will cause a sql exception";

            try
            {
                //will throw a sql exception since the SmallColumn property is too long
                tr2 = tr2.Save();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex.Message.StartsWith("DataPortal.Update failed"), "Invalid exception message");
            }

            //within the DataPortal_Insert method, two commands are run to insert data into
            //the database. Here we verify that both commands have been rolled back
            try
            {
                cn.Open();
                SqlDataReader dr = cm.ExecuteReader();

                //should not have rows since both commands were rolled back
                Assert.AreEqual(false, dr.HasRows);
                dr.Close();
            }
            catch (Exception ex)
            {
                //do nothing
            }
            finally
            {
                cn.Close();
            }

            ClearDataBase();
        }