internal static void CurrentTransactionIsNull() { #if DEBUG Console.WriteLine("CurrentTransactionIsNull"); #endif var managedPath = "Data" + Path.DirectorySeparatorChar + "edit-file-already-exists-no-current-transaction.txt"; var manager = new EditExistingFileManager( managedPath); 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)); }
public static void OnCommit() { #if DEBUG Console.WriteLine("FileAlreadyExists_OnCommit"); #endif var managedPath = "Data" + Path.DirectorySeparatorChar + "edit-file-already-exists-on-commit.txt"; var manager = new EditExistingFileManager( managedPath); List <FileManager> managers = new List <FileManager> { manager }; void results() { Assert.IsTrue(File.Exists(managedPath)); // The manager wrote the NewContent string to the managed file using the // BinaryWriter.Write Method (String), the writer being instantiated // with the Encoding.UTF8 encoding. // Since such method writes a length-prefixed string to the stream, // the number of written bytes is equal to the String.Length of // NewContent plus 1, the prefixed byte representing such length. int numberOfExpectedBytes = manager.NewContent.Length + 1; var expectedBytes = new byte[numberOfExpectedBytes]; expectedBytes[0] = Convert.ToByte(numberOfExpectedBytes - 1); var stringBytes = Encoding.UTF8.GetBytes(manager.NewContent); stringBytes.CopyTo(expectedBytes, 1); var actualBytes = File.ReadAllBytes(managedPath); int numberOfActualBytes = actualBytes.Length; Assert.AreEqual(numberOfExpectedBytes, numberOfActualBytes); for (int i = 0; i < expectedBytes.Length; i++) { Assert.AreEqual(expectedBytes[i], actualBytes[i]); } } void rolledBack(Exception e) { } TransactionScopeHelper.Using( managers, results, rolledBack); }
/// <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 EditExistingFileManager(managedPath); }, expectedType: typeof(ArgumentNullException), expectedPartialMessage: ArgumentExceptionAssert.NullPartialMessage, expectedParameterName: "managedPath"); }
public static void OnRollbackNoScope() { #if DEBUG Console.WriteLine("FileAlreadyExists_OnRollbackNoScope"); #endif var managedPath = "Data" + Path.DirectorySeparatorChar + "edit-file-already-exists-on-rollback-no-scope.txt"; // Simulate a preparation var manager = new EditExistingFileManager( managedPath); var stream = (FileStream)FileManagerReflection.Invoke( manager, "OnPrepareFileStream", new string[] { managedPath }); FileManagerReflection.SetStream(manager, stream); stream = null; // Simulate a rollback (NOP) // We want to access the managed file to inspect its // edited contents, hence we need to dispose // its manager. manager.Dispose(); // 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 Dispose() { #if DEBUG Console.WriteLine("Dispose"); #endif var managedPath = "Data" + Path.DirectorySeparatorChar + "edit-file-already-exists-dispose.txt"; // Simulate a preparation var manager = new EditExistingFileManager( managedPath); 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(); } ); }