public void CanSetAndGetValues()
        {
            SPSite    spSite    = new SPSite("http://localhost:9001/sites/pssportal");
            SPContext spContext = SPContext.GetContext(spSite.RootWeb);

            Isolate.WhenCalled(() => SPContext.Current.Site).WillReturn(spSite);
            Isolate.WhenCalled(() => SPContext.Current).WillReturn(spContext);

            //Set Values at levels of hierarchy
            HierarchicalConfig target1 = new HierarchicalConfig();

            target1.SetInPropertyBag("IntegrationTest-FarmLevelKey", "FarmLevelValue", new SPFarmPropertyBag());
            target1.SetInPropertyBag("IntegrationTest-WebAppLevelKey", "WebAppLevelValue", new SPWebAppPropertyBag());
            target1.SetInPropertyBag("IntegrationTest-SiteLevelKey", "SiteLevelValue", new SPSitePropertyBag());
            target1.SetInPropertyBag("IntegrationTest-WebLevelKey", "WebLevelValue", new SPWebPropertyBag());

            HierarchicalConfig target2 = new HierarchicalConfig();

            Assert.AreEqual("FarmLevelValue", target2.GetByKey <string>("IntegrationTest-FarmLevelKey"));
            Assert.AreEqual("WebAppLevelValue", target2.GetByKey <string>("IntegrationTest-WebAppLevelKey"));
            Assert.AreEqual("SiteLevelValue", target2.GetByKey <string>("IntegrationTest-SiteLevelKey"));
            Assert.AreEqual("WebLevelValue", target2.GetByKey <string>("IntegrationTest-WebLevelKey"));

            Assert.AreEqual("FarmLevelValue", SPFarm.Local.Properties["IntegrationTest-FarmLevelKey"]);
            Assert.AreEqual("WebAppLevelValue", SPContext.Current.Site.WebApplication.Properties["IntegrationTest-WebAppLevelKey"]);
            Assert.AreEqual("SiteLevelValue", SPContext.Current.Site.RootWeb.AllProperties["IntegrationTest-SiteLevelKey"]);
            Assert.AreEqual("WebLevelValue", SPContext.Current.Web.AllProperties["IntegrationTest-WebLevelKey"]);
        }
        public void CanSetValueAndOverrideAtLowerLevel()
        {
            SPSite    spSite    = new SPSite("http://localhost:9001/sites/pssportal");
            SPContext spContext = SPContext.GetContext(spSite.RootWeb);

            Isolate.WhenCalled(() => SPContext.Current.Site).WillReturn(spSite);
            Isolate.WhenCalled(() => SPContext.Current).WillReturn(spContext);

            //Set Values at levels of hierarchy
            HierarchicalConfig target = new HierarchicalConfig();

            target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "FarmLevelValue", new SPFarmPropertyBag());
            Assert.AreEqual("FarmLevelValue", target.GetByKey <string>("IntegrationTest-FarmLevelKey"));

            target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "Over-riddenValue1", new SPWebAppPropertyBag());
            Assert.AreEqual("Over-riddenValue1", target.GetByKey <string>("IntegrationTest-FarmLevelKey"));

            target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "Over-riddenValue2", new SPWebAppPropertyBag());
            Assert.AreEqual("Over-riddenValue2", target.GetByKey <string>("IntegrationTest-FarmLevelKey"));

            target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "Over-riddenValue3", new SPSitePropertyBag());
            Assert.AreEqual("Over-riddenValue3", target.GetByKey <string>("IntegrationTest-FarmLevelKey"));

            target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "Over-riddenValue4", new SPWebPropertyBag());
            Assert.AreEqual("Over-riddenValue4", target.GetByKey <string>("IntegrationTest-FarmLevelKey"));
        }
Exemplo n.º 3
0
        public void GetSettingFrom_GetASettingWithHierarchyDoesntFindKey_FromLevelAboveKey()
        {
            //Arrange
            var           hierarchy     = new TestablePropertyBagHierarchy();
            string        key           = TestsConstants.TestGuidName;
            string        namespacedKey = ConfigManager.PnPKeyNamespace + "." + key;
            string        value         = "{80C23A3E-566B-4B11-A881-5868F2BCB198}";
            BIPropertyBag bag           = GetPropertyBag(ConfigLevel.CurrentSPWeb);

            bag.Values[namespacedKey] = value;
            hierarchy.AddPropertyBag(bag);
            hierarchy.AddPropertyBag(GetPropertyBag(ConfigLevel.CurrentSPSite));
            hierarchy.AddPropertyBag(GetPropertyBag(ConfigLevel.CurrentSPWebApplication));
            var target = new HierarchicalConfig(hierarchy);
            ConfigurationException configEx = null;

            //Act
            try
            {
                string result = target.GetByKey <string>(key, ConfigLevel.CurrentSPSite);
            }
            catch (ConfigurationException ex)
            {
                configEx = ex;
            }

            //Assert
            Assert.IsNotNull(configEx);
            Assert.IsTrue(configEx.Message.Contains(key));
        }
