private void SetupVariables()
 {
     nonPublicInstance = BindingFlags.Instance | BindingFlags.NonPublic;
     sender            = new object();
     eventArgs         = EventArgs.Empty;
     now        = DateTime.Now;
     guid       = Guid.Parse(GuidString);
     dataReader = new ShimSqlDataReader()
     {
         Read             = () => true,
         Close            = () => { },
         IsDBNullInt32    = _ => false,
         GetInt32Int32    = _ => 1,
         GetStringInt32   = _ => DummyString,
         GetGuidInt32     = _ => guid,
         GetDateTimeInt32 = _ => now
     };
     spSite = new ShimSPSite()
     {
         WebApplicationGet = () => new ShimSPWebApplication(),
         IDGet             = () => guid
     };
     spWeb = new ShimSPWeb()
     {
         SiteGet = () => spSite,
         ServerRelativeUrlGet = () => DummyString,
         IDGet = () => guid,
         AllowUnsafeUpdatesSetBoolean = _ => { }
     };
 }
Exemplo n.º 2
0
        public void RegisterCommentCreationActivity_WhenThreadNotNull_RegistersActivity()
        {
            // Arrange
            const bool registerExpected = true;

            var registerActual = false;
            var data           = CreateDataObject();

            data.Add(CommentIdString, 1);
            data.Add(CommentString, CommentString);
            data.Add(CommentersColumn, Commenters);
            data.Add(ThreadValue, new Thread()
            {
                Id = guid
            });

            var threadTable = CreateThreadTable();

            ShimSqlCommand.AllInstances.ExecuteNonQuery = sqlCommand =>
            {
                if (sqlCommand.CommandText.StartsWith("INSERT INTO SS_Activities"))
                {
                    registerActual = true;
                }
                return(1);
            };
            ShimSqlCommand.AllInstances.ExecuteReader = sqlCommand =>
            {
                var reader = new ShimSqlDataReader()
                {
                    Close = () => { }
                };
                return(reader);
            };
            ShimDataRowCollection.AllInstances.CountGet     = _ => 1;
            ShimDataRowCollection.AllInstances.ItemGetInt32 = (_, _1) =>
            {
                DataRow result = null;
                ShimsContext.ExecuteWithoutShims(() =>
                {
                    result = threadTable.Rows[0];
                });
                return(result);
            };

            var args = new ProcessActivityEventArgs(ObjectKind.List, ActivityKind.CommentAdded, data, spWeb, streamManager, threadManager, activityManager);

            // Act
            privateObj.Invoke(
                RegisterCommentCreationActivityMethod,
                BindingFlags.Instance | BindingFlags.NonPublic,
                new object[] { args });

            // Assert
            registerActual.ShouldBe(registerExpected);
        }
Exemplo n.º 3
0
 private void ShimQuerySelectCustomFields01Reader(SqlDataReader dataReader, string fieldName, bool editable)
 {
     var readCount      = 0;
     var shimDataReader = new ShimSqlDataReader(dataReader)
     {
         Read            = () => ++ readCount == 1,
         GetStringInt32  = (index) => index == 0 ? fieldName : null,
         GetBooleanInt32 = index => index == 1 && editable
     };
 }
Exemplo n.º 4
0
        private void ArrangeSqlConnection(bool configureRead = true)
        {
            ShimSqlConnection.ConstructorString = (instance, connectionString) => new ShimSqlConnection(instance)
            {
                Open           = () => { },
                DisposeBoolean = boolValue => { }
            };
            var listInt = new List <int>
            {
                10,
                11,
                0
            };
            var listDateTime = new List <DateTime>
            {
                new DateTime(2018, 6, 11),
                new DateTime(2018, 6, 12)
            };
            IEnumerator enumerator = listInt.GetEnumerator();

            ShimSqlCommand.AllInstances.ExecuteReader = instance =>
            {
                var shim = new ShimSqlDataReader()
                {
                    Read = () =>
                    {
                        if (!configureRead)
                        {
                            configureRead = true;
                            return(false);
                        }
                        return(enumerator.MoveNext());
                    },
                    GetInt32Int32    = intIndex => listInt[intIndex],
                    GetDateTimeInt32 = intIndex => listDateTime[intIndex],
                    GetBooleanInt32  = intIndex => true
                };
                new ShimDbDataReader(shim)
                {
                    Dispose = () =>
                    {
                        enumerator = listDateTime.GetEnumerator();
                    }
                };
                return(shim);
            };
            ShimDbDataAdapter.AllInstances.FillDataSet = (instance, dataSet) =>
            {
                dataSet.Tables.Add(GetDataTable("DataTable1"));
                dataSet.Tables.Add(GetDataTable("DataTable2"));
                return(0);
            };
        }
