Пример #1
0
        public void AddAndRemove()
        {
            //Arrange
            SPWebAppPropertyBag.ClearCache();
            MSPPersistedObject webPO;
            WebAppSettingStore wss;
            string             key = "flintstone";

            var webApp = new BSPConfiguredWebApp();

            wss = new WebAppSettingStore();
            wss.Settings[key] = "fred";
            webPO             = new MSPPersistedObject((SPPersistedObject)webApp.Instance);
            webPO.GetChildString <WebAppSettingStore>((s) => wss);
            var wssPO = new MSPPersistedObject(wss);

            wssPO.Update = () =>
            {
            };

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

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

            // Assert
            Assert.IsFalse(result);
            Assert.IsTrue(containsBeforeCondition);
        }
Пример #2
0
        public void AddAndContains()
        {
            //Arrange
            SPWebAppPropertyBag.ClearCache();
            MSPPersistedObject webPO;
            WebAppSettingStore wss;
            var webApp = new BSPConfiguredWebApp();

            wss   = new WebAppSettingStore();
            webPO = new MSPPersistedObject((SPPersistedObject)webApp.Instance);
            webPO.GetChildString <WebAppSettingStore>(
                (s) =>
            {
                return(wss);
            });
            var wssPO = new MSPPersistedObject(wss);

            wssPO.Update = () =>
            {
            };


            string key    = "key";
            string value  = "value";
            var    target = new SPWebAppPropertyBag(webApp);

            IPropertyBagTest.AddContains(target, key, value);
        }
        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);
        }
        /// <summary>
        /// Implements the operation for determining if a key exists in a web application
        /// </summary>
        /// <param name="args">The arguments for the contains key operation.  This must be an instance of ContainsKeyDataArgs.</param>
        /// <returns>true if the key found, false if the key is not found, or an exception representing an error if an error occurred</returns>
        public override object Execute(SPProxyOperationArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            var containsKeyArgs = args as ContainsKeyDataArgs;

            if (containsKeyArgs == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Resources.InvalidProxyArgumentType,
                                               typeof(ContainsKeyDataArgs).FullName, args.GetType().FullName);
                var ex = new ConfigurationException(message);
                return(ex);
            }
            else if (containsKeyArgs.Key == null)
            {
                return(new ArgumentNullException("ContainsKeyDataArgs.Key"));
            }

            try
            {
                ConfigLevel level = (ConfigLevel)containsKeyArgs.Level;

                if (containsKeyArgs.Key.StartsWith(ConfigManager.PnPKeyNamespace) == false)
                {
                    string message            = string.Format(CultureInfo.CurrentCulture, Resources.InvalidKeyName, containsKeyArgs.Key);
                    ConfigurationException ex = new ConfigurationException(message);
                    return(ex);
                }
                else
                {
                    bool contains = false;

                    if (level == ConfigLevel.CurrentSPWebApplication)
                    {
                        if (containsKeyArgs.SiteId == Guid.Empty)
                        {
                            return(new ConfigurationException(Resources.EmptySiteGuid));
                        }
                        using (SPSite site = new SPSite(containsKeyArgs.SiteId))
                        {
                            SPWebAppPropertyBag bag = new SPWebAppPropertyBag(site.WebApplication);
                            contains = bag.Contains(containsKeyArgs.Key);
                        }
                    }
                    else if (level == ConfigLevel.CurrentSPFarm)
                    {
                        var bag = new SPFarmPropertyBag(SPFarm.Local);
                        contains = bag.Contains(containsKeyArgs.Key);
                    }
                    else
                    {
                        string message            = string.Format(CultureInfo.CurrentCulture, Resources.InvalidConfigLevel, level.ToString());
                        ConfigurationException ex = new ConfigurationException(message);
                        return(ex);
                    }
                    return(contains);
                }
            }
            catch (Exception excpt)
            {
                return(excpt);
            }
        }