public void GetCurrent_AddNewFarmMappingIsAlsoAddedToSite()
        {
            //Setup
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>()
            };
            var farm = new MSPFarm();

            MSPFarm.LocalGet = () => farm;

            var expectedSite = new MSPSite()
            {
                IDGet = () => TestsConstants.TestGuid
            };

            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = typeMappings;
            sharepointLocator.SiteTypeMappings = new List <TypeMapping>();

            var target      = sharepointLocator.GetCurrent(expectedSite); // get current to make sure it is in memory
            var farmLocator = sharepointLocator.GetCurrent() as ActivatingServiceLocator;

            //Act
            farmLocator.RegisterTypeMapping <ILogger, SBaseLogger>();
            var targetLogger = target.GetInstance <ILogger>();

            //Assert
            Assert.IsInstanceOfType(targetLogger, typeof(SBaseLogger));
        }
예제 #2
0
        public void Execute_ContainsKeyReturnsFalse_WithValidKeyNotSetAtWebAppLevel()
        {
            //arrange
            var    args = new ContainsKeyDataArgs();
            string key  = ConfigManager.PnPKeyNamespace + "." + TestsConstants.TestGuidName;

            args.Key    = key;
            args.Level  = (int)ConfigLevel.CurrentSPWebApplication;
            args.SiteId = TestsConstants.TestGuid;
            var proxyOp = new ContainsKeyOperation();

            var webApp             = new BSPWebApplication();
            WebAppSettingStore wss = new WebAppSettingStore();
            var webPO = new MSPPersistedObject((SPPersistedObject)webApp.Instance);

            MSPSite.ConstructorGuid = (instance, guid) =>
            {
                var site = new MSPSite(instance)
                {
                    WebApplicationGet = () =>
                    {
                        webPO.GetChildString <WebAppSettingStore>((s) => wss);
                        return(webApp);
                    },
                    Dispose = () => { }
                };
            };

            //Act
            var target = proxyOp.Execute(args);

            //Assert
            Assert.IsInstanceOfType(target, typeof(bool));
            Assert.IsFalse((bool)target);
        }
        public void GetCurrent_WithContext_UsesSiteMappings()
        {
            //Arrange
            BSPFarm.SetLocal();
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>()
            };
            var siteTypeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SBaseLogger>()
            };
            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = typeMappings;
            sharepointLocator.SiteTypeMappings = siteTypeMappings;

            var site = new MSPSite()
            {
                IDGet = () => TestsConstants.TestGuid,
            };

            var context = new MSPContext
            {
                SiteGet = () => site,
            };

            MSPContext.CurrentGet = () => context;

            //Act
            var target = sharepointLocator.GetCurrent().GetInstance <ILogger>();

            // Assert
            Assert.IsInstanceOfType(target, typeof(SBaseLogger));
        }
