Ejemplo n.º 1
0
        private void InitializeStaticShims()
        {
            ShimEPMData.ConstructorGuid = (instance, siteId) => { };
            ShimCoreFunctions.getConfigSettingSPWebString = (web, name) => string.Empty;

            ShimSqlConnection.Constructor = instance =>
            {
                ConnectionsCreated.Add(instance);
                _sqlConnectionStringsByConnection.Add(instance, string.Empty);
            };
            ShimSqlConnection.ConstructorString = (instance, connectionString) =>
            {
                ConnectionsCreated.Add(instance);
                _sqlConnectionStringsByConnection.Add(instance, connectionString);
            };
            ShimSqlConnection.ConstructorStringSqlCredential = (instance, connectionString, sqlCredential) =>
            {
                ConnectionsCreated.Add(instance);
                _sqlConnectionStringsByConnection.Add(instance, connectionString);
            };
            ShimSqlConnection.AllInstances.ConnectionStringGet = instance =>
            {
                return(_sqlConnectionStringsByConnection.ContainsKey(instance)
                    ? _sqlConnectionStringsByConnection[instance]
                    : null);
            };
            ShimSqlConnection.AllInstances.Open = instance =>
            {
                ConnectionsOpened.Add(instance);
            };
            ShimSqlConnection.AllInstances.Close = instance =>
            {
                ConnectionsClosed.Add(instance);
            };
            ShimSqlConnection.AllInstances.StateGet = instance => System.Data.ConnectionState.Open;

            ShimSqlConnection.AllInstances.BeginTransaction = instance => new ShimSqlTransaction();

            ShimSqlCommand.ConstructorStringSqlConnection = (instance, commandText, connection) =>
            {
                instance.CommandText = commandText;
                instance.Connection  = connection;
                CommandsCreated.Add(instance);
            };
            ShimSqlCommand.AllInstances.ExecuteReader = instance =>
            {
                var result = new ShimSqlDataReader();

                DataReadersCreated.Add(instance, result.Instance);
                CommandsExecuted.Add(instance);

                ExecuteReaderCalled?.Invoke(instance, result.Instance);

                return(result);
            };
            ShimSqlCommand.AllInstances.ExecuteNonQuery = instance =>
            {
                CommandsExecuted.Add(instance);
                return(1);
            };
            ShimSqlCommand.AllInstances.ExecuteScalar = instance =>
            {
                CommandsExecuted.Add(instance);
                return(1);
            };
            ShimSqlCommand.ConstructorStringSqlConnection = (instance, commandText, connection) =>
            {
                instance.CommandText = commandText;
                instance.Connection  = connection;
                CommandsCreated.Add(instance);
            };
            ShimSqlCommand.ConstructorString = (instance, commandText) =>
            {
                instance.CommandText = commandText;
                CommandsCreated.Add(instance);
            };

            ShimDbDataReader.AllInstances.Dispose = instance =>
            {
                if (instance is SqlDataReader)
                {
                    DataReadersDisposed.Add(DataReadersCreated.Single(pred => pred.Value == instance as SqlDataReader));
                }
            };

            ShimSqlDataAdapter.ConstructorSqlCommand = (instance, command) =>
            {
                instance.SelectCommand = command;
                DataAdaptersCreated.Add(command, instance);
            };
            ShimDbDataAdapter.AllInstances.FillDataSet = (instance, dataSet) =>
            {
                dataSet.Tables.Add();
                return(1);
            };

            ShimComponent.AllInstances.Dispose = instance =>
            {
                var sqlConnection = instance as SqlConnection;
                if (sqlConnection != null)
                {
                    ConnectionsDisposed.Add(sqlConnection);
                }

                var sqlCommand = instance as SqlCommand;
                if (sqlCommand != null)
                {
                    CommandsDisposed.Add(sqlCommand);
                }

                var sqlDataAdatpter = instance as SqlDataAdapter;
                if (sqlDataAdatpter != null)
                {
                    DataAdaptersDisposed.Add(DataAdaptersCreated.Single(pred => pred.Value == sqlDataAdatpter));
                }
            };
        }
Ejemplo n.º 2
0
 public bool IsConnectionDisposed(string connectionString)
 {
     return(ConnectionsDisposed.Any(pred => pred.ConnectionString == connectionString));
 }