StreamWriterTest.cs 9.8 KB

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