StreamWriterTest.cs 9.2 KB

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