StreamWriterTest.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // StreamWriterTest.cs - NUnit Test Cases for the SystemIO.StreamWriter class
  2. //
  3. // David Brandt ([email protected])
  4. //
  5. // (C) Ximian, Inc. http://www.ximian.com
  6. //
  7. using NUnit.Framework;
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. namespace MonoTests.System.IO
  12. {
  13. public class StreamWriterTest : Assertion
  14. {
  15. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  16. private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  17. private string _thisCodeFileName = TempFolder + Path.DirectorySeparatorChar + "StreamWriterTest.temp";
  18. [SetUp]
  19. public void SetUp ()
  20. {
  21. if (Directory.Exists (TempFolder))
  22. Directory.Delete (TempFolder, true);
  23. Directory.CreateDirectory (TempFolder);
  24. if (!File.Exists (_thisCodeFileName))
  25. File.Create (_thisCodeFileName).Close ();
  26. }
  27. [TearDown]
  28. public void TearDown ()
  29. {
  30. if (Directory.Exists (TempFolder))
  31. Directory.Delete (TempFolder, true);
  32. }
  33. // TODO - ctors
  34. [Test]
  35. public void TestCtor1() {
  36. {
  37. bool errorThrown = false;
  38. try {
  39. StreamWriter r = new StreamWriter((Stream)null);
  40. } catch (ArgumentNullException) {
  41. errorThrown = true;
  42. } catch (Exception e) {
  43. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  44. }
  45. Assert("null string error not thrown", errorThrown);
  46. }
  47. {
  48. bool errorThrown = false;
  49. FileStream f = new FileStream(_thisCodeFileName,
  50. FileMode.Open,
  51. FileAccess.Read);
  52. try {
  53. StreamWriter r = new StreamWriter(f);
  54. r.Close();
  55. } catch (ArgumentException) {
  56. errorThrown = true;
  57. } catch (Exception e) {
  58. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  59. }
  60. f.Close();
  61. Assert("no read error not thrown", errorThrown);
  62. }
  63. {
  64. FileStream f = new FileStream(_codeFileName,
  65. FileMode.Append,
  66. FileAccess.Write);
  67. StreamWriter r = new StreamWriter(f);
  68. AssertNotNull("no stream writer", r);
  69. r.Close();
  70. f.Close();
  71. }
  72. }
  73. [Test]
  74. public void TestCtor2() {
  75. {
  76. bool errorThrown = false;
  77. try {
  78. StreamWriter r = new StreamWriter("");
  79. } catch (ArgumentException) {
  80. errorThrown = true;
  81. } catch (Exception e) {
  82. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  83. }
  84. Assert("empty string error not thrown", errorThrown);
  85. }
  86. {
  87. bool errorThrown = false;
  88. try {
  89. StreamWriter r = new StreamWriter((string)null);
  90. } catch (ArgumentNullException) {
  91. errorThrown = true;
  92. } catch (Exception e) {
  93. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  94. }
  95. Assert("null string error not thrown", errorThrown);
  96. }
  97. {
  98. bool errorThrown = false;
  99. try {
  100. StreamWriter r = new StreamWriter("nonexistentdir/file");
  101. } catch (DirectoryNotFoundException) {
  102. errorThrown = true;
  103. } catch (Exception e) {
  104. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  105. }
  106. Assert("dirNotFound error not thrown", errorThrown);
  107. }
  108. {
  109. bool errorThrown = false;
  110. try {
  111. StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
  112. } catch (IOException) {
  113. errorThrown = true;
  114. } catch (ArgumentException) {
  115. // FIXME - the spec says 'IOExc', but the
  116. // compiler says 'ArgExc'...
  117. errorThrown = true;
  118. } catch (Exception e) {
  119. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  120. }
  121. Assert("1 invalid filename error not thrown", errorThrown);
  122. }
  123. // TODO - Security/Auth exceptions
  124. {
  125. StreamWriter r = new StreamWriter(_codeFileName);
  126. AssertNotNull("no stream writer", r);
  127. r.Close();
  128. }
  129. }
  130. [Test]
  131. public void TestCtor3() {
  132. {
  133. bool errorThrown = false;
  134. try {
  135. StreamWriter r = new StreamWriter("", false);
  136. } catch (ArgumentException) {
  137. errorThrown = true;
  138. } catch (Exception e) {
  139. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  140. }
  141. Assert("empty string error not thrown", errorThrown);
  142. }
  143. {
  144. bool errorThrown = false;
  145. try {
  146. StreamWriter r = new StreamWriter((string)null, false);
  147. } catch (ArgumentNullException) {
  148. errorThrown = true;
  149. } catch (Exception e) {
  150. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  151. }
  152. Assert("null string error not thrown", errorThrown);
  153. }
  154. {
  155. bool errorThrown = false;
  156. try {
  157. StreamWriter r = new StreamWriter("nonexistentdir/file", false);
  158. } catch (DirectoryNotFoundException) {
  159. errorThrown = true;
  160. } catch (Exception e) {
  161. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  162. }
  163. Assert("dirNotFound error not thrown", errorThrown);
  164. }
  165. {
  166. bool errorThrown = false;
  167. try {
  168. StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
  169. } catch (IOException) {
  170. errorThrown = true;
  171. } catch (ArgumentException) {
  172. // FIXME - the spec says 'IOExc', but the
  173. // compiler says 'ArgExc'...
  174. errorThrown = true;
  175. } catch (Exception e) {
  176. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  177. }
  178. Assert("2 invalid filename error not thrown", errorThrown);
  179. }
  180. {
  181. StreamWriter r = new StreamWriter(_codeFileName, false);
  182. AssertNotNull("no stream writer", r);
  183. r.Close();
  184. }
  185. {
  186. bool errorThrown = false;
  187. try {
  188. StreamWriter r = new StreamWriter("", true);
  189. } catch (ArgumentException) {
  190. errorThrown = true;
  191. } catch (Exception e) {
  192. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  193. }
  194. Assert("empty string error not thrown", errorThrown);
  195. }
  196. {
  197. bool errorThrown = false;
  198. try {
  199. StreamWriter r = new StreamWriter((string)null, true);
  200. } catch (ArgumentNullException) {
  201. errorThrown = true;
  202. } catch (Exception e) {
  203. Fail ("Incorrect exception thrown at 6: " + e.ToString());
  204. }
  205. Assert("null string error not thrown", errorThrown);
  206. }
  207. {
  208. bool errorThrown = false;
  209. try {
  210. StreamWriter r = new StreamWriter("nonexistentdir/file", true);
  211. } catch (DirectoryNotFoundException) {
  212. errorThrown = true;
  213. } catch (Exception e) {
  214. Fail ("Incorrect exception thrown at 7: " + e.ToString());
  215. }
  216. Assert("dirNotFound error not thrown", errorThrown);
  217. }
  218. {
  219. bool errorThrown = false;
  220. try {
  221. StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
  222. } catch (IOException) {
  223. errorThrown = true;
  224. } catch (ArgumentException) {
  225. // FIXME - the spec says 'IOExc', but the
  226. // compiler says 'ArgExc'...
  227. errorThrown = true;
  228. } catch (Exception e) {
  229. Fail ("Incorrect exception thrown at 8: " + e.ToString());
  230. }
  231. Assert("3 invalid filename error not thrown", errorThrown);
  232. }
  233. {
  234. try {
  235. StreamWriter r = new StreamWriter(_codeFileName, true);
  236. AssertNotNull("no stream writer", r);
  237. r.Close();
  238. } catch (Exception e) {
  239. Fail ("Unxpected exception e=" + e.ToString());
  240. }
  241. }
  242. }
  243. // TODO - ctors with Encoding
  244. // TODO - AutoFlush
  245. [Test]
  246. public void TestAutoFlush() {
  247. {
  248. MemoryStream m = new MemoryStream();
  249. StreamWriter w = new StreamWriter(m);
  250. w.AutoFlush = false;
  251. w.Write(1);
  252. w.Write(2);
  253. w.Write(3);
  254. w.Write(4);
  255. AssertEquals("Should be nothing before flush",
  256. 0L, m.Length);
  257. w.Flush();
  258. AssertEquals("Should be something after flush",
  259. 4L, m.Length);
  260. }
  261. {
  262. MemoryStream m = new MemoryStream();
  263. StreamWriter w = new StreamWriter(m);
  264. w.AutoFlush = true;
  265. w.Write(1);
  266. w.Write(2);
  267. w.Write(3);
  268. w.Write(4);
  269. AssertEquals("Should be something before flush",
  270. 4L, m.Length);
  271. w.Flush();
  272. AssertEquals("Should be something after flush",
  273. 4L, m.Length);
  274. }
  275. }
  276. [Test]
  277. public void TestBaseStream() {
  278. FileStream f = new FileStream(_codeFileName,
  279. FileMode.Append,
  280. FileAccess.Write);
  281. StreamWriter r = new StreamWriter(f);
  282. AssertEquals("wrong base stream ", f, r.BaseStream);
  283. r.Close();
  284. f.Close();
  285. }
  286. [Test]
  287. public void TestEncoding() {
  288. StreamWriter r = new StreamWriter(_codeFileName);
  289. AssertEquals("wrong encoding",
  290. Encoding.UTF8.GetType(), r.Encoding.GetType());
  291. r.Close();
  292. }
  293. // TODO - Close - not entirely sure how to test Close
  294. //public void TestClose() {
  295. //{
  296. //MemoryStream m = new MemoryStream();
  297. //StreamWriter w = new StreamWriter(m);
  298. //StreamReader r = new StreamReader(m);
  299. //w.Write(1);
  300. //w.Write(2);
  301. //w.Write(3);
  302. //w.Write(4);
  303. //AssertEquals("Should be nothing before close",
  304. //0, m.Length);
  305. //AssertEquals("Should be nothing in reader",
  306. //-1, r.Peek());
  307. //w.Close();
  308. //AssertEquals("Should be something after close",
  309. //1, r.Peek());
  310. //}
  311. //}
  312. // TODO - Flush
  313. [Test]
  314. public void TestFlush() {
  315. {
  316. bool errorThrown = false;
  317. try {
  318. FileStream f = new FileStream(_codeFileName,
  319. FileMode.Append,
  320. FileAccess.Write);
  321. StreamWriter r = new StreamWriter(f);
  322. r.Close();
  323. r.Flush();
  324. } catch (ObjectDisposedException) {
  325. errorThrown = true;
  326. } catch (Exception e) {
  327. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  328. }
  329. Assert("can't flush closed error not thrown", errorThrown);
  330. }
  331. {
  332. MemoryStream m = new MemoryStream();
  333. StreamWriter w = new StreamWriter(m);
  334. w.Write(1);
  335. w.Write(2);
  336. w.Write(3);
  337. w.Write(4);
  338. AssertEquals("Should be nothing before flush",
  339. 0L, m.Length);
  340. w.Flush();
  341. AssertEquals("Should be something after flush",
  342. 4L, m.Length);
  343. }
  344. }
  345. // TODO - Write - test errors, functionality tested in TestFlush.
  346. }
  347. }