Exemple #1
0
        public void DeleteDirectory()
        {
            var tempPath    = Path.Combine(Path.GetTempPath(), "NConfiguration.Tests");
            var dirName     = Guid.NewGuid().ToString();
            var watchedFile = Path.Combine(tempPath, dirName, "test.xml");

            Directory.CreateDirectory(Path.Combine(tempPath, dirName));

            File.WriteAllText(watchedFile, _xmlCfg);

            var fileInfo    = ReadedFileInfo.Create(watchedFile, _ => _.CopyTo(Stream.Null));
            var fileChecker = FileChecker.TryCreate(fileInfo, WatchMode.System, TimeSpan.FromMilliseconds(100), CheckMode.All);

            Thread.Sleep(50);

            var wait = new ManualResetEvent(false);

            fileChecker.Changed += (a, e) =>
            {
                wait.Set();
            };

            Directory.Delete(Path.Combine(tempPath, dirName), true);
            Directory.CreateDirectory(Path.Combine(tempPath, dirName));             //no lock directory

            Assert.IsTrue(wait.WaitOne(10000), "no event");
        }
Exemple #2
0
        public void IncludedChange()
        {
            string cfgMainFile       = Path.GetTempFileName();
            string cfgAdditionalFile = Path.GetTempFileName();

            File.WriteAllText(cfgAdditionalFile, _xmlCfgAutoOrigin);
            File.WriteAllText(cfgMainFile, string.Format(_xmlCfgMain, cfgAdditionalFile));

            var loader = new SettingsLoader();

            loader.XmlFileBySection();
            var loadResult = loader.LoadSettings(new XmlFileSettings(cfgMainFile));

            var wait         = new ManualResetEvent(false);
            var fileCheckers = FileChecker.TryCreate(loadResult.Sources).ToArray();

            new FirstChange(fileCheckers).Changed += (s, e) =>
            {
                wait.Set();
                foreach (var fileChecker in fileCheckers)
                {
                    fileChecker.Dispose();
                }
            };

            var t = Task.Factory.StartNew(() =>
            {
                File.WriteAllText(cfgAdditionalFile, _xmlCfgAutoModify);
            }, TaskCreationOptions.LongRunning);

            Task.WaitAll(t);

            Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed");
        }
Exemple #3
0
        public void AutoChange()
        {
            string cfgFile = Path.GetTempFileName();

            File.WriteAllText(cfgFile, _xmlCfgAutoOrigin);

            var fileSettings = new XmlFileSettings(cfgFile);

            var wait = new ManualResetEvent(false);

            FileChecker.TryCreate(fileSettings).Changed += (a, e) =>
            {
                wait.Set();
            };

            var t = Task.Factory.StartNew(() =>
            {
                File.WriteAllText(cfgFile, _xmlCfgAutoModify);
            }, TaskCreationOptions.LongRunning);

            Task.WaitAll(t);

            Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed");

            var appSettings = new XmlFileSettings(cfgFile).ToAppSettings();

            Assert.That(appSettings.First <ExampleCombineConfig>("AdditionalConfig").F, Is.EqualTo("Modify"));
        }
Exemple #4
0
        public IChangeable createChecker(ReadedFileInfo fileInfo, CheckMode checkMode)
        {
            if (_fromConfig)
            {
                string modeText;
                if (_monitorType == "Periodic")
                {
                    modeText = "Time";
                }
                else if (_monitorType == "Watched")
                {
                    modeText = "System";
                }
                else
                {
                    throw new NotImplementedException();
                }

                string checkText = "None";
                if (checkMode.HasFlag(CheckMode.Hash))
                {
                    checkText = "Hash";
                }
                if (checkMode.HasFlag(CheckMode.Attr))
                {
                    if (checkText == "None")
                    {
                        checkText = "Attr";
                    }
                    else
                    {
                        checkText += ",Attr";
                    }
                }

                var settings = string.Format(_xmlFormat, modeText, checkText).ToXmlSettings();

                return(FileChecker.TryCreate(settings, fileInfo));
            }
            else
            {
                if (_monitorType == "Periodic")
                {
                    return(new PeriodicFileChecker(fileInfo, TimeSpan.FromMilliseconds(100), checkMode));
                }

                if (_monitorType == "Watched")
                {
                    return(new WatchedFileChecker(fileInfo, TimeSpan.FromMilliseconds(100), checkMode));
                }
            }

            throw new NotImplementedException();
        }
        public void None(int?ms)
        {
            TimeSpan?ts = null;

            if (ms != null)
            {
                ts = TimeSpan.FromMilliseconds(ms.Value);
            }

            string file     = Path.GetTempFileName();
            var    fileInfo = ReadedFileInfo.Create(file, _ => _.CopyTo(Stream.Null));

            Assert.IsNull(FileChecker.TryCreate(fileInfo, WatchMode.None, ts, CheckMode.All));
        }
        public void FileChange(WatchMode mode, int?ms)
        {
            TimeSpan?ts = null;

            if (ms != null)
            {
                ts = TimeSpan.FromMilliseconds(ms.Value);
            }

            string file = Path.GetTempFileName();

            File.WriteAllBytes(file, new byte[] { 1, 2, 3 });

            var fileInfo = ReadedFileInfo.Create(file, _ => _.CopyTo(Stream.Null));

            var m = FileChecker.TryCreate(fileInfo, mode, ts, CheckMode.All);

            var wait = new ManualResetEvent(false);

            m.Changed += (a, e) =>
            {
                wait.Set();
            };

            var t = Task.Factory.StartNew(() =>
            {
                Thread.Sleep(1000);
                using (var fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    fs.Position = 2;
                    fs.WriteByte(1);
                    fs.Close();
                };
            }, TaskCreationOptions.LongRunning);

            Task.WaitAll(t);

            Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed");

            wait.Reset();

            m.Changed += (a, e) =>
            {
                wait.Set();
            };

            Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed");
        }
        public void DeletedFile()
        {
            string file = Path.GetTempFileName();

            File.WriteAllBytes(file, new byte[] { 1, 2, 3 });

            var fileInfo = ReadedFileInfo.Create(file, _ => _.CopyTo(Stream.Null));

            File.Delete(file);

            var m = FileChecker.TryCreate(fileInfo, WatchMode.System, null, CheckMode.All);

            var wait = new ManualResetEvent(false);

            m.Changed += (a, e) =>
            {
                wait.Set();
            };

            Assert.IsTrue(wait.WaitOne(10000), "10 sec elapsed");
        }
        public void WrongTimeArguments_OutOfRange()
        {
            var fileInfo = ReadedFileInfo.Create(Path.GetTempFileName(), _ => _.CopyTo(Stream.Null));

            Assert.Throws <ArgumentOutOfRangeException>(() => FileChecker.TryCreate(fileInfo, WatchMode.Time, TimeSpan.FromMilliseconds(1), CheckMode.All));
        }
        public void WrongTimeArguments_Null()
        {
            var fileInfo = ReadedFileInfo.Create(Path.GetTempFileName(), _ => _.CopyTo(Stream.Null));

            Assert.Throws <ArgumentNullException>(() => FileChecker.TryCreate(fileInfo, WatchMode.Time, null, CheckMode.All));
        }