Exemplo n.º 5
0
 private void SetupVariables()
 {
     validations       = 0;
     publicInstance    = BindingFlags.Instance | BindingFlags.Public;
     nonPublicInstance = BindingFlags.Instance | BindingFlags.NonPublic;
     guid  = Guid.Parse(SampleGuidString1);
     spWeb = new ShimSPWeb()
     {
         IDGet    = () => guid,
         ListsGet = () => spListCollection,
         AllowUnsafeUpdatesSetBoolean = _ => { },
         AllUsersGet        = () => new ShimSPUserCollection(),
         SiteGroupsGet      = () => new ShimSPGroupCollection(),
         RoleDefinitionsGet = () => new ShimSPRoleDefinitionCollection()
     };
     spSite = new ShimSPSite()
     {
         IDGet = () => guid
     };
     spListCollection = new ShimSPListCollection()
     {
         ItemGetGuid = _ => spList
     };
     spList = new ShimSPList()
     {
         IDGet            = () => guid,
         FieldsGet        = () => spFieldCollection,
         GetItemByIdInt32 = _ => spListItem
     };
     spListItemCollection = new ShimSPListItemCollection();
     spListItem           = new ShimSPListItem()
     {
         IDGet         = () => DummyInt,
         TitleGet      = () => DummyString,
         ItemGetString = _ => DummyString
     };
     spFieldCollection = new ShimSPFieldCollection();
     spField           = new ShimSPField();
     spUser            = new ShimSPUser();
     dataReader        = new ShimSqlDataReader()
     {
         Close = () => { }
     };
 }