Exemplo n.º 4
0
        public void GetSettingFrom_GetASetting_NoKeyReturnsNull()
        {
            //Arrange
            var           hierarchy = new TestablePropertyBagHierarchy();
            BIPropertyBag bag       = GetPropertyBag(ConfigLevel.CurrentSPWeb);

            hierarchy.AddPropertyBag(bag);
            string key    = TestsConstants.TestGuidName;
            var    config = new HierarchicalConfig(hierarchy);
            ConfigurationException configEx = null;

            //Act
            try
            {
                string target = config.GetByKey <string>(key, ConfigLevel.CurrentSPWeb);
            }
            catch (ConfigurationException ex)
            {
                configEx = ex;
            }

            //Assert
            Assert.IsNotNull(configEx);
            Assert.IsTrue(configEx.Message.Contains(key));
        }
Exemplo n.º 5
0
        public void GetByKey_DeSerializeWrongType_ThrowsConfigurationException()
        {
            //Arrange
            var propertyBag = new BIPropertyBag();

            propertyBag.Values.Count = 0;
            string key           = "key";
            string nameSpacedKey = ConfigManager.PnPKeyNamespace + "." + key;
            string expectedError = "SerializationException";

            propertyBag.Values[nameSpacedKey] = "abc";
            var stack = new BIConfigStack();

            stack.Bags.Add(propertyBag);
            string errString = null;

            //Act
            var target = new HierarchicalConfig(stack, new MockConfigSettingSerializer()
            {
                ThrowError = true
            });

            try
            {
                target.GetByKey <DateTime>("key");
            }
            catch (ConfigurationException ex)
            {
                errString = ex.ToString();
            }

            // Assert
            Assert.IsNotNull(errString);
            Assert.IsTrue(errString.Contains(expectedError));
        }
Exemplo n.º 6
0
        public void GetByKey_KeyNotFound_ThrowsConfigurationException()
        {
            // Arrange
            var defaultPropertyBag = new BIPropertyBag();

            defaultPropertyBag.Values.Count = 0;
            var stack = new BIConfigStack();

            stack.Bags.Add(defaultPropertyBag);
            bool expectedExceptionThrown = false;

            var target = new HierarchicalConfig(stack);

            //Act
            try
            {
                target.GetByKey <string>("key");
            }
            catch (ConfigurationException)
            {
                expectedExceptionThrown = true;
            }

            // Assert
            Assert.IsTrue(expectedExceptionThrown, "Failed to throw exception on key not found");
        }
Exemplo n.º 7
0
        public void GetValueWillUseHierarchy()
        {
            MockPropertyBag parent = BuildPropertyBagHierarcy();

            parent["value"] = "value";

            var target = new HierarchicalConfig(parent.Children.First());

            Assert.AreEqual("value", target.GetByKey <string>("value"));
        }
Exemplo n.º 8
0
        public void CanGetValueBasedOnKey()
        {
            MockPropertyBag defaultPropertyBag = new MockPropertyBag();

            defaultPropertyBag["key"] = "3";
            var target      = new HierarchicalConfig(defaultPropertyBag, new MockConfigSettingSerializer());
            int configValue = target.GetByKey <int>("key");

            Assert.AreEqual(3, configValue);
        }
Exemplo n.º 9
0
        public void DeSerializeErrorGivesClearErrroMessage()
        {
            MockPropertyBag defaultPropertyBag = new MockPropertyBag();

            defaultPropertyBag.values.Add("key", "abc");
            var target = new HierarchicalConfig(defaultPropertyBag, new MockConfigSettingSerializer()
            {
                ThrowError = true
            });

            target.GetByKey <DateTime>("key");
        }
Exemplo n.º 10
0
        public void GetByKey_GetValueWithNullKey_ThrowsArgumentNullException()
        {
            var  target = new HierarchicalConfig();
            bool expectedExceptionThrown = false;

            //Act
            try
            {
                int configValue = target.GetByKey <int>(null);
            }
            catch (ArgumentNullException)
            {
                expectedExceptionThrown = true;
            }

            //Assert
            Assert.IsTrue(expectedExceptionThrown);
        }
Exemplo n.º 11
0
        public void ContainsKey_IfContextIsNull_ThrowsNoSharePointException()
        {
            //Arrange
            var  target = new HierarchicalConfig();
            bool expectedExceptionThrown = false;

            // Act
            try
            {
                target.GetByKey <DateTime>("key");
            }
            catch (NoSharePointContextException)
            {
                expectedExceptionThrown = true;
            }

            // Assert
            Assert.IsTrue(expectedExceptionThrown);
        }
