protected bool HasTransaction(out IFileTransaction transaction)
		{
			transaction = null;

			if (!_UseTransactions) return false;

			if (_TxManager != null && _TxManager.CurrentTransaction != null)
			{
				foreach (var resource in _TxManager.CurrentTransaction.Resources())
				{
					if (!(resource is FileResourceAdapter)) continue;

					transaction = (resource as FileResourceAdapter).Transaction;
					return true;
				}

				if (!_OnlyJoinExisting)
				{
					transaction = new FileTransaction("Autocreated File Transaction");
					_TxManager.CurrentTransaction.Enlist(new FileResourceAdapter(transaction));
					return true;
				}
			}
			
			return false;
		}
		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 NonExistentDir()
		{
			using (var t = new FileTransaction())
			{
				var dir = (t as IDirectoryAdapter);
				Assert.IsFalse(dir.Exists("/hahaha"));
				Assert.IsFalse(dir.Exists("another_non_existent"));
				dir.Create("existing");
				Assert.IsTrue(dir.Exists("existing"));
			}
			// no commit
			Assert.IsFalse(Directory.Exists("existing"));
		}
		public void NoCommit_MeansNoDirectory()
		{
			var directoryPath = "testing";
			Assert.That(Directory.Exists(directoryPath), Is.False);

			using (ITransaction tx = new FileTransaction())
			{
				Directory.Create(directoryPath);
				Assert.IsTrue(Directory.Exists(directoryPath));
				tx.Dispose();
			}

			Assert.That(!Directory.Exists(directoryPath));
		}
示例#6
0
		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 NoCommit_MeansNoDirectory()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			string directoryPath = "testing";
			Assert.That(Directory.Exists(directoryPath), Is.False);

			using (var tx = new FileTransaction())
			{
				tx.Begin();
				(tx as IDirectoryAdapter).Create(directoryPath);
			}

			Assert.That(!Directory.Exists(directoryPath));
		}
		public void ExistingDirWithTrailingBackslash()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			// From http://msdn.microsoft.com/en-us/library/aa364419(VS.85).aspx
			// An attempt to open a search with a trailing backslash always fails.
			// --> So I need to make it succeed.
			using (var t = new FileTransaction())
			{
				t.Begin();
				var dir = t as IDirectoryAdapter;
				dir.Create("something");
				Assert.That(dir.Exists("something"));
				Assert.That(dir.Exists("something\\"));
			}
		}
		public void CreatingFolder_InTransaction_AndCommitting_MeansExistsAfter()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			string directoryPath = "testing";
			Assert.That(Directory.Exists(directoryPath), Is.False);

			using (var tx = new FileTransaction())
			{
				tx.Begin();
				(tx as IDirectoryAdapter).Create(directoryPath);
				tx.Commit();
			}

			Assert.That(Directory.Exists(directoryPath));

			Directory.Delete(directoryPath);
		}