Exemplo n.º 6
0
        private void ShimQuerySelectCustomFields02Reader(SqlDataReader dataReader, string fieldTitle, string fieldTypeName)
        {
            var readCount      = 0;
            var shimDataReader = new ShimSqlDataReader(dataReader)
            {
                Read           = () => ++ readCount == 1,
                GetStringInt32 = (index) =>
                {
                    switch (index)
                    {
                    case 2:
                        return(fieldTitle);

                    case 7:
                        return(fieldTypeName);

                    default:
                        return(null);
                    }
                }
            };
        }
        private void SetupShim()
        {
            const int fieldsCount               = 1;
            var       shimSqlDataReader         = new ShimSqlDataReader();
            var       parameterList             = new List <SqlParameter>();
            var       sqlCommandCtorCommandText = string.Empty;

            readMethodSwitcher = true;
            getListsAndFields  = false;
            settingSaved       = false;
            _spLists           = new[] {
                new ShimSPList {
                    IDGet = () => Guid.NewGuid()
                }
            };
            _spListCollectionShim = new ShimSPListCollection()
            {
                TryGetListString = _ => new ShimSPList {
                },
                ItemGetString    = name => new ShimSPList
                {
                    FieldsGet = () =>
                    {
                        var list = new List <SPField>
                        {
                            new ShimSPField()
                            {
                                HiddenGet       = () => false,
                                InternalNameGet = () => DummyString
                            }
                        };
                        return(new ShimSPFieldCollection().Bind(list));
                    }
                }
            };
            _spListCollectionShim.Bind(_spLists.Select(shim => shim.Instance));
            SPWeb spWeb = new ShimSPWeb
            {
                IDGet = () => DummyGuid,
                ServerRelativeUrlGet = () => DummyUrl,
                ListsGet             = () => _spListCollectionShim,
                SiteGet = () => new ShimSPSite
                {
                    IDGet = () => DummyGuid
                }
            };

            _privateObj.SetFieldOrProperty(Web, spWeb);
            ShimSPContext.CurrentGet = () => new ShimSPContext
            {
                WebGet = () => spWeb
            };
            ShimCoreFunctions.getLockedWebSPWeb = a =>
            {
                adminPanelVisible = true;
                return(DummyGuid);
            };
            ShimCoreFunctions.setConfigSettingSPWebStringString = (_, __, ___) => settingSaved = true;
            ShimExtensionMethods.DecompressString         = _ => KeyVal;
            ShimCoreFunctions.getConfigSettingSPWebString = (_, __) =>
            {
                getListsAndFields = true;
                return(KeyVal4);
            };
            ShimMyWork.GetArchivedWebsGuid = _ => new List <Guid> {
                DummyGuid
            };
            ShimMyWork.GetSpContentDbSqlConnectionSPWeb = _ => new SqlConnection();
            ShimSqlConnection.AllInstances.Open         = _ => { };
            ShimSqlConnection.AllInstances.Close        = _ => { };

            ShimSqlCommand.ConstructorStringSqlConnection = (instance, commandText, connection) =>
            {
                sqlCommandCtorCommandText = commandText;
            };

            shimSqlDataReader.FieldCountGet  = () => fieldsCount;
            shimSqlDataReader.GetGuidInt32   = columnIndex => DummyGuid;
            shimSqlDataReader.GetStringInt32 = columnIndex => DummyString;
            shimSqlDataReader.Read           = () => readMethodSwitcher = !readMethodSwitcher;

            ShimSqlCommand.AllInstances.ExecuteReader = instance =>
            {
                executeReaderCalled = true;
                foreach (SqlParameter param in instance.Parameters)
                {
                    parameterList.Add(param);
                }
                return(shimSqlDataReader.Instance);
            };
            ShimExtensionMethods.ToPrettierNameStringStringSPWeb = (_, __, ___) => DummyString;
        }
        private void SetupShim()
        {
            const int fieldsCount               = 1;
            var       shimSqlDataReader         = new ShimSqlDataReader();
            var       parameterList             = new List <SqlParameter>();
            var       sqlCommandCtorCommandText = string.Empty;

            readMethodSwitcher = true;
            getListsAndFields  = false;
            settingSaved       = false;
            _spLists           = new[] {
                new ShimSPList {
                    IDGet = () => Guid.NewGuid()
                }
            };
            _spListCollectionShim = new ShimSPListCollection()
            {
                TryGetListString = _ => new ShimSPList {
                },
                ItemGetString    = name => new ShimSPList
                {
                    ItemsGet = () =>
                    {
                        var spListItemCollection = new List <SPListItem>
                        {
                            new ShimSPListItem
                            {
                                ItemGetGuid = guid => DummyString + ".MASTER",
                                FieldsGet   = () => new ShimSPFieldCollection
                                {
                                    GetFieldByInternalNameString = internalName => new ShimSPField
                                    {
                                        IdGet = () => DummyGuid
                                    }
                                }
                            }
                        };
                        return(new ShimSPListItemCollection().Bind(spListItemCollection));
                    },
                    FieldsGet = () =>
                    {
                        var list = new List <SPField>
                        {
                            new ShimSPField()
                            {
                                HiddenGet       = () => false,
                                InternalNameGet = () => DummyString
                            }
                        };
                        return(new ShimSPFieldCollection().Bind(list));
                    }
                }
            };
            _spListCollectionShim.Bind(_spLists.Select(shim => shim.Instance));
            SPWeb spWeb = new ShimSPWeb
            {
                IDGet = () => DummyGuid,
                ServerRelativeUrlGet = () => DummyUrl,
                ListsGet             = () => _spListCollectionShim,
                SiteGet = () => new ShimSPSite
                {
                    IDGet             = () => DummyGuid,
                    WebApplicationGet = () => new ShimSPWebApplication
                    {
                    }.Instance
                },
                CurrentUserGet = () => new ShimSPUser
                {
                    IsSiteAdminGet = () => false
                },
                PropertiesGet = () => new ShimSPPropertyBag {
                }.Instance
            };

            ShimSPWeb.AllInstances.PropertiesGet = (a) => new ShimSPPropertyBag {
                Update = () => new object { }
            };
            ShimStringDictionary.AllInstances.ContainsKeyString = (a, b) => true;
            ShimSPContext.CurrentGet = () => new ShimSPContext
            {
                WebGet = () => spWeb
            };
            ShimCoreFunctions.setConfigSettingSPWebStringString = (_, __, ___) => settingSaved = true;
            ShimCoreFunctions.getConfigSettingSPWebString       = (_, __) =>
            {
                getListsAndFields = true;
                return("true");
            };
            ShimMyWork.GetArchivedWebsGuid = _ => new List <Guid> {
                DummyGuid
            };

            ShimCoreFunctions.getConnectionStringGuid = guid => DummyString;
            ShimSqlConnection.ConstructorString       = (_, connectionString) => { };

            ShimMyWork.GetSpContentDbSqlConnectionSPWeb = _ => new SqlConnection();
            ShimSqlConnection.AllInstances.Open         = _ => { };
            ShimSqlConnection.AllInstances.Close        = _ => { };

            ShimSqlCommand.ConstructorStringSqlConnection = (instance, commandText, connection) =>
            {
                sqlCommandCtorCommandText = commandText;
            };

            shimSqlDataReader.FieldCountGet  = () => fieldsCount;
            shimSqlDataReader.GetGuidInt32   = columnIndex => DummyGuid;
            shimSqlDataReader.GetStringInt32 = columnIndex => DummyString;
            shimSqlDataReader.Read           = () => readMethodSwitcher = !readMethodSwitcher;

            ShimSqlCommand.AllInstances.ExecuteReader = instance =>
            {
                executeReaderCalled = true;
                foreach (SqlParameter param in instance.Parameters)
                {
                    parameterList.Add(param);
                }
                return(shimSqlDataReader.Instance);
            };
            ShimExtensionMethods.ToPrettierNameStringStringSPWeb = (_, __, ___) => DummyString;
            ShimPage.AllInstances.IsPostBackGet = _ => false;
        }