Exemplo n.º 12
0
        public void GetSettingFrom_GetASetting_WithNullKey()
        {
            //Arrange
            var  hierarchy = new TestablePropertyBagHierarchy();
            bool expectedExceptionThrown = false;
            var  target = new HierarchicalConfig(hierarchy);

            //Act
            try
            {
                target.GetByKey <string>(null, ConfigLevel.CurrentSPSite);
            }
            catch (ArgumentNullException)
            {
                expectedExceptionThrown = true;
            }

            Assert.IsTrue(expectedExceptionThrown);
        }
Exemplo n.º 13
0
        public void KeyNotFoundThrows()
        {
            var target = new HierarchicalConfig(new MockPropertyBag());

            try
            {
                target.GetByKey <string>("key");
                Assert.Fail();
            }
            catch (ConfigurationException configurationException)
            {
                Assert.AreEqual("There was no value configured for key 'key' in a propertyBag with level 'CurrentSPFarm' or above.",
                                configurationException.Message);
            }
            catch (Exception)
            {
                Assert.Fail();
            }
        }
Exemplo n.º 14
0
        public void GetSettingFrom_GetASetting()
        {
            //Arrange
            var           hierarchy = new TestablePropertyBagHierarchy();
            BIPropertyBag bag       = GetPropertyBag(ConfigLevel.CurrentSPSite);

            hierarchy.AddPropertyBag(bag);
            string key           = TestsConstants.TestGuidName;
            string namespacedKey = ConfigManager.PnPKeyNamespace + "." + key;
            string expected      = "{80C23A3E-566B-4B11-A881-5868F2BCB198}";

            bag.Values[namespacedKey] = expected;
            bag.Level = ConfigLevel.CurrentSPWeb;
            var config = new HierarchicalConfig(hierarchy);

            //Act
            string target = config.GetByKey <string>(key, ConfigLevel.CurrentSPWeb);

            //Assert
            Assert.AreEqual(expected, target);
        }
Exemplo n.º 15
0
        public void GetByKey_GetByValueWithLevelWithNullKey_ThrowsArgumentNullException()
        {
            //Arrange
            var locator = SharePointServiceLocator.GetCurrent() as ActivatingServiceLocator;
            var context = new MSPContext();

            MSPContext.CurrentGet = () => context;
            var  target = new HierarchicalConfig();
            bool expectedExceptionThrown = false;

            //Act
            try
            {
                string expectedContains = target.GetByKey <string>(null, ConfigLevel.CurrentSPFarm);
            }
            catch (ArgumentNullException)
            {
                expectedExceptionThrown = true;
            }

            //Assert
            Assert.IsTrue(expectedExceptionThrown);
        }
Exemplo n.º 16
0
        public void GetByKey_GetValueByKey()
        {
            //Arrange
            // This is an example of a behaved type
            // (a stub that is extended to implement general behavior
            // and can be re-used)
            var    defaultPropertyBag = new BIPropertyBag();
            string key          = "key";
            int    expected     = 3;
            string namespaceKey = ConfigManager.PnPKeyNamespace + "." + key;

            defaultPropertyBag.Values.SetOne(namespaceKey, expected.ToString());
            var stack = new BIConfigStack();

            stack.Bags.Add(defaultPropertyBag);

            var serializer = new SIConfigSettingSerializer()
            {
                DeserializeTypeString = (type, data) =>
                {
                    object ret = null;
                    if (type == typeof(int))
                    {
                        ret = int.Parse(data);
                    }
                    return(ret);
                },
            };

            //Act
            var target      = new HierarchicalConfig(stack, serializer);
            int configValue = target.GetByKey <int>(key);

            //Assert
            Assert.AreEqual(expected, configValue);
        }
Exemplo n.º 17
0
        public void GetByKey_GetValue_WillUseHierarchy()
        {
            //Arrange
            string key           = "{6313EE1A-5A12-46A3-A537-4905678FBD9E}";
            string namespacedKey = ConfigManager.PnPKeyNamespace + "." + key;
            string value         = "{821FC688-2C8C-4F8B-A700-EDB81400B63B}";

            var parent = new BIPropertyBag();
            var child  = new BIPropertyBag();

            parent.Values.SetOne(namespacedKey, value);
            child.Values.Count = 0;
            var stack = new BIConfigStack();

            stack.Bags.Add(child);
            stack.Bags.Add(parent);

            //Act
            var    target = new HierarchicalConfig(stack);
            string actual = target.GetByKey <string>(key);

            // Assert
            Assert.AreEqual(value, actual);
        }
Exemplo n.º 18
0
        public void IfContextIsNullDefaultPropertyBagThrows()
        {
            var target = new HierarchicalConfig();

            target.GetByKey <DateTime>("key");
        }