StreamWriterTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. [TestFixture]
  14. public class StreamWriterTest : Assertion
  15. {
  16. static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  17. private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  18. private string _thisCodeFileName = TempFolder + Path.DirectorySeparatorChar + "StreamWriterTest.temp";
  19. [SetUp]
  20. public void SetUp ()
  21. {
  22. if (Directory.Exists (TempFolder))
  23. Directory.Delete (TempFolder, true);
  24. Directory.CreateDirectory (TempFolder);
  25. if (!File.Exists (_thisCodeFileName))
  26. File.Create (_thisCodeFileName).Close ();
  27. }
  28. [TearDown]
  29. public void TearDown ()
  30. {
  31. if (Directory.Exists (TempFolder))
  32. Directory.Delete (TempFolder, true);
  33. }
  34. // TODO - ctors
  35. [Test]
  36. public void TestCtor1() {
  37. {
  38. bool errorThrown = false;
  39. try {
  40. StreamWriter r = new StreamWriter((Stream)null);
  41. } catch (ArgumentNullException) {
  42. errorThrown = true;
  43. } catch (Exception e) {
  44. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  45. }
  46. Assert("null string error not thrown", errorThrown);
  47. }
  48. {
  49. bool errorThrown = false;
  50. FileStream f = new FileStream(_thisCodeFileName,
  51. FileMode.Open,
  52. FileAccess.Read);
  53. try {
  54. StreamWriter r = new StreamWriter(f);
  55. r.Close();
  56. } catch (ArgumentException) {
  57. errorThrown = true;
  58. } catch (Exception e) {
  59. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  60. }
  61. f.Close();
  62. Assert("no read error not thrown", errorThrown);
  63. }
  64. {
  65. FileStream f = new FileStream(_codeFileName,
  66. FileMode.Append,
  67. FileAccess.Write);
  68. StreamWriter r = new StreamWriter(f);
  69. AssertNotNull("no stream writer", r);
  70. r.Close();
  71. f.Close();
  72. }
  73. }
  74. [Test]
  75. public void TestCtor2() {
  76. {
  77. bool errorThrown = false;
  78. try {
  79. StreamWriter r = new StreamWriter("");
  80. } catch (ArgumentException) {
  81. errorThrown = true;
  82. } catch (Exception e) {
  83. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  84. }
  85. Assert("empty string error not thrown", errorThrown);
  86. }
  87. {
  88. bool errorThrown = false;
  89. try {
  90. StreamWriter r = new StreamWriter((string)null);
  91. } catch (ArgumentNullException) {
  92. errorThrown = true;
  93. } catch (Exception e) {
  94. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  95. }
  96. Assert("null string error not thrown", errorThrown);
  97. }
  98. {
  99. bool errorThrown = false;
  100. try {
  101. StreamWriter r = new StreamWriter("nonexistentdir/file");
  102. } catch (DirectoryNotFoundException) {
  103. errorThrown = true;
  104. } catch (Exception e) {
  105. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  106. }
  107. Assert("dirNotFound error not thrown", errorThrown);
  108. }
  109. {
  110. bool errorThrown = false;
  111. try {
  112. StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
  113. } catch (IOException) {
  114. errorThrown = true;
  115. } catch (ArgumentException) {
  116. // FIXME - the spec says 'IOExc', but the
  117. // compiler says 'ArgExc'...
  118. errorThrown = true;
  119. } catch (Exception e) {
  120. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  121. }
  122. Assert("1 invalid filename error not thrown", errorThrown);
  123. }
  124. // TODO - Security/Auth exceptions
  125. {
  126. StreamWriter r = new StreamWriter(_codeFileName);
  127. AssertNotNull("no stream writer", r);
  128. r.Close();
  129. }
  130. }
  131. [Test]
  132. public void TestCtor3() {
  133. {
  134. bool errorThrown = false;
  135. try {
  136. StreamWriter r = new StreamWriter("", false);
  137. } catch (ArgumentException) {
  138. errorThrown = true;
  139. } catch (Exception e) {
  140. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  141. }
  142. Assert("empty string error not thrown", errorThrown);
  143. }
  144. {
  145. bool errorThrown = false;
  146. try {
  147. StreamWriter r = new StreamWriter((string)null, false);
  148. } catch (ArgumentNullException) {
  149. errorThrown = true;
  150. } catch (Exception e) {
  151. Fail ("Incorrect exception thrown at 2: " + e.ToString());
  152. }
  153. Assert("null string error not thrown", errorThrown);
  154. }
  155. {
  156. bool errorThrown = false;
  157. try {
  158. StreamWriter r = new StreamWriter("nonexistentdir/file", false);
  159. } catch (DirectoryNotFoundException) {
  160. errorThrown = true;
  161. } catch (Exception e) {
  162. Fail ("Incorrect exception thrown at 3: " + e.ToString());
  163. }
  164. Assert("dirNotFound error not thrown", errorThrown);
  165. }
  166. {
  167. bool errorThrown = false;
  168. try {
  169. StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
  170. } catch (IOException) {
  171. errorThrown = true;
  172. } catch (ArgumentException) {
  173. // FIXME - the spec says 'IOExc', but the
  174. // compiler says 'ArgExc'...
  175. errorThrown = true;
  176. } catch (Exception e) {
  177. Fail ("Incorrect exception thrown at 4: " + e.ToString());
  178. }
  179. Assert("2 invalid filename error not thrown", errorThrown);
  180. }
  181. {
  182. StreamWriter r = new StreamWriter(_codeFileName, false);
  183. AssertNotNull("no stream writer", r);
  184. r.Close();
  185. }
  186. {
  187. bool errorThrown = false;
  188. try {
  189. StreamWriter r = new StreamWriter("", true);
  190. } catch (ArgumentException) {
  191. errorThrown = true;
  192. } catch (Exception e) {
  193. Fail ("Incorrect exception thrown at 5: " + e.ToString());
  194. }
  195. Assert("empty string error not thrown", errorThrown);
  196. }
  197. {
  198. bool errorThrown = false;
  199. try {
  200. StreamWriter r = new StreamWriter((string)null, true);
  201. } catch (ArgumentNullException) {
  202. errorThrown = true;
  203. } catch (Exception e) {
  204. Fail ("Incorrect exception thrown at 6: " + e.ToString());
  205. }
  206. Assert("null string error not thrown", errorThrown);
  207. }
  208. {
  209. bool errorThrown = false;
  210. try {
  211. StreamWriter r = new StreamWriter("nonexistentdir/file", true);
  212. } catch (DirectoryNotFoundException) {
  213. errorThrown = true;
  214. } catch (Exception e) {
  215. Fail ("Incorrect exception thrown at 7: " + e.ToString());
  216. }
  217. Assert("dirNotFound error not thrown", errorThrown);
  218. }
  219. {
  220. bool errorThrown = false;
  221. try {
  222. StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
  223. } catch (IOException) {
  224. errorThrown = true;
  225. } catch (ArgumentException) {
  226. // FIXME - the spec says 'IOExc', but the
  227. // compiler says 'ArgExc'...
  228. errorThrown = true;
  229. } catch (Exception e) {
  230. Fail ("Incorrect exception thrown at 8: " + e.ToString());
  231. }
  232. Assert("3 invalid filename error not thrown", errorThrown);
  233. }
  234. {
  235. try {
  236. StreamWriter r = new StreamWriter(_codeFileName, true);
  237. AssertNotNull("no stream writer", r);
  238. r.Close();
  239. } catch (Exception e) {
  240. Fail ("Unxpected exception e=" + e.ToString());
  241. }
  242. }
  243. }
  244. // TODO - ctors with Encoding
  245. // TODO - AutoFlush
  246. [Test]
  247. public void TestAutoFlush() {
  248. {
  249. MemoryStream m = new MemoryStream();
  250. StreamWriter w = new StreamWriter(m);
  251. w.AutoFlush = false;
  252. w.Write(1);
  253. w.Write(2);
  254. w.Write(3);
  255. w.Write(4);
  256. AssertEquals("Should be nothing before flush",
  257. 0L, m.Length);
  258. w.Flush();
  259. AssertEquals("Should be something after flush",
  260. 4L, m.Length);
  261. }
  262. {
  263. MemoryStream m = new MemoryStream();
  264. StreamWriter w = new StreamWriter(m);
  265. w.AutoFlush = true;
  266. w.Write(1);
  267. w.Write(2);
  268. w.Write(3);
  269. w.Write(4);
  270. AssertEquals("Should be something before flush",
  271. 4L, m.Length);
  272. w.Flush();
  273. AssertEquals("Should be something after flush",
  274. 4L, m.Length);
  275. }
  276. }
  277. [Test]
  278. public void TestBaseStream() {
  279. FileStream f = new FileStream(_codeFileName,
  280. FileMode.Append,
  281. FileAccess.Write);
  282. StreamWriter r = new StreamWriter(f);
  283. AssertEquals("wrong base stream ", f, r.BaseStream);
  284. r.Close();
  285. f.Close();
  286. }
  287. [Test]
  288. public void TestEncoding() {
  289. StreamWriter r = new StreamWriter(_codeFileName);
  290. AssertEquals("wrong encoding",
  291. Encoding.UTF8.GetType(), r.Encoding.GetType());
  292. r.Close();
  293. }
  294. // TODO - Close - not entirely sure how to test Close
  295. //public void TestClose() {
  296. //{
  297. //MemoryStream m = new MemoryStream();
  298. //StreamWriter w = new StreamWriter(m);
  299. //StreamReader r = new StreamReader(m);
  300. //w.Write(1);
  301. //w.Write(2);
  302. //w.Write(3);
  303. //w.Write(4);
  304. //AssertEquals("Should be nothing before close",
  305. //0, m.Length);
  306. //AssertEquals("Should be nothing in reader",
  307. //-1, r.Peek());
  308. //w.Close();
  309. //AssertEquals("Should be something after close",
  310. //1, r.Peek());
  311. //}
  312. //}
  313. // TODO - Flush
  314. [Test]
  315. public void TestFlush() {
  316. {
  317. bool errorThrown = false;
  318. try {
  319. FileStream f = new FileStream(_codeFileName,
  320. FileMode.Append,
  321. FileAccess.Write);
  322. StreamWriter r = new StreamWriter(f);
  323. r.Close();
  324. r.Flush();
  325. } catch (ObjectDisposedException) {
  326. errorThrown = true;
  327. } catch (Exception e) {
  328. Fail ("Incorrect exception thrown at 1: " + e.ToString());
  329. }
  330. Assert("can't flush closed error not thrown", errorThrown);
  331. }
  332. {
  333. MemoryStream m = new MemoryStream();
  334. StreamWriter w = new StreamWriter(m);
  335. w.Write(1);
  336. w.Write(2);
  337. w.Write(3);
  338. w.Write(4);
  339. AssertEquals("Should be nothing before flush",
  340. 0L, m.Length);
  341. w.Flush();
  342. AssertEquals("Should be something after flush",
  343. 4L, m.Length);
  344. }
  345. }
  346. [Test]
  347. [ExpectedException (typeof (ObjectDisposedException))]
  348. public void AutoFlush_Disposed ()
  349. {
  350. StreamWriter w = new StreamWriter (new MemoryStream ());
  351. w.Close ();
  352. w.AutoFlush = true;
  353. }
  354. [Test]
  355. [ExpectedException (typeof (ObjectDisposedException))]
  356. public void WriteChar_Disposed ()
  357. {
  358. StreamWriter w = new StreamWriter (new MemoryStream ());
  359. w.Close ();
  360. w.Write ('A');
  361. }
  362. [Test]
  363. [ExpectedException (typeof (ObjectDisposedException))]
  364. public void WriteCharArray_Disposed ()
  365. {
  366. char[] c = new char [2] { 'a', 'b' };
  367. StreamWriter w = new StreamWriter (new MemoryStream ());
  368. w.Close ();
  369. w.Write (c, 0, 2);
  370. }
  371. [Test]
  372. // accepted [ExpectedException (typeof (ArgumentNullException))]
  373. public void WriteCharArray_Null ()
  374. {
  375. char[] c = null;
  376. StreamWriter w = new StreamWriter (new MemoryStream ());
  377. w.Write (c);
  378. }
  379. [Test]
  380. [ExpectedException (typeof (ArgumentException))]
  381. public void WriteCharArray_IndexOverflow ()
  382. {
  383. char[] c = new char [2] { 'a', 'b' };
  384. StreamWriter w = new StreamWriter (new MemoryStream ());
  385. w.Write (c, Int32.MaxValue, 2);
  386. }
  387. [Test]
  388. [ExpectedException (typeof (ArgumentException))]
  389. public void WriteCharArray_CountOverflow ()
  390. {
  391. char[] c = new char [2] { 'a', 'b' };
  392. StreamWriter w = new StreamWriter (new MemoryStream ());
  393. w.Write (c, 1, Int32.MaxValue);
  394. }
  395. [Test]
  396. [ExpectedException (typeof (ObjectDisposedException))]
  397. public void WriteString_Disposed ()
  398. {
  399. StreamWriter w = new StreamWriter (new MemoryStream ());
  400. w.Close ();
  401. w.Write ("mono");
  402. }
  403. [Test]
  404. // accepted [ExpectedException (typeof (ArgumentNullException))]
  405. public void WriteString_Null ()
  406. {
  407. string s = null;
  408. StreamWriter w = new StreamWriter (new MemoryStream ());
  409. w.Write (s);
  410. }
  411. [Test]
  412. public void NoPreambleOnAppend ()
  413. {
  414. MemoryStream ms = new MemoryStream ();
  415. StreamWriter w = new StreamWriter (ms, Encoding.UTF8);
  416. w.Write ("a");
  417. w.Flush ();
  418. AssertEquals ("Incorrect size after writing 1 byte plus header", ms.Position, 4);
  419. // Append 1 byte, should skip the preamble now.
  420. w.Write ("a");
  421. w.Flush ();
  422. w = new StreamWriter (ms, Encoding.UTF8);
  423. AssertEquals ("Incorrect size after writing 1 byte, must have been 5", ms.Position, 5);
  424. }
  425. // TODO - Write - test errors, functionality tested in TestFlush.
  426. }
  427. }