Beispiel #1
0
            public void WhenChangingSavedValidatorBindingSettings_ThenPersistsChanges()
            {
                var settings = new ValidationBindingSettings
                {
                    TypeId     = "Foo",
                    Properties =
                    {
                        new PropertyBindingSettings
                        {
                            Name  = "Message",
                            Value = "Hello",
                        },
                        new PropertyBindingSettings
                        {
                            Name          = "From",
                            ValueProvider = new ValueProviderBindingSettings
                            {
                                TypeId = "CurrentUserProvider",
                            }
                        },
                    },
                };

                ((PropertySchema)this.property).RawValidationRules = BindingSerializer.Serialize <ValidationBindingSettings[]>(new ValidationBindingSettings[] { settings });

                ((BindingSettings)this.property.ValidationSettings.First()).Properties[0].Value = "World";
                ((BindingSettings)this.property.ValidationSettings.First()).Properties[1].ValueProvider.TypeId = "AnotherProvider";

                var saved = BindingSerializer.Deserialize <ValidationBindingSettings[]>(((PropertySchema)this.property).RawValidationRules);

                Assert.Equal(1, saved.Length);
                Assert.Equal("World", saved[0].Properties[0].Value);
                Assert.Equal("AnotherProvider", saved[0].Properties[1].ValueProvider.TypeId);
            }
Beispiel #2
0
        private ObservableCollection <IPropertyBindingSettings> GetPropertySettings()
        {
            var properties = string.IsNullOrEmpty(this.Properties) ?
                             new ObservableCollection <IPropertyBindingSettings>() :
                             BindingSerializer.Deserialize <ObservableCollection <IPropertyBindingSettings> >(this.Properties);

            properties.CollectionChanged += (sender, args) =>
            {
                if (args.OldItems != null)
                {
                    args.OldItems.OfType <IPropertyBindingSettings>()
                    .ToList()
                    .ForEach(binding => binding.PropertyChanged -= OnSaveRules);
                }
                if (args.NewItems != null)
                {
                    args.NewItems.OfType <IPropertyBindingSettings>()
                    .ToList()
                    .ForEach(binding => binding.PropertyChanged += OnSaveRules);
                }
            };

            properties
            .ForEach(binding => binding.PropertyChanged += OnSaveRules);

            return(properties);
        }
Beispiel #3
0
            public void WhenSettingValidatorBindingSettings_ThenPersistsChanges()
            {
                var settings = new ValidationBindingSettings
                {
                    TypeId     = "Foo",
                    Properties =
                    {
                        new PropertyBindingSettings
                        {
                            Name  = "Message",
                            Value = "Hello",
                        },
                        new PropertyBindingSettings
                        {
                            Name          = "From",
                            ValueProvider = new ValueProviderBindingSettings
                            {
                                TypeId = "CurrentUserProvider",
                            }
                        },
                    },
                };

                this.property.ValidationSettings = new IBindingSettings[] { settings };

                var saved = BindingSerializer.Deserialize <ValidationBindingSettings[]>(((PropertySchema)this.property).RawValidationRules);

                Assert.Equal(1, saved.Length);
                Assert.Equal("Foo", saved[0].TypeId);
                Assert.Equal(2, saved[0].Properties.Count);
                Assert.Equal("Message", saved[0].Properties[0].Name);
                Assert.Equal("Hello", saved[0].Properties[0].Value);
                Assert.Equal("From", saved[0].Properties[1].Name);
                Assert.Equal("CurrentUserProvider", saved[0].Properties[1].ValueProvider.TypeId);
            }
        private ConditionBindingSettings[] GetConditionSettings()
        {
            if (string.IsNullOrEmpty(this.Conditions))
            {
                return(new ConditionBindingSettings[0]);
            }

            return(BindingSerializer.Deserialize <ConditionBindingSettings[]>(this.Conditions));
        }
        private ValidationBindingSettings[] GetValidationSettings()
        {
            if (string.IsNullOrEmpty(this.ValidationRules))
            {
                return(new ValidationBindingSettings[0]);
            }

            return(BindingSerializer.Deserialize <ValidationBindingSettings[]>(this.ValidationRules));
        }