예제 #4
0
        public void RegisterTypeMapping_SaveNewMappingWithSite()
        {
            var    mappings   = new List <TypeMapping>();
            string savedkey   = null;
            object savedValue = null;
            var    configData = new ServiceLocationConfigData(mappings);

            BSPFarm.SetLocal();
            const string expectedKey = "Microsoft.Practices.SharePoint.Common.TypeMappings";
            var          web         = new MSPWeb();
            var          propBag     = new BIPropertyBag();

            var mgr = new SIConfigManager()
            {
                SetInPropertyBagStringObjectIPropertyBag = (key, value, bag) =>
                {
                    savedkey   = key;
                    savedValue = value;
                },

                ContainsKeyInPropertyBagStringIPropertyBag = (key, bag) =>
                {
                    if (key == expectedKey)
                    {
                        return(true);
                    }
                    return(false);
                },
                CanAccessFarmConfigGet    = () => true,
                GetPropertyBagConfigLevel = (level) => propBag,
                SetWebSPWeb = (w) => { },
            };

            mgr.GetFromPropertyBagStringIPropertyBag <ServiceLocationConfigData>((key, bag) =>
            {
                if (key == expectedKey)
                {
                    return(configData);
                }
                return(null);
            });

            var configSite = new MSPSite()
            {
                RootWebGet = () => web
            };

            var target = new ServiceLocatorConfig(mgr);

            target.Site = configSite;

            //act
            target.RegisterTypeMapping <ISomething, Something>();

            //assert
            Assert.IsNotNull(savedValue);
            Assert.AreEqual(expectedKey, savedkey);
        }
        public void AddAndContains()
        {
            MSPSite site  = SetupSite();
            string  key   = "key";
            string  value = "value";

            var target = new SPSitePropertyBag(site);

            IPropertyBagTest.AddContains(target, key, value);
        }
        public void Execute_TracesMessage()
        {
            //Arrange
            var  args       = new TracingOperationArgs();
            Guid createGuid = Guid.Empty;

            args.Message  = TestsConstants.TestGuidName;
            args.EventId  = -99;
            args.Category = TestsConstants.AreasCategories;
            args.Severity = (int)SandboxTraceSeverity.High;
            args.SiteID   = TestsConstants.TestGuid;

            SharePointServiceLocator.ReplaceCurrentServiceLocator(new ActivatingServiceLocator());
            ((ActivatingServiceLocator)SharePointServiceLocator.GetCurrent())
            .RegisterTypeMapping <ILogger, TestTraceLogger>(InstantiationType.AsSingleton);

            var operation = new TracingOperation();

            BSPFarm.SetLocal();
            MSPSite site;
            BSPWeb  bweb = new BSPWeb();
            var     mweb = new MSPWeb(bweb)
            {
                NameGet = () => "foo.bar",
            };


            MSPSite.ConstructorGuid = (instance, g) =>
            {
                site = new MSPSite(instance)
                {
                    Dispose    = () => { },
                    IDGet      = () => TestsConstants.TestGuid,
                    RootWebGet = () => mweb
                };

                createGuid = g;
            };


            //Act
            object target = operation.Execute(args);

            //Assert
            var logger = SharePointServiceLocator.GetCurrent().GetInstance <ILogger>() as TestTraceLogger;

            Assert.IsNotInstanceOfType(target, typeof(Exception));
            Assert.AreEqual(TestsConstants.TestGuid, createGuid);
            Assert.IsTrue(logger.Message.Contains(args.Message));
            Assert.AreEqual(logger.Category, TestsConstants.AreasCategories);
            Assert.AreEqual(logger.EventId, args.EventId);
            Assert.AreEqual(logger.Severity, SandboxTraceSeverity.High);
        }
예제 #7
0
        public void WriteSandboxLog_LogsMessage()
        {
            //Arrange
            SharePointEnvironment.Reset();
            var context = new SIApplicationContextProvider()
            {
                GetCurrentAppDomainFriendlyName = () => "SandboxDomain",
                IsProxyCheckerInstalled         = () => true,
                IsProxyInstalledStringString    = (a, t) => true,
            };

            SharePointEnvironment.ApplicationContextProvider = context;
            string testMessage = testMessageString;
            int    testEventId = 99;
            LoggingOperationArgs loggingArgs = null;
            BSPSite site = new BSPSite();
            MSPSite s    = new MSPSite(site)
            {
                IDGet = () => TestsConstants.TestGuid
            };

            BSPContext.SetCurrent();
            MSPContext c = new MSPContext(BSPContext.Current)
            {
                SiteGet = () => site
            };

            MSPUtility.ExecuteRegisteredProxyOperationStringStringSPProxyOperationArgs = (a, t, args) =>
            {
                loggingArgs =
                    args as
                    LoggingOperationArgs;
                return(null);
            };


            //Act
            var target = new SharePointLogger();

            target.LogToOperations(testMessage, testEventId, SandboxEventSeverity.Error, TestsConstants.AreasCategories);

            // Assert
            Assert.IsNotNull(loggingArgs);
            Assert.AreEqual(loggingArgs.Message, testMessage);
            Assert.AreEqual(loggingArgs.Category, TestsConstants.AreasCategories);
            Assert.AreEqual(loggingArgs.EventId, testEventId);
            Assert.AreEqual((SandboxEventSeverity)loggingArgs.Severity, SandboxEventSeverity.Error);
        }