Exemplo n.º 9
0
        public void PerformPostRegistrationSteps_WhenCalled_Updates()
        {
            // Arrange
            const bool updateExpected = true;

            var updateActual   = false;
            var dataTableCount = 1;
            var data           = CreateDataObject();

            data.Add(CommentersColumn, Commenters);
            data.Add(IsMassOperation, true);
            data.Add(ActivityValue, new Activity()
            {
                Id = guid
            });
            data.Add(AssociatedListItems, $"{guid.ToString()}|1,{guid.ToString()}|2");
            data.Add(ThreadValue, new Thread()
            {
                Id = guid
            });

            var userTable = new DataTable();

            userTable.Columns.Add(UserIdColumn, typeof(int));
            userTable.Columns.Add(RoleColumn, typeof(UserRole));
            var row = userTable.NewRow();

            row[UserIdColumn] = 1;
            row[RoleColumn]   = UserRole.Commenter;
            userTable.Rows.Add(row);

            ShimSqlCommand.AllInstances.ExecuteScalar = sqlCommand =>
            {
                if (sqlCommand.CommandText.StartsWith("SELECT Id FROM SS_Streams"))
                {
                    return(guid);
                }
                else if (sqlCommand.CommandText.StartsWith("SELECT TOP (1) Role FROM SS_ThreadUsers"))
                {
                    return(UserRole.Commenter);
                }
                return(UserRole.Commenter);
            };
            ShimSqlCommand.AllInstances.ExecuteReader = sqlCommand =>
            {
                var reader = new ShimSqlDataReader()
                {
                    Close = () => { }
                };
                return(reader);
            };

            ShimDataTableExtensions.AsEnumerableDataTable = _ =>
            {
                EnumerableRowCollection <DataRow> result = null;
                ShimsContext.ExecuteWithoutShims(() =>
                {
                    if (dataTableCount == 1)
                    {
                        dataTableCount++;
                        result = CreateActivityTable().AsEnumerable();
                    }
                    else
                    {
                        dataTableCount++;
                        result = userTable.AsEnumerable();
                    }
                });
                return(result);
            };
            ShimDataRowCollection.AllInstances.CountGet = _ => 1;

            var args = new ProcessActivityEventArgs(ObjectKind.List, ActivityKind.CommentAdded, data, spWeb, streamManager, threadManager, activityManager);

            ShimSqlCommand.AllInstances.ExecuteNonQuery = sqlCommand =>
            {
                if (sqlCommand.CommandText.Contains("INSERT INTO SS_AssociatedThreads") && sqlCommand.CommandText.Contains("UPDATE SS_AssociatedThreads"))
                {
                    updateActual = true;
                }
                return(1);
            };

            // Act
            privateObj.Invoke(
                PerformPostRegistrationStepsMethod,
                BindingFlags.Instance | BindingFlags.NonPublic,
                new object[] { args });

            // Assert
            updateActual.ShouldBe(updateExpected);
        }
Exemplo n.º 10
0
        private void ShimIQueueItemMessage()
        {
            ShimSPSite.ConstructorGuid = (site, _) =>
            {
                var shimSite = new ShimSPSite(site)
                {
                    OpenWebGuid = webGuid =>
                    {
                        if (webGuid == _oWebId)
                        {
                            return(new ShimSPWeb
                            {
                                IDGet = () => Guid.NewGuid(),
                                SiteGet = () => new ShimSPSite
                                {
                                    IDGet = () => Guid.NewGuid()
                                },
                                ListsGet = () => new ShimSPListCollection
                                {
                                    ItemGetGuid = key =>
                                    {
                                        if (key == new Guid(ListId))
                                        {
                                            return new ShimSPList
                                            {
                                                GetItemByIdInt32 = id =>
                                                {
                                                    if (id == ItemId)
                                                    {
                                                        return CreateShimSpListItem(_webAppGuid1);
                                                    }
                                                    return null;
                                                }
                                            };
                                        }
                                        else
                                        {
                                            return null;
                                        }
                                    },
                                    TryGetListString = key =>
                                    {
                                        if (key == ListName)
                                        {
                                            return new ShimSPList
                                            {
                                                GetItemByIdInt32 = id =>
                                                {
                                                    if (id == ItemId)
                                                    {
                                                        return CreateShimSpListItem(_webAppGuid2);
                                                    }
                                                    return null;
                                                }
                                            };
                                        }
                                        else
                                        {
                                            return null;
                                        }
                                    }
                                }
                            });
                        }
                        else
                        {
                            return(new ShimSPWeb
                            {
                                IDGet = () => Guid.NewGuid(),
                                SiteGet = () => new ShimSPSite
                                {
                                    IDGet = () => Guid.NewGuid()
                                },
                                ListsGet = () => new ShimSPListCollection
                                {
                                    ItemGetGuid = key =>
                                    {
                                        if (key == new Guid(ListId))
                                        {
                                            return new ShimSPList
                                            {
                                                GetItemByIdInt32 = id =>
                                                {
                                                    if (id == ItemId)
                                                    {
                                                        return CreateShimSpListItem(_webAppGuid3);
                                                    }
                                                    return null;
                                                }
                                            };
                                        }
                                        else
                                        {
                                            return null;
                                        }
                                    },
                                    TryGetListString = key =>
                                    {
                                        if (key == ListName)
                                        {
                                            return new ShimSPList
                                            {
                                                GetItemByIdInt32 = id =>
                                                {
                                                    if (id == ItemId)
                                                    {
                                                        return CreateShimSpListItem(_webAppGuid4);
                                                    }
                                                    return null;
                                                }
                                            };
                                        }
                                        else
                                        {
                                            return null;
                                        }
                                    }
                                }
                            });
                        }
                    },
                    WebApplicationGet = () =>
                    {
                        var shimSPWebApplication = new ShimSPWebApplication();
                        var shimSPWebApplicationPersistedObject = new ShimSPPersistedObject(shimSPWebApplication)
                        {
                            IdGet = () => Guid.NewGuid()
                        };
                        return(shimSPWebApplication);
                    }
                };
            };

            _createdConnection = null;
            ShimSqlConnection.ConstructorString = (connection, _) =>
            {
                _createdConnection = connection;
            };

            ShimGetCoreInformation();

            _openedConnection = null;
            ShimSqlConnection.AllInstances.Open = connection =>
            {
                _openedConnection = connection;
            };

            _closedConnection = null;
            ShimSqlConnection.AllInstances.Close = (connection) =>
            {
                _closedConnection = connection;
            };

            _disposedConnection = null;
            ShimSqlConnection.AllInstances.DisposeBoolean = (connection, disposing) =>
            {
                _disposedConnection = connection;
            };

            _createdCommands = new List <SqlCommand>();
            ShimSqlCommand.ConstructorStringSqlConnection = (command, text, conn) =>
            {
                command.Connection  = conn;
                command.CommandText = text;
                _createdCommands.Add(command);
            };
            ShimSqlCommand.Constructor = command =>
            {
                _createdCommands.Add(command);
            };

            _executeReaderCommandsCalled = new List <SqlCommand>();
            ShimSqlCommand.AllInstances.ExecuteReader = command =>
            {
                _executeReaderCommandsCalled.Add(command);

                var shimReader = new ShimSqlDataReader
                {
                    Read = () =>
                    {
                        return(true);
                    },
                    GetGuidInt32 = _ =>
                    {
                        var readerGuid = new Guid("E2954076-F6FD-4FFE-9B10-642C9D08368F");
                        return(readerGuid);
                    }
                };
                return(shimReader);
            };

            _executeNonQueryCommands = new List <SqlCommand>();
            ShimSqlCommand.AllInstances.ExecuteNonQuery = command =>
            {
                _executeNonQueryCommands.Add(command);
                return(0);
            };

            _disposedCommands = new List <SqlCommand>();
            ShimSqlCommand.AllInstances.DisposeBoolean = (command, disposing) =>
            {
                _disposedCommands.Add(command);
            };

            _dataSetCommands = new List <SqlCommand>();
            ShimDbDataAdapter.AllInstances.FillDataSet = (adapter, dataSet) =>
            {
                var command = (SqlCommand)adapter.SelectCommand;
                _dataSetCommands.Add(command);

                var userTable = new DataTable();
                userTable.Columns.Add("userid", typeof(string));
                var userRow = userTable.NewRow();
                userRow["userid"] = "177";
                userTable.Rows.Add(userRow);
                dataSet.Tables.Add(userTable);
                return(0);
            };
        }
