public void Can_be_invoked_twice_without_throwing()
            {
                var handler = new CommitFailureHandler();

                handler.Dispose();
                handler.Dispose();
            }
            public void BeganTransaction_does_not_fail_if_exception_thrown_such_that_there_is_no_transaction()
            {
                var context = MockHelper.CreateMockObjectContext <object>();
                var handler = new CommitFailureHandler();

                handler.Initialize(context);

                var interceptionContext = new BeginTransactionInterceptionContext().WithObjectContext(context);

                Assert.DoesNotThrow(() => handler.BeganTransaction(new Mock <DbConnection>().Object, interceptionContext));
            }
            public void Returns_false_with_ObjectContext_if_nothing_matches()
            {
                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(MockHelper.CreateMockObjectContext <object>());

                    Assert.False(
                        handler.MatchesParentContext(
                            new Mock <DbConnection>().Object,
                            new DbInterceptionContext().WithObjectContext(MockHelper.CreateMockObjectContext <object>())
                            .WithDbContext(new DbContext("c"))));
                }
            }
 public void Throws_for_null_parameters()
 {
     using (var handler = new CommitFailureHandler())
     {
         Assert.Equal(
             "connection",
             Assert.Throws <ArgumentNullException>(() => handler.MatchesParentContext(null, new DbInterceptionContext()))
             .ParamName);
         Assert.Equal(
             "interceptionContext",
             Assert.Throws <ArgumentNullException>(() => handler.MatchesParentContext(new Mock <DbConnection>().Object, null))
             .ParamName);
     }
 }
            public void Returns_true_with_DbContext_if_no_context_same_connection()
            {
                var connection = CreateMockConnection();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(new DbContext("c"), connection);

                    Assert.True(
                        handler.MatchesParentContext(
                            connection,
                            new DbInterceptionContext()));
                }
            }
            public void Initializes_with_DbContext()
            {
                var context = new DbContext("c");

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context, context.Database.Connection);

                    Assert.Null(handler.ObjectContext);
                    Assert.Same(context, handler.DbContext);
                    Assert.Same(context.Database.Connection, handler.Connection);
                    Assert.IsType<TransactionContext>(handler.TransactionContext);
                }
            }
            public void Returns_true_with_ObjectContext_if_no_context_same_connection()
            {
                var context = MockHelper.CreateMockObjectContext <object>();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.True(
                        handler.MatchesParentContext(
                            ((EntityConnection)context.Connection).StoreConnection,
                            new DbInterceptionContext()));
                }
            }
            public void Initializes_with_DbContext()
            {
                var context = new DbContext("c");

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context, context.Database.Connection);

                    Assert.Null(handler.ObjectContext);
                    Assert.Same(context, handler.DbContext);
                    Assert.Same(context.Database.Connection, handler.Connection);
                    Assert.IsType <TransactionContext>(handler.TransactionContext);
                }
            }
            public void Initializes_with_ObjectContext()
            {
                var context = MockHelper.CreateMockObjectContext <object>();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.Same(context, handler.ObjectContext);
                    Assert.Same(context.InterceptionContext.DbContexts.FirstOrDefault(), handler.DbContext);
                    Assert.Same(((EntityConnection)context.Connection).StoreConnection, handler.Connection);
                    Assert.Same(((EntityConnection)context.Connection).StoreConnection, handler.Connection);
                    Assert.IsType <TransactionContext>(handler.TransactionContext);
                }
            }
 public void Throws_for_null_parameters()
 {
     using (var handler = new CommitFailureHandler())
     {
         Assert.Equal(
             "connection",
             Assert.Throws<ArgumentNullException>(() => handler.Initialize(new DbContext("c"), null)).ParamName);
         Assert.Equal(
             "context",
             Assert.Throws<ArgumentNullException>(() => handler.Initialize(null, new Mock<DbConnection>().Object)).ParamName);
         Assert.Equal(
             "context",
             Assert.Throws<ArgumentNullException>(() => handler.Initialize(null)).ParamName);
     }
 }
            public void Returns_true_with_ObjectContext_if_same_ObjectContext()
            {
                var context = MockHelper.CreateMockObjectContext <object>();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.True(
                        handler.MatchesParentContext(
                            new Mock <DbConnection>().Object,
                            new DbInterceptionContext().WithObjectContext(context)
                            .WithDbContext(new DbContext("c"))));
                }
            }
            public void Initializes_with_ObjectContext()
            {
                var context = MockHelper.CreateMockObjectContext<object>();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.Same(context, handler.ObjectContext);
                    Assert.Same(context.InterceptionContext.DbContexts.FirstOrDefault(), handler.DbContext);
                    Assert.Same(((EntityConnection)context.Connection).StoreConnection, handler.Connection);
                    Assert.Same(((EntityConnection)context.Connection).StoreConnection, handler.Connection);
                    Assert.IsType<TransactionContext>(handler.TransactionContext);
                }
            }
 public void Throws_for_null_parameters()
 {
     using (var handler = new CommitFailureHandler())
     {
         Assert.Equal(
             "connection",
             Assert.Throws <ArgumentNullException>(() => handler.Initialize(new DbContext("c"), null)).ParamName);
         Assert.Equal(
             "context",
             Assert.Throws <ArgumentNullException>(() => handler.Initialize(null, new Mock <DbConnection>().Object)).ParamName);
         Assert.Equal(
             "context",
             Assert.Throws <ArgumentNullException>(() => handler.Initialize(null)).ParamName);
     }
 }
            public void Returns_false_with_DbContext_if_different_context_same_connection()
            {
                var connection = CreateMockConnection();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(new DbContext("c"), connection);

                    Assert.False(
                        handler.MatchesParentContext(
                            connection,
                            new DbInterceptionContext().WithObjectContext(MockHelper.CreateMockObjectContext <object>())
                            .WithDbContext(new DbContext("c"))));
                }
            }
            public void Throws_if_already_initialized_with_ObjectContext()
            {
                var context = MockHelper.CreateMockObjectContext<object>();

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.Equal(
                        Strings.TransactionHandler_AlreadyInitialized,
                        Assert.Throws<InvalidOperationException>(() => handler.Initialize(context)).Message);

                    var dbContext = new DbContext("c");
                    Assert.Equal(
                        Strings.TransactionHandler_AlreadyInitialized,
                        Assert.Throws<InvalidOperationException>(() => handler.Initialize(dbContext, dbContext.Database.Connection)).Message);
                }
            }
            public void Throws_if_already_initialized_with_DbContext()
            {
                var dbContext = new DbContext("c");

                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(dbContext, dbContext.Database.Connection);

                    var context = MockHelper.CreateMockObjectContext <object>();
                    Assert.Equal(
                        Strings.TransactionHandler_AlreadyInitialized,
                        Assert.Throws <InvalidOperationException>(() => handler.Initialize(context)).Message);

                    Assert.Equal(
                        Strings.TransactionHandler_AlreadyInitialized,
                        Assert.Throws <InvalidOperationException>(() => handler.Initialize(dbContext, dbContext.Database.Connection)).Message);
                }
            }
 public void Throws_for_null_parameters()
 {
     using (var handler = new CommitFailureHandler())
     {
         Assert.Equal(
             "connection",
             Assert.Throws<ArgumentNullException>(() => handler.MatchesParentContext(null, new DbInterceptionContext()))
                 .ParamName);
         Assert.Equal(
             "interceptionContext",
             Assert.Throws<ArgumentNullException>(() => handler.MatchesParentContext(new Mock<DbConnection>().Object, null))
                 .ParamName);
     }
 }