예제 #8
0
        public void WriteSandboxTrace_NoContext_TracesMessage()
        {
            //Arrange
            SharePointEnvironment.Reset();
            string testMessage = testMessageString;
            int    testEventId = 99;
            TracingOperationArgs tracingArgs = null;
            BSPSite site = new BSPSite();
            MSPSite s    = new MSPSite(site)
            {
                IDGet = () => TestsConstants.TestGuid
            };

            MSPContext.CurrentGet = () => null;
            MSPUtility.ExecuteRegisteredProxyOperationStringStringSPProxyOperationArgs = (a, t, args) =>
            {
                tracingArgs =
                    args as
                    TracingOperationArgs;
                return(null);
            };

            var context = new SIApplicationContextProvider()
            {
                GetCurrentAppDomainFriendlyName = () => "SandboxDomain",
                IsProxyCheckerInstalled         = () => true,
                IsProxyInstalledStringString    = (a, t) => true,
            };

            SharePointEnvironment.ApplicationContextProvider = context;

            //Act
            var target = new SharePointLogger();

            target.TraceToDeveloper(testMessage, testEventId, SandboxTraceSeverity.High, TestsConstants.AreasCategories);

            // Assert
            Assert.IsNotNull(tracingArgs);
            Assert.AreEqual(tracingArgs.Message, testMessage);
            Assert.AreEqual(tracingArgs.Category, TestsConstants.AreasCategories);
            Assert.AreEqual(tracingArgs.EventId, testEventId);
            Assert.AreEqual((SandboxTraceSeverity)tracingArgs.Severity, SandboxTraceSeverity.High);
        }
        public void AddAndRemove()
        {
            // Arrange
            MSPSite site   = SetupSite();
            string  key    = "foo";
            string  value  = "fred";
            var     target = new SPSitePropertyBag(site);

            target[key] = value;

            // Act
            var containsBeforeCondition = target.Contains(key);

            target.Remove(key);
            var result = target.Contains(key);

            // Assert
            Assert.IsFalse(result);
            Assert.IsTrue(containsBeforeCondition);
        }
        private MSPSite SetupSite()
        {
            var bag = new System.Collections.Hashtable();

            MSPWeb web = new MSPWeb()
            {
                AllPropertiesGet        = () => bag,
                SetPropertyObjectObject = (key, value) => bag[key] = value,
                GetPropertyObject       = (key) => bag[key],
                DeletePropertyObject    = (key) => bag.Remove(key),
                AddPropertyObjectObject = (key, value) => bag.Add(key, value),
                Update = () => { }
            };

            MSPSite site = new MSPSite()
            {
                RootWebGet = () => web,
            };

            return(site);
        }
        public void Execute_ReturnsKeyData_WithValidKeyAtWebAppLevel()
        {
            //arrange
            SPWebAppPropertyBag.ClearCache();
            var    args = new ReadConfigArgs();
            string key  = ConfigManager.PnPKeyNamespace + "." + TestsConstants.TestGuidName;

            args.Key    = key;
            args.Level  = (int)ConfigLevel.CurrentSPWebApplication;
            args.SiteId = TestsConstants.TestGuid;
            var    proxyOp      = new ReadConfigurationOperation();
            string expectedData = "{92700BB6-B144-434F-A97B-5F696068A425}";

            MSPPersistedObject webPO;
            WebAppSettingStore wss;

            MSPSite.ConstructorGuid = (instance, guid) =>
            {
                var site = new MSPSite(instance)
                {
                    WebApplicationGet = () =>
                    {
                        var webApp = new BSPConfiguredWebApp();
                        wss = new WebAppSettingStore();
                        wss.Settings[key] = expectedData;
                        webPO             = new MSPPersistedObject((SPPersistedObject)webApp.Instance);
                        webPO.GetChildString <WebAppSettingStore>((s) => wss);
                        return(webApp);
                    },
                    Dispose = () => { }
                };
            };

            //Act
            object target = proxyOp.Execute(args);

            //Assert .
            Assert.IsInstanceOfType(target, typeof(string));
            Assert.AreEqual(expectedData, (string)target);
        }
        public void Execute_ReturnsNull_WithValidKeyNotSetAtWebAppLevel()
        {
            //arrange
            SPWebAppPropertyBag.ClearCache();
            var    args = new ReadConfigArgs();
            string key  = ConfigManager.PnPKeyNamespace + "." + TestsConstants.TestGuidName;

            args.Key    = key;
            args.Level  = (int)ConfigLevel.CurrentSPWebApplication;
            args.SiteId = TestsConstants.TestGuid;
            var proxyOp = new ReadConfigurationOperation();
            MSPPersistedObject webPO;
            WebAppSettingStore wss;

            MSPSite.ConstructorGuid = (instance, guid) =>
            {
                var site = new MSPSite(instance)
                {
                    WebApplicationGet = () =>
                    {
                        var webApp = new BSPConfiguredWebApp();
                        wss   = new WebAppSettingStore();
                        webPO = new MSPPersistedObject((SPPersistedObject)webApp.Instance);
                        webPO.GetChildString <WebAppSettingStore>((s) => wss);
                        return(webApp);
                    },
                    Dispose = () => { }
                };
            };

            //Act
            object target = proxyOp.Execute(args);

            //Assert
            Assert.IsNull(target);
        }