StreamWriterTest.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. using System.Threading;
  12. namespace MonoTests.System.IO
  13. {
  14. [TestFixture]
  15. public class StreamWriterTest
  16. {
  17. class MockStream : Stream
  18. {
  19. bool canRead, canSeek, canWrite;
  20. public event Action OnFlush;
  21. public event Func<byte[], int, int, int> OnRead;
  22. public event Action<byte[], int, int> OnWrite;
  23. long length;
  24. public MockStream (bool canRead, bool canSeek, bool canWrite)
  25. {
  26. this.canRead = canRead;
  27. this.canSeek = canSeek;
  28. this.canWrite = canWrite;
  29. }
  30. public override bool CanRead {
  31. get {
  32. return canRead;
  33. }
  34. }
  35. public override bool CanSeek {
  36. get {
  37. return canSeek;
  38. }
  39. }
  40. public override bool CanWrite {
  41. get {
  42. return canWrite;
  43. }
  44. }
  45. public override void Flush ()
  46. {
  47. if (OnFlush != null)
  48. OnFlush ();
  49. }
  50. public override long Length {
  51. get {
  52. return length;
  53. }
  54. }
  55. public override long Position {
  56. get {
  57. throw new NotImplementedException ();
  58. }
  59. set {
  60. throw new NotImplementedException ();
  61. }
  62. }
  63. public override int Read (byte[] buffer, int offset, int count)
  64. {
  65. if (OnRead != null)
  66. return OnRead (buffer, offset, count);
  67. return -1;
  68. }
  69. public override long Seek (long offset, SeekOrigin origin)
  70. {
  71. throw new NotImplementedException ();
  72. }
  73. public override void SetLength (long value)
  74. {
  75. this.length = value;
  76. }
  77. public override void Write (byte[] buffer, int offset, int count)
  78. {
  79. if (OnWrite != null)
  80. OnWrite (buffer, offset, count);
  81. }
  82. }
  83. private string _tmpFolder;
  84. private string _codeFileName;
  85. private string _thisCodeFileName;
  86. [SetUp]
  87. public void SetUp ()
  88. {
  89. _tmpFolder = Path.GetTempFileName ();
  90. if (File.Exists (_tmpFolder))
  91. File.Delete (_tmpFolder);
  92. _codeFileName = _tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
  93. _thisCodeFileName = _tmpFolder + Path.DirectorySeparatorChar + "StreamWriterTest.temp";
  94. if (Directory.Exists (_tmpFolder))
  95. Directory.Delete (_tmpFolder, true);
  96. Directory.CreateDirectory (_tmpFolder);
  97. if (!File.Exists (_thisCodeFileName))
  98. File.Create (_thisCodeFileName).Close ();
  99. }
  100. [TearDown]
  101. public void TearDown ()
  102. {
  103. if (Directory.Exists (_tmpFolder))
  104. Directory.Delete (_tmpFolder, true);
  105. }
  106. [Test] // .ctor (Stream)
  107. public void Constructor1 ()
  108. {
  109. FileStream f = new FileStream(_codeFileName,
  110. FileMode.Append,
  111. FileAccess.Write);
  112. StreamWriter r = new StreamWriter (f);
  113. Assert.IsFalse (r.AutoFlush, "#1");
  114. Assert.AreSame (f, r.BaseStream, "#2");
  115. Assert.IsNotNull (r.Encoding, "#3");
  116. Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#4");
  117. r.Close();
  118. f.Close();
  119. }
  120. [Test] // .ctor (Stream)
  121. public void Constructor1_Stream_NotWritable ()
  122. {
  123. FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
  124. FileAccess.Read);
  125. try {
  126. new StreamWriter (f);
  127. Assert.Fail ("#B1");
  128. } catch (ArgumentException ex) {
  129. // Stream was not writable
  130. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  131. Assert.IsNull (ex.InnerException, "#3");
  132. Assert.IsNotNull (ex.Message, "#4");
  133. Assert.IsNull (ex.ParamName, "#5");
  134. } finally {
  135. f.Close ();
  136. }
  137. }
  138. [Test] // .ctor (Stream)
  139. public void Constructor1_Stream_Null ()
  140. {
  141. try {
  142. new StreamWriter((Stream) null);
  143. Assert.Fail ("#1");
  144. } catch (ArgumentNullException ex) {
  145. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  146. Assert.IsNull (ex.InnerException, "#3");
  147. Assert.IsNotNull (ex.Message, "#4");
  148. Assert.AreEqual ("stream", ex.ParamName, "#5");
  149. }
  150. }
  151. [Test] // .ctor (String)
  152. public void Constructor2 ()
  153. {
  154. // TODO - Security/Auth exceptions
  155. using (StreamWriter r = new StreamWriter (_codeFileName)) {
  156. Assert.IsFalse (r.AutoFlush, "#1");
  157. Assert.IsNotNull (r.BaseStream, "#2");
  158. Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#3");
  159. Assert.IsNotNull (r.Encoding, "#4");
  160. Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#5");
  161. r.Close ();
  162. }
  163. }
  164. [Test] // .ctor (String)
  165. public void Constructor2_Path_DirectoryNotFound ()
  166. {
  167. Directory.Delete (_tmpFolder, true);
  168. try {
  169. new StreamWriter (_codeFileName);
  170. Assert.Fail ("#1");
  171. } catch (DirectoryNotFoundException ex) {
  172. // Could not find a part of the path '...'
  173. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
  174. Assert.IsNull (ex.InnerException, "#3");
  175. Assert.IsNotNull (ex.Message, "#4");
  176. Assert.IsTrue (ex.Message.IndexOf (_tmpFolder) != -1, "#5");
  177. }
  178. }
  179. [Test] // .ctor (String)
  180. public void Constructor2_Path_Empty ()
  181. {
  182. try {
  183. new StreamWriter (string.Empty);
  184. Assert.Fail ("#1");
  185. } catch (ArgumentException ex) {
  186. // Empty path name is not legal
  187. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  188. Assert.IsNull (ex.InnerException, "#3");
  189. Assert.IsNotNull (ex.Message, "#4");
  190. Assert.IsNull (ex.ParamName, "#5");
  191. }
  192. }
  193. [Test] // .ctor (String)
  194. public void Constructor2_Path_IllegalChars ()
  195. {
  196. try {
  197. new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0]);
  198. Assert.Fail ("#1");
  199. } catch (ArgumentException ex) {
  200. // Illegal characters in path
  201. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  202. Assert.IsNull (ex.InnerException, "#3");
  203. Assert.IsNotNull (ex.Message, "#4");
  204. Assert.IsNull (ex.ParamName, "#5");
  205. }
  206. }
  207. [Test] // .ctor (String)
  208. public void Constructor2_Path_Null ()
  209. {
  210. try {
  211. new StreamWriter ((string) null);
  212. Assert.Fail ("#1");
  213. } catch (ArgumentNullException ex) {
  214. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  215. Assert.IsNull (ex.InnerException, "#3");
  216. Assert.IsNotNull (ex.Message, "#4");
  217. Assert.AreEqual ("path", ex.ParamName, "#5");
  218. }
  219. }
  220. [Test] // .ctor (Stream, Encoding)
  221. public void Constructor3 ()
  222. {
  223. FileStream f = new FileStream (_codeFileName,
  224. FileMode.Append,
  225. FileAccess.Write);
  226. StreamWriter r = new StreamWriter (f, Encoding.ASCII);
  227. Assert.IsFalse (r.AutoFlush, "#1");
  228. Assert.AreSame (f, r.BaseStream, "#2");
  229. Assert.IsNotNull (r.Encoding, "#3");
  230. Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#4");
  231. r.Close ();
  232. f.Close ();
  233. }
  234. [Test] // .ctor (Stream, Encoding)
  235. public void Constructor3_Encoding_Null ()
  236. {
  237. MemoryStream m = new MemoryStream ();
  238. try {
  239. new StreamWriter (m, (Encoding) null);
  240. Assert.Fail ("#1");
  241. } catch (ArgumentNullException ex) {
  242. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  243. Assert.IsNull (ex.InnerException, "#3");
  244. Assert.IsNotNull (ex.Message, "#4");
  245. Assert.AreEqual ("encoding", ex.ParamName, "#5");
  246. }
  247. }
  248. [Test] // .ctor (Stream, Encoding)
  249. public void Constructor3_Stream_NotWritable ()
  250. {
  251. FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
  252. FileAccess.Read);
  253. try {
  254. new StreamWriter (f, Encoding.UTF8);
  255. Assert.Fail ("#B1");
  256. } catch (ArgumentException ex) {
  257. // Stream was not writable
  258. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  259. Assert.IsNull (ex.InnerException, "#3");
  260. Assert.IsNotNull (ex.Message, "#4");
  261. Assert.IsNull (ex.ParamName, "#5");
  262. } finally {
  263. f.Close ();
  264. }
  265. }
  266. [Test] // .ctor (Stream, Encoding)
  267. public void Constructor3_Stream_Null ()
  268. {
  269. try {
  270. new StreamWriter ((Stream) null, Encoding.UTF8);
  271. Assert.Fail ("#1");
  272. } catch (ArgumentNullException ex) {
  273. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  274. Assert.IsNull (ex.InnerException, "#3");
  275. Assert.IsNotNull (ex.Message, "#4");
  276. Assert.AreEqual ("stream", ex.ParamName, "#5");
  277. }
  278. }
  279. [Test] // .ctor (String, Boolean)
  280. public void Constructor4 ()
  281. {
  282. using (StreamWriter r = new StreamWriter (_codeFileName, false)) {
  283. Assert.IsFalse (r.AutoFlush, "#A1");
  284. Assert.IsNotNull (r.BaseStream, "#A2");
  285. Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#A3");
  286. Assert.IsNotNull (r.Encoding, "#A4");
  287. Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#A5");
  288. r.Close();
  289. }
  290. using (StreamWriter r = new StreamWriter(_codeFileName, true)) {
  291. Assert.IsFalse (r.AutoFlush, "#B1");
  292. Assert.IsNotNull (r.BaseStream, "#B2");
  293. Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#B3");
  294. Assert.IsNotNull (r.Encoding, "#B4");
  295. Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B5");
  296. r.Close();
  297. }
  298. }
  299. [Test] // .ctor (String, Boolean)
  300. public void Constructor4_Path_DirectoryNotFound ()
  301. {
  302. Directory.Delete (_tmpFolder, true);
  303. try {
  304. new StreamWriter (_codeFileName, false);
  305. Assert.Fail ("#A1");
  306. } catch (DirectoryNotFoundException ex) {
  307. // Could not find a part of the path '...'
  308. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
  309. Assert.IsNull (ex.InnerException, "#A3");
  310. Assert.IsNotNull (ex.Message, "#A4");
  311. Assert.IsTrue (ex.Message.IndexOf (_tmpFolder) != -1, "#A5");
  312. }
  313. try {
  314. new StreamWriter (_codeFileName, true);
  315. Assert.Fail ("#B1");
  316. } catch (DirectoryNotFoundException ex) {
  317. // Could not find a part of the path '...'
  318. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#B2");
  319. Assert.IsNull (ex.InnerException, "#B3");
  320. Assert.IsNotNull (ex.Message, "#B4");
  321. Assert.IsTrue (ex.Message.IndexOf (_tmpFolder) != -1, "#B5");
  322. }
  323. }
  324. [Test] // .ctor (String, Boolean)
  325. public void Constructor4_Path_Empty ()
  326. {
  327. try {
  328. new StreamWriter (string.Empty, false);
  329. Assert.Fail ("#A1");
  330. } catch (ArgumentException ex) {
  331. // Empty path name is not legal
  332. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  333. Assert.IsNull (ex.InnerException, "#A3");
  334. Assert.IsNotNull (ex.Message, "#A4");
  335. Assert.IsNull (ex.ParamName, "#A5");
  336. }
  337. try {
  338. new StreamWriter (string.Empty, true);
  339. Assert.Fail ("#B1");
  340. } catch (ArgumentException ex) {
  341. // Empty path name is not legal
  342. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  343. Assert.IsNull (ex.InnerException, "#B3");
  344. Assert.IsNotNull (ex.Message, "#B4");
  345. Assert.IsNull (ex.ParamName, "#B5");
  346. }
  347. }
  348. [Test] // .ctor (String, Boolean)
  349. public void Constructor4_Path_InvalidChars ()
  350. {
  351. try {
  352. new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0], false);
  353. Assert.Fail ("#A1");
  354. } catch (ArgumentException ex) {
  355. // Illegal characters in path
  356. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  357. Assert.IsNull (ex.InnerException, "#A3");
  358. Assert.IsNotNull (ex.Message, "#A4");
  359. Assert.IsNull (ex.ParamName, "#A5");
  360. }
  361. try {
  362. new StreamWriter ("!$what? what? Huh? !$*#" + Path.InvalidPathChars [0], true);
  363. Assert.Fail ("#B1");
  364. } catch (ArgumentException ex) {
  365. // Illegal characters in path
  366. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  367. Assert.IsNull (ex.InnerException, "#B3");
  368. Assert.IsNotNull (ex.Message, "#B4");
  369. Assert.IsNull (ex.ParamName, "#B5");
  370. }
  371. }
  372. [Test] // .ctor (String, Boolean)
  373. public void Constructor4_Path_Null ()
  374. {
  375. try {
  376. new StreamWriter ((string) null, false);
  377. Assert.Fail ("#A1");
  378. } catch (ArgumentNullException ex) {
  379. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  380. Assert.IsNull (ex.InnerException, "#A3");
  381. Assert.IsNotNull (ex.Message, "#A4");
  382. Assert.AreEqual ("path", ex.ParamName, "#A5");
  383. }
  384. try {
  385. new StreamWriter ((string) null, true);
  386. Assert.Fail ("#B1");
  387. } catch (ArgumentNullException ex) {
  388. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  389. Assert.IsNull (ex.InnerException, "#B3");
  390. Assert.IsNotNull (ex.Message, "#B4");
  391. Assert.AreEqual ("path", ex.ParamName, "#B5");
  392. }
  393. }
  394. [Test] // .ctor (Stream, Encoding, Int32)
  395. public void Constructor5 ()
  396. {
  397. MemoryStream m;
  398. StreamWriter r;
  399. m = new MemoryStream ();
  400. r = new StreamWriter (m, Encoding.ASCII, 10);
  401. Assert.IsFalse (r.AutoFlush, "#A1");
  402. Assert.AreSame (m, r.BaseStream, "#A2");
  403. Assert.IsNotNull (r.Encoding, "#A3");
  404. Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#A4");
  405. r.Close ();
  406. m.Close ();
  407. m = new MemoryStream ();
  408. r = new StreamWriter (m, Encoding.UTF8, 1);
  409. Assert.IsFalse (r.AutoFlush, "#B1");
  410. Assert.AreSame (m, r.BaseStream, "#B2");
  411. Assert.IsNotNull (r.Encoding, "#B3");
  412. Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B4");
  413. r.Close ();
  414. m.Close ();
  415. }
  416. [Test] // .ctor (Stream, Encoding, Int32)
  417. public void Constructor5_BufferSize_NotPositive ()
  418. {
  419. MemoryStream m = new MemoryStream ();
  420. try {
  421. new StreamWriter (m, Encoding.UTF8, 0);
  422. Assert.Fail ("#A1");
  423. } catch (ArgumentOutOfRangeException ex) {
  424. // Positive number required
  425. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  426. Assert.IsNull (ex.InnerException, "#A3");
  427. Assert.IsNotNull (ex.Message, "#A4");
  428. Assert.AreEqual ("bufferSize", ex.ParamName, "#A5");
  429. }
  430. try {
  431. new StreamWriter (m, Encoding.UTF8, -1);
  432. Assert.Fail ("#B1");
  433. } catch (ArgumentOutOfRangeException ex) {
  434. // Positive number required
  435. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  436. Assert.IsNull (ex.InnerException, "#B3");
  437. Assert.IsNotNull (ex.Message, "#B4");
  438. Assert.AreEqual ("bufferSize", ex.ParamName, "#B5");
  439. }
  440. }
  441. [Test] // .ctor (Stream, Encoding, Int32)
  442. public void Constructor5_Encoding_Null ()
  443. {
  444. MemoryStream m = new MemoryStream ();
  445. try {
  446. new StreamWriter (m, (Encoding) null, 10);
  447. Assert.Fail ("#1");
  448. } catch (ArgumentNullException ex) {
  449. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  450. Assert.IsNull (ex.InnerException, "#3");
  451. Assert.IsNotNull (ex.Message, "#4");
  452. Assert.AreEqual ("encoding", ex.ParamName, "#5");
  453. }
  454. }
  455. [Test] // .ctor (Stream, Encoding, Int32)
  456. public void Constructor5_Stream_NotWritable ()
  457. {
  458. FileStream f = new FileStream (_thisCodeFileName, FileMode.Open,
  459. FileAccess.Read);
  460. try {
  461. new StreamWriter (f, Encoding.UTF8, 10);
  462. Assert.Fail ("#B1");
  463. } catch (ArgumentException ex) {
  464. // Stream was not writable
  465. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  466. Assert.IsNull (ex.InnerException, "#3");
  467. Assert.IsNotNull (ex.Message, "#4");
  468. Assert.IsNull (ex.ParamName, "#5");
  469. } finally {
  470. f.Close ();
  471. }
  472. }
  473. [Test] // .ctor (Stream, Encoding, Int32)
  474. public void Constructor5_Stream_Null ()
  475. {
  476. try {
  477. new StreamWriter ((Stream) null, Encoding.UTF8, 10);
  478. Assert.Fail ("#1");
  479. } catch (ArgumentNullException ex) {
  480. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  481. Assert.IsNull (ex.InnerException, "#3");
  482. Assert.IsNotNull (ex.Message, "#4");
  483. Assert.AreEqual ("stream", ex.ParamName, "#5");
  484. }
  485. }
  486. [Test] // .ctor (String, Boolean, Encoding)
  487. public void Constructor6 ()
  488. {
  489. using (StreamWriter r = new StreamWriter (_codeFileName, false, Encoding.ASCII)) {
  490. Assert.IsFalse (r.AutoFlush, "#A1");
  491. Assert.IsNotNull (r.BaseStream, "#A2");
  492. Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#A3");
  493. Assert.IsNotNull (r.Encoding, "#A4");
  494. Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#A5");
  495. r.Close ();
  496. }
  497. using (StreamWriter r = new StreamWriter (_codeFileName, true, Encoding.UTF8)) {
  498. Assert.IsFalse (r.AutoFlush, "#B1");
  499. Assert.IsNotNull (r.BaseStream, "#B2");
  500. Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#B3");
  501. Assert.IsNotNull (r.Encoding, "#B4");
  502. Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B5");
  503. r.Close ();
  504. }
  505. }
  506. [Test] // .ctor (String, Boolean, Encoding)
  507. public void Constructor6_Encoding_Null ()
  508. {
  509. try {
  510. new StreamWriter (_codeFileName, false, (Encoding) null);
  511. Assert.Fail ("#A1");
  512. } catch (ArgumentNullException ex) {
  513. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  514. Assert.IsNull (ex.InnerException, "#A3");
  515. Assert.IsNotNull (ex.Message, "#A4");
  516. Assert.AreEqual ("encoding", ex.ParamName, "#A5");
  517. }
  518. try {
  519. new StreamWriter (_codeFileName, true, (Encoding) null);
  520. Assert.Fail ("#B1");
  521. } catch (ArgumentNullException ex) {
  522. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  523. Assert.IsNull (ex.InnerException, "#B3");
  524. Assert.IsNotNull (ex.Message, "#B4");
  525. Assert.AreEqual ("encoding", ex.ParamName, "#B5");
  526. }
  527. }
  528. [Test] // .ctor (String, Boolean, Encoding)
  529. public void Constructor6_Path_DirectoryNotFound ()
  530. {
  531. Directory.Delete (_tmpFolder, true);
  532. try {
  533. new StreamWriter (_codeFileName, false, Encoding.UTF8);
  534. Assert.Fail ("#A1");
  535. } catch (DirectoryNotFoundException ex) {
  536. // Could not find a part of the path '...'
  537. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
  538. Assert.IsNull (ex.InnerException, "#A3");
  539. Assert.IsNotNull (ex.Message, "#A4");
  540. Assert.IsTrue (ex.Message.IndexOf (_tmpFolder) != -1, "#A5");
  541. }
  542. try {
  543. new StreamWriter (_codeFileName, true, Encoding.UTF8);
  544. Assert.Fail ("#B1");
  545. } catch (DirectoryNotFoundException ex) {
  546. // Could not find a part of the path '...'
  547. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#B2");
  548. Assert.IsNull (ex.InnerException, "#B3");
  549. Assert.IsNotNull (ex.Message, "#B4");
  550. Assert.IsTrue (ex.Message.IndexOf (_tmpFolder) != -1, "#B5");
  551. }
  552. }
  553. [Test] // .ctor (String, Boolean, Encoding)
  554. public void Constructor6_Path_Empty ()
  555. {
  556. try {
  557. new StreamWriter (string.Empty, false, Encoding.UTF8);
  558. Assert.Fail ("#A1");
  559. } catch (ArgumentException ex) {
  560. // Empty path name is not legal
  561. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  562. Assert.IsNull (ex.InnerException, "#A3");
  563. Assert.IsNotNull (ex.Message, "#A4");
  564. Assert.IsNull (ex.ParamName, "#A5");
  565. }
  566. try {
  567. new StreamWriter (string.Empty, true, Encoding.UTF8);
  568. Assert.Fail ("#B1");
  569. } catch (ArgumentException ex) {
  570. // Empty path name is not legal
  571. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  572. Assert.IsNull (ex.InnerException, "#B3");
  573. Assert.IsNotNull (ex.Message, "#B4");
  574. Assert.IsNull (ex.ParamName, "#B5");
  575. }
  576. }
  577. [Test] // .ctor (String, Boolean, Encoding)
  578. public void Constructor6_Path_InvalidChars ()
  579. {
  580. try {
  581. new StreamWriter ("!$what? what? Huh? !$*#" +
  582. Path.InvalidPathChars [0], false,
  583. Encoding.UTF8);
  584. Assert.Fail ("#A1");
  585. } catch (ArgumentException ex) {
  586. // Illegal characters in path
  587. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  588. Assert.IsNull (ex.InnerException, "#A3");
  589. Assert.IsNotNull (ex.Message, "#A4");
  590. Assert.IsNull (ex.ParamName, "#A5");
  591. }
  592. try {
  593. new StreamWriter ("!$what? what? Huh? !$*#" +
  594. Path.InvalidPathChars [0], true,
  595. Encoding.UTF8);
  596. Assert.Fail ("#B1");
  597. } catch (ArgumentException ex) {
  598. // Illegal characters in path
  599. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  600. Assert.IsNull (ex.InnerException, "#B3");
  601. Assert.IsNotNull (ex.Message, "#B4");
  602. Assert.IsNull (ex.ParamName, "#B5");
  603. }
  604. }
  605. [Test] // .ctor (String, Boolean, Encoding)
  606. public void Constructor6_Path_Null ()
  607. {
  608. try {
  609. new StreamWriter ((string) null, false, Encoding.UTF8);
  610. Assert.Fail ("#A1");
  611. } catch (ArgumentNullException ex) {
  612. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  613. Assert.IsNull (ex.InnerException, "#A3");
  614. Assert.IsNotNull (ex.Message, "#A4");
  615. Assert.AreEqual ("path", ex.ParamName, "#A5");
  616. }
  617. try {
  618. new StreamWriter ((string) null, true, Encoding.UTF8);
  619. Assert.Fail ("#B1");
  620. } catch (ArgumentNullException ex) {
  621. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  622. Assert.IsNull (ex.InnerException, "#B3");
  623. Assert.IsNotNull (ex.Message, "#B4");
  624. Assert.AreEqual ("path", ex.ParamName, "#B5");
  625. }
  626. }
  627. [Test] // .ctor (String, Boolean, Encoding, Int32)
  628. public void Constructor7 ()
  629. {
  630. using (StreamWriter r = new StreamWriter (_codeFileName, false, Encoding.ASCII, 10)) {
  631. Assert.IsFalse (r.AutoFlush, "#A1");
  632. Assert.IsNotNull (r.BaseStream, "#A2");
  633. Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#A3");
  634. Assert.IsNotNull (r.Encoding, "#A4");
  635. Assert.AreEqual (typeof (ASCIIEncoding), r.Encoding.GetType (), "#A5");
  636. r.Close ();
  637. }
  638. using (StreamWriter r = new StreamWriter (_codeFileName, true, Encoding.UTF8, 1)) {
  639. Assert.IsFalse (r.AutoFlush, "#B1");
  640. Assert.IsNotNull (r.BaseStream, "#B2");
  641. Assert.AreEqual (typeof (FileStream), r.BaseStream.GetType (), "#B3");
  642. Assert.IsNotNull (r.Encoding, "#B4");
  643. Assert.AreEqual (typeof (UTF8Encoding), r.Encoding.GetType (), "#B5");
  644. r.Close ();
  645. }
  646. }
  647. [Test] // .ctor (String, Boolean, Encoding, Int32)
  648. public void Constructor7_BufferSize_NotPositive ()
  649. {
  650. try {
  651. new StreamWriter (_codeFileName, false, Encoding.UTF8, 0);
  652. Assert.Fail ("#A1");
  653. } catch (ArgumentOutOfRangeException ex) {
  654. // Positive number required
  655. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
  656. Assert.IsNull (ex.InnerException, "#A3");
  657. Assert.IsNotNull (ex.Message, "#A4");
  658. Assert.AreEqual ("bufferSize", ex.ParamName, "#A5");
  659. }
  660. try {
  661. new StreamWriter (_codeFileName, false, Encoding.UTF8, -1);
  662. Assert.Fail ("#B1");
  663. } catch (ArgumentOutOfRangeException ex) {
  664. // Positive number required
  665. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  666. Assert.IsNull (ex.InnerException, "#B3");
  667. Assert.IsNotNull (ex.Message, "#B4");
  668. Assert.AreEqual ("bufferSize", ex.ParamName, "#B5");
  669. }
  670. }
  671. [Test] // .ctor (String, Boolean, Encoding, Int32)
  672. public void Constructor7_Encoding_Null ()
  673. {
  674. try {
  675. new StreamWriter (_codeFileName, false, (Encoding) null, 10);
  676. Assert.Fail ("#A1");
  677. } catch (ArgumentNullException ex) {
  678. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  679. Assert.IsNull (ex.InnerException, "#A3");
  680. Assert.IsNotNull (ex.Message, "#A4");
  681. Assert.AreEqual ("encoding", ex.ParamName, "#A5");
  682. }
  683. try {
  684. new StreamWriter (_codeFileName, true, (Encoding) null, 10);
  685. Assert.Fail ("#B1");
  686. } catch (ArgumentNullException ex) {
  687. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  688. Assert.IsNull (ex.InnerException, "#B3");
  689. Assert.IsNotNull (ex.Message, "#B4");
  690. Assert.AreEqual ("encoding", ex.ParamName, "#B5");
  691. }
  692. }
  693. [Test] // .ctor (String, Boolean, Encoding, Int32)
  694. public void Constructor7_Path_DirectoryNotFound ()
  695. {
  696. Directory.Delete (_tmpFolder, true);
  697. try {
  698. new StreamWriter (_codeFileName, false, Encoding.UTF8, 10);
  699. Assert.Fail ("#A1");
  700. } catch (DirectoryNotFoundException ex) {
  701. // Could not find a part of the path '...'
  702. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
  703. Assert.IsNull (ex.InnerException, "#A3");
  704. Assert.IsNotNull (ex.Message, "#A4");
  705. Assert.IsTrue (ex.Message.IndexOf (_tmpFolder) != -1, "#A5");
  706. }
  707. try {
  708. new StreamWriter (_codeFileName, true, Encoding.UTF8, 10);
  709. Assert.Fail ("#B1");
  710. } catch (DirectoryNotFoundException ex) {
  711. // Could not find a part of the path '...'
  712. Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#B2");
  713. Assert.IsNull (ex.InnerException, "#B3");
  714. Assert.IsNotNull (ex.Message, "#B4");
  715. Assert.IsTrue (ex.Message.IndexOf (_tmpFolder) != -1, "#B5");
  716. }
  717. }
  718. [Test] // .ctor (String, Boolean, Encoding, Int32)
  719. public void Constructor7_Path_Empty ()
  720. {
  721. try {
  722. new StreamWriter (string.Empty, false, Encoding.UTF8, 10);
  723. Assert.Fail ("#A1");
  724. } catch (ArgumentException ex) {
  725. // Empty path name is not legal
  726. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  727. Assert.IsNull (ex.InnerException, "#A3");
  728. Assert.IsNotNull (ex.Message, "#A4");
  729. Assert.IsNull (ex.ParamName, "#A5");
  730. }
  731. try {
  732. new StreamWriter (string.Empty, true, Encoding.UTF8, 10);
  733. Assert.Fail ("#B1");
  734. } catch (ArgumentException ex) {
  735. // Empty path name is not legal
  736. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  737. Assert.IsNull (ex.InnerException, "#B3");
  738. Assert.IsNotNull (ex.Message, "#B4");
  739. Assert.IsNull (ex.ParamName, "#B5");
  740. }
  741. }
  742. [Test] // .ctor (String, Boolean, Encoding, Int32)
  743. public void Constructor7_Path_InvalidChars ()
  744. {
  745. try {
  746. new StreamWriter ("!$what? what? Huh? !$*#" +
  747. Path.InvalidPathChars [0], false,
  748. Encoding.UTF8, 10);
  749. Assert.Fail ("#A1");
  750. } catch (ArgumentException ex) {
  751. // Illegal characters in path
  752. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  753. Assert.IsNull (ex.InnerException, "#A3");
  754. Assert.IsNotNull (ex.Message, "#A4");
  755. Assert.IsNull (ex.ParamName, "#A5");
  756. }
  757. try {
  758. new StreamWriter ("!$what? what? Huh? !$*#" +
  759. Path.InvalidPathChars [0], true,
  760. Encoding.UTF8, 10);
  761. Assert.Fail ("#B1");
  762. } catch (ArgumentException ex) {
  763. // Illegal characters in path
  764. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  765. Assert.IsNull (ex.InnerException, "#B3");
  766. Assert.IsNotNull (ex.Message, "#B4");
  767. Assert.IsNull (ex.ParamName, "#B5");
  768. }
  769. }
  770. [Test] // .ctor (String, Boolean, Encoding, Int32)
  771. public void Constructor7_Path_Null ()
  772. {
  773. try {
  774. new StreamWriter ((string) null, false, Encoding.UTF8, 10);
  775. Assert.Fail ("#A1");
  776. } catch (ArgumentNullException ex) {
  777. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  778. Assert.IsNull (ex.InnerException, "#A3");
  779. Assert.IsNotNull (ex.Message, "#A4");
  780. Assert.AreEqual ("path", ex.ParamName, "#A5");
  781. }
  782. try {
  783. new StreamWriter ((string) null, true, Encoding.UTF8, 10);
  784. Assert.Fail ("#B1");
  785. } catch (ArgumentNullException ex) {
  786. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
  787. Assert.IsNull (ex.InnerException, "#B3");
  788. Assert.IsNotNull (ex.Message, "#B4");
  789. Assert.AreEqual ("path", ex.ParamName, "#B5");
  790. }
  791. }
  792. [Test]
  793. public void AutoFlush ()
  794. {
  795. MemoryStream m;
  796. StreamWriter w;
  797. m = new MemoryStream ();
  798. w = new StreamWriter (m);
  799. w.Write (1);
  800. w.Write (2);
  801. w.Write (3);
  802. w.Write (4);
  803. Assert.AreEqual (0, m.Length, "#A1");
  804. w.AutoFlush = true;
  805. Assert.IsTrue (w.AutoFlush, "#A2");
  806. Assert.AreEqual (4, m.Length, "#A3");
  807. w.Flush ();
  808. Assert.AreEqual (4, m.Length, "#A4");
  809. m = new MemoryStream ();
  810. w = new StreamWriter(m);
  811. w.AutoFlush = true;
  812. Assert.IsTrue (w.AutoFlush, "#B1");
  813. w.Write (1);
  814. w.Write (2);
  815. w.Write (3);
  816. w.Write (4);
  817. Assert.AreEqual (4, m.Length, "#B2");
  818. w.Flush ();
  819. Assert.AreEqual (4, m.Length, "#B3");
  820. w.AutoFlush = false;
  821. Assert.IsFalse (w.AutoFlush, "#B4");
  822. w.Write (4);
  823. Assert.AreEqual (4, m.Length, "#B5");
  824. w.Flush ();
  825. Assert.AreEqual (5, m.Length, "#B6");
  826. }
  827. [Test]
  828. public void AutoFlush_Disposed ()
  829. {
  830. StreamWriter w;
  831. w = new StreamWriter (new MemoryStream ());
  832. w.Close ();
  833. w.AutoFlush = false;
  834. Assert.IsFalse (w.AutoFlush, "#A1");
  835. try {
  836. w.AutoFlush = true;
  837. Assert.Fail ("#A2");
  838. } catch (ObjectDisposedException) {
  839. }
  840. Assert.IsTrue (w.AutoFlush, "#A3");
  841. w = new StreamWriter (new MemoryStream ());
  842. w.AutoFlush = true;
  843. w.Close ();
  844. Assert.IsTrue (w.AutoFlush, "#B1");
  845. try {
  846. w.AutoFlush = true;
  847. Assert.Fail ("#B2");
  848. } catch (ObjectDisposedException) {
  849. }
  850. Assert.IsTrue (w.AutoFlush, "#B3");
  851. w.AutoFlush = false;
  852. Assert.IsFalse (w.AutoFlush, "#B4");
  853. }
  854. [Test]
  855. public void Close ()
  856. {
  857. Encoding encoding = Encoding.ASCII;
  858. MemoryStream m = new MemoryStream ();
  859. StreamWriter w = new StreamWriter (m, encoding);
  860. w.Write (2);
  861. Assert.AreEqual (0, m.Length, "#1");
  862. w.Close ();
  863. Assert.IsFalse (m.CanWrite, "#2");
  864. Assert.AreEqual (50, m.GetBuffer () [0], "#3");
  865. Assert.IsNull (w.BaseStream, "#4");
  866. Assert.IsNull (w.Encoding, "#5");
  867. }
  868. [Test]
  869. public void Flush ()
  870. {
  871. MemoryStream m = new MemoryStream();
  872. StreamWriter w = new StreamWriter(m);
  873. w.Write(1);
  874. w.Write(2);
  875. w.Write(3);
  876. w.Write(4);
  877. Assert.AreEqual (0L, m.Length, "#1");
  878. w.Flush();
  879. Assert.AreEqual (4L, m.Length, "#2");
  880. }
  881. [Test]
  882. public void Flush_Disposed ()
  883. {
  884. StreamWriter w = new StreamWriter(new MemoryStream ());
  885. w.Close();
  886. try {
  887. w.Flush ();
  888. Assert.Fail ("#1");
  889. } catch (ObjectDisposedException) {
  890. }
  891. }
  892. [Test]
  893. [ExpectedException (typeof (ObjectDisposedException))]
  894. public void WriteChar_Disposed ()
  895. {
  896. StreamWriter w = new StreamWriter (new MemoryStream ());
  897. w.Close ();
  898. w.Write ('A');
  899. }
  900. [Test]
  901. [ExpectedException (typeof (ObjectDisposedException))]
  902. public void WriteCharArray_Disposed ()
  903. {
  904. char[] c = new char [2] { 'a', 'b' };
  905. StreamWriter w = new StreamWriter (new MemoryStream ());
  906. w.Close ();
  907. w.Write (c, 0, 2);
  908. }
  909. [Test]
  910. public void WriteCharArray_Null ()
  911. {
  912. char[] c = null;
  913. StreamWriter w = new StreamWriter (new MemoryStream ());
  914. w.Write (c);
  915. }
  916. [Test]
  917. [ExpectedException (typeof (ArgumentException))]
  918. public void WriteCharArray_IndexOverflow ()
  919. {
  920. char[] c = new char [2] { 'a', 'b' };
  921. StreamWriter w = new StreamWriter (new MemoryStream ());
  922. w.Write (c, Int32.MaxValue, 2);
  923. }
  924. [Test]
  925. [ExpectedException (typeof (ArgumentException))]
  926. public void WriteCharArray_CountOverflow ()
  927. {
  928. char[] c = new char [2] { 'a', 'b' };
  929. StreamWriter w = new StreamWriter (new MemoryStream ());
  930. w.Write (c, 1, Int32.MaxValue);
  931. }
  932. [Test]
  933. [ExpectedException (typeof (ObjectDisposedException))]
  934. public void WriteString_Disposed ()
  935. {
  936. StreamWriter w = new StreamWriter (new MemoryStream ());
  937. w.Close ();
  938. w.Write ("mono");
  939. }
  940. [Test]
  941. public void WriteString_Null ()
  942. {
  943. string s = null;
  944. StreamWriter w = new StreamWriter (new MemoryStream ());
  945. w.Write (s);
  946. }
  947. [Test]
  948. public void NoPreambleOnAppend ()
  949. {
  950. MemoryStream ms = new MemoryStream ();
  951. StreamWriter w = new StreamWriter (ms, Encoding.UTF8);
  952. w.Write ("a");
  953. w.Flush ();
  954. Assert.AreEqual (4, ms.Position, "#1");
  955. // Append 1 byte, should skip the preamble now.
  956. w.Write ("a");
  957. w.Flush ();
  958. w = new StreamWriter (ms, Encoding.UTF8);
  959. Assert.AreEqual (5, ms.Position, "#2");
  960. }
  961. [Test]
  962. public void FlushAsync ()
  963. {
  964. ManualResetEvent mre = new ManualResetEvent (false);
  965. var m = new MockStream(true, false, true);
  966. var w = new StreamWriter (m);
  967. w.Write(1);
  968. Assert.AreEqual (0L, m.Length, "#1");
  969. var t = w.WriteLineAsync ();
  970. Assert.IsTrue (t.Wait (1000), "#2");
  971. Assert.IsTrue (w.FlushAsync ().Wait (1000), "#3");
  972. }
  973. [Test]
  974. public void KeepOpenWithDispose ()
  975. {
  976. var ms = new MemoryStream ();
  977. using (StreamWriter writer = new StreamWriter (ms, new UTF8Encoding (false), 4096, true)) {
  978. writer.Write ('X');
  979. }
  980. Assert.AreEqual (1, ms.Length);
  981. }
  982. [Test]
  983. public void WriteAsync ()
  984. {
  985. var m = new MockStream(true, false, true);
  986. var w = new StreamWriter (m);
  987. var t = w.WriteAsync ("v");
  988. Assert.IsTrue (t.Wait (1000), "#1");
  989. t = w.WriteAsync ((string) null);
  990. Assert.IsTrue (t.Wait (1000), "#2");
  991. t = w.WriteLineAsync ("line");
  992. Assert.IsTrue (t.Wait (1000), "#3");
  993. t = w.WriteLineAsync ((string) null);
  994. Assert.IsTrue (t.Wait (1000), "#4");
  995. t = w.WriteLineAsync ('c');
  996. Assert.IsTrue (t.Wait (1000), "#5");
  997. }
  998. // TODO - Write - test errors, functionality tested in TestFlush.
  999. }
  1000. }