public void DetachAddSubfolderTest()
        {
            string subFolderName = "DetachSubfolderTest";

            int initialSubfoldersCount = FixtureRootRepoFolder.SubFolders.Count;

            IFolder subfolder1 = (IFolder)FixtureRootRepoFolder.CreateSubfolder(subFolderName);

            Assert.AreEqual(initialSubfoldersCount + 1, FixtureRootRepoFolder.SubFolders.Count
                            , "Subfolders count not incremented by creating new subfolder");

            string fullPath = subfolder1.FullPath;

            Assert.IsNotNull(FixtureRootRepoFolder.GetSubFolder(subFolderName), "Making sure new folder is found by parent");

            bool detached = FixtureRootRepoFolder.RemoveFromChildList(subfolder1, true);

            Assert.IsTrue(detached, "Detach real subfolder returned failure");
            detached = FixtureRootRepoFolder.RemoveFromChildList(subfolder1, true);
            Assert.IsFalse(detached, "Detach already detached subfolder returned success");

            Assert.IsTrue(subfolder1.IsDetached);
            Assert.IsNull(subfolder1.ParentFolder);

            Assert.IsNull(FixtureRootRepoFolder.GetSubFolder(subFolderName), "Detached subfolder is still found by parent");

            FixtureRootRepoFolder.AddToChildList(subfolder1, true);

            Assert.IsNotNull(FixtureRootRepoFolder.GetSubFolder(subFolderName), "Re-added after detach still not found by parent");
        }
        public void AddToReaderTest()
        {
            string name    = "AddToReaderTest";
            string subName = "SubName";

            IRepositoryFolder target    = FixtureRootRepoFolder.CreateSubfolder(name);
            IRepositoryFolder subFolder = target.CreateSubfolder(subName);

            //-----------------------------------

            Mock.RepositoryReaderMock mockedReader = new Mock.RepositoryReaderMock();
            target.AddToReader(mockedReader, false);
            Assert.AreEqual(1, mockedReader.AddFolderCalled);
            Assert.AreSame(mockedReader.LastFolderArgument, target);

            mockedReader.Reset();

            target.AddToReader(mockedReader, true);
            Assert.AreEqual(2, mockedReader.AddFolderCalled);
            Assert.IsTrue(mockedReader.FolderArguments.Contains(target));
            Assert.IsTrue(mockedReader.FolderArguments.Contains(subFolder));

            //------------------------------------
            //now real reader

            IRepositoryReader reader = Repository.RootFolder.GetReader(DateTime.Now.AddDays(-1), false);

            Assert.AreEqual(1, reader.Folders.Count, "Reader just created from a folder");

            target.AddToReader(reader, false);

            Assert.AreEqual(2, reader.Folders.Count, "Another reader added");
        }
Example #3
0
        public void DisposedStateTrackingTest()
        {
            const string folderName         = "DisposedStateTrackingTest";
            var          originalDescendant = FixtureRootRepoFolder.CreateSubfolder(folderName);

            using (var repo = GetStandaloneRepository())
            {
                var descendant = repo.RootFolder.GetDescendant(originalDescendant.LogicalPath, false);
                Assert.IsNotNull(descendant);
                Assert.IsTrue(descendant.Exists);

                repo.Dispose();

                Assert.Throws <ObjectDisposedException>(() => repo.IsDataBeingAccessed(folder: descendant, subtree: true));
                Assert.Throws <ObjectDisposedException>(() => repo.RegisterReader(reader: null));
                Assert.Throws <ObjectDisposedException>(() => repo.RegisterWriter(writer: null));
                Assert.Throws <ObjectDisposedException>(() => repo.UnRegisterReader(reader: null));
                Assert.Throws <ObjectDisposedException>(() => repo.UnRegisterWriter(writer: null));

                Assert.Throws <ObjectDisposedException>(() => repo.RootFolder.Refresh());
                Assert.Throws <ObjectDisposedException>(() => descendant.Refresh());

                Assert.Throws <ObjectDisposedException>(() => repo.ObjectFactory.CreateNewFile((IFolder)descendant));
            }
        }
        public void CreateSubfolderTest()
        {
            string subFolderName = "CreateSubfolderTest";

            int initialSubfoldersCount = FixtureRootRepoFolder.SubFolders.Count;

            IRepositoryFolder subfolder1 = FixtureRootRepoFolder.CreateSubfolder(subFolderName);
            IRepositoryFolder rootFoder  = Repository.RootFolder;

            string fullPath = subfolder1.FullPath;

            Assert.IsTrue(((IFolder)subfolder1).Exists);
            Assert.IsTrue(Directory.Exists(fullPath));
            Assert.AreEqual(initialSubfoldersCount + 1, FixtureRootRepoFolder.SubFolders.Count());

            string customPropertyName  = "CustomProperty1";
            string customPropertyValue = "CustomPropertyValue1";

            subfolder1.Properties.SetCustomProperty(customPropertyName, "CustomPropertyValue1");
            subfolder1.Properties.Load();
            Assert.AreEqual(customPropertyValue, subfolder1.Properties.GetCustomProperty(customPropertyName)
                            , "Custom property value not persisted");

            IRepositoryWriter writer = subfolder1.GetWriter();

            writer.AllowSubfoldersCreation = true;

            Populate(writer, 100);

            writer.Close();

            subfolder1.Delete(true, true);
            Assert.IsFalse(Directory.Exists(fullPath), "Directory not removed from disk");
            Assert.AreEqual(initialSubfoldersCount, FixtureRootRepoFolder.SubFolders.Count);
        }
