PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/XAct.IO/XAct.IO.FS.Tests/IFSIOServiceFileTests.cs

https://bitbucket.org/xact/cs.ff.xact.lib
C# | 351 lines | 179 code | 99 blank | 73 comment | 1 complexity | 645435453b49a050338d94fd2e7c9162 MD5 | raw file
  1. namespace XAct.IO.FS.Tests
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using NUnit.Framework;
  7. using XAct;
  8. using XAct.IO;
  9. using XAct.IO.Implementations;
  10. using XAct.Tests;
  11. /// <summary>
  12. /// Unit Tests
  13. /// </summary>
  14. [TestFixture]
  15. public class IFSIOServiceFileTests
  16. {
  17. /// <summary>
  18. /// Sets up to do before any tests
  19. /// within this test fixture are run.
  20. /// </summary>
  21. [TestFixtureSetUp]
  22. public void TestFixtureSetUp()
  23. {
  24. //run once before any tests in this testfixture have been run...
  25. //...setup vars common to all the upcoming tests...
  26. Singleton<IocContext>.Instance.ResetIoC();
  27. }
  28. /// <summary>
  29. /// Tear down after all tests in this fixture.
  30. /// </summary>
  31. [TestFixtureTearDown]
  32. public void TestFixtureTearDown()
  33. {
  34. }
  35. [TearDown]
  36. public void MyTestTearDown()
  37. {
  38. GC.Collect();
  39. }
  40. [Test]
  41. public void CanGetIFSIOService()
  42. {
  43. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  44. Assert.IsNotNull(ioService);
  45. }
  46. [Test]
  47. public void CanGetIFSIOServiceOfExpectedType()
  48. {
  49. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  50. Assert.AreEqual(typeof(FSIOService), ioService.GetType());
  51. }
  52. [Test]
  53. public void FileExistsWorksWithNonExistentFiles()
  54. {
  55. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  56. Assert.IsFalse(ioService.FileExistsAsync("c:\\NonExistentFile.txt").Result);
  57. }
  58. [Test]
  59. public void FileExistsWorksWithNonExistentFilesAsync()
  60. {
  61. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  62. Assert.IsFalse(ioService.FileExistsAsync("c:\\NonExistentFile.txt").WaitAndGetResult());
  63. }
  64. [Test]
  65. public void FileExistsWorksWithExistentFiles()
  66. {
  67. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  68. const string filePath = "%userprofile%\\unittest_check_B3.txt";
  69. EnsureFileDoesNotExist(filePath);
  70. Assert.IsFalse(ioService.FileExistsAsync(filePath).Result);
  71. }
  72. [Test]
  73. public void FileExistsWorksWithExistentFilesAsync()
  74. {
  75. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  76. const string filePath = "%userprofile%\\unittest_check_B4.txt";
  77. EnsureFileDoesNotExist(filePath);
  78. Assert.IsFalse(ioService.FileExistsAsync(filePath).WaitAndGetResult());
  79. }
  80. /* NOT GAURANTEED TO COMPLETE IN TIME
  81. [Test]
  82. public void FileCreateWorksWithExistentFiles()
  83. {
  84. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  85. const string filePath = "%userprofile%\\unittest_check_B6.txt";
  86. EnsureFileDoesNotExist(filePath);
  87. Assert.IsFalse(ioService.FileExistsAsync(filePath).Result,"Check1");
  88. ioService.FileOpenWriteAsync(filePath).Result.AppendAllText("test");
  89. Assert.IsTrue(ioService.FileExistsAsync(filePath).Result);
  90. ioService.FileDeleteAsync(filePath);
  91. Assert.IsFalse(ioService.FileExistsAsync(filePath).Result, "Check2");
  92. }
  93. */
  94. [Test]
  95. public void FileCreateWorksWithExistentFilesAsync()
  96. {
  97. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  98. const string filePath = "%userprofile%\\unittest_check_B7.txt";
  99. EnsureFileDoesNotExist(filePath);
  100. Assert.IsFalse(ioService.FileExistsAsync(filePath).Result);
  101. ioService.FileOpenWriteAsync(filePath).Result.AppendAllText("test");
  102. Assert.IsTrue(ioService.FileExistsAsync(filePath).Result);
  103. ioService.FileDeleteAsync(filePath).Wait();
  104. Assert.IsFalse(ioService.FileExistsAsync(filePath).WaitAndGetResult());
  105. }
  106. [Test]
  107. public void FileCreateWorksAndSoDoesFileOpenRead()
  108. {
  109. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  110. const string filePath = "%userprofile%\\unittest_check_B8.txt";
  111. EnsureFileDoesNotExist(filePath);
  112. Assert.IsFalse(ioService.FileExistsAsync(filePath).Result);
  113. ioService.FileOpenWriteAsync(filePath).Result.AppendAllText("test");
  114. Assert.IsTrue(ioService.FileExistsAsync(filePath).Result,"Check #1");
  115. string check = ioService.FileOpenReadAsync(filePath).Result.ReadToEnd();
  116. Assert.IsTrue(check.StartsWith("test"), "Check #2");
  117. ioService.FileDeleteAsync(filePath);
  118. Assert.IsFalse(ioService.FileExistsAsync(filePath).Result, "Check #3");
  119. }
  120. [Test]
  121. public void FileCreateWorksAndSoDoesFileOpenReadAsync()
  122. {
  123. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  124. const string filePath = "%userprofile%\\unittest_check_B2.txt";
  125. EnsureFileDoesNotExist(filePath);
  126. Assert.IsFalse(ioService.FileExistsAsync(filePath).WaitAndGetResult());
  127. ioService.FileOpenWriteAsync(filePath).WaitAndGetResult().AppendAllText("test");
  128. Assert.IsTrue(ioService.FileExistsAsync(filePath).WaitAndGetResult(), "Check #1");
  129. string check = ioService.FileOpenReadAsync(filePath).WaitAndGetResult().ReadToEnd();
  130. Assert.IsTrue(check.StartsWith("test"), "Check #2");
  131. ioService.FileDeleteAsync(filePath).Wait();
  132. Assert.IsFalse(ioService.FileExistsAsync(filePath).WaitAndGetResult(), "Check #3");
  133. }
  134. /* NOT GAURANTEED TO COMPLETE IN TIME
  135. [Test]
  136. public void FileCreateWorksAndSoDoesFileReadWrittingSeveralLines()
  137. {
  138. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  139. const string filePath = "%userprofile%\\unittest_check_B0.txt";
  140. EnsureFileDoesNotExist(filePath);
  141. * Assert.IsFalse(ioService.FileExistsAsync(filePath).Result);
  142. string[] lines = new string[] {"test", "line2", "line3"};
  143. ioService.FileOpenWriteAsync(filePath).Result.AppendAllText(lines);
  144. Assert.IsTrue(ioService.FileExistsAsync(filePath).Result);
  145. string check = ioService.FileOpenReadAsync(filePath).Result.ReadToEnd();
  146. Assert.IsTrue(check.StartsWith("test"));
  147. string[] check2 = ioService.FileOpenReadAsync(filePath).Result.ReadLines().ToArray();
  148. Assert.AreEqual(3, check2.Length);
  149. Assert.IsTrue(check2[0].StartsWith("test"));
  150. ioService.FileDeleteAsync(filePath);
  151. Assert.IsFalse(ioService.FileExistsAsync(filePath).Result);
  152. }
  153. */
  154. [Test]
  155. public void FileCreateWorksAndSoDoesFileReadWrittingSeveralLinesAsync()
  156. {
  157. IFSIOService ioService = XAct.DependencyResolver.Current.GetInstance<IFSIOService>();
  158. const string filePath = "%userprofile%\\unittest_check_B1.txt";
  159. EnsureFileDoesNotExist(filePath);
  160. Assert.IsFalse(ioService.FileExistsAsync(filePath).WaitAndGetResult());
  161. string[] lines = new string[] { "test", "line2", "line3" };
  162. ioService.FileOpenWriteAsync(filePath).WaitAndGetResult().AppendAllText(lines);
  163. Assert.IsTrue(ioService.FileExistsAsync(filePath).WaitAndGetResult());
  164. string check = ioService.FileOpenReadAsync(filePath).WaitAndGetResult().ReadToEnd();
  165. Assert.IsTrue(check.StartsWith("test"));
  166. string[] check2 = ioService.FileOpenReadAsync(filePath).WaitAndGetResult().ReadLines().ToArray();
  167. Assert.AreEqual(3, check2.Length);
  168. Assert.IsTrue(check2[0].StartsWith("test"));
  169. ioService.FileDeleteAsync(filePath).Wait();
  170. Assert.IsFalse(ioService.FileExistsAsync(filePath).WaitAndGetResult());
  171. }
  172. /// <summary>
  173. /// TODO: Describe Test.
  174. /// </summary>
  175. [Test]
  176. public void UnitTestWriteReadFromSameFile()
  177. {
  178. string lineText = "Test";
  179. IFSIOService ioService = DependencyResolver.Current.GetInstance<IFSIOService>();
  180. //Should have got back IsolatedStorage service:
  181. using (Stream stream = ioService.FileOpenWriteAsync("%userprofile%\\Test.txt").Result)
  182. {
  183. using (StreamWriter streamWriter = stream.CreateStreamWriter())
  184. {
  185. streamWriter.WriteLine(lineText + ": " + DateTime.Now);
  186. }
  187. }
  188. using (Stream stream = ioService.FileOpenReadAsync("%userprofile%\\Test.txt").Result)
  189. {
  190. using (StreamReader streamWriter = stream.CreateStreamReader())
  191. {
  192. string result = streamWriter.ReadLine();
  193. Assert.AreEqual(result.Substring(0, lineText.Length), lineText);
  194. }
  195. }
  196. Assert.IsTrue(true);
  197. }
  198. /// <summary>
  199. /// TODO: Describe Test.
  200. /// </summary>
  201. [Test]
  202. public void UnitTestWriteReadFromSameFileAsync()
  203. {
  204. string lineText = "Test";
  205. IFSIOService ioService = DependencyResolver.Current.GetInstance<IFSIOService>();
  206. //Should have got back IsolatedStorage service:
  207. using (Stream stream = ioService.FileOpenWriteAsync("%userprofile%\\Test.txt").WaitAndGetResult())
  208. {
  209. using (StreamWriter streamWriter = stream.CreateStreamWriter())
  210. {
  211. streamWriter.WriteLine(lineText + ": " + DateTime.Now);
  212. }
  213. }
  214. using (Stream stream = ioService.FileOpenReadAsync("%userprofile%\\Test.txt").WaitAndGetResult())
  215. {
  216. using (StreamReader streamWriter = stream.CreateStreamReader())
  217. {
  218. string result = streamWriter.ReadLine();
  219. Assert.AreEqual(result.Substring(0, lineText.Length), lineText);
  220. }
  221. }
  222. Assert.IsTrue(true);
  223. }
  224. static void EnsureFileDoesNotExist(string filePath)
  225. {
  226. IIOService ioService = XAct.DependencyResolver.Current.GetInstance<IIOService>();
  227. if (ioService.FileExistsAsync(filePath).WaitAndGetResult())
  228. {
  229. ioService.FileDeleteAsync(filePath).Wait();
  230. }
  231. }
  232. }
  233. }