Exemplo n.º 11
0
 private void SetupVariables()
 {
     validations       = 0;
     publicStatic      = BindingFlags.Static | BindingFlags.Public;
     nonPublicStatic   = BindingFlags.Static | BindingFlags.NonPublic;
     publicInstance    = BindingFlags.Instance | BindingFlags.Public;
     nonPublicInstance = BindingFlags.Instance | BindingFlags.NonPublic;
     guid    = Guid.Parse(SampleGuidString1);
     request = new Dictionary <string, string>()
     {
         ["period"]     = DummyString,
         ["action"]     = DummyString,
         ["duser"]      = DummyString,
         ["ts_uids"]    = DummyString,
         ["tsitemuids"] = DummyString
     };
     currentDate = DateTime.Now;
     spWeb       = new ShimSPWeb()
     {
         IDGet                = () => guid,
         SiteGet              = () => spSite,
         ListsGet             = () => spListCollection,
         GetFolderString      = _ => spFolder,
         GetFileString        = _ => spFile,
         FoldersGet           = () => spFolderCollection,
         CurrentUserGet       = () => spUser,
         ServerRelativeUrlGet = () => SampleUrl,
         AllUsersGet          = () => new ShimSPUserCollection(),
         SiteUsersGet         = () => new ShimSPUserCollection(),
         TitleGet             = () => DummyString
     };
     spSite = new ShimSPSite()
     {
         IDGet             = () => guid,
         WebApplicationGet = () => new ShimSPWebApplication(),
         RootWebGet        = () => spWeb,
         FeaturesGet       = () => new ShimSPFeatureCollection()
         {
             ItemGetGuid = _ => new ShimSPFeature()
         },
         ContentDatabaseGet = () => new ShimSPContentDatabase()
     };
     spListCollection = new ShimSPListCollection()
     {
         TryGetListString = _ => spList,
         ItemGetString    = _ => spList,
         ItemGetGuid      = _ => spList
     };
     spList = new ShimSPList()
     {
         IDGet             = () => guid,
         FieldsGet         = () => spFieldCollection,
         GetItemByIdInt32  = _ => spListItem,
         ItemsGet          = () => spListItemCollection,
         GetItemsSPQuery   = _ => spListItemCollection,
         RootFolderGet     = () => spFolder,
         ParentWebGet      = () => spWeb,
         DefaultViewGet    = () => spView,
         ViewsGet          = () => spViewCollection,
         ContentTypesGet   = () => spContentTypeCollection,
         TitleGet          = () => DummyString,
         EventReceiversGet = () => new ShimSPEventReceiverDefinitionCollection(),
         DefaultViewUrlGet = () => SampleUrl
     };
     spListItemCollection = new ShimSPListItemCollection()
     {
         CountGet     = () => DummyInt,
         ItemGetInt32 = _ => spListItem
     };
     spListItem = new ShimSPListItem()
     {
         IDGet             = () => DummyInt,
         TitleGet          = () => DummyString,
         ItemGetString     = _ => DummyString,
         ItemGetGuid       = _ => DummyString,
         ItemSetGuidObject = (_, __) => { },
         Update            = () => { },
         FileGet           = () => spFile,
         ParentListGet     = () => spList,
         NameGet           = () => DummyString
     };
     spFieldCollection = new ShimSPFieldCollection()
     {
         GetFieldByInternalNameString = _ => spField,
         ContainsFieldString          = _ => false,
         GetFieldString = _ => spField,
         ItemGetString  = _ => spField
     };
     spField = new ShimSPField()
     {
         IdGet            = () => guid,
         TitleGet         = () => DummyString,
         InternalNameGet  = () => DummyString,
         ReadOnlyFieldGet = () => false,
         HiddenGet        = () => false,
         ReorderableGet   = () => true,
         TypeAsStringGet  = () => DummyString
     };
     spUser = new ShimSPUser()
     {
         IDGet          = () => DummyInt,
         IsSiteAdminGet = () => true,
         UserTokenGet   = () => new ShimSPUserToken(),
         EmailGet       = () => DummyString
     };
     spFolderCollection = new ShimSPFolderCollection()
     {
         ItemGetString = _ => spFolder,
         AddString     = _ => spFolder
     };
     spFolder = new ShimSPFolder()
     {
         ExistsGet     = () => false,
         SubFoldersGet = () => spFolderCollection,
         FilesGet      = () => spFileCollection,
         UrlGet        = () => SampleUrl,
         UniqueIdGet   = () => guid,
         ParentWebGet  = () => spWeb
     };
     spFileCollection = new ShimSPFileCollection()
     {
         CountGet = () => DummyInt,
         AddStringByteArrayBoolean = (_1, _2, _3) => spFile,
         AddStringStream           = (_1, _2) => spFile,
         ItemGetString             = _ => spFile
     };
     spFile = new ShimSPFile()
     {
         Delete                 = () => { },
         OpenBinaryStream       = () => null,
         NameGet                = () => DummyString,
         GetListItemStringArray = _ => spListItem
     };
     spViewCollection = new ShimSPViewCollection()
     {
         ItemGetString = _ => spView
     };
     spView = new ShimSPView()
     {
         ViewFieldsGet        = () => spViewFieldCollection,
         ServerRelativeUrlGet = () => SampleUrl
     };
     spViewFieldCollection   = new ShimSPViewFieldCollection();
     spContentTypeCollection = new ShimSPContentTypeCollection()
     {
         ItemGetString = _ => spContentType
     };
     spContentType = new ShimSPContentType()
     {
         IdGet         = () => default(SPContentTypeId),
         FieldLinksGet = () => spFieldLinkCollection
     };
     spFieldLinkCollection = new ShimSPFieldLinkCollection()
     {
         ItemGetGuid = _ => new ShimSPFieldLink()
     };
     transaction = new ShimSqlTransaction()
     {
         Commit   = () => { },
         Rollback = () => { }
     };
     dataReader = new ShimSqlDataReader()
     {
         Read             = () => false,
         GetInt32Int32    = _ => One,
         GetDateTimeInt32 = _ => currentDate,
         GetStringInt32   = _ => DummyString,
         GetGuidInt32     = _ => guid,
         Close            = () => { }
     };
 }
