Пример #1
0
 private void TestValidateMainSettings(MainSettings mainSettings, string expectedError)
 {
     try
     {
         mainSettings.ValidateSettings();
         Assert.Fail("Should have failed to validate main settings");
     }
     catch (ArgumentException e)
     {
         Assert.AreEqual(e.Message, expectedError);
     }
 }
        private void TestGetExistingFilesForInstrument(string testDir, string instrument)
        {
            var folderToWatch = CreateDirectory(testDir, instrument);

            // Create a .sky files
            var skyFile = CreateFile(folderToWatch, "test.sky");

            List <string> dataFiles;

            SetupTestFolder(folderToWatch, instrument, out dataFiles);

            var          watcher = new AutoQCFileSystemWatcher(new TestLogger(), new TestConfigRunner());
            AutoQcConfig config  = new AutoQcConfig();

            var mainSettings = MainSettings.GetDefault();

            config.MainSettings = mainSettings;

            var defaultFileFilter = FileFilter.GetFileFilter(AllFileFilter.NAME, string.Empty);

            Assert.AreEqual(mainSettings.QcFileFilter, defaultFileFilter);
            mainSettings = new MainSettings(skyFile, folderToWatch, false, defaultFileFilter, true,
                                            MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);

            config.MainSettings = mainSettings;
            mainSettings.ValidateSettings();

            watcher.Init(config);
            var files = watcher.GetExistingFiles();

            Assert.AreEqual(1, files.Count);
            Assert.AreEqual(dataFiles[0], files[0]);

            // mainSettings.IncludeSubfolders = true;
            mainSettings = new MainSettings(skyFile, folderToWatch, true, defaultFileFilter, true,
                                            MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);
            config.MainSettings = mainSettings;
            mainSettings.ValidateSettings();

            watcher.Init(config);
            files = watcher.GetExistingFiles();
            Assert.AreEqual(5, files.Count);
            Assert.IsTrue(files.Contains(dataFiles[0]));
            Assert.IsTrue(files.Contains(dataFiles[1]));
            Assert.IsTrue(files.Contains(dataFiles[2]));
            Assert.IsTrue(files.Contains(dataFiles[3]));
            Assert.IsTrue(files.Contains(dataFiles[4]));

            /* Files:
             * "root_QC_"
             * "QC_one"
             * "one_1_a_QC_"
             * "one_1_b_QC"
             * "two_qc_"
             */
            var fileFilter = FileFilter.GetFileFilter(ContainsFilter.NAME, "QC");

            // mainSettings.QcFileFilter = FileFilter.GetFileFilter(ContainsFilter.NAME, "QC");
            mainSettings = new MainSettings(skyFile, folderToWatch, true, fileFilter, true,
                                            MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);
            config.MainSettings = mainSettings;
            watcher.Init(config);
            files = watcher.GetExistingFiles();
            Assert.AreEqual(4, files.Count);
            Assert.IsTrue(files.Contains(dataFiles[0]));
            Assert.IsTrue(files.Contains(dataFiles[1]));
            Assert.IsTrue(files.Contains(dataFiles[2]));
            Assert.IsTrue(files.Contains(dataFiles[3]));

            fileFilter = FileFilter.GetFileFilter(StartsWithFilter.NAME, "QC_");
            // mainSettings.QcFileFilter = FileFilter.GetFileFilter(StartsWithFilter.NAME, "QC_");
            mainSettings = new MainSettings(skyFile, folderToWatch, true, fileFilter, true,
                                            MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);
            config.MainSettings = mainSettings;
            watcher.Init(config);
            files = watcher.GetExistingFiles();
            Assert.AreEqual(1, files.Count);
            Assert.IsTrue(files.Contains(dataFiles[1]));

            fileFilter = FileFilter.GetFileFilter(EndsWithFilter.NAME, "_QC_");
            // mainSettings.QcFileFilter = FileFilter.GetFileFilter(EndsWithFilter.NAME, "_QC_");
            mainSettings = new MainSettings(skyFile, folderToWatch, true, fileFilter, true,
                                            MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);
            config.MainSettings = mainSettings;
            watcher.Init(config);
            files = watcher.GetExistingFiles();
            Assert.AreEqual(2, files.Count);
            Assert.IsTrue(files.Contains(dataFiles[0]));
            Assert.IsTrue(files.Contains(dataFiles[2]));

            fileFilter = FileFilter.GetFileFilter(RegexFilter.NAME, "[ab]_QC");
            // mainSettings.QcFileFilter = FileFilter.GetFileFilter(RegexFilter.NAME, "[ab]_QC");
            mainSettings = new MainSettings(skyFile, folderToWatch, true, fileFilter, true,
                                            MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);
            config.MainSettings = mainSettings;
            watcher.Init(config);
            files = watcher.GetExistingFiles();
            Assert.AreEqual(2, files.Count);
            Assert.IsTrue(files.Contains(dataFiles[2]));
            Assert.IsTrue(files.Contains(dataFiles[3]));
        }
        private static void TestGetNewFilesForInstrument(string testDir, string instrument)
        {
            var           logger       = new TestLogger();
            IConfigRunner configRunner = new TestConfigRunner();

            configRunner.ChangeStatus(ConfigRunner.RunnerStatus.Running);


            // folder to watch
            var folderToWatch = CreateDirectory(testDir, instrument);
            // Create a .sky files
            var skyFile = CreateFile(folderToWatch, "test2_a.sky");

            var config = new AutoQcConfig();

            // 1. Look for files in folderToWatchOnly
            var watcher = new AutoQCFileSystemWatcher(logger, configRunner);

            var defaultFileFilter = FileFilter.GetFileFilter(AllFileFilter.NAME, string.Empty);
            var mainSettings      = new MainSettings(skyFile, folderToWatch, false, defaultFileFilter, true,
                                                     MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);

            config.MainSettings = mainSettings;
            mainSettings.ValidateSettings();
            watcher.Init(config);
            watcher.StartWatching();                              // Start watching
            Assert.AreEqual(0, watcher.GetExistingFiles().Count); // No existing files

            // Create new files in the folder
            List <string> dataFiles;

            SetupTestFolder(folderToWatch, instrument, out dataFiles);

            // Only one file should have been added to the queue since we are not monitoring sub-folders
            Assert.AreEqual(1, watcher.GetExistingFiles().Count);
            var    files = new List <string>();
            string f;

            while ((f = watcher.GetFile()) != null)
            {
                files.Add(f);
            }
            Assert.AreEqual(1, files.Count);
            Assert.IsTrue(files.Contains(dataFiles[0]));
            Assert.IsNull(watcher.GetFile()); // Nothing in the queue

            watcher.Stop();

            // 2. Look for files in subfolders
            watcher = new AutoQCFileSystemWatcher(logger, configRunner);
            // folder to watch
            folderToWatch = CreateDirectory(testDir, instrument + "_2");
            // Create a .sky files
            skyFile = CreateFile(folderToWatch, "test2_b.sky");
            // mainSettings.IncludeSubfolders = true; // watch sub-folders
            mainSettings = new MainSettings(skyFile, folderToWatch, true, defaultFileFilter, true,
                                            MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);

            config.MainSettings = mainSettings;
            mainSettings.ValidateSettings();
            watcher.Init(config);

            watcher.StartWatching();                              // Start watching
            Assert.AreEqual(0, watcher.GetExistingFiles().Count); // No existing files

            dataFiles.Clear();
            SetupTestFolder(folderToWatch, instrument, out dataFiles); // Create new files in the folder

            files = new List <string>();
            while ((f = watcher.GetFile()) != null)
            {
                files.Add(f);
            }
            Assert.AreEqual(5, files.Count);
            Assert.IsTrue(files.Contains(dataFiles[0]));
            Assert.IsTrue(files.Contains(dataFiles[1]));
            Assert.IsTrue(files.Contains(dataFiles[2]));
            Assert.IsTrue(files.Contains(dataFiles[3]));
            Assert.IsTrue(files.Contains(dataFiles[4]));

            watcher.Stop();

            //  3. Look for files in subfolders matching a pattern
            watcher = new AutoQCFileSystemWatcher(logger, configRunner);
            // folder to watch
            folderToWatch = CreateDirectory(testDir, instrument + "_3");
            // Create a .sky files
            skyFile = CreateFile(folderToWatch, "test2_c.sky");
            // mainSettings.IncludeSubfolders = true; // watch sub-folders
            var fileFilter = FileFilter.GetFileFilter(ContainsFilter.NAME, "_QC_"); // file name pattern

            mainSettings = new MainSettings(skyFile, folderToWatch, true, fileFilter, true,
                                            MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);

            config.MainSettings = mainSettings;
            mainSettings.ValidateSettings();
            watcher.Init(config);

            watcher.StartWatching();                              // Start watching
            Assert.AreEqual(0, watcher.GetExistingFiles().Count); // No existing files

            dataFiles.Clear();
            SetupTestFolder(folderToWatch, instrument, out dataFiles); // Create new files in the folder

            files = new List <string>();
            while ((f = watcher.GetFile()) != null)
            {
                files.Add(f);
            }
            Assert.AreEqual(2, files.Count);
            Assert.IsTrue(files.Contains(dataFiles[0]));
            Assert.IsTrue(files.Contains(dataFiles[2]));

            watcher.Stop();

            // 4. Add new files in directory by first creating a temp file/directory and renaming it.
            // This should trigger a "Renamed" event for the FileSystemWatcher
            watcher = new AutoQCFileSystemWatcher(logger, configRunner);
            // folder to watch
            folderToWatch = CreateDirectory(testDir, instrument + "_4");
            // Create a .sky files
            skyFile      = CreateFile(folderToWatch, "test2_d.sky");
            mainSettings = new MainSettings(skyFile, folderToWatch, false, defaultFileFilter, true,
                                            MainSettings.ACCUM_TIME_WINDOW, instrument, MainSettings.ACQUISITION_TIME);

            config.MainSettings = mainSettings;
            mainSettings.ValidateSettings();
            watcher.Init(config);

            watcher.StartWatching();                              // Start watching
            Assert.AreEqual(0, watcher.GetExistingFiles().Count); // No existing files

            dataFiles.Clear();
            SetupTestFolder(folderToWatch, instrument, out dataFiles,
                            true); // Create temp file first and rename it

            watcher.CheckDrive();  // Otherwise UNC tests might fail on //net-lab3

            files = new List <string>();
            while ((f = watcher.GetFile()) != null)
            {
                files.Add(f);
            }
            Assert.AreEqual(1, files.Count);
            Assert.IsTrue(files.Contains(dataFiles[0]));

            watcher.Stop();
        }