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 ItemsPerFileTest()
        {
            const string topFolderName = "ItemsPerFileTest";

            IFolder topFolder = CreateNewTestFolder(topFolderName);

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

            Assert.AreEqual(0, targetFolder.Properties.DesiredItemsPerFile);

            const int itemsPerFile = 12;

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

                topFolderInner.Properties.DesiredItemsPerFile = itemsPerFile;

                Assert.AreEqual(itemsPerFile, topFolderInner.Properties.DesiredItemsPerFile);

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

                Assert.AreEqual(itemsPerFile, targetFolderInner.Properties.DesiredItemsPerFile);
            }

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


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

                Assert.AreEqual(itemsPerFile, targetFolderInner.Properties.DesiredItemsPerFile);

                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);


                    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);
                    }
                }
            }
        }
        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);
                    }
                }
            }
        }
        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();
        }