public void Opening_dispatches_to_interceptors() { var interceptionContext = new DbInterceptionContext(); var connection = new Mock<EntityConnection>().Object; var mockInterceptor = new Mock<IEntityConnectionInterceptor>(); mockInterceptor.Setup(m => m.ConnectionOpening(connection, interceptionContext)).Returns(true); var dispatcher = new EntityConnectionDispatcher(); var internalDispatcher = dispatcher.InternalDispatcher; internalDispatcher.Add(mockInterceptor.Object); Assert.True(dispatcher.Opening(connection, interceptionContext)); mockInterceptor.Verify(m => m.ConnectionOpening(connection, interceptionContext)); }
public void Opening_returns_false_if_any_interceptor_returns_false() { var mockInterceptor1 = new Mock<IEntityConnectionInterceptor>(); mockInterceptor1.Setup(m => m.ConnectionOpening(It.IsAny<EntityConnection>(), It.IsAny<DbInterceptionContext>())).Returns(true); var mockInterceptor2 = new Mock<IEntityConnectionInterceptor>(); mockInterceptor2.Setup(m => m.ConnectionOpening(It.IsAny<EntityConnection>(), It.IsAny<DbInterceptionContext>())).Returns(false); var mockInterceptor3 = new Mock<IEntityConnectionInterceptor>(); mockInterceptor3.Setup(m => m.ConnectionOpening(It.IsAny<EntityConnection>(), It.IsAny<DbInterceptionContext>())).Returns(true); var dispatcher = new EntityConnectionDispatcher(); var internalDispatcher = dispatcher.InternalDispatcher; internalDispatcher.Add(mockInterceptor1.Object); internalDispatcher.Add(mockInterceptor2.Object); internalDispatcher.Add(mockInterceptor3.Object); Assert.False(dispatcher.Opening(new Mock<EntityConnection>().Object, new DbInterceptionContext())); }
public void Open_should_dispatch_and_optionally_open_underlying_connection() { var mockConnection = new Mock<DbConnection>(); mockConnection.SetupGet(m => m.DataSource).Returns("Foo"); var dispatcher = new EntityConnectionDispatcher(); var mockConnectionInterceptor = new Mock<IEntityConnectionInterceptor>(); dispatcher.InternalDispatcher.Add(mockConnectionInterceptor.Object); var mockStoreItemCollection = new Mock<StoreItemCollection>(); mockStoreItemCollection .SetupGet(m => m.StoreProviderFactory) .Returns(DbProviderServices.GetProviderFactory(new SqlConnection())); var mockMetadataWorkspace = new Mock<MetadataWorkspace>(); mockMetadataWorkspace .Setup(m => m.GetItemCollection(DataSpace.SSpace)) .Returns(mockStoreItemCollection.Object); var connection = new EntityConnection( mockMetadataWorkspace.Object, mockConnection.Object, true, true, dispatcher); var objectContext = new ObjectContext(); connection.AssociateContext(objectContext); mockConnectionInterceptor .Setup(m => m.ConnectionOpening(connection, It.IsAny<DbInterceptionContext>())) .Callback<EntityConnection, DbInterceptionContext>( (_, c) => Assert.Equal(new[] { objectContext }, c.ObjectContexts)) .Returns(false); connection.Open(); mockConnection.Verify(m => m.Open(), Times.Never()); Assert.Equal(ConnectionState.Open, connection.State); mockConnectionInterceptor .Setup(m => m.ConnectionOpening(connection, It.IsAny<DbInterceptionContext>())) .Callback<EntityConnection, DbInterceptionContext>( (_, c) => Assert.Equal(new[] { objectContext }, c.ObjectContexts)) .Returns(true); mockConnection .Setup(m => m.Open()) .Callback( () => mockConnection .SetupGet(m => m.State).Returns(ConnectionState.Open) .Raises( m => m.StateChange += null, new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open))); connection.Open(); mockConnection.Verify(m => m.Open(), Times.Once()); Assert.Equal(ConnectionState.Open, connection.State); }
public EntityConnection(string connectionString) { ChangeConnectionString(connectionString); _dispatcher = Interception.Dispatch.EntityConnection; }
/// <summary> /// This constructor allows to skip the initialization code for testing purposes. /// </summary> internal EntityConnection( MetadataWorkspace workspace, DbConnection connection, bool skipInitialization, bool entityConnectionOwnsStoreConnection, EntityConnectionDispatcher dispatcher = null) { if (!skipInitialization) { if (!workspace.IsItemCollectionAlreadyRegistered(DataSpace.CSpace)) { throw new ArgumentException(Strings.EntityClient_ItemCollectionsNotRegisteredInWorkspace("EdmItemCollection")); } if (!workspace.IsItemCollectionAlreadyRegistered(DataSpace.SSpace)) { throw new ArgumentException(Strings.EntityClient_ItemCollectionsNotRegisteredInWorkspace("StoreItemCollection")); } if (!workspace.IsItemCollectionAlreadyRegistered(DataSpace.CSSpace)) { throw new ArgumentException( Strings.EntityClient_ItemCollectionsNotRegisteredInWorkspace("StorageMappingItemCollection")); } // Verify that a factory can be retrieved var providerFactory = connection.GetProviderFactory(); if (providerFactory == null) { throw new ProviderIncompatibleException(Strings.EntityClient_DbConnectionHasNoProvider(connection)); } var collection = (StoreItemCollection)workspace.GetItemCollection(DataSpace.SSpace); _providerFactory = collection.StoreProviderFactory; Debug.Assert(_providerFactory == providerFactory); _initialized = true; } _metadataWorkspace = workspace; _storeConnection = connection; _entityConnectionShouldDisposeStoreConnection = entityConnectionOwnsStoreConnection; _dispatcher = dispatcher ?? Interception.Dispatch.EntityConnection; if (_storeConnection != null) { _entityClientConnectionState = _storeConnection.State; } SubscribeToStoreConnectionStateChangeEvents(); }