Beispiel #6
0
        private List <ConditionBindingSettings> GetConditionSettings()
        {
            if (string.IsNullOrEmpty(this.Conditions))
            {
                return(new List <ConditionBindingSettings>());
            }

            return(BindingSerializer.Deserialize <List <ConditionBindingSettings> >(this.Conditions));
        }
Beispiel #7
0
            public void WhenChangingDefaultValueSettings_ThenPersistsChangesAutomatically()
            {
                var settings = (PropertyBindingSettings)this.property.DefaultValue;

                settings.Value = "hello";

                var saved = BindingSerializer.Deserialize <PropertyBindingSettings>(this.property.RawDefaultValue);

                Assert.Equal("hello", saved.Value);
            }
Beispiel #8
0
            public void WhenChangingValueProviderSettings_ThenPersistsChangesAutomatically()
            {
                var settings = this.property.ValueProvider;

                settings.TypeId = "Foo";

                var saved = BindingSerializer.Deserialize <IValueProviderBindingSettings>(this.property.RawValueProvider);

                Assert.Equal("Foo", saved.TypeId);
            }
Beispiel #9
0
        private ValueProviderBindingSettings CreateValueProviderSettings()
        {
            var settings = string.IsNullOrEmpty(this.rawValueGetter()) ?
                           new ValueProviderBindingSettings() :
                           BindingSerializer.Deserialize <ValueProviderBindingSettings>(this.rawValueGetter());

            // Automatically serialize whenever something is changed in the binding.
            settings.PropertyChanged += OnSaveValue;

            return(settings);
        }
Beispiel #10
0
        /// <summary>
        /// Gets the deserialized <see cref="Collection{T}"/> from the underlying serialized string.
        /// </summary>
        public override object GetValue(object component)
        {
            var value  = base.GetValue(component) as string;
            var values = string.IsNullOrEmpty(value) ? new Collection <T>() : BindingSerializer.Deserialize <Collection <T> >(value);

            if (this.IsReadOnly)
            {
                return(new ReadOnlyCollection <T>(values));
            }

            return(values);
        }
Beispiel #11
0
        private IBindingSettings[] GetValidationSettings()
        {
            if (string.IsNullOrEmpty(this.RawValidationRules))
            {
                return(new IBindingSettings[0]);
            }

            this.validationSettings = BindingSerializer.Deserialize <ValidationBindingSettings[]>(this.RawValidationRules);
            MonitorValidationRules();

            return(this.validationSettings);
        }
 internal static ReferenceTag GetReference(string referenceTag)
 {
     try
     {
         return(BindingSerializer.Deserialize <ReferenceTag>(referenceTag));
     }
     catch
     {
         return(new ReferenceTag {
             SyncNames = false, TargetFileName = string.Empty
         });
     }
 }
Beispiel #13
0
        /// <summary>
        /// Attemps to deserialize a <see cref="ReferenceTag"/> from a
        /// serialized string.
        /// </summary>
        /// <param name="serialized">The serialized reference tag.</param>
        /// <returns>The deserialized instance, or <see langword="null"/> if it can't be deserialized.</returns>
        public static ReferenceTag TryDeserialize(string serialized)
        {
            if (string.IsNullOrEmpty(serialized))
            {
                return(null);
            }

            try
            {
                return(BindingSerializer.Deserialize <ReferenceTag>(serialized));
            }
            catch
            {
                return(null);
            }
        }
        public void WhenSerialingAnObjectWithAStringProperty_ThenItDeserializesItProperly()
        {
            var foos = new List <Foo> {
                new Foo {
                    Bar = 1, Baz = "Baz1"
                }, new Foo {
                    Bar = 2, Baz = "Baz2"
                }
            };

            var ser = BindingSerializer.Serialize(foos);

            var dserFoos = BindingSerializer.Deserialize <List <Foo> >(ser);

            Assert.Equal(dserFoos.Count, foos.Count);
            Assert.Equal(dserFoos.ElementAt(0).Bar, foos.ElementAt(0).Bar);
            Assert.Equal(dserFoos.ElementAt(0).Baz, foos.ElementAt(0).Baz);
            Assert.Equal(dserFoos.ElementAt(1).Bar, foos.ElementAt(1).Bar);
            Assert.Equal(dserFoos.ElementAt(1).Baz, foos.ElementAt(1).Baz);
        }