Exemplo n.º 12
0
        public void LoadData_Invoke_VerifyMemoryLeak()
        {
            // Arrange
            const int ExpectedSqlCommandCount    = 2;
            const int ExpectedSqlConnectionCount = 1;

            var sqlCommandCtorCallCount    = 0;
            var sqlConnectionCtorCallCount = 0;
            var sqlConnectionDisposedCount = 0;
            var sqlCommandDisposedCount    = 0;

            var spWeb = new ShimSPWeb
            {
                SiteGet = () => new ShimSPSite
                {
                    WebApplicationGet = () => new ShimSPWebApplication()
                }
            };

            spWeb.Instance.Site.WebApplication.Id = Guid.Empty;

            ShimSPSecurity.RunWithElevatedPrivilegesWaitCallbackObject          = (_, __) => { };
            ShimSPSecurity.RunWithElevatedPrivilegesSPSecurityCodeToRunElevated = _ => { };

            var shimSqlDataReader = new ShimSqlDataReader
            {
                FieldCountGet = () => DummyValue,
                GetGuidInt32  = columnIndex => Guid.Empty
            };

            var readMethodSwitcher = false;

            shimSqlDataReader.Read = () => readMethodSwitcher = !readMethodSwitcher;
            ShimSqlCommand.ConstructorStringSqlConnection = (_, __, ___) => sqlCommandCtorCallCount++;
            ShimSqlConnection.ConstructorString           = (_, __) => sqlConnectionCtorCallCount++;
            ShimSqlConnection.AllInstances.Close          = _ => { };
            ShimComponent.AllInstances.Dispose            = component =>
            {
                if (component is SqlConnection)
                {
                    sqlConnectionDisposedCount++;
                }

                if (component is SqlCommand)
                {
                    sqlCommandDisposedCount++;
                }
            };

            ShimSqlCommand.AllInstances.ExecuteReader = _ => shimSqlDataReader.Instance;

            // Act
            _privateObj.Invoke(LoadData, spWeb.Instance);
            _testObj?.Dispose();

            // Assert
            this.ShouldSatisfyAllConditions(
                () => sqlCommandCtorCallCount.ShouldBe(ExpectedSqlCommandCount),
                () => sqlCommandDisposedCount.ShouldBe(ExpectedSqlCommandCount),
                () => sqlConnectionCtorCallCount.ShouldBe(ExpectedSqlConnectionCount),
                () => sqlConnectionDisposedCount.ShouldBe(ExpectedSqlConnectionCount));
        }