Example #5
0
        public void RegistrationTest()
        {
            using (var reader = FixtureRootRepoFolder.GetReader(DateTime.MinValue, false))
            {
                Assert.IsTrue(reader.IsAccessing(FixtureRootRepoFolder, false));
                Assert.IsTrue(Repository.IsDataBeingAccessed(FixtureRootRepoFolder, false));
                Assert.IsTrue(Repository.IsDataBeingReadFrom(FixtureRootRepoFolder, false));
                var readers = Repository.GetReaders(FixtureRootRepoFolder, false);
                Assert.AreEqual(1, readers.Count);
                Assert.AreSame(reader, readers[0]);

                reader.Close();

                Assert.IsFalse(Repository.IsDataBeingAccessed(FixtureRootRepoFolder, false));
                Assert.IsFalse(Repository.IsDataBeingReadFrom(FixtureRootRepoFolder, false));
                Assert.IsFalse(reader.IsAccessing(FixtureRootRepoFolder, false));
                readers = Repository.GetReaders(FixtureRootRepoFolder, false);
                Assert.AreEqual(0, readers.Count);

                reader.Dispose();

                Assert.IsFalse(Repository.UnRegisterReader(reader), "Dispose did not unregister reader");

                Assert.Throws <ObjectDisposedException>(() => reader.AddFolder(FixtureRootRepoFolder));
                Assert.Throws <ObjectDisposedException>(() => reader.Seek(DateTime.Now));
                Assert.Throws <ObjectDisposedException>(() => reader.Direction = bfs.Repository.Util.EnumerationDirection.Backwards);
                Assert.Throws <ObjectDisposedException>(() => reader.CanChangeDirection.ToString());
                Assert.Throws <ObjectDisposedException>(() => reader.Read());
                Assert.Throws <ObjectDisposedException>(() => reader.RemoveFolder(null));

                Assert.IsFalse(reader.IsAccessing(FixtureRootRepoFolder, true));
            }
        }
        public void MyClassInitialize()
        {
            IRepositoryWriter writer = FixtureRootRepoFolder.GetWriter();

            writer.AllowSubfoldersCreation = true;

            Populate(writer, 1000);
            writer.Flush();
            writer.Close();
        }