Beispiel #15
0
 private static Guid GetIdFromReferenceTag(IReference r)
 {
     try
     {
         return(BindingSerializer.Deserialize <ReferenceTag>(r.Tag).Id);
     }
     catch
     {
         Guid result;
         if (Guid.TryParse(r.Tag, out result))
         {
             // Try to get the legacy settings guid
             return(result);
         }
         else
         {
             return(Guid.Empty);
         }
     }
 }
            public void WhenSettingValue_ThenSerializesToJson()
            {
                var property = new Mock <PropertyDescriptor>("foo", new Attribute[0]);

                property.Setup(x => x.Name).Returns("Property");
                property.Setup(x => x.Attributes).Returns(new AttributeCollection());

                string serializedValue = null;

                property
                .Setup(x => x.SetValue(It.IsAny <object>(), It.IsAny <object>()))
                .Callback <object, object>((component, value) => serializedValue = (string)value);

                var descriptor = new StringCollectionPropertyDescriptor <Model>(property.Object);

                descriptor.SetValue(new object(), this.models);

                var conditions = BindingSerializer.Deserialize <Collection <Model> >(serializedValue);

                Assert.Equal(2, conditions.Count);
            }
Beispiel #17
0
        /// <summary>
        /// Finishes initialization, resolving the conditions and custom status bindings.
        /// </summary>
        public override void EndInit()
        {
            base.EndInit();

            if (this.BindingFactory != null)
            {
                try
                {
                    this.Conditions = this.Settings.ConditionSettings.Select(x => this.BindingFactory.CreateBinding <ICondition>(x)).ToArray();
                }
                catch (Exception e)
                {
                    tracer.Error(e, Resources.MenuAutomation_FailedToParseConditions, this.Name);
                    if (Microsoft.VisualStudio.ErrorHandler.IsCriticalException(e))
                    {
                        throw;
                    }
                }

                if (!string.IsNullOrEmpty(this.Settings.CustomStatus))
                {
                    try
                    {
                        var bindingSettings = BindingSerializer.Deserialize <BindingSettings>(this.Settings.CustomStatus);
                        if (!string.IsNullOrEmpty(bindingSettings.TypeId))
                        {
                            this.CommandStatus = this.BindingFactory.CreateBinding <ICommandStatus>(bindingSettings);
                        }
                    }
                    catch (Exception e)
                    {
                        tracer.Error(e, Resources.MenuAutomation_FailedToParseCustomStatus, this.Name);
                        if (Microsoft.VisualStudio.ErrorHandler.IsCriticalException(e))
                        {
                            throw;
                        }
                    }
                }
            }
        }
        public void WhenRoundTrippingFullBinding_ThenSucceeds()
        {
            IBindingSettings binding = new BindingSettings
            {
                TypeId     = "foo",
                Properties =
                {
                    new PropertyBindingSettings
                    {
                        Name          = "Name",
                        Value         = "Value",
                        ValueProvider = new ValueProviderBindingSettings
                        {
                            TypeId     = "ValueProvider",
                            Properties =
                            {
                                new PropertyBindingSettings
                                {
                                    Name  = "Id",
                                    Value = "1"
                                }
                            }
                        }
                    }
                }
            };

            var serialized   = BindingSerializer.Serialize(binding);
            var deserialized = BindingSerializer.Deserialize <IBindingSettings>(serialized);

            Assert.Equal(binding.TypeId, deserialized.TypeId);
            Assert.Equal(binding.Properties.Count, deserialized.Properties.Count);
            Assert.Equal(binding.Properties[0].Name, deserialized.Properties[0].Name);
            Assert.Equal(binding.Properties[0].Value, deserialized.Properties[0].Value);
            Assert.Equal(binding.Properties[0].ValueProvider.TypeId, deserialized.Properties[0].ValueProvider.TypeId);
            Assert.Equal(binding.Properties[0].ValueProvider.Properties[0].Name, deserialized.Properties[0].ValueProvider.Properties[0].Name);
            Assert.Equal(binding.Properties[0].ValueProvider.Properties[0].Value, deserialized.Properties[0].ValueProvider.Properties[0].Value);
        }
        /// <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
                            }
                        }
                    }
                });
            }
        }
