public void SetUp()
        {
            _delayIntervalInSeconds = 0;
            _parsedResult = null;

            DirectoryUtility.EnsureClearFolder(FolderName);
        }
        public void DeserializeCache_FromSerializedCache_CacheIsCorrect()
        {
            //Arrange
            List<FileSystemItem> items = new List<FileSystemItem>
                                             {
                                                 new FileSystemItem("name1", "folder1"),
                                                 new FileSystemItem("C:\\")

                                             };
            DateTime dateTime = DateTime.Now;
            FileSystemCache cache = new FileSystemCache(items, dateTime);

            _serializer.SerializeCache(cache);

            //Act
            FileSystemCache actualCache = _serializer.DeserializeCache();

            //Assert
            //Serialization does not preserve milliseconds, so we use epsilon check.
            Assert.That(dateTime - actualCache.LastFullScanTime, Is.LessThan(new TimeSpan(0, 0, 1)));
            Assert.That(actualCache.Items.Count, Is.EqualTo(2));

            Assert.That(actualCache.Items[0].FullPath, Is.EqualTo(Path.GetFullPath("folder1")));
            Assert.That(actualCache.Items[0].Name, Is.EqualTo("name1"));

            Assert.That(actualCache.Items[1].FullPath, Is.EqualTo(Path.GetFullPath("C:\\")));
            Assert.That(actualCache.Items[1].Name, Is.EqualTo(""));
        }
        public void DeserializeCache_WithExistingCacheAndChanges_CacheIsCorrect()
        {
            //Arrange
            List<FileSystemItem> cacheItems = new List<FileSystemItem>
                                             {
                                                 new FileSystemItem("name1", "folder1"),
                                                 new FileSystemItem("name1", "folder2"),
                                                 new FileSystemItem("C:\\")
                                             };
            FileSystemCache cache = new FileSystemCache(cacheItems, DateTime.Now.AddDays(-2));
            _serializer.SerializeCache(cache);

            List<FileSystemChangeEventArgs> changeItems = new List<FileSystemChangeEventArgs>
                                                              {
                                                                  new FileSystemChangeEventArgs("folder1", null),
                                                                  new FileSystemChangeEventArgs(null, "folder3"),
                                                                  new FileSystemChangeEventArgs("folder2", "folder4")
                                                              };

            DateTime dateTime = DateTime.Now;
            FileSystemChanges changes = new FileSystemChanges { Changes = changeItems, LastFullScanTime = dateTime };
            _serializer.SerializeCacheChanges(changes);

            //Act
            FileSystemCache actualCache = _serializer.DeserializeCache();

            //Assert
            //Serialization does not preserve milliseconds, so we use epsilon check.
            Assert.That(dateTime - actualCache.LastFullScanTime, Is.LessThan(new TimeSpan(0, 0, 1)));
            Assert.That(actualCache.Items.Count, Is.EqualTo(3));

            Assert.That(actualCache.Items[0].FullPath, Is.EqualTo(Path.GetFullPath("C:\\")));
            Assert.That(actualCache.Items[0].Name, Is.EqualTo(""));

            Assert.That(actualCache.Items[1].FullPath, Is.EqualTo(Path.GetFullPath("folder3")));
            Assert.That(actualCache.Items[1].Name, Is.EqualTo("folder3"));

            Assert.That(actualCache.Items[2].FullPath, Is.EqualTo(Path.GetFullPath("folder4")));
            Assert.That(actualCache.Items[2].Name, Is.EqualTo("folder4"));
        }
 private void ParseFileSystem()
 {
     //Don't set any restrictions on this parsing, as want to grab the entire system.
     List<FileSystemItem> fileSystemItems = _fileSystemParser.GetSubFolders();
     _fileSystem = new FileSystemCache(fileSystemItems, DateTime.Now);
 }
        private List<FileSystemItem> ReadCache(bool appRunOnStartup, out bool fullCacheUpToDate, out bool cacheFolderCreated, out DateTime lastFullScanTime)
        {
            cacheFolderCreated = false;

            //Parse file system
            FileSystemCache fullCache = _cacheSerializer.DeserializeCache();
            if (fullCache != null)
            {
                //The cache file can be up to date only if the current Navigation Assistant has been run on startup
                //and if it had been closed just on system shutdown and the current parser is created at application start.
                //In this case no additional folders can be created during NavAssistant being inactive.
                fullCacheUpToDate = CacheUpToDate(fullCache) && appRunOnStartup;
            }
            else
            {
                //The application is loaded for the first time (no cache stored on disk).
                string cacheFolder = GetCacheFolder();
                bool cacheFolderExisted = Directory.Exists(cacheFolder);

                //Run this method in the main thread, thus freezing it.
                //Don't set any restrictions on this parsing, as want to grab the entire system.
                fullCache = new FileSystemCache(_fileSystemParser.GetSubFolders(), DateTime.Now);
                _cacheSerializer.SerializeCache(fullCache);

                //Updating the cache if cache folder has been created
                bool cacheFolderExists = Directory.Exists(cacheFolder);
                cacheFolderCreated = !cacheFolderExisted && cacheFolderExists;

                fullCacheUpToDate = true;
            }

            List<FileSystemItem> filteredCache = FilterCache(fullCache);
            lastFullScanTime = fullCache.LastFullScanTime;
            fullCache = null;
            GC.Collect();

            return filteredCache;
        }
 private List<FileSystemItem> FilterCache(FileSystemCache fullCache)
 {
     return (fullCache != null && fullCache.Items != null)
                ? _fileSystemFilter.FilterItems(fullCache.Items)
                : new List<FileSystemItem>();
 }
        private bool CacheUpToDate(FileSystemCache cache)
        {
            DateTime lastShutDownTime = _registryService.GetLastSystemShutDownTime();
            DateTime lastCacheWriteTime = cache.LastFullScanTime;

            TimeSpan timeDifference = lastShutDownTime - lastCacheWriteTime;
            bool cacheValid = timeDifference.TotalSeconds < CacheValidityPeriodInSeconds;
            return cacheValid;
        }
        private FileSystemCache ReadCache()
        {
            string[] lines = File.ReadAllLines(_cacheFilePath);

            List<FileSystemItem> items = lines.Skip(1).Select(ParseCacheItem).Where(i => i != null).ToList();
            DateTime lastFullScanTime = DateTime.Parse(lines[0], CultureInfo.InvariantCulture);
            FileSystemCache cache = new FileSystemCache(items, lastFullScanTime);

            return cache;
        }
        public void SerializeCache(FileSystemCache cache)
        {
            if (cache == null || cache.Items == null)
            {
                throw new ArgumentNullException("cache");
            }

            //File system path can not contain ?, so this format is not ambiguous
            List<string> lines = cache.Items.Select(GetCacheItemLine).ToList();
            lines.Insert(0, cache.LastFullScanTime.ToString(CultureInfo.InvariantCulture));

            lock (CacheSync)
            {
                DirectoryUtility.EnsureFolder(Path.GetDirectoryName(_cacheFilePath));
                File.WriteAllLines(_cacheFilePath, lines.ToArray());

                if (File.Exists(_cacheChangesFilePath))
                {
                    File.Delete(_cacheChangesFilePath);
                }
            }

            GC.Collect();
        }
 private void HandleParsingFinished(object sender, ItemEventArgs<FileSystemCache> e)
 {
     lock (_parsingSync)
     {
         _parsedResult = e.Item;
         _parsingFinish = DateTime.Now;
     }
 }