Example #7
0
        public void ReadEmptyFolderTest()
        {
            string            targetFolderName = "ReadEmptyFolderTest";
            IRepositoryFolder targetFolder     = FixtureRootRepoFolder.CreateSubfolder(targetFolderName);

            using (IRepositoryReader target = targetFolder.GetReader(DateTime.MinValue, true))
            {
                Assert.IsFalse(target.HasData);
            }
        }
        public void GetUnsavedItemsWithFlushTest()
        {
            const int subfolderCount = 3;

            string subFolderName = "GetUnsavedItemsTest";

            IRepositoryFolder targetFolder = FixtureRootRepoFolder.CreateSubfolder(subFolderName);

            IRepositoryWriter writer = targetFolder.GetWriter();

            IDataRouter dataRouter = new Mock.NumberedDataRouter(subfolderCount);

            writer.DataRouter = dataRouter;
            writer.AllowSubfoldersCreation = true;

            string fullPath = targetFolder.FullPath;

            Mock.TestDataItem item;

            int lastFlushCount = 0;

            for (int n = 0; n < 10000; ++n)
            {
                item = Mock.TestDataItem.GetTestItem(n);
                writer.Write(item);

                if ((n + 1) % 10 == 0)
                {
                    IDictionary <string, IList <IDataItem> > unsavedItems = writer.GetUnsavedItems();
                    Assert.IsNotNull(unsavedItems);
                    Assert.AreEqual(Math.Min(n + 1, subfolderCount), unsavedItems.Count, "Unsaved items dictionary entry count is not equal to the direct writers count");

                    Assert.AreEqual(n + 1 - lastFlushCount, unsavedItems.Values.Sum((l) => l.Count), "Total number of unsaved items incorrect");
                }
                else if ((n + 1) % 134 == 0)
                {
                    writer.Flush();
                    lastFlushCount = n + 1;

                    IDictionary <string, IList <IDataItem> > unsavedItems = writer.GetUnsavedItems();
                    Assert.IsNotNull(unsavedItems);
                    Assert.AreEqual(Math.Min(n + 1, subfolderCount), unsavedItems.Count, "Unsaved items dictionary entry count is not equal to the direct writers count");

                    Assert.AreEqual(0, unsavedItems.Values.Sum((l) => l.Count), "Total number of unsaved items after flush must be 0");
                }
            }

            writer.Close();
        }
        public void WriteTest()
        {
            string subFolderName = "WriteTest";

            int initialSubfoldersCount = Repository.RootFolder.SubFolders.Count;

            IRepositoryFolder targetFolder = FixtureRootRepoFolder.CreateSubfolder(subFolderName);

            IRepositoryWriter writer = targetFolder.GetWriter();

            writer.AllowSubfoldersCreation = true;

            string fullPath = targetFolder.FullPath;

            Mock.TestDataItem item;

            for (int n = 0; n < 100; ++n)
            {
                item = Mock.TestDataItem.GetTestItem(n);
                writer.Write(item);
            }

            // testing automatic subfolder creation
            item = Mock.TestDataItem.GetTestItem(1000);

            item.RelativePath = "AutoSubfolder1/Auto2";

            writer.Write(item);

            Assert.AreEqual(1, targetFolder.SubFolders.Count, "Automatic subfolder creation during write failed");
            Assert.IsNotNull(targetFolder.GetSubFolder("AutoSubfolder1"));
            Assert.AreEqual(1, targetFolder.GetSubFolder("AutoSubfolder1").SubFolders.Count);
            Assert.IsNotNull(targetFolder.GetSubFolder("AutoSubfolder1").GetSubFolder("Auto2"));

            writer.Flush();
            writer.Close();

            targetFolder.Delete(true, true);
            Assert.IsFalse(Directory.Exists(fullPath), "Directory not removed from disk");
            Assert.AreEqual(initialSubfoldersCount, Repository.RootFolder.SubFolders.Count);
        }