Beispiel #20
0
        /// <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=' any of the old feature commands
            var commandSettings = document.Descendants(CommandSettingsElementName)
                                  .Where(cs => cs.Attribute(CommandSettingsTypeIdName) != null)
                                  .Where(cs => cs.Attribute(CommandSettingsTypeIdName).Value == ActivateCommandTypeName ||
                                         cs.Attribute(CommandSettingsTypeIdName).Value == InstantiateCommandTypeName ||
                                         cs.Attribute(CommandSettingsTypeIdName).Value == ActivateOrInstantiateCommandTypeName)
                                  .Distinct();

            if (commandSettings.Any())
            {
                tracer.Info(Resources.GuidanceCommandUpgradeProcessor_TraceDeserialize);

                // Enumerate each <commandSettings> element
                commandSettings.ForEach(cmdSettingsElement =>
                {
                    // Rename command TypeId
                    var typeId    = cmdSettingsElement.Attribute(CommandSettingsTypeIdName) != null ? cmdSettingsElement.Attribute(CommandSettingsTypeIdName).Value : string.Empty;
                    var newTypeId = string.Empty;
                    switch (typeId)
                    {
                    case ActivateCommandTypeName:
                        newTypeId = typeof(ActivateGuidanceWorkflowCommand).FullName;
                        break;

                    case InstantiateCommandTypeName:
                        newTypeId = typeof(InstantiateGuidanceWorkflowCommand).FullName;
                        break;

                    case ActivateOrInstantiateCommandTypeName:
                        newTypeId = typeof(ActivateOrInstantiateSharedGuidanceWorkflowCommand).FullName;
                        break;
                    }

                    if (!String.IsNullOrEmpty(newTypeId))
                    {
                        // Rename command typeId
                        cmdSettingsElement.Attribute(CommandSettingsTypeIdName).SetValue(newTypeId);
                        this.IsModified = true;

                        //Rename 'FeatureId' property to 'ExtensionId' property
                        // Note, not all these commands have properties
                        var cmdPropsElement = cmdSettingsElement.Descendants(CommandSettingsPropertiesElementName).FirstOrDefault();
                        if (cmdPropsElement != null)
                        {
                            var propertiesValue = cmdPropsElement.Value;
                            if (!string.IsNullOrEmpty(propertiesValue))
                            {
                                try
                                {
                                    var bindings = BindingSerializer.Deserialize <IEnumerable <IPropertyBindingSettings> >(propertiesValue).ToList();
                                    if (bindings != null && bindings.Any())
                                    {
                                        // Note, not all the commands had the 'FeatureId' property
                                        var featureIdProperty = bindings.FirstOrDefault(p => p.Name == FeatureIdPropertyName);
                                        if (featureIdProperty != null)
                                        {
                                            // Replace 'FeatureId' Property binding
                                            featureIdProperty.Name = Reflector <InstantiateGuidanceWorkflowCommand> .GetPropertyName(x => x.ExtensionId);

                                            // Write bindings back
                                            cmdPropsElement.SetValue(BindingSerializer.Serialize(bindings));
                                            this.IsModified = true;
                                        }
                                    }
                                }
                                catch (BindingSerializationException)
                                {
                                    // Ignore deserializaton exception
                                }
                            }
                        }
                    }
                });
            }
        }