예제 #18
0
 /// <summary>
 /// Gets the <see cref="T:System.Data.Entity.Infrastructure.CommitFailureHandler" /> associated with the <paramref name="context" /> if there is one;
 /// otherwise returns <c>null</c>.
 /// </summary>
 /// <param name="context">The context</param>
 /// <returns>The associated <see cref="T:System.Data.Entity.Infrastructure.CommitFailureHandler" />.</returns>
 public static CommitFailureHandler FromContext(DbContext context)
 {
     Check.NotNull <DbContext>(context, nameof(context));
     return(CommitFailureHandler.FromContext(((IObjectContextAdapter)context).ObjectContext));
 }
            public void Returns_true_with_DbContext_if_no_context_same_connection()
            {
                var connection = CreateMockConnection();
                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(new DbContext("c"), connection);

                    Assert.True(
                        handler.MatchesParentContext(
                            connection,
                            new DbInterceptionContext()));
                }
            }
            public void Returns_false_with_DbContext_if_different_context_same_connection()
            {
                var connection = CreateMockConnection();
                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(new DbContext("c"), connection);

                    Assert.False(
                        handler.MatchesParentContext(
                            connection,
                            new DbInterceptionContext().WithObjectContext(MockHelper.CreateMockObjectContext<object>())
                                .WithDbContext(new DbContext("c"))));
                }
            }
            public void Returns_false_with_ObjectContext_if_nothing_matches()
            {
                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(MockHelper.CreateMockObjectContext<object>());

                    Assert.False(
                        handler.MatchesParentContext(
                            new Mock<DbConnection>().Object,
                            new DbInterceptionContext().WithObjectContext(MockHelper.CreateMockObjectContext<object>())
                                .WithDbContext(new DbContext("c"))));
                }
            }
            public void Returns_true_with_ObjectContext_if_same_ObjectContext()
            {
                var context = MockHelper.CreateMockObjectContext<object>();
                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.True(
                        handler.MatchesParentContext(
                            new Mock<DbConnection>().Object,
                            new DbInterceptionContext().WithObjectContext(context)
                                .WithDbContext(new DbContext("c"))));
                }
            }
            public void Returns_true_with_ObjectContext_if_no_context_same_connection()
            {
                var context = MockHelper.CreateMockObjectContext<object>();
                using (var handler = new CommitFailureHandler())
                {
                    handler.Initialize(context);

                    Assert.True(
                        handler.MatchesParentContext(
                            ((EntityConnection)context.Connection).StoreConnection,
                            new DbInterceptionContext()));
                }
            }
            public void BeganTransaction_does_not_fail_if_exception_thrown_such_that_there_is_no_transaction()
            {
                var context = MockHelper.CreateMockObjectContext<object>();
                var handler = new CommitFailureHandler();
                handler.Initialize(context);

                var interceptionContext = new BeginTransactionInterceptionContext().WithObjectContext(context);

                Assert.DoesNotThrow(() => handler.BeganTransaction(new Mock<DbConnection>().Object, interceptionContext));
            }
            public void Can_be_invoked_twice_without_throwing()
            {
                var handler = new CommitFailureHandler();

                handler.Dispose();
                handler.Dispose();
            }