public void TestIterationAndRemoval()
        {
            DefaultDiskStorage storage     = GetStorageSupplier(1).Get();
            string             resourceId0 = "file0";
            string             resourceId1 = "file1";
            string             resourceId2 = "file2";
            string             resourceId3 = "file3";

            byte[] CONTENT0 = Encoding.UTF8.GetBytes("content0");
            byte[] CONTENT1 = Encoding.UTF8.GetBytes("content1-bigger");
            byte[] CONTENT2 = Encoding.UTF8.GetBytes("content2");
            byte[] CONTENT3 = Encoding.UTF8.GetBytes("content3-biggest");

            IList <FileSystemInfo> files = new List <FileSystemInfo>(4);

            DateTime time1 = DateTime.Now;

            files.Add(Write(storage, resourceId0, CONTENT0));

            DateTime time2 = time1.AddMilliseconds(1000);

            _clock.SetDateTime(time2);
            files.Add(Write(storage, resourceId1, CONTENT1));

            DateTime time3 = time2.AddMilliseconds(1000);

            _clock.SetDateTime(time3);
            files.Add(Write(storage, resourceId2, CONTENT2));

            DateTime time4 = time3.AddMilliseconds(1000);

            _clock.SetDateTime(time4);
            files.Add(Write(storage, resourceId3, CONTENT3));

            IList <DefaultDiskStorage.EntryImpl> entries = RetrieveEntries(storage);

            Assert.AreEqual(4, entries.Count);
            Assert.AreEqual(files[0].FullName, ((FileBinaryResource)entries[0].Resource).File.FullName);
            Assert.AreEqual(files[1].FullName, ((FileBinaryResource)entries[1].Resource).File.FullName);
            Assert.AreEqual(files[2].FullName, ((FileBinaryResource)entries[2].Resource).File.FullName);
            Assert.AreEqual(files[3].FullName, ((FileBinaryResource)entries[3].Resource).File.FullName);

            // Try the same after removing 2 entries
            foreach (IEntry entry in storage.GetEntries())
            {
                // Delete the 2 biggest files: key1 and key3 (see the content values)
                if (entry.GetSize() >= CONTENT1.Length)
                {
                    storage.Remove(entry);
                }
            }

            IList <DefaultDiskStorage.EntryImpl> entriesAfterRemoval = RetrieveEntries(storage);

            Assert.AreEqual(2, entriesAfterRemoval.Count);
            Assert.AreEqual(files[0].FullName,
                            ((FileBinaryResource)entriesAfterRemoval[0].Resource).File.FullName);
            Assert.AreEqual(files[2].FullName,
                            ((FileBinaryResource)entriesAfterRemoval[1].Resource).File.FullName);
        }
        public void TestRemoveById()
        {
            DefaultDiskStorage storage = GetStorageSupplier(1).Get();

            string resourceId1 = "resource1";

            byte[] value1 = new byte[100];
            WriteFileToStorage(storage, resourceId1, value1);
            string resourceId2 = "resource2";

            byte[] value2 = new byte[100];
            WriteFileToStorage(storage, resourceId2, value2);
            string resourceId3 = "resource3";

            byte[] value3 = new byte[100];
            WriteFileToStorage(storage, resourceId3, value3);

            Assert.IsTrue(storage.Contains(resourceId1, null));
            Assert.IsTrue(storage.Contains(resourceId2, null));
            Assert.IsTrue(storage.Contains(resourceId3, null));

            storage.Remove(resourceId2);
            Assert.IsTrue(storage.Contains(resourceId1, null));
            Assert.IsFalse(storage.Contains(resourceId2, null));
            Assert.IsTrue(storage.Contains(resourceId3, null));

            storage.Remove(resourceId1);
            Assert.IsFalse(storage.Contains(resourceId1, null));
            Assert.IsFalse(storage.Contains(resourceId2, null));
            Assert.IsTrue(storage.Contains(resourceId3, null));

            storage.Remove(resourceId3);
            Assert.IsFalse(storage.Contains(resourceId1, null));
            Assert.IsFalse(storage.Contains(resourceId2, null));
            Assert.IsFalse(storage.Contains(resourceId3, null));
        }
        public void TestRemoveWithIterator()
        {
            DefaultDiskStorage storage = GetStorageSupplier(1).Get();

            string resourceId1 = "resource1";

            byte[] value1 = new byte[100];
            value1[80] = 101;
            string resourceId2 = "resource2";

            byte[] value2 = new byte[104];
            value2[80] = 102;
            string resourceId3 = "resource3";

            byte[] value3 = new byte[106];
            value3[80] = 103;

            DateTime time1 = DateTime.Now;

            WriteFileToStorage(storage, resourceId1, value1);

            DateTime time2 = time1.AddMilliseconds(1000);

            _clock.SetDateTime(time2);
            WriteFileToStorage(storage, resourceId2, value2);

            _clock.SetDateTime(time2.AddMilliseconds(1000));
            WriteFileToStorage(storage, resourceId3, value3);

            IList <FileSystemInfo> files = FindNewFiles(_directory, new HashSet <FileSystemInfo>(), /*recurse*/ true);

            // There should be 1 file per entry
            Assert.AreEqual(3, files.Count);

            // Now delete entry2
            ICollection <IEntry> entries = storage.GetEntries();

            foreach (var item in entries)
            {
                if (Math.Abs((item.Timestamp - time2).TotalMilliseconds) < 500)
                {
                    storage.Remove(item);
                }
            }

            Assert.IsFalse(storage.Contains(resourceId2, null));
            IList <FileSystemInfo> remaining = FindNewFiles(
                _directory, new HashSet <FileSystemInfo>(), /*recurse*/ true);

            // 2 entries remain
            Assert.AreEqual(2, remaining.Count);

            // None of them with timestamp close to time2
            IList <IEntry> entries1 = new List <IEntry>(storage.GetEntries());

            Assert.AreEqual(2, entries1.Count);

            // First
            IEntry entry = entries1[0];

            Assert.IsFalse(Math.Abs((entry.Timestamp - time2).TotalMilliseconds) < 500);

            // Second
            entry = entries1[1];
            Assert.IsFalse(Math.Abs((entry.Timestamp - time2).TotalMilliseconds) < 500);
        }