// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. namespace System.Data.Entity.Config { using System.Data.Common; using System.Data.Entity.Core.Common; using System.Data.Entity.Infrastructure; using System.Data.Entity.Infrastructure.Pluralization; using System.Data.Entity.Internal; using System.Data.Entity.Migrations; using System.Data.Entity.Migrations.History; using System.Data.Entity.Migrations.Sql; using System.Data.Entity.Resources; using System.Data.Entity.Spatial; using System.Data.Entity.TestHelpers; using Moq; using Xunit; public class DbConfigurationTests { public class SetConfiguration { [Fact] public void DbConfiguration_cannot_be_set_to_null() { Assert.Equal( "configuration", Assert.Throws(() => DbConfiguration.SetConfiguration(null)).ParamName); } } public class AddDependencyResolver { [Fact] public void AddDependencyResolver_throws_if_given_a_null_resolver() { Assert.Equal( "resolver", Assert.Throws(() => new DbConfiguration().AddDependencyResolver(null)).ParamName); } [Fact] public void AddDependencyResolver_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("AddDependencyResolver"), Assert.Throws( () => configuration.AddDependencyResolver(new Mock().Object)).Message); } [Fact] public void AddDependencyResolver_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var resolver = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object).AddDependencyResolver(resolver); mockInternalConfiguration.Verify(m => m.AddDependencyResolver(resolver, false)); } } public class OnLockingConfiguration { [Fact] public void OnLockingConfiguration_throws_when_attempting_to_add_or_remove_a_null_handler() { Assert.Equal( "value", Assert.Throws(() => DbConfiguration.OnLockingConfiguration += null).ParamName); Assert.Equal( "value", Assert.Throws(() => DbConfiguration.OnLockingConfiguration -= null).ParamName); } } public class AddDbProviderServices { [Fact] public void AddDbProviderServices_throws_if_given_a_null_provider_or_bad_invariant_name() { Assert.Equal( "provider", Assert.Throws(() => new DbConfiguration().AddDbProviderServices("Karl", null)).ParamName); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("providerInvariantName"), Assert.Throws( () => new DbConfiguration().AddDbProviderServices(null, new Mock().Object)).Message); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("providerInvariantName"), Assert.Throws( () => new DbConfiguration().AddDbProviderServices("", new Mock().Object)).Message); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("providerInvariantName"), Assert.Throws( () => new DbConfiguration().AddDbProviderServices(" ", new Mock().Object)).Message); } [Fact] public void AddDbProviderServices_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var providerServices = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object).AddDbProviderServices("900.FTW", providerServices); mockInternalConfiguration.Verify(m => m.RegisterSingleton(providerServices, "900.FTW")); } [Fact] public void AddDbProviderServices_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("AddDbProviderServices"), Assert.Throws( () => configuration.AddDbProviderServices("Karl", new Mock().Object)).Message); } } public class AddDbProviderFactory { [Fact] public void AddDbProviderFactory_throws_if_given_a_null_provider_or_bad_invariant_name() { Assert.Equal( "providerFactory", Assert.Throws(() => new DbConfiguration().AddDbProviderFactory("Karl", null)).ParamName); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("providerInvariantName"), Assert.Throws( () => new DbConfiguration().AddDbProviderFactory(null, new Mock().Object)).Message); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("providerInvariantName"), Assert.Throws( () => new DbConfiguration().AddDbProviderFactory("", new Mock().Object)).Message); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("providerInvariantName"), Assert.Throws( () => new DbConfiguration().AddDbProviderFactory(" ", new Mock().Object)).Message); } [Fact] public void AddDbProviderFactory_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var providerFactory = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object).AddDbProviderFactory("920.FTW", providerFactory); mockInternalConfiguration.Verify(m => m.RegisterSingleton(providerFactory, "920.FTW")); mockInternalConfiguration.Verify(m => m.AddDependencyResolver(new InvariantNameResolver(providerFactory, "920.FTW"), false)); } [Fact] public void AddDbProviderFactory_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("AddDbProviderFactory"), Assert.Throws( () => configuration.AddDbProviderFactory("Karl", new Mock().Object)).Message); } } public class SetDefaultConnectionFactory : TestBase { [Fact] public void Setting_DefaultConnectionFactory_throws_if_given_a_null_factory() { Assert.Equal( "connectionFactory", Assert.Throws(() => new DbConfiguration().SetDefaultConnectionFactory(null)).ParamName); } [Fact] public void Setting_DefaultConnectionFactory_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("SetDefaultConnectionFactory"), Assert.Throws( () => configuration.SetDefaultConnectionFactory(new Mock().Object)).Message); } [Fact] public void SetDefaultConnectionFactory_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var connectionFactory = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object).SetDefaultConnectionFactory(connectionFactory); mockInternalConfiguration.Verify(m => m.RegisterSingleton(connectionFactory, null)); } [Fact] public void DefaultConnectionFactory_set_in_code_can_be_overriden_before_config_is_locked() { Assert.IsType(DbConfiguration.GetService()); Assert.IsType(FunctionalTestsConfiguration.OriginalConnectionFactories[0]); } } public class SetPluralizationService { [Fact] public void Setting_PluralizationService_throws_if_given_a_null_service() { var configuration = CreatedLockedConfiguration(); Assert.Equal( "pluralizationService", Assert.Throws(() => new DbConfiguration().SetPluralizationService(null)).ParamName); } [Fact] public void Setting_PluralizationService_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("SetPluralizationService"), Assert.Throws( () => configuration.SetPluralizationService(new Mock().Object)).Message); } [Fact] public void SetPluralizationService_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var pluralizationService = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object).SetPluralizationService(pluralizationService); mockInternalConfiguration.Verify(m => m.RegisterSingleton(pluralizationService, null)); } } public class GetService { [Fact] public void Delegates_to_internal_configuration() { Assert.NotNull(DbConfiguration.GetService(null)); } } public class DependencyResolver { [Fact] public void Default_IDbModelCacheKeyFactory_is_returned_by_default() { Assert.IsType(DbConfiguration.GetService()); } } public class SetDatabaseInitializer { [Fact] public void SetDatabaseInitializer_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("SetDatabaseInitializer"), Assert.Throws( () => configuration.SetDatabaseInitializer(new Mock>().Object)).Message); } [Fact] public void SetDatabaseInitializer_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var initializer = new Mock>().Object; new DbConfiguration(mockInternalConfiguration.Object).SetDatabaseInitializer(initializer); mockInternalConfiguration.Verify(m => m.RegisterSingleton(initializer, null)); } [Fact] public void SetDatabaseInitializer_creates_null_initializer_when_given_null_argument() { var mockInternalConfiguration = new Mock(); new DbConfiguration(mockInternalConfiguration.Object).SetDatabaseInitializer(null); mockInternalConfiguration.Verify( m => m.RegisterSingleton>(It.IsAny>(), null)); } } public class AddMigrationSqlGenerator { [Fact] public void AddMigrationSqlGenerator_throws_if_given_a_null_generator_or_bad_invariant_name() { Assert.Equal( "sqlGenerator", Assert.Throws(() => new DbConfiguration().AddMigrationSqlGenerator("Karl", null)).ParamName); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("providerInvariantName"), Assert.Throws( () => new DbConfiguration().AddMigrationSqlGenerator(null, () => new Mock().Object)).Message); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("providerInvariantName"), Assert.Throws( () => new DbConfiguration().AddMigrationSqlGenerator("", () => new Mock().Object)).Message); Assert.Equal( Strings.ArgumentIsNullOrWhitespace("providerInvariantName"), Assert.Throws( () => new DbConfiguration().AddMigrationSqlGenerator(" ", () => new Mock().Object)).Message); } [Fact] public void AddMigrationSqlGenerator_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("AddMigrationSqlGenerator"), Assert.Throws( () => configuration.AddMigrationSqlGenerator("Karl", () => new Mock().Object)).Message); } [Fact] public void AddMigrationSqlGenerator_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var generator = new Func(() => new Mock().Object); new DbConfiguration(mockInternalConfiguration.Object).AddMigrationSqlGenerator("Karl", generator); mockInternalConfiguration.Verify( m => m.AddDependencyResolver(It.IsAny>(), false)); } } public class SetManifestTokenService { [Fact] public void SetManifestTokenService_throws_if_given_a_null_service() { Assert.Equal( "service", Assert.Throws(() => new DbConfiguration().SetManifestTokenService(null)).ParamName); } [Fact] public void SetManifestTokenService_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("SetManifestTokenService"), Assert.Throws( () => configuration.SetManifestTokenService(new Mock().Object)).Message); } [Fact] public void SetManifestTokenService_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var service = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object).SetManifestTokenService(service); mockInternalConfiguration.Verify(m => m.RegisterSingleton(service, null)); } } public class SetProviderFactoryService { [Fact] public void SetProviderFactoryService_throws_if_given_a_null_service() { Assert.Equal( "providerFactoryService", Assert.Throws(() => new DbConfiguration().SetProviderFactoryService(null)).ParamName); } [Fact] public void SetProviderFactoryService_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("SetProviderFactoryService"), Assert.Throws( () => configuration.SetProviderFactoryService(new Mock().Object)).Message); } [Fact] public void SetProviderFactoryService_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var service = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object).SetProviderFactoryService(service); mockInternalConfiguration.Verify(m => m.RegisterSingleton(service, null)); } } public class SetModelCacheKeyFactory { [Fact] public void SetModelCacheKeyFactory_throws_if_given_a_null_factory() { Assert.Equal( "keyFactory", Assert.Throws(() => new DbConfiguration().SetModelCacheKeyFactory(null)).ParamName); } [Fact] public void SetModelCacheKeyFactory_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("SetModelCacheKeyFactory"), Assert.Throws( () => configuration.SetModelCacheKeyFactory(new Mock().Object)).Message); } [Fact] public void SetModelCacheKeyFactory_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var factory = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object).SetModelCacheKeyFactory(factory); mockInternalConfiguration.Verify(m => m.RegisterSingleton(factory, null)); } } public class SetHistoryContextFactory { [Fact] public void Throws_if_given_a_null_factory() { Assert.Equal( "historyContextFactory", Assert.Throws( () => new DbConfiguration().SetHistoryContextFactory(null)).ParamName); } [Fact] public void Throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("SetHistoryContextFactory"), Assert.Throws( () => configuration.SetHistoryContextFactory( new Mock().Object)).Message); } [Fact] public void Delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var factory = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object) .SetHistoryContextFactory(factory); mockInternalConfiguration.Verify(m => m.RegisterSingleton(factory, typeof(DbMigrationsConfiguration))); } } public class SetSpatialProvider { [Fact] public void SetSpatialProvider_throws_if_given_a_null_factory() { Assert.Equal( "spatialProvider", Assert.Throws(() => new DbConfiguration().SetSpatialProvider(null)).ParamName); } [Fact] public void SetSpatialProvider_throws_if_the_configuation_is_locked() { var configuration = CreatedLockedConfiguration(); Assert.Equal( Strings.ConfigurationLocked("SetSpatialProvider"), Assert.Throws( () => configuration.SetSpatialProvider(new Mock().Object)).Message); } [Fact] public void SetSpatialProvider_delegates_to_internal_configuration() { var mockInternalConfiguration = new Mock(); var provider = new Mock().Object; new DbConfiguration(mockInternalConfiguration.Object).SetSpatialProvider(provider); mockInternalConfiguration.Verify(m => m.RegisterSingleton(provider, null)); } } private static DbConfiguration CreatedLockedConfiguration() { var configuration = new DbConfiguration(); configuration.InternalConfiguration.Lock(); return configuration; } } }