示例#10
0
		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 NotUsingTransactionScope_IsNotDistributed_AboveNegated()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			using (var tx = new FileTransaction("Not distributed transaction"))
			{
				tx.Begin();
				Assert.That(tx.IsAmbient, Is.False);
				tx.Commit();
			}
		}
		public void CanNotDelete_NonRecursively_NonEmptyDir()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			// 1. create dir and file
			string dir = dllPath.CombineAssert("testing");
			string file = dir.Combine("file");
			File.WriteAllText(file, "hello");

			// 2. test it
			using (var t = new FileTransaction("Can not delete non-empty directory"))
			{
				IDirectoryAdapter da = t;
				t.Begin();
				Assert.That(da.Delete(dir, false),
							Is.False,
							"Did not delete non-empty dir.");
				IFileAdapter fa = t;
				fa.Delete(file);

				Assert.That(da.Delete(dir, false),
							"After deleting the file in the folder, the folder is also deleted.");

				t.Commit();
			}
		}
		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 Using_NormalStates()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			using (var tx = new FileTransaction())
			{
				Assert.That(tx.Status, Is.EqualTo(TransactionStatus.NoTransaction));
				tx.Begin();
				Assert.That(tx.Status, Is.EqualTo(TransactionStatus.Active));
				tx.Commit();
				Assert.That(tx.Status, Is.EqualTo(TransactionStatus.Committed));
			}
		}
		public void CanDelete_NonRecursively_EmptyDir()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			// 1. create dir
			string dir = dllPath.CombineAssert("testing");

			// 2. test it
			using (var t = new FileTransaction("Can delete empty directory"))
			{
				IDirectoryAdapter da = t;
				t.Begin();
				Assert.That(da.Delete(dir, false), "Successfully deleted.");
				t.Commit();
			}
		}
		public void InvalidStateOnCreate_Throws()
		{
			using (var tx = new FileTransaction())
			{
				Assert.Throws(typeof (TransactionException), () => (tx as IDirectoryAdapter).Create("lol"),
				              "The transaction hasn't begun, throws.");
			}
		}
		public void Using_TransactionScope_IsDistributed_AlsoTestingStatusWhenRolledBack()
        {
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

            using (new TransactionScope())
			{
				using (var tx = new FileTransaction())
				{
					tx.Begin();

					Assert.That(tx.IsAmbient);

					tx.Rollback();
					Assert.That(tx.IsRollbackOnlySet);
					Assert.That(tx.Status, Is.EqualTo(TransactionStatus.RolledBack));
				}
			}
		}
		public void FailingResource_TxStillRolledBack()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			using (var tx = new FileTransaction())
			{
				tx.Enlist(new R());
				tx.Begin();
				try
				{
					try
					{
						tx.Rollback();
						Assert.Fail("Tests is wrong or the transaction doesn't rollback resources.");
					}
					catch (Exception)
					{
					}

					Assert.That(tx.Status == TransactionStatus.RolledBack);
				}
				catch (RollbackResourceException rex)
				{
					// good.
					Assert.That(rex.FailedResources[0].First, Is.InstanceOf(typeof (R)));
				}
			}
		}
		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 ExistingDirWithTrailingBackslash()
		{
			// From http://msdn.microsoft.com/en-us/library/aa364419(VS.85).aspx
			// An attempt to open a search with a trailing backslash always fails.
			// --> So I need to make it succeed.
			using (var t = new FileTransaction())
			{
				var dir = t as IDirectoryAdapter;
				dir.Create("something");
				Assert.That(dir.Exists("something"));
				Assert.That(dir.Exists("something\\"));
			}
		}
		public void CanCreate_AndFind_Directory_WithinTx()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			using (var tx = new FileTransaction("s"))
			{
				tx.Begin();
				Assert.That((tx as IDirectoryAdapter).Exists("something"), Is.False);
				(tx as IDirectoryAdapter).Create("something");
				Assert.That((tx as IDirectoryAdapter).Exists("something"));
				tx.Rollback();
			}
		}
		public void NonExistentDir()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			using (var t = new FileTransaction())
			{
				t.Begin();
				var dir = (t as IDirectoryAdapter);
				Assert.IsFalse(dir.Exists("/hahaha"));
				Assert.IsFalse(dir.Exists("another_non_existent"));
				dir.Create("existing");
				Assert.IsTrue(dir.Exists("existing"));
			}
			// no commit
			Assert.IsFalse(Directory.Exists("existing"));
		}
		public void CanCreateDirectory_NLengths_DownInNonExistentDirectory()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			string directoryPath = "testing/apa/apa2";
			Assert.That(Directory.Exists(directoryPath), Is.False);

			using (var t = new FileTransaction())
			{
				t.Begin();
				(t as IDirectoryAdapter).Create(directoryPath);
				t.Commit();
			}

			Assert.That(Directory.Exists(directoryPath));
			Directory.Delete(directoryPath);
		}
		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_Recursively()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			// 1. Create directory
			string pr = dllPath.Combine("testing");
			Directory.CreateDirectory(pr);
			Directory.CreateDirectory(pr.Combine("one"));
			Directory.CreateDirectory(pr.Combine("two"));
			Directory.CreateDirectory(pr.Combine("three"));

			// 2. Write contents
			File.WriteAllLines(Exts.Combine(pr, "one").Combine("fileone"), new[] { "Hello world", "second line" });
			File.WriteAllLines(Exts.Combine(pr, "one").Combine("filetwo"), new[] { "two", "second line" });
			File.WriteAllLines(Exts.Combine(pr, "two").Combine("filethree"), new[] { "three", "second line" });

			// 3. test
			using (var t = new FileTransaction())
			{
				t.Begin();
				Assert.IsTrue((t as IDirectoryAdapter).Delete(pr, true));
				t.Commit();
			}
		}
		public void CannotCommitAfterSettingRollbackOnly()
		{
            if (Environment.OSVersion.Version.Major < 6)
            {
                Assert.Ignore("TxF not supported");
                return;
            }

			using (var tx = new FileTransaction())
			{
				tx.Begin();
				tx.SetRollbackOnly();
				Assert.Throws(typeof(TransactionException), tx.Commit,
							  "Should not be able to commit after rollback is set.");
			}
		}
		public void CTorTests()
		{
			var t = new FileTransaction();
			Assert.That(t.Status, Is.EqualTo(TransactionStatus.NoTransaction));
		}
		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 CanMoveDirectory()
		{
			string dir1 = dllPath.CombineAssert("a");
			string dir2 = dllPath.Combine("b");

			Assert.That(Directory.Exists(dir2), Is.False);
			Assert.That(File.Exists(dir2), Is.False, "Lingering files should not be allowed to disrupt the testing.");


			string aFile = dir1.Combine("file");
			File.WriteAllText(aFile, "I should also be moved.");
			infosCreated.Add(aFile);

			using (var t = new FileTransaction("moving tx"))
			{
				t.Begin();

				(t as IDirectoryAdapter).Move(dir1, dir2);
				Assert.IsFalse(Directory.Exists(dir2), "The directory should not yet exist.");

				t.Commit();
				Assert.That(Directory.Exists(dir2), "Now after committing it should.");
				infosCreated.Add(dir2);

				Assert.That(File.Exists(dir2.Combine(Path.GetFileName(aFile))), "And so should the file in the directory.");
			}
		}
		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();
			}
		}