public void WhenContextInitializerNotNull_ThenInvokesItOnEvaluation()
        {
            var settings = new PropertyBindingSettings { ValueProvider = new ValueProviderBindingSettings { } };
            bool invoked = false;

            settings.Evaluate(this.bindingFactory, this.tracer, c => invoked = true);

            Assert.True(invoked);
        }
        public void WhenValueProviderBindingFails_ThenThrowsInvalidOperationExceptionAndTraces()
        {
            var settings = new PropertyBindingSettings { ValueProvider = new ValueProviderBindingSettings { } };

            Mock.Get(this.binding)
                .Setup(x => x.Evaluate(It.IsAny<IDynamicBindingContext>()))
                .Returns(false);

            Assert.Throws<InvalidOperationException>(() => settings.Evaluate(this.bindingFactory, this.tracer));
            Mock.Get(this.tracer)
                .Verify(x => x.Trace(TraceEventType.Error, It.IsAny<string>(), It.IsAny<object[]>()));
        }
        public void WhenPropertyValueProviderSet_ThenBindingRaisesChanged()
        {
            var binding = new BindingSettings();
            var property = new PropertyBindingSettings();
            var raised = false;
            binding.Properties.Add(property);

            binding.PropertyChanged += (sender, args) => raised = true;

            property.ValueProvider = new ValueProviderBindingSettings();

            Assert.True(raised);
        }
        public void WhenPropertyNameChanged_ThenBindingRaisesChanged()
        {
            var binding = new BindingSettings();
            var property = new PropertyBindingSettings();
            var raised = false;
            binding.Properties.Add(property);

            binding.PropertyChanged += (sender, args) => raised = true;

            property.Name = "hello";

            Assert.True(raised);
        }
        public void WhenValueProviderPropertyChanged_ThenBindingRaisesChanged()
        {
            var raised = false;
            var property = new PropertyBindingSettings();
            var binding = new BindingSettings
            {
                Properties = 
                {
                    new PropertyBindingSettings
                    {
                        ValueProvider = new ValueProviderBindingSettings
                        {
                            Properties = 
                            {
                                property,
                            }
                        }
                    }
                }
            };

            binding.PropertyChanged += (sender, args) => raised = true;

            property.Value = "foo";

            Assert.True(raised);
        }
        public void WhenValueProviderExists_ThenEvaluatesItsValue()
        {
            var settings = new PropertyBindingSettings { ValueProvider = new ValueProviderBindingSettings { } };

            Assert.Equal("Foo", settings.Evaluate(this.bindingFactory, this.tracer));
        }
        public void WhenNoValueProviderExists_ThenReturnsBindingValue()
        {
            var settings = new PropertyBindingSettings { Value = "Foo" };

            Assert.Equal("Foo", settings.Evaluate(this.bindingFactory, this.tracer));
        }
        private static void AddPropertySettings(XElement cmdPropsElement, IList<IPropertyBindingSettings> bindings, XElement propSettingElement, IList<XElement> processedPropertySettings)
        {
            // Create a PropertyBindingSettings from the <PropertySettings> element.
            if (propSettingElement.Attributes().Any(a => a.Name == PropertySettingsNameName))
            {
                var binding = new PropertyBindingSettings
                {
                    Name = TrimName(propSettingElement.Attribute(PropertySettingsNameName).Value),
                    Value = propSettingElement.Attributes().Any(a => a.Name == PropertySettingsValueName)
                            ? DecodeValue(propSettingElement.Attribute(PropertySettingsValueName).Value)
                            : null,
                };

                // Determine if has a ValueProvider
                var valueProviderElement = propSettingElement.Descendants(ValueProviderElementName)
                                      .FirstOrDefault();
                if (valueProviderElement != null)
                {
                    var valueProviderSettingsElement = valueProviderElement.Descendants(ValueProviderSettingElementName)
                                            .FirstOrDefault();
                    if (valueProviderSettingsElement != null)
                    {
                        if (valueProviderSettingsElement.Attributes().Any(a => a.Name == ValueProviderSettingsIdName))
                        {
                            var vpBinding = new ValueProviderBindingSettings
                            {
                                TypeId = valueProviderSettingsElement.Attribute(ValueProviderSettingsTypeIdName).Value,
                            };

                            // Determine if the ValueProvider has nested <propertySettings> elements
                            var propSettingsMonikers = valueProviderSettingsElement.Descendants(PropertySettingMonikerElementName);
                            if (propSettingsMonikers.Any())
                            {
                                // Match each nested <propertySettings> element, which should be linked to <commandSettings><properties>
                                propSettingsMonikers.ForEach(psm =>
                                {
                                    var psmId = psm.Attribute(PropertySettingsMonikerIdName).Value;
                                    var vpPropSettings = cmdPropsElement.Descendants(PropertySettingsElementName)
                                            .FirstOrDefault(pse => pse.Attribute(PropertySettingsIdName).Value == psmId);
                                    if (vpPropSettings != null)
                                    {
                                        // Cache <propertySetting> element
                                        processedPropertySettings.Add(vpPropSettings);

                                        // Create Bindings
                                        AddPropertySettings(cmdPropsElement, vpBinding.Properties, vpPropSettings, processedPropertySettings);
                                    }
                                });
                            }

                            binding.ValueProvider = vpBinding;
                        }
                    }
                }

                bindings.Add(binding);
            }
        }
        /// <summary>
        /// Processes the document
        /// </summary>
        /// <param name="document">The document to process.</param>
        public void ProcessSchema(XDocument document)
        {
            Guard.NotNull(() => document, document);

            this.IsModified = false;

            // Locate all <commandSettings> that have a 'typeId="NuPattern.Library.Commands.AggregatorCommand"'
            var commandSettings = document.Descendants(CommandSettingsElementName)
                        .Where(cs => cs.Attribute(CommandSettingsTypeIdName) != null && cs.Attribute(CommandSettingsTypeIdName).Value == typeof(AggregatorCommand).FullName)
                        .Distinct();
            if (commandSettings.Any())
            {
                tracer.Info(Resources.AggregatorCommandUpgradeProcessor_TraceDeserialize);

                // Enumerate each <commandSettings> element
                commandSettings.ForEach(cmdSettingsElement =>
                    {
                        var id = cmdSettingsElement.Attribute(CommandSettingsIdName) != null ? cmdSettingsElement.Attribute(CommandSettingsIdName).Value : string.Empty;
                        var cmdPropsElement = cmdSettingsElement.Descendants(CommandSettingsPropertiesElementName).FirstOrDefault();
                        if (cmdPropsElement != null)
                        {
                            // Ensure we have a value for <properties>
                            var propertiesValue = cmdPropsElement.Value;
                            if (!string.IsNullOrEmpty(propertiesValue))
                            {
                                try
                                {
                                    // Determine if has a serialized 'string' value, as opposed to a 'Collection<CommmandReference>'
                                    var bindings = BindingSerializer.Deserialize<IEnumerable<IPropertyBindingSettings>>(propertiesValue);
                                    if (bindings != null && bindings.Any())
                                    {
                                        var existingBinding = bindings.FirstOrDefault(b => b.Name == Reflector<AggregatorCommand>.GetPropertyName(x => x.CommandReferenceList));
                                        if (existingBinding != null)
                                        {
                                            // Ensure there is a value
                                            if (!String.IsNullOrEmpty(existingBinding.Value))
                                            {
                                                //Ensure value is previous GUID list format
                                                if (Regex.IsMatch(existingBinding.Value, DelimitedListGuidRegEx))
                                                {
                                                    // Read the delimitied array of strings
                                                    var referenceStrings = existingBinding.Value.Split(new[] { CommandReferenceDelimitier }, StringSplitOptions.RemoveEmptyEntries);
                                                    if (referenceStrings.Any())
                                                    {
                                                        tracer.Info(Resources.AggregatorCommandUpgradeProcessor_TraceDeserializeCommandSettings, id);

                                                        // Convert to command references
                                                        var references = new List<CommandReference>();
                                                        referenceStrings.ForEach(rs =>
                                                            {
                                                                Guid refId;
                                                                if (Guid.TryParse(rs, out refId))
                                                                {
                                                                    references.Add(new CommandReference(null)
                                                                        {
                                                                            CommandId = refId,
                                                                        });
                                                                }
                                                            });

                                                        // Update value of <properties> element
                                                        var newBinding = new PropertyBindingSettings
                                                        {
                                                            Name = Reflector<AggregatorCommand>.GetPropertyName(x => x.CommandReferenceList),
                                                            Value = BindingSerializer.Serialize(new Collection<CommandReference>(references)),
                                                        };
                                                        cmdPropsElement.SetValue(BindingSerializer.Serialize(new IPropertyBindingSettings[] { newBinding }));
                                                        this.IsModified = true;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (BindingSerializationException)
                                {
                                    // Ignore deserializaton exception
                                }
                            }
                        }
                    });
            }
        }
            public void WhenValueProviderAndNoDefaultValue_ThenValidateValueProviderOrDefaultValueSucceeds()
            {
                PropertyBindingSettings defaultValueBindings = new PropertyBindingSettings();
                defaultValueBindings.Value = "";
                defaultValueBindings.ValueProvider = new ValueProviderBindingSettings
                {
                    TypeId = ""
                };
                ValueProviderBindingSettings valueProviderBindings = new ValueProviderBindingSettings
                {
                    TypeId = "Foo"
                };
                this.store.TransactionManager.DoWithinTransaction(() =>
                {
                    this.property.DefaultValue = defaultValueBindings;
                    this.property.ValueProvider = valueProviderBindings;
                });
                this.property.ValidateValueProviderOrDefaultValue(validationContext);

                Assert.True(validationContext.CurrentViolations.Count == 0);
                Assert.True(validationContext.ValidationSubjects.IndexOf(this.property) == 0);
            }
            public void WhenDefaultValueHasValueAndValueProvider_ThenValidateDefaultValueHasBothValueOrValueProviderFails()
            {
                PropertyBindingSettings propertyBindings = new PropertyBindingSettings();
                propertyBindings.Value = "Foo";
                propertyBindings.ValueProvider = new ValueProviderBindingSettings
                {
                    TypeId = "FooType"
                };

                this.store.TransactionManager.DoWithinTransaction(() =>
                {
                    this.property.DefaultValue = propertyBindings;
                });
                this.property.ValidateDefaultValueHasValueOrValueProvider(validationContext);

                Assert.True(validationContext.CurrentViolations.Count == 1);
                Assert.True(validationContext.ValidationSubjects.IndexOf(this.property) == 0);
            }
            public void WhenDefaultValueHasOnlyValue_ThenValidateDefaultValueHasBothValueOrValueProviderSucceeds()
            {
                PropertyBindingSettings propertyBindings = new PropertyBindingSettings();
                propertyBindings.Value = "Foo";

                this.store.TransactionManager.DoWithinTransaction(() =>
                {
                    this.property.DefaultValue = propertyBindings;
                });
                this.property.ValidateDefaultValueHasValueOrValueProvider(validationContext);

                Assert.True(validationContext.CurrentViolations.Count == 0);
            }
            public void WhenDefaultValueHasOnlyValueProvider_ThenValidateDefaultValueHasBothValueOrValueProviderSucceeds()
            {
                var propertyBindings = new PropertyBindingSettings
                {
                    Value = string.Empty,
                    ValueProvider = new ValueProviderBindingSettings
                    {
                        TypeId = "FooType"
                    }
                };

                this.property.DefaultValue = propertyBindings;

                this.property.ValidateDefaultValueHasValueOrValueProvider(validationContext);

                Assert.True(validationContext.CurrentViolations.Count == 0);
            }