Example #10
0
        public void TestRegisterUnregisterAccessors()
        {
            Assert.Throws <ArgumentNullException>(() => Repository.RegisterReader(null));
            Assert.Throws <ArgumentNullException>(() => Repository.RegisterWriter(null));

            using (var writer = FixtureRootRepoFolder.GetWriter())
            {
                Assert.IsFalse(Repository.UnRegisterReader(writer), "Writer reported to have been successfully unregistered as reader");

                // must have been registered
                Assert.IsTrue(Repository.UnRegisterWriter(writer));
                Assert.IsFalse(Repository.UnRegisterWriter(writer), "Unregistered writer reported to have been successfully unregistered again");
            }

            using (var reader = FixtureRootRepoFolder.GetReader(DateTime.Now, false))
            {
                Assert.IsFalse(Repository.UnRegisterWriter(reader), "Reader reported to have been successfully unregistered as writer");

                // must have been registered
                Assert.IsTrue(Repository.UnRegisterReader(reader: reader));
                Assert.IsFalse(Repository.UnRegisterReader(reader: reader), "Unregistered reader reported to have been successfully unregistered again");
            }
        }
        public void CoderEncryptorConfigTest()
        {
            const string topFolderName = "CoderEncryptorConfigTest";

            IFolder topFolder = (IFolder)FixtureRootRepoFolder.GetSubFolder(topFolderName);

            if (topFolder != null)
            {
                topFolder.Delete(true, true);
            }

            topFolder = (IFolder)FixtureRootRepoFolder.CreateSubfolder(topFolderName);

            var targetFolder = topFolder.GetDescendant("Intermediate/Target", true);

            Assert.AreEqual(string.Empty, targetFolder.Properties.Encryptor);
            Assert.AreEqual(string.Empty, targetFolder.Properties.Compressor);

            const string coderKey = "my-coder";
            const string encKey   = "my-encryptor";

            using (var repo1 = GetStandaloneRepository())
            {
                var topFolderInner = repo1.RootFolder.GetDescendant(topFolder.LogicalPath, false);
                Assume.That(null != topFolderInner);

                Assert.Throws <ArgumentException>(() => topFolderInner.Properties.Compressor = coderKey);
                Assert.Throws <ArgumentException>(() => topFolderInner.Properties.Encryptor  = encKey);

                repo1.ObjectFactory.AddCompressor(new CoderMock(coderKey), false);
                repo1.ObjectFactory.AddEncryptor(new CoderMock(encKey), false);

                topFolderInner.Properties.Compressor       = coderKey;
                topFolderInner.Properties.Encryptor        = encKey;
                topFolderInner.Properties.EnableEncryption = true;

                Assert.AreEqual(coderKey, topFolderInner.Properties.Compressor);
                Assert.AreEqual(encKey, topFolderInner.Properties.Encryptor);

                var targetFolderInner = repo1.RootFolder.GetDescendant(targetFolder.LogicalPath, false);
                Assume.That(null != targetFolderInner);

                Assert.AreEqual(coderKey, targetFolderInner.Properties.Compressor);
                Assert.AreEqual(encKey, targetFolderInner.Properties.Encryptor);
            }

            using (var repo1 = GetStandaloneRepository())
            {
                var topFolderInner = repo1.RootFolder.GetDescendant(topFolder.LogicalPath, false);
                Assume.That(null != topFolderInner);

                repo1.ObjectFactory.AddCompressor(new CoderMock(coderKey), false);
                repo1.ObjectFactory.AddEncryptor(new CoderMock(encKey), false);

                var targetFolderInner = repo1.RootFolder.GetDescendant(targetFolder.LogicalPath, false);
                Assume.That(null != targetFolderInner);

                Assert.AreEqual(coderKey, targetFolderInner.Properties.Compressor);
                Assert.AreEqual(encKey, targetFolderInner.Properties.Encryptor);

                using (var writer = targetFolderInner.GetWriter())
                {
                    IDataRouter dataRouter = new Mock.NumberedDataRouter(2);
                    writer.DataRouter = dataRouter;
                    writer.AllowSubfoldersCreation = true;

                    IDataItem[] data = RepositoryWriterTest.GetTestData(100, DateTime.Now, 2);

                    for (int n = 0; n < 100; ++n)
                    {
                        writer.Write(data[n]);
                    }
                    //writer.Flush();
                    // flushes data
                    writer.Close();

                    var enm = targetFolderInner.SubFolders.GetEnumerator();
                    Assert.IsTrue(enm.MoveNext());
                    var dataFile = ((IFolder)enm.Current).RootDataFolder.FindFirstDataFile(false);
                    Assert.IsNotNull(dataFile);

                    Assert.IsTrue(dataFile.Path.EndsWith(encKey));

                    using (var reader = targetFolderInner.GetReader(DateTime.MinValue, true))
                    {
                        IDataItem[] dataRead = new IDataItem[data.Length];
                        int         n        = 0;
                        while (reader.HasData)
                        {
                            dataRead[n] = reader.Read().DataItem;
                            Assert.AreEqual(data[n], dataRead[n]);
                            ++n;
                        }
                        Assert.AreEqual(data.Length, n);
                    }
                }
            }
        }
