StreamWriterTest.cs 9.3 KB

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