1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// 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.Collections.Concurrent;
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.History;
using System.Data.Entity.Migrations.Sql;
using System.Data.Entity.SqlServer;
using System.Data.SqlClient;
using System.Linq;
using Moq;
using Xunit;
public class RootDependencyResolverTests : TestBase
{
[Fact]
public void The_root_resolver_uses_the_default_provider_services_resolver_and_caches_provider_instances()
{
var providerServices = new Mock<DbProviderServices>().Object;
var mockProviderResolver = new Mock<DefaultProviderServicesResolver>();
mockProviderResolver
.Setup(m => m.GetService(typeof(DbProviderServices), "FooClient"))
.Returns(providerServices);
var resolver = new RootDependencyResolver(
mockProviderResolver.Object,
new DatabaseInitializerResolver());
Assert.Same(providerServices, resolver.GetService<DbProviderServices>("FooClient"));
mockProviderResolver.Verify(m => m.GetService(typeof(DbProviderServices), "FooClient"), Times.Once());
Assert.Same(providerServices, resolver.GetService<DbProviderServices>("FooClient"));
mockProviderResolver.Verify(m => m.GetService(typeof(DbProviderServices), "FooClient"), Times.Once());
}
[Fact]
public void The_root_resolver_can_return_a_default_history_context_factory()
{
Assert.IsType<DefaultHistoryContextFactory>(
new RootDependencyResolver(
new DefaultProviderServicesResolver(),
new DatabaseInitializerResolver()).GetService<IHistoryContextFactory>());
}
[Fact]
public void The_root_resolver_can_return_a_default_model_cache_key_factory()
{
Assert.IsType<DefaultModelCacheKeyFactory>(
new RootDependencyResolver(
new DefaultProviderServicesResolver(),
new DatabaseInitializerResolver()).GetService<IDbModelCacheKeyFactory>());
}
[Fact]
public void The_root_resolver_returns_the_SQL_Server_connection_factory()
{
Assert.IsType<SqlConnectionFactory>(
new RootDependencyResolver().GetService<IDbConnectionFactory>());
}
[Fact]
public void The_root_resolver_returns_default_pluralization_service()
{
Assert.IsType<EnglishPluralizationService>(new RootDependencyResolver().GetService<IPluralizationService>());
}
[Fact]
public void The_root_resolver_uses_the_database_initializer_resolver()
{
var initializer = new Mock<IDatabaseInitializer<FakeContext>>().Object;
var mockInitializerResolver = new Mock<DatabaseInitializerResolver>();
mockInitializerResolver.Setup(m => m.GetService(typeof(IDatabaseInitializer<FakeContext>), null)).Returns(initializer);
Assert.Same(
initializer,
new RootDependencyResolver(
new DefaultProviderServicesResolver(), mockInitializerResolver.Object)
.GetService<IDatabaseInitializer<FakeContext>>());
}
[Fact]
public void The_database_initializer_resolver_can_be_obtained_from_the_root_resolver()
{
var initializerResolver = new DatabaseInitializerResolver();
Assert.Same(
initializerResolver,
new RootDependencyResolver(
new DefaultProviderServicesResolver(), initializerResolver)
.DatabaseInitializerResolver);
}
[Fact]
public void The_root_resolver_returns_the_default_manifest_token_service()
{
Assert.IsType<DefaultManifestTokenService>(new RootDependencyResolver().GetService<IManifestTokenService>());
}
[Fact]
public void The_root_resolver_returns_the_default_command_interceptor_service()
{
Assert.IsType<DefaultCommandInterceptor>(new RootDependencyResolver().GetService<IDbCommandInterceptor>());
}
[Fact]
public void The_root_resolver_returns_the_default_sql_generators()
{
Assert.IsType<SqlServerMigrationSqlGenerator>(
new RootDependencyResolver().GetService<MigrationSqlGenerator>("System.Data.SqlClient"));
Assert.IsType<SqlCeMigrationSqlGenerator>(
new RootDependencyResolver().GetService<MigrationSqlGenerator>("System.Data.SqlServerCe.4.0"));
}
[Fact]
public void The_root_resolver_returns_default_provider_factory_service()
{
Assert.IsType<DefaultDbProviderFactoryService>(new RootDependencyResolver().GetService<IDbProviderFactoryService>());
}
[Fact]
public void The_root_resolver_uses_the_default_invariant_name_resolver_to_return_an_invariant_name()
{
Assert.Equal(
"System.Data.SqlClient",
new RootDependencyResolver().GetService<IProviderInvariantName>(SqlClientFactory.Instance).Name);
}
[Fact]
public void The_root_resolver_uses_the_default_DbProviderFactory_resolver_to_return_a_provider_factory()
{
Assert.Same(
SqlClientFactory.Instance,
new RootDependencyResolver().GetService<DbProviderFactory>("System.Data.SqlClient"));
}
/// <summary>
/// This test makes calls from multiple threads such that we have at least some chance of finding threading
/// issues. As with any test of this type just because the test passes does not mean that the code is
/// correct. On the other hand if this test ever fails (EVEN ONCE) then we know there is a problem to
/// be investigated. DON'T just re-run and think things are okay if the test then passes.
/// </summary>
[Fact]
public void GetService_can_be_accessed_from_multiple_threads_concurrently()
{
for (var i = 0; i < 30; i++)
{
var bag = new ConcurrentBag<DbProviderServices>();
var resolver = new RootDependencyResolver();
ExecuteInParallel(() => bag.Add(resolver.GetService<DbProviderServices>("System.Data.SqlClient")));
Assert.Equal(20, bag.Count);
Assert.True(bag.All(c => SqlProviderServices.Instance == c));
}
}
public class FakeContext : DbContext
{
}
}
}
|