public void ReadMixedlines4()
 {
     using (var tester = new OptimisticLockedTextFileTester(Line + WindowsLine))
     {
         tester.AssertLines(Line + WindowsLine);
     }
 }
 public void ReadWindowsLine()
 {
     using (var tester = new OptimisticLockedTextFileTester(WindowsLine))
     {
         tester.AssertLines(WindowsLine);
     }
 }
 public void ReadUnixLine()
 {
     using (var tester = new OptimisticLockedTextFileTester(UnixLine))
     {
         tester.AssertLines(UnixLine);
     }
 }
 public void ReadMixedNewLines2()
 {
     using (var tester = new OptimisticLockedTextFileTester(UnixNewLine + WindowsNewLine))
     {
         tester.AssertLines(UnixNewLine, WindowsNewLine);
     }
 }
 public void ReadUnixLine()
 {
     using (var tester = new OptimisticLockedTextFileTester(UnixLine))
     {
         tester.AssertLines(UnixLine);
     }
 }
 public void PersistLine()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.AssertPersist(Line, Line);
     }
 }
 public void PersistAddsLineEnding()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.AssertPersist(Line + Environment.NewLine + Line, Line, Line);
     }
 }
 public void ReadWindowsNewLine()
 {
     using (var tester = new OptimisticLockedTextFileTester(WindowsNewLine))
     {
         tester.AssertLines(WindowsNewLine);
     }
 }
 public void ReadMixedNewLines2()
 {
     using (var tester = new OptimisticLockedTextFileTester(UnixNewLine + WindowsNewLine))
     {
         tester.AssertLines(UnixNewLine, WindowsNewLine);
     }
 }
 public void PersistEmpty()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.TextFile.Persist();
         tester.AssertFileContents(String.Empty);
     }
 }
 public void PersistTwice()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.AssertPersist(WindowsLine, WindowsLine);
         tester.AssertPersist(WindowsLine + WindowsLine, WindowsLine);
     }
 }
 public void ProperTruncation()
 {
     using (var tester = new OptimisticLockedTextFileTester())
     {
         tester.AssertPersist(UnixLine + UnixLine, UnixLine, UnixLine);
         tester.TextFile.Lines.RemoveAt(0);
         tester.TextFile.Persist();
         tester.AssertFileContents(UnixLine);
     }
 }
 public void FileLockedByAnotherProcess()
 {
     using (var tester = new OptimisticLockedTextFileTester())
     {
         tester.TextFile.Lines.Add(Line);
         using (var streamWriter = File.AppendText(tester.TextFile.FilePath))
         {
             AssertExtensions.ExpectException(() =>
             {
                 tester.TextFile.Persist();
             }, typeof(IOException), string.Format(FileLockedMessageFormat, tester.TextFile.FilePath));
         }
     }
 }
 public void FileChanged()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.TextFile.Lines.Add(Line);
         using (var streamWriter = File.AppendText(tester.TextFile.FilePath))
         {
             streamWriter.Write("changed");
         }
         AssertExtensions.ExpectException(() =>
         {
             tester.TextFile.Persist();
         }, typeof(IOException), string.Format(FileChangedMessageFormat, tester.TextFile.FilePath));
     }
 }
        /// <summary>
        /// This test needs to be run manually since it relies on a race condition and a manual code change.
        /// It's been run as part of development but it won't be run in the normal build process.
        ///
        /// This test makes sure that the OptimisticLockedTextFile.Persist() method locks the file properly while it's being written.
        /// To run this test:
        /// 1.  temporarily add System.Threading.Thread.Sleep(5000); to OptimisticLockedTextFile.Persist() just after the file is opened
        /// 2.  uncomment the [TestMethod] attribute on this method
        /// 3.  run the test
        /// 4.  REVERT YOUR CHANGES FROM STEPS 1 AND 2
        /// </summary>
        //[TestMethod]
        public void FileLockedByThisProcess()
        {
            using (var tester = new OptimisticLockedTextFileTester(true))
            {
                // start the persist thread
                Thread thread = new Thread(new ThreadStart(() => { tester.TextFile.Persist(); }));
                thread.Start();

                // give it time to open the file
                Thread.Sleep(2000);

                // try something that requires minimal access to the file and make sure the correct exception is thrown
                AssertExtensions.ExpectException(() =>
                {
                    using (File.Open(tester.TextFile.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                    }
                }, typeof(IOException), string.Format(FileLockedMessageFormat, tester.TextFile.FilePath));

                // wait for the persist to complete
                thread.Join();
            }
        }
 public void PersistEmpty()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.TextFile.Persist();
         tester.AssertFileContents(String.Empty);
     }
 }
 public void PersistTwice()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.AssertPersist(WindowsLine, WindowsLine);
         tester.AssertPersist(WindowsLine + WindowsLine, WindowsLine);
     }
 }
 public void ProperTruncation()
 {
     using (var tester = new OptimisticLockedTextFileTester())
     {
         tester.AssertPersist(UnixLine + UnixLine, UnixLine, UnixLine);
         tester.TextFile.Lines.RemoveAt(0);
         tester.TextFile.Persist();
         tester.AssertFileContents(UnixLine);
     }
 }
 public void PersistAddsLineEnding()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.AssertPersist(Line + Environment.NewLine + Line, Line, Line);
     }
 }
 public void FileChanged()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.TextFile.Lines.Add(Line);
         using (var streamWriter = File.AppendText(tester.TextFile.FilePath))
         {
             streamWriter.Write("changed");
         }
         AssertExtensions.ExpectException(() =>
         {
             tester.TextFile.Persist();
         }, typeof(IOException), string.Format(FileChangedMessageFormat, tester.TextFile.FilePath));
     }
 }
 public void FileLockedByAnotherProcess()
 {
     using (var tester = new OptimisticLockedTextFileTester())
     {
         tester.TextFile.Lines.Add(Line);
         using (var streamWriter = File.AppendText(tester.TextFile.FilePath))
         {
             AssertExtensions.ExpectException(() =>
             {
                 tester.TextFile.Persist();
             }, typeof(IOException), string.Format(FileLockedMessageFormat, tester.TextFile.FilePath));
         }
     }
 }
        /// <summary>
        /// This test needs to be run manually since it relies on a race condition and a manual code change.
        /// It's been run as part of development but it won't be run in the normal build process.
        ///
        /// This test makes sure that the OptimisticLockedTextFile.Persist() method locks the file properly while it's being written.
        /// To run this test:
        /// 1.  temporarily add System.Threading.Thread.Sleep(5000); to OptimisticLockedTextFile.Persist() just after the file is opened
        /// 2.  uncomment the [TestMethod] attribute on this method
        /// 3.  run the test
        /// 4.  REVERT YOUR CHANGES FROM STEPS 1 AND 2
        /// </summary>
        //[TestMethod]
        public void FileLockedByThisProcess()
        {
            using (var tester = new OptimisticLockedTextFileTester(true))
            {
                // start the persist thread
                Thread thread = new Thread(new ThreadStart(() => { tester.TextFile.Persist(); }));
                thread.Start();

                // give it time to open the file
                Thread.Sleep(2000);

                // try something that requires minimal access to the file and make sure the correct exception is thrown
                AssertExtensions.ExpectException(() =>
                {
                    using (File.Open(tester.TextFile.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                    }
                }, typeof(IOException), string.Format(FileLockedMessageFormat, tester.TextFile.FilePath));

                // wait for the persist to complete
                thread.Join();
            }
        }
 public void ReadMixedlines4()
 {
     using (var tester = new OptimisticLockedTextFileTester(Line + WindowsLine))
     {
         tester.AssertLines(Line + WindowsLine);
     }
 }
 public void PersistLine()
 {
     using (var tester = new OptimisticLockedTextFileTester(true))
     {
         tester.AssertPersist(Line, Line);
     }
 }