public void WhenShieldedActionThrows_ThenExceptionIsLogged()
        {
            var traceSource = new Mock <ITracer>();

            using (new DialogBoxPurger(0))
            {
                UIThreadInvoker.Invoke((Action)(() =>
                {
                    TraceSourceExtensions.ShieldUI(
                        traceSource.Object,
                        () => { throw new ArgumentException("Foo"); },
                        "Bar");
                }));
            }

            traceSource.Verify(x => x.Trace(TraceEventType.Error,
                                            It.Is <ArgumentException>(ex => ex.Message == "Foo"),
                                            "Bar"),
                               Times.Once());
        }
 public void WhenShieldingNullMessageArgs_ThenThrowsArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => TraceSourceExtensions.ShieldUI(new Mock <ITracer>().Object, () => { }, "foo", null));
 }
 public void WhenShieldingEmptyMessage_ThenThrowsArgumentOutOfRangeException()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => TraceSourceExtensions.ShieldUI(new Mock <ITracer>().Object, () => { }, string.Empty));
 }
 public void WhenShieldingNullAction_ThenThrowsArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => TraceSourceExtensions.ShieldUI(new Mock <ITracer>().Object, null, "foo"));
 }
 public void WhenShieldingNullTraceSource_ThenThrowsArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => TraceSourceExtensions.ShieldUI(null, () => { }, "foo"));
 }
        /// <summary>
        /// Executes the upgrade.
        /// </summary>
        /// <param name="context">The context of the upgrade</param>
        public void Execute(ISchemaUpgradeContext context)
        {
            Guard.NotNull(() => context, context);

            tracer.Info(ShellResources.PatternModelSchemaUpgradeManager_TraceExecute, context.SchemaFilePath);

            TraceSourceExtensions.Shield(tracer, () =>
            {
                if (context.UpgradeProcessors != null && context.UpgradeProcessors.Any())
                {
                    // Order processors by declared 'Order' field
                    var upgradeProcessors = context.UpgradeProcessors.ToList()
                                            .OrderBy(proc => proc.Metadata.Order);

                    // Trace all processors found
                    upgradeProcessors
                    .ForEach(proc =>
                    {
                        tracer.Verbose(
                            ShellResources.PatternModelSchemaUpgradeManager_TraceFoundUpgradeRule,
                            proc.Value.GetType(), proc.Metadata.TargetVersion, proc.Metadata.Order);
                    });

                    // Determine schema versions
                    var previousVersion = context.SchemaVersion;
                    var currentVersion  = context.RuntimeVersion;
                    if (previousVersion < currentVersion)
                    {
                        // Query Processors
                        var processorsToExecute = new List <IPatternModelSchemaUpgradeProcessor>();
                        upgradeProcessors
                        .ForEach(proc =>
                        {
                            // Is this processor required to execute for this version of schema?
                            Version procVersion;
                            if (Version.TryParse(proc.Metadata.TargetVersion, out procVersion))
                            {
                                if (procVersion == previousVersion)
                                {
                                    processorsToExecute.Add(proc.Value);
                                }
                            }
                        });

                        if (processorsToExecute.Any())
                        {
                            tracer.Verbose(
                                ShellResources.PatternModelSchemaUpgradeManager_TraceExecuteUpgradeProcessors, previousVersion, currentVersion);

                            // Initialize document
                            var document = context.OpenSchema();

                            // Backup original schema
                            context.BackupSchema();

                            // Execute processors
                            processorsToExecute.ForEach(proc =>
                            {
                                try
                                {
                                    tracer.Info(ShellResources.PatternModelSchemaUpgradeManager_TraceExecuteUpgradeRule,
                                                proc.GetType(), previousVersion);

                                    proc.ProcessSchema(document);
                                }
                                catch (Exception ex)
                                {
                                    tracer.Error(ShellResources.PatternModelSchemaUpgradeManager_ErrorUpgradeRuleFailed,
                                                 proc.GetType(), ex.Message);
                                    throw;
                                }
                            });
                        }

                        // Update and save
                        if ((previousVersion < currentVersion) ||
                            context.UpgradeProcessors.Any(mp => mp.Value.IsModified))
                        {
                            // Upgrade schema version
                            context.SchemaVersion = currentVersion;

                            // Save migrated document
                            context.SaveSchema();
                        }
                    }
                }
            }, ShellResources.PatternModelSchemaUpgradeManager_ErrorUpgradeRulesFailed);
        }