Exemplo n.º 13
0
        public void saveSettings_DataReaderNoRead_VerifyMemoryLeak()
        {
            // Arrange
            const int ExpectedSqlCommandCount           = 2;
            const int ExpectedSqlConnectionCount        = 1;
            const int ExpectedSqlConnectionDisposeCount = 2;

            var sqlCommandCtorCallCount    = 0;
            var sqlConnectionCtorCallCount = 0;
            var expectedIntListId          = Guid.Empty;
            var sqlConnectionDisposedCount = 0;
            var sqlCommandDisposedCount    = 0;

            var spWeb = new ShimSPWeb
            {
                SiteGet = () => new ShimSPSite
                {
                    WebApplicationGet = () => new ShimSPWebApplication()
                }
            };

            spWeb.Instance.Site.WebApplication.Id = Guid.Empty;

            var spSite = new ShimSPSite
            {
                WebApplicationGet = () => new ShimSPWebApplication()
            };

            spSite.Instance.WebApplication.Id = Guid.Empty;

            ShimSPContext.CurrentGet = () => new ShimSPContext
            {
                SiteGet = () => spSite.Instance,
                WebGet  = () => spWeb.Instance
            };

            ShimSPSecurity.RunWithElevatedPrivilegesSPSecurityCodeToRunElevated = _ => { };
            ShimSPUtility.RedirectStringSPRedirectFlagsHttpContext = (_, __, ___) => true;

            var shimSqlDataReader = new ShimSqlDataReader
            {
                FieldCountGet = () => DummyValue,
                GetGuidInt32  = columnIndex => expectedIntListId,
                Read          = () => false
            };

            ShimSqlCommand.ConstructorStringSqlConnection = (instance, commandText, connection) => sqlCommandCtorCallCount++;
            ShimSqlConnection.ConstructorString           = (connection, s) => sqlConnectionCtorCallCount++;
            ShimSqlConnection.AllInstances.Close          = connection => { };
            ShimComponent.AllInstances.Dispose            = component =>
            {
                if (component is SqlConnection)
                {
                    sqlConnectionDisposedCount++;
                }

                if (component is SqlCommand)
                {
                    sqlCommandDisposedCount++;
                }
            };

            ShimSqlCommand.AllInstances.ExecuteReader           = instance => shimSqlDataReader.Instance;
            ShimSqlCommand.AllInstances.ExecuteNonQuery         = instance => DummyValue;
            ShimCoreFunctions.setConfigSettingSPWebStringString = (_, __, ___) => { };

            // Act
            _privateObj.Invoke(SaveSettings, spWeb.Instance);
            _testObj?.Dispose();

            // Assert
            this.ShouldSatisfyAllConditions(
                () => sqlCommandCtorCallCount.ShouldBe(ExpectedSqlCommandCount),
                () => sqlCommandDisposedCount.ShouldBe(ExpectedSqlCommandCount),
                () => sqlConnectionCtorCallCount.ShouldBe(ExpectedSqlConnectionCount),
                () => sqlConnectionDisposedCount.ShouldBe(ExpectedSqlConnectionDisposeCount));
        }
