public object Clone()
        {
            var r = new TestAllProjectsInFolderOptions();

            r.CopyFrom(this);
            return(r);
        }
			public Reporter(TestAllProjectsInFolderOptions testOptions, Altaxo.Main.Services.IOutputService previousOutputService)
			{
				_previousOutputService = previousOutputService;

				if (!string.IsNullOrEmpty(testOptions.ProtocolFileName))
				{
					var stream = new System.IO.FileStream(testOptions.ProtocolFileName, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read);
					_wr = new System.IO.StreamWriter(stream, Encoding.UTF8);

					_wr.WriteLine();
					_wr.WriteLine("******************************************************************************");
					_wr.WriteLine("{0}: Test of Altaxo project files. Folder(s) to test: {1}, SaveAndReopeningProjects: {2}", DateTime.Now, testOptions.FolderPaths, testOptions.TestSavingAndReopening);
					_wr.WriteLine("------------------------------------------------------------------------------");
				}
			}
            public Reporter(TestAllProjectsInFolderOptions testOptions, Altaxo.Main.Services.ITextOutputService previousOutputService)
            {
                _previousOutputService = previousOutputService;

                if (!string.IsNullOrEmpty(testOptions.ProtocolFileName))
                {
                    var stream = new System.IO.FileStream(testOptions.ProtocolFileName, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read);
                    _wr = new System.IO.StreamWriter(stream, Encoding.UTF8);

                    _wr.WriteLine();
                    _wr.WriteLine("******************************************************************************");
                    _wr.WriteLine("{0}: Test of Altaxo project files. Folder(s) to test: {1}, SaveAndReopeningProjects: {2}", DateTime.Now, testOptions.FolderPaths, testOptions.TestSavingAndReopening);
                    _wr.WriteLine("------------------------------------------------------------------------------");
                }
            }
        public static void ShowDialogToVerifyOpeningOfDocumentsWithoutException()
        {
            if (Current.Project.IsDirty)
            {
                var e = new System.ComponentModel.CancelEventArgs();
                Current.IProjectService.AskForSavingOfProject(e);
                if (e.Cancel)
                {
                    return;
                }
            }

            var testOptions = new TestAllProjectsInFolderOptions();

            if (!Current.Gui.ShowDialog(ref testOptions, "Test Altaxo project files on your disk", false))
            {
                return;
            }

            var monitor = new Altaxo.Main.Services.ExternalDrivenBackgroundMonitor();

            Current.Gui.ShowBackgroundCancelDialog(10, () => InternalVerifyOpeningOfDocumentsWithoutExceptionStart(testOptions, monitor), monitor);
        }
        private static void InternalVerifyOpeningOfDocumentsWithoutExceptionStart(TestAllProjectsInFolderOptions testOptions, Altaxo.Main.Services.ExternalDrivenBackgroundMonitor monitor)
        {
            var reporter         = new Reporter(testOptions, Current.Console);
            var oldOutputService = Current.GetService <Services.ITextOutputService>();

            Current.RemoveService <Services.ITextOutputService>();
            Current.AddService <Services.ITextOutputService>(reporter);
            if (!object.ReferenceEquals(Current.Console, reporter))
            {
                throw new InvalidProgramException("Current console now should be the reporter! Please debug.");
            }

            var test = new TestAllProjectsInFolder(reporter, testOptions);

            Current.IProjectService.ProjectChanged += test.EhProjectChanged;

            try
            {
                test.InternalVerifyOpeningOfDocumentsWithoutException(testOptions, monitor);
            }
            catch (Exception ex)
            {
                reporter.WriteLine("Fatal error: Test has to close due to an unhandled exception. The exception details:");
                reporter.WriteLine(ex.ToString());
            }
            finally
            {
                Current.IProjectService.ProjectChanged += test.EhProjectChanged;

                reporter.WriteLine("----------------------- End of test ------------------------------------------");
                reporter.Close();

                Current.RemoveService <Services.ITextOutputService>();
                Current.AddService <Services.ITextOutputService>(oldOutputService);
            }
        }
		public object Clone()
		{
			var r = new TestAllProjectsInFolderOptions();
			r.CopyFrom(this);
			return r;
		}
		private void InternalVerifyOpeningOfDocumentsWithoutException(TestAllProjectsInFolderOptions testOptions, Altaxo.Main.Services.ExternalDrivenBackgroundMonitor monitor)
		{
			monitor.ReportProgress("Searching Altaxo project files ...", 0);
			var path = testOptions.FolderPaths;
			Current.Console.WriteLine("Begin of test. Search path(s): {0}", path);

			var filelist = GetAltaxoProjectFileNames(path);

			int numberOfProjectsTested = 0;
			int numberOfProjectsFailedToLoad = 0;

			double totalFilesToTest = filelist.Count;

			monitor.ReportProgress(string.Format("Searching done, {0} Altaxo project files found.", totalFilesToTest));

			foreach (var filename in filelist)
			{
				if (monitor.CancellationPending)
					break;

				try
				{
					System.Diagnostics.Debug.WriteLine(string.Format("Begin opening Altaxo project file {0}", filename));

					monitor.ReportProgress(string.Format(
						"Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
						"Currently opening: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

					++numberOfProjectsTested;
					Current.Gui.Execute(Current.ProjectService.OpenProject, filename, true);

					monitor.ReportProgress(string.Format(
						"Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
						"Loaded successfully: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

					System.Threading.Thread.Sleep(1000);
				}
				catch (Exception)
				{
					++numberOfProjectsFailedToLoad;
					monitor.ReportProgress(string.Format(
						"Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
						"Failed to load: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);
					Current.Console.WriteLine("Error opening file {0}", filename);
				}

				// Project is now opened from the original file

#if DEBUG && TRACEDOCUMENTNODES

				{
					GC.Collect();
					System.Threading.Thread.Sleep(500);
					bool areThereAnyProblems = false;
					areThereAnyProblems |= Main.SuspendableDocumentNodeBase.ReportNotConnectedDocumentNodes(false);
					areThereAnyProblems |= Main.SuspendableDocumentNode.ReportChildListProblems();
					areThereAnyProblems |= Main.SuspendableDocumentNode.ReportWrongChildParentRelations();

					if (areThereAnyProblems)
					{
						Current.Console.WriteLine("Above listed problems were detected after opening the file {0}", filename);
						Current.Console.WriteLine();
					}
				}
#endif

				if (testOptions.TestSavingAndReopening)
				{
					// Test saving of the project (now with the current version of Altaxo)
					string tempFileName = System.IO.Path.GetTempFileName();
					try
					{
						monitor.ReportProgress(string.Format(
							"Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
							"Currently saving: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

						Current.Gui.Execute(Current.ProjectService.SaveProject, tempFileName);

						monitor.ReportProgress(string.Format(
							"Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
							"Saved successfully: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);
					}
					catch (Exception)
					{
						++numberOfProjectsFailedToLoad;
						monitor.ReportProgress(string.Format(
							"Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
							"Failed to save: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);
						Current.Console.WriteLine("Error saving file {0}", filename);
					}

					// Close the project now
					try
					{
						Current.Gui.Execute(Current.ProjectService.CloseProject, true);
						System.Threading.Thread.Sleep(1000);
					}
					catch (Exception ex)
					{
						Current.Console.WriteLine("Error closing file (after saving) {0}; Message: {1}", filename, ex);
						Current.Console.WriteLine("Operation will be stopped here because of error on closing");
						return;
					}

					// Re-Open the project
					try
					{
						monitor.ReportProgress(string.Format(
							"Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
							"Currently re-opening: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

						Current.Gui.Execute(Current.ProjectService.OpenProject, tempFileName, true);

						monitor.ReportProgress(string.Format(
							"Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
							"Re-opened successfully: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

						System.Threading.Thread.Sleep(1000);
					}
					catch (Exception ex)
					{
						++numberOfProjectsFailedToLoad;
						monitor.ReportProgress(string.Format(
							"Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
							"Failed to re-open: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);
						Current.Console.WriteLine("Error re-opening file {0}, Message: {1}", filename, ex);
					}

#if DEBUG && TRACEDOCUMENTNODES

					{
						GC.Collect();
						System.Threading.Thread.Sleep(500);
						bool areThereAnyProblems = false;
						areThereAnyProblems |= Main.SuspendableDocumentNodeBase.ReportNotConnectedDocumentNodes(false);
						areThereAnyProblems |= Main.SuspendableDocumentNode.ReportChildListProblems();
						areThereAnyProblems |= Main.SuspendableDocumentNode.ReportWrongChildParentRelations();

						if (areThereAnyProblems)
						{
							Current.Console.WriteLine("Above listed problems were detected after saving and reopening project {0}", filename);
							Current.Console.WriteLine();
						}
					}

#endif

					// Close the project now
					try
					{
						Current.Gui.Execute(Current.ProjectService.CloseProject, true);
						System.Threading.Thread.Sleep(1000);
					}
					catch (Exception ex)
					{
						Current.Console.WriteLine("Error closing file (after re-opening it) {0}; Message: {1}", filename, ex);
						Current.Console.WriteLine("Operation will be stopped here because of error on closing");
						return;
					}

					// delete the temporary project
					try
					{
						System.IO.File.Delete(tempFileName);
					}
					catch (Exception ex)
					{
						Current.Console.WriteLine("Error deleting temporary Altaxo project file {0}, original from file {1}; Message: {2}", tempFileName, filename, ex.Message);
					}
				}
				else
				{
					try
					{
						Current.Gui.Execute(Current.ProjectService.CloseProject, true);
						System.Threading.Thread.Sleep(1000);
					}
					catch (Exception ex)
					{
						Current.Console.WriteLine("Error closing file {0}; Message: {1}", filename, ex);
						Current.Console.WriteLine("Operation will be stopped here because of error on closing");
						return;
					}
				}
			}

			Current.Console.WriteLine("End of test. {0} projects tested, {1} projects failed to load", numberOfProjectsTested, numberOfProjectsFailedToLoad);
		}
		private static void InternalVerifyOpeningOfDocumentsWithoutExceptionStart(TestAllProjectsInFolderOptions testOptions, Altaxo.Main.Services.ExternalDrivenBackgroundMonitor monitor)
		{
			var reporter = new Reporter(testOptions, Current.Console);
			var oldOutputService = Current.SetOutputService(reporter);

			var test = new TestAllProjectsInFolder(reporter, testOptions);

			Current.ProjectService.ProjectChanged += test.EhProjectChanged;

			try
			{
				test.InternalVerifyOpeningOfDocumentsWithoutException(testOptions, monitor);
			}
			catch (Exception ex)
			{
				reporter.WriteLine("Fatal error: Test has to close due to an unhandled exception. The exception details:");
				reporter.WriteLine(ex.ToString());
			}
			finally
			{
				Current.ProjectService.ProjectChanged += test.EhProjectChanged;

				reporter.WriteLine("----------------------- End of test ------------------------------------------");
				reporter.Close();
				Current.SetOutputService(oldOutputService);
			}
		}
		public static void ShowDialogToVerifyOpeningOfDocumentsWithoutException()
		{
			if (Current.Project.IsDirty)
			{
				var e = new System.ComponentModel.CancelEventArgs();
				Current.ProjectService.AskForSavingOfProject(e);
				if (e.Cancel)
					return;
			}

			var testOptions = new TestAllProjectsInFolderOptions();
			if (!Current.Gui.ShowDialog(ref testOptions, "Test Altaxo project files on your disk", false))
				return;

			var monitor = new Altaxo.Main.Services.ExternalDrivenBackgroundMonitor();
			Current.Gui.ShowBackgroundCancelDialog(10, monitor, () => InternalVerifyOpeningOfDocumentsWithoutExceptionStart(testOptions, monitor));
		}
		private TestAllProjectsInFolder(Reporter reporter, TestAllProjectsInFolderOptions testOptions)
		{
			_reporter = reporter;
			_testOptions = testOptions;
		}
        private void InternalVerifyOpeningOfDocumentsWithoutException(TestAllProjectsInFolderOptions testOptions, Altaxo.Main.Services.ExternalDrivenBackgroundMonitor monitor)
        {
            monitor.ReportProgress("Searching Altaxo project files ...", 0);
            var path = testOptions.FolderPaths;

            Current.Console.WriteLine("Begin of test. Search path(s): {0}", path);

            var filelist = GetAltaxoProjectFileNames(path);

            int numberOfProjectsTested       = 0;
            int numberOfProjectsFailedToLoad = 0;

            double totalFilesToTest = filelist.Count;

            monitor.ReportProgress(string.Format("Searching done, {0} Altaxo project files found.", totalFilesToTest));

            foreach (var filename in filelist)
            {
                if (monitor.CancellationPending)
                {
                    break;
                }

                try
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("Begin opening Altaxo project file {0}", filename));

                    monitor.ReportProgress(string.Format(
                                               "Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
                                               "Currently opening: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

                    ++numberOfProjectsTested;
                    Current.Dispatcher.InvokeIfRequired(Current.IProjectService.OpenProject, filename, true);

                    monitor.ReportProgress(string.Format(
                                               "Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
                                               "Loaded successfully: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

                    System.Threading.Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    ++numberOfProjectsFailedToLoad;
                    monitor.ReportProgress(string.Format(
                                               "Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
                                               "Failed to load: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);
                    Current.Console.WriteLine("Error opening file {0}", filename);
                }

                // Project is now opened from the original file

#if DEBUG && TRACEDOCUMENTNODES
                {
                    GC.Collect();
                    System.Threading.Thread.Sleep(500);
                    bool areThereAnyProblems = false;
                    areThereAnyProblems |= Main.SuspendableDocumentNodeBase.ReportNotConnectedDocumentNodes(false);
                    areThereAnyProblems |= Main.SuspendableDocumentNode.ReportChildListProblems();
                    areThereAnyProblems |= Main.SuspendableDocumentNode.ReportWrongChildParentRelations();

                    if (areThereAnyProblems)
                    {
                        Current.Console.WriteLine("Above listed problems were detected after opening the file {0}", filename);
                        Current.Console.WriteLine();
                    }
                }
#endif

                if (testOptions.TestSavingAndReopening)
                {
                    // Test saving of the project (now with the current version of Altaxo)
                    string tempFileName = System.IO.Path.GetTempFileName();
                    try
                    {
                        monitor.ReportProgress(string.Format(
                                                   "Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
                                                   "Currently saving: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

                        Current.Dispatcher.InvokeIfRequired(Current.IProjectService.SaveProject, tempFileName);

                        monitor.ReportProgress(string.Format(
                                                   "Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
                                                   "Saved successfully: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);
                    }
                    catch (Exception)
                    {
                        ++numberOfProjectsFailedToLoad;
                        monitor.ReportProgress(string.Format(
                                                   "Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
                                                   "Failed to save: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);
                        Current.Console.WriteLine("Error saving file {0}", filename);
                    }

                    // Close the project now
                    try
                    {
                        Current.Dispatcher.InvokeIfRequired(() => Current.IProjectService.CloseProject(true));
                        System.Threading.Thread.Sleep(1000);
                    }
                    catch (Exception ex)
                    {
                        Current.Console.WriteLine("Error closing file (after saving) {0}; Message: {1}", filename, ex);
                        Current.Console.WriteLine("Operation will be stopped here because of error on closing");
                        return;
                    }

                    // Re-Open the project
                    try
                    {
                        monitor.ReportProgress(string.Format(
                                                   "Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
                                                   "Currently re-opening: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

                        Current.Dispatcher.InvokeIfRequired(Current.IProjectService.OpenProject, tempFileName, true);

                        monitor.ReportProgress(string.Format(
                                                   "Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
                                                   "Re-opened successfully: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);

                        System.Threading.Thread.Sleep(1000);
                    }
                    catch (Exception ex)
                    {
                        ++numberOfProjectsFailedToLoad;
                        monitor.ReportProgress(string.Format(
                                                   "Successfully loaded: {0}, failed to load: {1}, total: {2}/{3} projects.\r\n" +
                                                   "Failed to re-open: {4}", numberOfProjectsTested - numberOfProjectsFailedToLoad, numberOfProjectsFailedToLoad, numberOfProjectsTested, totalFilesToTest, filename), numberOfProjectsTested / totalFilesToTest);
                        Current.Console.WriteLine("Error re-opening file {0}, Message: {1}", filename, ex);
                    }

#if DEBUG && TRACEDOCUMENTNODES
                    {
                        GC.Collect();
                        System.Threading.Thread.Sleep(500);
                        bool areThereAnyProblems = false;
                        areThereAnyProblems |= Main.SuspendableDocumentNodeBase.ReportNotConnectedDocumentNodes(false);
                        areThereAnyProblems |= Main.SuspendableDocumentNode.ReportChildListProblems();
                        areThereAnyProblems |= Main.SuspendableDocumentNode.ReportWrongChildParentRelations();

                        if (areThereAnyProblems)
                        {
                            Current.Console.WriteLine("Above listed problems were detected after saving and reopening project {0}", filename);
                            Current.Console.WriteLine();
                        }
                    }
#endif

                    // Close the project now
                    try
                    {
                        Current.Dispatcher.InvokeIfRequired(() => Current.IProjectService.CloseProject(true));
                        System.Threading.Thread.Sleep(1000);
                    }
                    catch (Exception ex)
                    {
                        Current.Console.WriteLine("Error closing file (after re-opening it) {0}; Message: {1}", filename, ex);
                        Current.Console.WriteLine("Operation will be stopped here because of error on closing");
                        return;
                    }

                    // delete the temporary project
                    try
                    {
                        System.IO.File.Delete(tempFileName);
                    }
                    catch (Exception ex)
                    {
                        Current.Console.WriteLine("Error deleting temporary Altaxo project file {0}, original from file {1}; Message: {2}", tempFileName, filename, ex.Message);
                    }
                }
                else
                {
                    try
                    {
                        Current.Dispatcher.InvokeIfRequired(() => Current.IProjectService.CloseProject(true));
                        System.Threading.Thread.Sleep(1000);
                    }
                    catch (Exception ex)
                    {
                        Current.Console.WriteLine("Error closing file {0}; Message: {1}", filename, ex);
                        Current.Console.WriteLine("Operation will be stopped here because of error on closing");
                        return;
                    }
                }
            }

            Current.Console.WriteLine("End of test. {0} projects tested, {1} projects failed to load", numberOfProjectsTested, numberOfProjectsFailedToLoad);
        }
 private TestAllProjectsInFolder(Reporter reporter, TestAllProjectsInFolderOptions testOptions)
 {
     _reporter    = reporter;
     _testOptions = testOptions;
 }