Example #12
0
        public void QuickReaderTest()
        {
            string            targetFolderName = "QuickReaderTest";
            IRepositoryFolder targetFolder     = FixtureRootRepoFolder.GetSubFolder(targetFolderName);

            if (targetFolder != null)
            {
                targetFolder.Delete(true, true);
            }
            targetFolder = FixtureRootRepoFolder.CreateSubfolder(targetFolderName);

            string targetFolderPath = targetFolder.FullPath;

            const int subfolderCount     = 3;
            const int itemsIntervalHours = 1;
            const int desiredFileSize    = 2000;

            targetFolder.Properties.DesiredItemsPerFile = desiredFileSize;
            IRepositoryWriter writer = targetFolder.GetWriter();

            IDataRouter dataRouter = new NumberedDataRouter(subfolderCount);

            writer.DataRouter = dataRouter;
            writer.AllowSubfoldersCreation = true;

            DateTime firstTime  = DateTime.Now.AddDays(-10);
            DateTime lastTime   = DateTime.MinValue;
            int      itemsCount = 100000;
            int      n;

            for (n = 0; n < itemsCount; ++n)
            {
                Mock.TestDataItem item = Mock.TestDataItem.GetTestItem(n);
                lastTime      = firstTime.AddHours(n * itemsIntervalHours);
                item.DateTime = lastTime;
                writer.Write(item);
            }

            writer.Flush();
            writer.Close();
            // will test lazy loading
            targetFolder.UnloadSubfolders();

            Assert.IsTrue(targetFolder.SubFolders.Count == subfolderCount, "Router had to make writer create the configured number of subfolders");

            IRepositoryFolder firstItemSubfolder = targetFolder.GetDescendant(
                dataRouter.GetRelativePath(Mock.TestDataItem.GetTestItem(0)), false);

            Assert.AreEqual(firstTime, firstItemSubfolder.FirstTimestamp
                            , "Fisrt item timestamp reported incorrectly by Folder.FirstTimestamp");

            Assert.AreEqual(firstTime, targetFolder.GetFirstItemTimestamp(true, false)
                            , "Fisrt item timestamp reported incorrectly by Folder.GetFirstItemTimestamp");

            IRepositoryReader reader = targetFolder.GetReader(firstTime, true);

            Assert.IsTrue(reader.HasData, "Folder just populated but no data can be read");

            IDataItemRead ritem = null;

            n = 0;

            IRepositoryReader  altReader          = null;
            SeekStatusListener seekStatusListener = new SeekStatusListener();

            while (reader.HasData)
            {
                if (n > 0 && n % 100 == 0)
                {
                    altReader = Repository.ObjectFactory.GetReader(reader.Position, seekStatusListener.HanldeStatus);
                }
                ritem = reader.Read();
                Assert.IsNotNull(ritem, "reader.Read() returned null after returning true from HasData");
                Assert.AreNotSame(targetFolder, ritem.RepositoryFolder, "Router failed");

                Assert.IsInstanceOf <Mock.TestDataItem>(ritem.DataItem, "Data item read from repository is of different type");
                Assert.AreEqual(firstTime.AddHours(n * itemsIntervalHours), ritem.DataItem.DateTime);
                ((Mock.TestDataItem)ritem.DataItem).Check(n);

                if (altReader != null)
                {
                    IDataItemRead altItem = altReader.Read();
                    Assert.AreEqual(ritem.DataItem.DateTime, altItem.DataItem.DateTime);
                    Assert.AreEqual(0, seekStatusListener.Statuses.Count);
                }

                ++n;
            }

            Assert.AreEqual(lastTime, ritem.DataItem.DateTime, "Last item has unexpected timestamp");
            Assert.AreEqual(itemsCount, n, "Unexpected number of data items read");

            DateTime timestampToSeek = firstTime.AddHours(desiredFileSize / 3 * itemsIntervalHours);

            reader.Seek(timestampToSeek);
            Assert.IsTrue(reader.HasData, "Repeated Seek after reading all failed");
            ritem = reader.Read();
            Assert.IsNotNull(ritem);

            Assert.AreEqual(timestampToSeek, ritem.DataItem.DateTime, "First read item timestamp unexpected");

            reader.Direction = bfs.Repository.Util.EnumerationDirection.Backwards;

            Assert.IsTrue(reader.HasData, "No data after reversing in the middle of data");
            //ritem = reader.Read();
            //Assert.AreEqual<DateTime>(timestampToSeek, ritem.DataItem.DateTime
            //	, "First read item timestamp unexpected after changing direction");
            n         = 0;
            altReader = null;

            while (reader.HasData)
            {
                if (n > 0 && n % 100 == 0)
                {
                    if (altReader != null)
                    {
                        altReader.Dispose();
                    }
                    altReader = Repository.ObjectFactory.GetReader(reader.Position, seekStatusListener.HanldeStatus);
                }
                ritem = reader.Read();
                Assert.IsNotNull(ritem, "reader.Read() returned null after returning true from HasData");
                Assert.AreEqual(timestampToSeek.AddHours(-n * itemsIntervalHours), ritem.DataItem.DateTime);

                if (altReader != null)
                {
                    IDataItemRead altItem = altReader.Read();
                    Assert.AreEqual(ritem.DataItem.DateTime, altItem.DataItem.DateTime);
                }

                ++n;
            }

            Assert.AreEqual(firstTime, ritem.DataItem.DateTime, "Did not pick up first item after reversing");

            // reversing after reaching end
            reader.Direction = bfs.Repository.Util.EnumerationDirection.Forwards;
            ritem            = reader.Read();
            Assert.IsNotNull(ritem, "Did not read firts item reversing after reaching end");

            Assert.AreEqual(firstTime, ritem.DataItem.DateTime, "Did not pick up first item after reversing after reaching end");

            // cleanup
            //targetFolder.Delete(true, false);
            //Assert.IsFalse(Directory.Exists(targetFolderPath), "Test repo directory not removed from disk by Delete()");
        }
        public void GetUnsavedItemsAmbientTransactionTest()
        {
            const int subfolderCount = 3;

            const string subFolderName = "GetUnsavedItemsAmbientTransactionTest";

            IRepositoryFolder targetFolder = FixtureRootRepoFolder.CreateSubfolder(subFolderName);

            IRepositoryWriter writer = targetFolder.GetWriter();

            targetFolder.Properties.DesiredItemsPerFile = 100;

            IDataRouter dataRouter = new Mock.NumberedDataRouter(subfolderCount);

            writer.DataRouter = dataRouter;
            writer.AllowSubfoldersCreation = true;

            string fullPath = targetFolder.FullPath;

            Mock.TestDataItem item;
            IDictionary <string, IList <IDataItem> > unsavedItems;

            using (TransactionScope scope = new TransactionScope())
            {
                Assert.IsNotNull(Transaction.Current);

                const int count = 10000;

                for (int n = 0; n < count; ++n)
                {
                    item = Mock.TestDataItem.GetTestItem(n);
                    writer.Write(item);

                    if ((n + 1) % 134 == 0)
                    {
                        writer.Flush();

                        unsavedItems = writer.GetUnsavedItems();

                        Assert.IsNotNull(unsavedItems);

                        Assert.AreEqual(Math.Min(n + 1, subfolderCount), unsavedItems.Count
                                        , "Unsaved items dictionary entry count is not equal to the direct writers count");

                        Assert.AreEqual(n + 1, unsavedItems.Values.Sum((l) => l.Count)
                                        , "Total number of unsaved items after flush must not change if in ambient transaction");
                    }
                }

                unsavedItems = writer.GetUnsavedItems();

                Assert.IsNotNull(unsavedItems);

                Assert.AreEqual(subfolderCount, unsavedItems.Count
                                , "Unsaved items dictionary entry count is not equal to the direct writers count");

                Assert.AreEqual(count, unsavedItems.Values.Sum((l) => l.Count)
                                , "Total number of unsaved items must equal number of added items if in ambient transaction");
                scope.Complete();
            }

            Thread.Sleep(50);

            unsavedItems = writer.GetUnsavedItems();

            Assert.IsNotNull(unsavedItems);

            Assert.AreEqual(subfolderCount, unsavedItems.Count
                            , "Unsaved items dictionary entry count is not equal to the direct writers count");

            Assert.AreEqual(0, unsavedItems.Values.Sum((l) => l.Count)
                            , "Total number of unsaved items after committing ambient transaction must be 0");

            writer.Close();
        }