Exemplo n.º 14
0
        public void BtnRunNow_Click_Invoke_VerifyMemoryLeak()
        {
            // Arrange
            const int ExpectedSqlCommandCount    = 2;
            const int ExpectedSqlConnectionCount = 1;

            var sqlCommandCtorCallCount    = 0;
            var sqlConnectionCtorCallCount = 0;
            var sqlConnectionDisposedCount = 0;
            var sqlCommandDisposedCount    = 0;

            var spSite = new ShimSPSite
            {
                WebApplicationGet = () => new ShimSPWebApplication()
            };

            spSite.Instance.WebApplication.Id = Guid.Empty;

            ShimSPContext.CurrentGet = () => new ShimSPContext
            {
                SiteGet = () => spSite.Instance,
                WebGet  = () => new ShimSPWeb().Instance
            };

            Shimtimersettings.AllInstances.saveSettingsSPWeb = (_, __) => { };
            ShimSPSecurity.RunWithElevatedPrivilegesSPSecurityCodeToRunElevated = _ => { };
            ShimSPUtility.RedirectStringSPRedirectFlagsHttpContext = (_, __, ___) => true;

            var shimSqlDataReader = new ShimSqlDataReader
            {
                FieldCountGet = () => DummyValue,
                GetGuidInt32  = columnIndex => Guid.Empty
            };

            var readMethodSwitcher = false;

            shimSqlDataReader.Read = () => readMethodSwitcher = !readMethodSwitcher;
            ShimSqlCommand.ConstructorStringSqlConnection = (instance, commandText, connection) => sqlCommandCtorCallCount++;
            ShimSqlConnection.ConstructorString           = (connection, s) => sqlConnectionCtorCallCount++;
            ShimSqlConnection.AllInstances.Close          = connection => { };
            ShimComponent.AllInstances.Dispose            = component =>
            {
                if (component is SqlConnection)
                {
                    sqlConnectionDisposedCount++;
                }

                if (component is SqlCommand)
                {
                    sqlCommandDisposedCount++;
                }
            };

            ShimSqlCommand.AllInstances.ExecuteReader = instance => shimSqlDataReader.Instance;

            // Act
            _privateObj.Invoke(BtnRunNowClick, null, EventArgs.Empty);

            // Assert
            this.ShouldSatisfyAllConditions(
                () => sqlCommandCtorCallCount.ShouldBe(ExpectedSqlCommandCount),
                () => sqlCommandDisposedCount.ShouldBe(ExpectedSqlCommandCount),
                () => sqlConnectionCtorCallCount.ShouldBe(ExpectedSqlConnectionCount),
                () => sqlConnectionDisposedCount.ShouldBe(ExpectedSqlConnectionCount));
        }
        private void SetupVariables()
        {
            guid = Guid.NewGuid();
            now  = DateTime.Now;

            userList = new List <SPFieldUserValue>()
            {
                new ShimSPFieldUserValue()
                {
                    UserGet = () => new ShimSPUser()
                    {
                        LoginNameGet = () => GetStringOutput,
                        IDGet        = () => 1
                    }
                }.Instance
            };

            listItem = new ShimSPListItem()
            {
                IDGet         = () => 1,
                ItemGetGuid   = _ => new object(),
                SystemUpdate  = () => { },
                ItemGetString = key =>
                {
                    var result = key;
                    switch (key)
                    {
                    case "taskorder":
                    case "taskuid":
                        result = "1";
                        break;
                    }
                    return(result);
                },
                TitleGet = () => Title
            };

            list = new ShimSPList()
            {
                FieldsGet = () => new ShimSPFieldCollection()
                {
                    GetFieldByInternalNameString = _ => new ShimSPField()
                    {
                        IdGet = () => guid
                    },
                    ItemGetGuid = _ => new ShimSPField()
                    {
                        GetFieldValueString = _1 => GetStringOutput
                    }
                },
                GetItemsSPQuery = _ => new ShimSPListItemCollection()
                {
                    CountGet     = () => 1,
                    ItemGetInt32 = _1 => listItem
                },
                GetItemByIdInt32 = _ => listItem,
                IDGet            = () => guid,
                ParentWebGet     = () => new ShimSPWeb()
                {
                    GetSiteDataSPSiteDataQuery = _ => dTable
                },
                Update = () => { }
            };

            spField = new ShimSPField()
            {
                IdGet           = () => guid,
                InternalNameGet = () => InternalNameString,
                SchemaXmlGet    = () => PublishStatusShowResultsFalse
            };

            defs = new Dictionary <string, PlannerDefinition>()
            {
                [GetStringOutput] = new PlannerDefinition()
            };

            spSite = new ShimSPSite()
            {
                IDGet             = () => guid,
                WebApplicationGet = () => new ShimSPWebApplication()
            };

            spWeb = new ShimSPWeb()
            {
                CurrentUserGet = () => new ShimSPUser()
                {
                    IDGet = () => 1
                },
                IDGet    = () => guid,
                ListsGet = () => new ShimSPListCollection()
                {
                    ItemGetString    = key => list,
                    TryGetListString = _ => list
                },
                GetFileString = url => new ShimSPFile()
                {
                    ExistsGet       = () => true,
                    ParentFolderGet = () => new ShimSPFolder()
                    {
                        NameGet = () => Planner
                    }
                },
                SiteGet = () => spSite
            };

            dataReader = new ShimSqlDataReader()
            {
                Close            = () => { },
                GetGuidInt32     = index => guid,
                IsDBNullInt32    = index => false,
                GetInt32Int32    = index => 2,
                GetStringInt32   = index => GetStringOutput,
                GetDateTimeInt32 = index => now
            };

            parameterList = new List <string>();
            commandsList  = new List <string>();
        }
Exemplo n.º 16
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));
                }
            };
        }