예제 #1
0
        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(""));
        }
예제 #2
0
        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);
        }