public void NotUsingTransactionScope_IsNotDistributed_AboveNegated() { using (ITransaction tx = new FileTransaction("Not distributed transaction")) { Assert.That(System.Transactions.Transaction.Current, Is.Null); tx.Complete(); } }
public void CompletedState() { using (ITransaction tx = new FileTransaction()) { Assert.That(tx.State, Is.EqualTo(TransactionState.Active)); tx.Complete(); Assert.That(tx.State, Is.EqualTo(TransactionState.CommittedOrCompleted)); } }
public void CanDelete_NonRecursively_EmptyDir() { // 1. create dir var dir = _TfPath.CombineAssert("testing"); // 2. test it using (ITransaction t = new FileTransaction("Can delete empty directory")) { Assert.That(((IDirectoryAdapter)t).Delete(dir, false), "Successfully deleted."); t.Complete(); } }
public void CanCreateDirectory_NLengths_DownInNonExistentDirectory() { var directoryPath = "testing/apa/apa2"; Assert.That(Directory.Exists(directoryPath), Is.False); using (ITransaction t = new FileTransaction()) { Directory.Create(directoryPath); t.Complete(); } Assert.That(Directory.Exists(directoryPath)); Directory.Delete(directoryPath); }
public void CreatingFolder_InTransaction_AndCommitting_MeansExistsAfter() { var directoryPath = "testing"; Assert.That(Directory.Exists(directoryPath), Is.False); using (ITransaction tx = new FileTransaction()) { Directory.Create(directoryPath); tx.Complete(); } Assert.That(Directory.Exists(directoryPath)); Directory.Delete(directoryPath); }
public void WriteAllText() { var filepath = _TfPath.Combine("write-text.txt"); using (ITransaction tx = new FileTransaction("Commit TX")) { var fa = (IFileAdapter)tx; fa.WriteAllText(filepath, "Transacted file."); tx.Complete(); Assert.That(tx.State == TransactionState.CommittedOrCompleted); } File.Exists(filepath).Should("exist after the transaction") .Be.True(); File.ReadAllLines(filepath) .First().Should() .Be.EqualTo("Transacted file."); }
public void WriteAllText() { var filepath = _TfPath.Combine("write-text.txt"); using (ITransaction tx = new FileTransaction("Commit TX")) { var fa = (IFileAdapter) tx; fa.WriteAllText(filepath, "Transacted file."); tx.Complete(); Assert.That(tx.State == TransactionState.CommittedOrCompleted); } File.Exists(filepath).Should("exist after the transaction") .Be.True(); File.ReadAllLines(filepath) .First().Should() .Be.EqualTo("Transacted file."); }
public void Move_ToDirectory_PlusFileName() { // given var folder = _TfPath.CombineAssert("source_folder"); var toFolder = _TfPath.CombineAssert("target_folder"); const string fileName = "Move_ToDirectory_PlusFileName.txt"; var file = folder.Combine(fileName); File.Exists(file) .Should() .Be.False(); File.WriteAllText(file, "testing move"); // when using (ITransaction t = new FileTransaction()) { File.Exists(toFolder.Combine(fileName)) .Should("not exist before move") .Be.False(); // method under test: ((IFileAdapter)t).Move(file, toFolder.Combine(fileName)); File.Exists(file) .Should("call through tx and be deleted") .Be.False(); t.Complete(); } // then File.Exists(toFolder.Combine(fileName)) .Should("call through tx and visible in its new location") .Be.True(); File.ReadAllText(toFolder.Combine(fileName)) .Should("have same contents") .Be("testing move"); }
public void Move_ToDirectory() { var folder = _TfPath.CombineAssert("source_folder"); var toFolder = _TfPath.CombineAssert("target_folder"); const string fileName = "Move_ToDirectory.txt"; var file = folder.Combine(fileName); Directory.Exists(toFolder).Should().Be.False(); (File.Exists(file)).Should().Be.False(); File.WriteAllText(file, "this string is the contents of the file"); using (ITransaction t = new FileTransaction("moving file")) { File.Exists(toFolder.Combine("file")) .Should("not exist before move") .Be.False(); // moving file to folder ((IFileAdapter)t) .Move(file, toFolder); File.Exists(file) .Should("call through tx and be deleted") .Be.False(); File.Exists(toFolder.Combine("file")) .Should("call through tx and visible in its new location") .Be.True(); t.Complete(); } File.Exists(toFolder.Combine("file")) .Should( "be visible to the outside now and since we tried to move it to " + " an existing folder, it should put itself in that folder with its current name.") .Be.True(); }
public void CanDelete_Recursively() { // 1. Create directory var pr = _TfPath.Combine("testing"); Directory.Create(pr); Directory.Create(pr.Combine("one")); Directory.Create(pr.Combine("two")); Directory.Create(pr.Combine("three")); // 2. Write contents File.WriteAllLines(pr.Combine("one", "fileone"), new[] { "Hello world", "second line" }); File.WriteAllLines(pr.Combine("one", "filetwo"), new[] { "two", "second line" }); File.WriteAllLines(pr.Combine("two", "filethree"), new[] { "three", "second line" }); // 3. test using (ITransaction t = new FileTransaction()) { Assert.IsTrue(((IDirectoryAdapter)t).Delete(pr, true)); t.Complete(); } }
public void CanNotDelete_NonRecursively_NonEmptyDir() { // 1. create dir and file var dir = _TfPath.CombineAssert("testing"); var file = dir.Combine("file"); File.WriteAllText(file, "hello"); // 2. test it using (ITransaction t = new FileTransaction("Can not delete non-empty directory")) { Assert.That(Directory.DeleteDirectory(dir, false), Is.False, "Did not delete non-empty dir."); File.Delete(file); Assert.That(Directory.DeleteDirectory(dir, false), "After deleting the file in the folder, the folder is also deleted."); t.Complete(); } }
public void Write_And_ReplaceContents() { var filePath = _TfPath.Combine("Write_And_ReplaceContents.txt"); // simply write something to to file. using (var wr = File.CreateText(filePath)) wr.WriteLine("Hello"); using (ITransaction tx = new FileTransaction()) { using (var fs = ((IFileAdapter)tx).Create(filePath)) { var str = new UTF8Encoding().GetBytes("Goodbye"); fs.Write(str, 0, str.Length); fs.Flush(); } tx.Complete(); } File.ReadAllLines(filePath) .First().Should() .Be.EqualTo("Goodbye"); }
public void Move_ToDirectory_PlusFileName() { // given var folder = _TfPath.CombineAssert("source_folder"); var toFolder = _TfPath.CombineAssert("target_folder"); const string fileName = "Move_ToDirectory_PlusFileName.txt"; var file = folder.Combine(fileName); File.Exists(file) .Should() .Be.False(); File.WriteAllText(file, "testing move"); // when using (ITransaction t = new FileTransaction()) { File.Exists(toFolder.Combine(fileName)) .Should("not exist before move") .Be.False(); // method under test: ((IFileAdapter) t).Move(file, toFolder.Combine(fileName)); File.Exists(file) .Should("call through tx and be deleted") .Be.False(); t.Complete(); } // then File.Exists(toFolder.Combine(fileName)) .Should("call through tx and visible in its new location") .Be.True(); File.ReadAllText(toFolder.Combine(fileName)) .Should("have same contents") .Be("testing move"); }
public void TwoTransactions_SameName_FirstSleeps() { var t1_started = new ManualResetEvent(false); var t2_started = new ManualResetEvent(false); Exception e = null; // non transacted thread var t1 = new Thread(() => { try { // modifies the file using (var fs = File.OpenWrite("abb")) { Console.WriteLine("t2 start"); Console.Out.Flush(); t2_started.Set(); // before the transacted thread does Console.WriteLine("t2 wait for t1 to start"); Console.Out.Flush(); t1_started.WaitOne(); fs.Write(new byte[] { 0x1 }, 0, 1); fs.Close(); } } catch (Exception ee) { e = ee; } finally { Console.WriteLine("t2 finally"); Console.Out.Flush(); t2_started.Set(); } }); t1.Start(); using (ITransaction t = new FileTransaction()) { Console.WriteLine("t1 wait for t2 to start"); Console.Out.Flush(); t2_started.WaitOne(); try { Console.WriteLine("t1 started"); // the transacted thread should receive ERROR_TRANSACTIONAL_CONFLICT, but it gets permission denied. using (var fs = ((IFileAdapter)t).Create("abb")) { fs.WriteByte(0x2); } } finally { Console.WriteLine("t1 finally"); Console.Out.Flush(); t1_started.Set(); } t.Complete(); } if (e != null) { Console.WriteLine(e); Assert.Fail(e.Message); } }
public void Move_ToDirectory() { var folder = _TfPath.CombineAssert("source_folder"); var toFolder = _TfPath.CombineAssert("target_folder"); const string fileName = "Move_ToDirectory.txt"; var file = folder.Combine(fileName); Directory.Exists(toFolder).Should().Be.False(); (File.Exists(file)).Should().Be.False(); File.WriteAllText(file, "this string is the contents of the file"); using (ITransaction t = new FileTransaction("moving file")) { File.Exists(toFolder.Combine("file")) .Should("not exist before move") .Be.False(); // moving file to folder ((IFileAdapter) t) .Move(file, toFolder); File.Exists(file) .Should("call through tx and be deleted") .Be.False(); File.Exists(toFolder.Combine("file")) .Should("call through tx and visible in its new location") .Be.True(); t.Complete(); } File.Exists(toFolder.Combine("file")) .Should( "be visible to the outside now and since we tried to move it to " + " an existing folder, it should put itself in that folder with its current name.") .Be.True(); }
public void CanDelete_Recursively() { // 1. Create directory var pr = _TfPath.Combine("testing"); Directory.Create(pr); Directory.Create(pr.Combine("one")); Directory.Create(pr.Combine("two")); Directory.Create(pr.Combine("three")); // 2. Write contents File.WriteAllLines(pr.Combine("one", "fileone"), new[] {"Hello world", "second line"}); File.WriteAllLines(pr.Combine("one", "filetwo"), new[] {"two", "second line"}); File.WriteAllLines(pr.Combine("two", "filethree"), new[] {"three", "second line"}); // 3. test using (ITransaction t = new FileTransaction()) { Assert.IsTrue(((IDirectoryAdapter) t).Delete(pr, true)); t.Complete(); } }
public void CanDelete_NonRecursively_EmptyDir() { // 1. create dir var dir = _TfPath.CombineAssert("testing"); // 2. test it using (ITransaction t = new FileTransaction("Can delete empty directory")) { Assert.That(((IDirectoryAdapter) t).Delete(dir, false), "Successfully deleted."); t.Complete(); } }
public void CanMove_File() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } var folder = dllPath.CombineAssert("testing"); Console.WriteLine(string.Format("Directory \"{0}\"", folder)); var toFolder = dllPath.CombineAssert("testing2"); var file = folder.Combine("file"); Assert.That(File.Exists(file), Is.False); var file2 = folder.Combine("file2"); Assert.That(File.Exists(file2), Is.False); File.WriteAllText(file, "hello world"); File.WriteAllText(file2, "hello world 2"); infosCreated.Add(file); infosCreated.Add(file2); infosCreated.Add(toFolder.Combine("file2")); infosCreated.Add(toFolder.Combine("file")); infosCreated.Add(toFolder); using (ITransaction t = new FileTransaction("moving file")) { Assert.That(File.Exists(toFolder.Combine("file")), Is.False, "Should not exist before move"); Assert.That(File.Exists(toFolder.Combine("file2")), Is.False, "Should not exist before move"); (t as IFileAdapter).Move(file, toFolder); // moving file to folder (t as IFileAdapter).Move(file2, toFolder.Combine("file2")); // moving file to folder+new file name. Assert.That(File.Exists(toFolder.Combine("file")), Is.False, "Should not be visible to the outside"); Assert.That(File.Exists(toFolder.Combine("file2")), Is.False, "Should not be visible to the outside"); t.Complete(); Assert.That(File.Exists(toFolder.Combine("file")), Is.True, "Should be visible to the outside now and since we tried to move it to an existing folder, it should put itself in that folder with its current name."); Assert.That(File.Exists(toFolder.Combine("file2")), Is.True, "Should be visible to the outside now."); } Assert.That(File.ReadAllText(toFolder.Combine("file2")), Is.EqualTo("hello world 2"), "Make sure we moved the contents."); }
public void CreateFileAndReplaceContents() { if (Environment.OSVersion.Version.Major < 6) { Assert.Ignore("TxF not supported"); return; } var filePath = testFixturePath.CombineAssert("temp").Combine("temp__"); infosCreated.Add(filePath); // simply write something to to file. using (var wr = File.CreateText(filePath)) { wr.WriteLine("Hello"); } using (ITransaction tx = new FileTransaction()) { using (var fs = (tx as IFileAdapter).Create(filePath)) { var str = new UTF8Encoding().GetBytes("Goodbye"); fs.Write(str, 0, str.Length); fs.Flush(); } tx.Complete(); } Assert.That(File.ReadAllLines(filePath)[0], Is.EqualTo("Goodbye")); }
public void Write_And_ReplaceContents() { var filePath = _TfPath.Combine("Write_And_ReplaceContents.txt"); // simply write something to to file. using (var wr = File.CreateText(filePath)) wr.WriteLine("Hello"); using (ITransaction tx = new FileTransaction()) { using (var fs = ((IFileAdapter) tx).Create(filePath)) { var str = new UTF8Encoding().GetBytes("Goodbye"); fs.Write(str, 0, str.Length); fs.Flush(); } tx.Complete(); } File.ReadAllLines(filePath) .First().Should() .Be.EqualTo("Goodbye"); }