public void UnregisterFactoryTest()
        {
            ClearRegisteredFactories();
            RegisterSqlClientAndTestRegistration(() => DbProviderFactories.RegisterFactory("System.Data.SqlClient", System.Data.SqlClient.SqlClientFactory.Instance));
            Assert.True(DbProviderFactories.UnregisterFactory("System.Data.SqlClient"));
            DataTable providerTable = DbProviderFactories.GetFactoryClasses();

            Assert.Equal(0, providerTable.Rows.Count);
        }
        public static void RegisterFactory(string providerInvariantName, Type providerFactoryClass)
        {
            var providerTable = DbProviderFactories.GetFactoryClasses();

            providerTable.Rows
            .Add(new object[] { null, null, providerInvariantName, providerFactoryClass.AssemblyQualifiedName });
            typeof(DbProviderFactories)
            .GetField("_providerTable", BindingFlags.Static | BindingFlags.NonPublic)
            .SetValue(null, providerTable);
        }
        public void GetFactoryClassesDataTableShapeTest()
        {
            DataTable initializedTable = DbProviderFactories.GetFactoryClasses();

            Assert.NotNull(initializedTable);
            Assert.Equal(4, initializedTable.Columns.Count);
            Assert.Equal("Name", initializedTable.Columns[0].ColumnName);
            Assert.Equal("Description", initializedTable.Columns[1].ColumnName);
            Assert.Equal("InvariantName", initializedTable.Columns[2].ColumnName);
            Assert.Equal("AssemblyQualifiedName", initializedTable.Columns[3].ColumnName);
        }
        public void GetProviderInvariantNamesTest()
        {
            ClearRegisteredFactories();
            RegisterSqlClientAndTestRegistration(() => DbProviderFactories.RegisterFactory("System.Data.SqlClient", typeof(System.Data.SqlClient.SqlClientFactory)));
            DbProviderFactories.RegisterFactory("System.Data.Common.TestProvider", typeof(TestProviderFactory));
            DataTable providerTable = DbProviderFactories.GetFactoryClasses();

            Assert.Equal(2, providerTable.Rows.Count);
            List <string> invariantNames = DbProviderFactories.GetProviderInvariantNames().ToList();

            Assert.Equal(invariantNames.Count, 2);
            Assert.True(invariantNames.Contains("System.Data.Common.TestProvider"));
            Assert.True(invariantNames.Contains("System.Data.SqlClient"));
        }
        public void ReplaceFactoryWithRegisterFactoryWithTypeTest()
        {
            ClearRegisteredFactories();
            RegisterSqlClientAndTestRegistration(() => DbProviderFactories.RegisterFactory("System.Data.SqlClient", typeof(System.Data.SqlClient.SqlClientFactory)));
            DbProviderFactories.RegisterFactory("System.Data.SqlClient", typeof(TestProviderFactory));
            DataTable providerTable = DbProviderFactories.GetFactoryClasses();

            Assert.Equal(1, providerTable.Rows.Count);
            DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient");

            Assert.NotNull(factory);
            Assert.Equal(typeof(TestProviderFactory), factory.GetType());
            Assert.Equal(TestProviderFactory.Instance, factory);
        }
        public void RegisterFactoryWithWrongAssemblyQualifiedNameTest()
        {
            ClearRegisteredFactories();
            Assert.Throws <ArgumentException>(() => DbProviderFactories.GetFactory("System.Data.SqlClient"));
            DataTable providerTable = DbProviderFactories.GetFactoryClasses();

            Assert.Equal(0, providerTable.Rows.Count);
            // register the connection type which is the wrong type. Registraton should succeed, as type registration/checking is deferred.
            DbProviderFactories.RegisterFactory("System.Data.SqlClient", typeof(System.Data.SqlClient.SqlConnection).AssemblyQualifiedName);
            providerTable = DbProviderFactories.GetFactoryClasses();
            Assert.Equal(1, providerTable.Rows.Count);
            // obtaining the factory will kick in the checks of the registered type name, which will cause exceptions. The checks were deferred till the GetFactory() call.
            Assert.Throws <ArgumentException>(() => DbProviderFactories.GetFactory(providerTable.Rows[0]));
            Assert.Throws <ArgumentException>(() => DbProviderFactories.GetFactory("System.Data.SqlClient"));
        }
        private void RegisterSqlClientAndTestRegistration(Action registrationFunc)
        {
            Assert.NotNull(registrationFunc);
            Assert.Throws <ArgumentException>(() => DbProviderFactories.GetFactory("System.Data.SqlClient"));
            DataTable providerTable = DbProviderFactories.GetFactoryClasses();

            Assert.Equal(0, providerTable.Rows.Count);
            registrationFunc();
            providerTable = DbProviderFactories.GetFactoryClasses();
            Assert.Equal(1, providerTable.Rows.Count);
            DbProviderFactory factory = DbProviderFactories.GetFactory(providerTable.Rows[0]);

            Assert.NotNull(factory);
            Assert.Equal(typeof(System.Data.SqlClient.SqlClientFactory), factory.GetType());
            Assert.Equal(System.Data.SqlClient.SqlClientFactory.Instance, factory);
        }