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 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 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 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 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 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_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 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 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_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_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_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_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));
            }