internal static void CurrentTransactionIsNull()
        {
#if DEBUG
            Console.WriteLine("CurrentTransactionIsNull");
#endif
            var managedPath =
                "Data" +
                Path.DirectorySeparatorChar +
                "create-file-is-new-no-current-transaction.txt";

            var manager = new CreateNonEmptyFileManager(
                managedPath, overwrite: true);

            FileStream stream = (FileStream)FileManagerReflection.Invoke(
                manager,
                "OnPrepareFileStream",
                new string[] { managedPath });

            ExceptionAssert.IsThrown(
                () => { manager.EnlistVolatile(EnlistmentOptions.None); },
                expectedType: typeof(InvalidOperationException),
                expectedMessage: String.Format(
                    "Cannot enlist resource {0}: no ambient transaction detected.",
                    managedPath));
        }
            /// <summary>
            /// Tests the constructor when parameter <i>managedPath</i> is <b>null</b>.
            /// </summary>
            public static void ManagedPathIsNull()
            {
#if DEBUG
                Console.WriteLine("Constructor_ManagedPathIsNull");
#endif
                string managedPath = null;
                ArgumentExceptionAssert.IsThrown(
                    () => { var manager = new CreateNonEmptyFileManager(managedPath, overwrite: true); },
                    expectedType: typeof(ArgumentNullException),
                    expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage,
                    expectedParameterName: "managedPath");
            }
        public static void Dispose(bool overwrite)
        {
#if DEBUG
            Console.WriteLine("Dispose");
#endif
            string managedPath =
                overwrite
                ?
                "Data" +
                Path.DirectorySeparatorChar +
                "create-file-is-new-dispose.txt"
                :
                "Data" +
                Path.DirectorySeparatorChar +
                "create-file-is-new-dispose-cannot-overwrite.txt";

            // Assure that the managed file is new
            // in case of overwrite: false
            if (!overwrite)
            {
                File.Delete(managedPath);
            }

            // Simulate a preparation

            var manager = new CreateNonEmptyFileManager(
                managedPath, overwrite: overwrite);

            Assert.AreEqual(false, FileManagerReflection.GetField(manager, "disposed"));

            FileStream stream = (FileStream)FileManagerReflection.Invoke(
                manager,
                "OnPrepareFileStream",
                new string[] { managedPath });

            FileManagerReflection.SetStream(manager, stream);
            stream = null;

            // Dispose the manager

            manager.Dispose();

            // Expected results

            Assert.AreEqual(true, FileManagerReflection.GetField(manager, "disposed"));

            // Dispose the manager, again

            ExceptionAssert.IsNotThrown(
                () => { manager.Dispose(); }
                );
        }
            public static void OnRollbackNoScope()
            {
#if DEBUG
                Console.WriteLine("FileAlreadyExists_OnRollbackNoScope");
#endif
                var managedPath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "create-file-already-exists-on-rollback-no-scope.txt";

                // Simulate a preparation

                bool overwrite = true;
                var  manager   = new CreateNonEmptyFileManager(
                    managedPath, overwrite: overwrite);

                FileStream stream = (FileStream)FileManagerReflection.Invoke(
                    manager,
                    "OnPrepareFileStream",
                    new string[] { managedPath });

                FileManagerReflection.SetStream(manager, stream);
                stream = null;

                Assert.AreEqual(overwrite, manager.CanOverwrite);
                Assert.AreEqual(true, manager.FileAlreadyExists);

                // Simulate a rollback

                FileManagerReflection.Invoke(
                    manager,
                    "OnRollback",
                    null);

                // Expected results

                Assert.IsTrue(File.Exists(managedPath));
                using (stream = File.OpenRead(managedPath))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        var content = reader.ReadLine();
                        Assert.AreEqual("existing-file", content);
                    }
                }
            }
            public static void OnRollbackNoScope(bool overwrite)
            {
#if DEBUG
                Console.WriteLine("FileIsNew_OnRollbackNoScope");
#endif
                var managedPath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "create-file-is-new-on-rollback-no-scope.txt";

                // Simulate a preparation

                var manager = new CreateNonEmptyFileManager(
                    managedPath, overwrite: overwrite);

                FileStream stream = (FileStream)FileManagerReflection.Invoke(
                    manager,
                    "OnPrepareFileStream",
                    new string[] { managedPath });

                FileManagerReflection.SetStream(manager, stream);
                stream = null;

                Assert.AreEqual(overwrite, manager.CanOverwrite);
                Assert.AreEqual(false, manager.FileAlreadyExists);

                // Simulate a rollback

                FileManagerReflection.Invoke(
                    manager,
                    "OnRollback",
                    null);

                // Expected results

                // Dispose the manager, so that
                // the following call to File.Exists
                // has the required permissions
                // to investigate the managed file.
                manager.Dispose();

                Assert.IsTrue(!File.Exists(managedPath));
            }
            public static void OnCommit(bool overwrite)
            {
#if DEBUG
                Console.WriteLine("FileIsNew_OnCommit");
#endif
                var managedPath =
                    "Data" +
                    Path.DirectorySeparatorChar +
                    "create-file-is-new-on-commit.txt";

                var createManager = new CreateNonEmptyFileManager(
                    managedPath, overwrite: overwrite);

                List <FileManager> managers = new List <FileManager>
                {
                    createManager
                };

                void results()
                {
                    Assert.IsTrue(File.Exists(managedPath));
                    using (Stream stream = File.OpenRead(managedPath))
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                        {
                            var content = reader.ReadString();
                            Assert.AreEqual(createManager.ContentWrittenOnCreation, content);
                        }
                    }
                }

                void rolledBack(Exception e)
                {
                }

                TransactionScopeHelper.Using(
                    managers,
                    results,
                    rolledBack);
            }