1 // StreamWriterTest.cs - NUnit Test Cases for the SystemIO.StreamWriter class
\r
3 // David Brandt (bucky@keystreams.com)
\r
5 // (C) Ximian, Inc. http://www.ximian.com
\r
8 using NUnit.Framework;
\r
13 namespace MonoTests.System.IO
\r
17 public class StreamWriterTest : Assertion
\r
20 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
\r
21 private string _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
\r
22 private string _thisCodeFileName = TempFolder + Path.DirectorySeparatorChar + "StreamWriterTest.temp";
\r
25 public void SetUp ()
\r
27 if (Directory.Exists (TempFolder))
\r
28 Directory.Delete (TempFolder, true);
\r
29 Directory.CreateDirectory (TempFolder);
\r
31 if (!File.Exists (_thisCodeFileName))
\r
32 File.Create (_thisCodeFileName).Close ();
\r
36 public void TearDown ()
\r
38 if (Directory.Exists (TempFolder))
\r
39 Directory.Delete (TempFolder, true);
\r
45 public void TestCtor1() {
\r
47 bool errorThrown = false;
\r
49 StreamWriter r = new StreamWriter((Stream)null);
\r
50 } catch (ArgumentNullException) {
\r
52 } catch (Exception e) {
\r
53 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
55 Assert("null string error not thrown", errorThrown);
\r
58 bool errorThrown = false;
\r
59 FileStream f = new FileStream(_thisCodeFileName,
\r
63 StreamWriter r = new StreamWriter(f);
\r
65 } catch (ArgumentException) {
\r
67 } catch (Exception e) {
\r
68 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
71 Assert("no read error not thrown", errorThrown);
\r
74 FileStream f = new FileStream(_codeFileName,
\r
77 StreamWriter r = new StreamWriter(f);
\r
78 AssertNotNull("no stream writer", r);
\r
85 public void TestCtor2() {
\r
87 bool errorThrown = false;
\r
89 StreamWriter r = new StreamWriter("");
\r
90 } catch (ArgumentException) {
\r
92 } catch (Exception e) {
\r
93 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
95 Assert("empty string error not thrown", errorThrown);
\r
98 bool errorThrown = false;
\r
100 StreamWriter r = new StreamWriter((string)null);
\r
101 } catch (ArgumentNullException) {
\r
102 errorThrown = true;
\r
103 } catch (Exception e) {
\r
104 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
106 Assert("null string error not thrown", errorThrown);
\r
109 bool errorThrown = false;
\r
111 StreamWriter r = new StreamWriter("nonexistentdir/file");
\r
112 } catch (DirectoryNotFoundException) {
\r
113 errorThrown = true;
\r
114 } catch (Exception e) {
\r
115 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
117 Assert("dirNotFound error not thrown", errorThrown);
\r
120 bool errorThrown = false;
\r
122 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
\r
123 } catch (IOException) {
\r
124 errorThrown = true;
\r
125 } catch (ArgumentException) {
\r
126 // FIXME - the spec says 'IOExc', but the
\r
127 // compiler says 'ArgExc'...
\r
128 errorThrown = true;
\r
129 } catch (Exception e) {
\r
130 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
132 Assert("1 invalid filename error not thrown", errorThrown);
\r
134 // TODO - Security/Auth exceptions
\r
136 StreamWriter r = new StreamWriter(_codeFileName);
\r
137 AssertNotNull("no stream writer", r);
\r
143 public void TestCtor3() {
\r
145 bool errorThrown = false;
\r
147 StreamWriter r = new StreamWriter("", false);
\r
148 } catch (ArgumentException) {
\r
149 errorThrown = true;
\r
150 } catch (Exception e) {
\r
151 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
153 Assert("empty string error not thrown", errorThrown);
\r
156 bool errorThrown = false;
\r
158 StreamWriter r = new StreamWriter((string)null, false);
\r
159 } catch (ArgumentNullException) {
\r
160 errorThrown = true;
\r
161 } catch (Exception e) {
\r
162 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
164 Assert("null string error not thrown", errorThrown);
\r
167 bool errorThrown = false;
\r
169 StreamWriter r = new StreamWriter("nonexistentdir/file", false);
\r
170 } catch (DirectoryNotFoundException) {
\r
171 errorThrown = true;
\r
172 } catch (Exception e) {
\r
173 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
175 Assert("dirNotFound error not thrown", errorThrown);
\r
178 bool errorThrown = false;
\r
180 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
\r
181 } catch (IOException) {
\r
182 errorThrown = true;
\r
183 } catch (ArgumentException) {
\r
184 // FIXME - the spec says 'IOExc', but the
\r
185 // compiler says 'ArgExc'...
\r
186 errorThrown = true;
\r
187 } catch (Exception e) {
\r
188 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
190 Assert("2 invalid filename error not thrown", errorThrown);
\r
193 StreamWriter r = new StreamWriter(_codeFileName, false);
\r
194 AssertNotNull("no stream writer", r);
\r
198 bool errorThrown = false;
\r
200 StreamWriter r = new StreamWriter("", true);
\r
201 } catch (ArgumentException) {
\r
202 errorThrown = true;
\r
203 } catch (Exception e) {
\r
204 Fail ("Incorrect exception thrown at 5: " + e.ToString());
\r
206 Assert("empty string error not thrown", errorThrown);
\r
209 bool errorThrown = false;
\r
211 StreamWriter r = new StreamWriter((string)null, true);
\r
212 } catch (ArgumentNullException) {
\r
213 errorThrown = true;
\r
214 } catch (Exception e) {
\r
215 Fail ("Incorrect exception thrown at 6: " + e.ToString());
\r
217 Assert("null string error not thrown", errorThrown);
\r
220 bool errorThrown = false;
\r
222 StreamWriter r = new StreamWriter("nonexistentdir/file", true);
\r
223 } catch (DirectoryNotFoundException) {
\r
224 errorThrown = true;
\r
225 } catch (Exception e) {
\r
226 Fail ("Incorrect exception thrown at 7: " + e.ToString());
\r
228 Assert("dirNotFound error not thrown", errorThrown);
\r
231 bool errorThrown = false;
\r
233 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
\r
234 } catch (IOException) {
\r
235 errorThrown = true;
\r
236 } catch (ArgumentException) {
\r
237 // FIXME - the spec says 'IOExc', but the
\r
238 // compiler says 'ArgExc'...
\r
239 errorThrown = true;
\r
240 } catch (Exception e) {
\r
241 Fail ("Incorrect exception thrown at 8: " + e.ToString());
\r
243 Assert("3 invalid filename error not thrown", errorThrown);
\r
247 StreamWriter r = new StreamWriter(_codeFileName, true);
\r
248 AssertNotNull("no stream writer", r);
\r
250 } catch (Exception e) {
\r
251 Fail ("Unxpected exception e=" + e.ToString());
\r
256 // TODO - ctors with Encoding
\r
258 // TODO - AutoFlush
\r
260 public void TestAutoFlush() {
\r
262 MemoryStream m = new MemoryStream();
\r
263 StreamWriter w = new StreamWriter(m);
\r
264 w.AutoFlush = false;
\r
269 AssertEquals("Should be nothing before flush",
\r
272 AssertEquals("Should be something after flush",
\r
276 MemoryStream m = new MemoryStream();
\r
277 StreamWriter w = new StreamWriter(m);
\r
278 w.AutoFlush = true;
\r
283 AssertEquals("Should be something before flush",
\r
286 AssertEquals("Should be something after flush",
\r
292 public void TestBaseStream() {
\r
293 FileStream f = new FileStream(_codeFileName,
\r
296 StreamWriter r = new StreamWriter(f);
\r
297 AssertEquals("wrong base stream ", f, r.BaseStream);
\r
303 public void TestEncoding() {
\r
304 StreamWriter r = new StreamWriter(_codeFileName);
\r
305 AssertEquals("wrong encoding",
\r
306 Encoding.UTF8.GetType(), r.Encoding.GetType());
\r
310 // TODO - Close - not entirely sure how to test Close
\r
311 //public void TestClose() {
\r
313 //MemoryStream m = new MemoryStream();
\r
314 //StreamWriter w = new StreamWriter(m);
\r
315 //StreamReader r = new StreamReader(m);
\r
320 //AssertEquals("Should be nothing before close",
\r
322 //AssertEquals("Should be nothing in reader",
\r
325 //AssertEquals("Should be something after close",
\r
332 public void TestFlush() {
\r
334 bool errorThrown = false;
\r
336 FileStream f = new FileStream(_codeFileName,
\r
339 StreamWriter r = new StreamWriter(f);
\r
342 } catch (ObjectDisposedException) {
\r
343 errorThrown = true;
\r
344 } catch (Exception e) {
\r
345 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
347 Assert("can't flush closed error not thrown", errorThrown);
\r
350 MemoryStream m = new MemoryStream();
\r
351 StreamWriter w = new StreamWriter(m);
\r
356 AssertEquals("Should be nothing before flush",
\r
359 AssertEquals("Should be something after flush",
\r
364 // TODO - Write - test errors, functionality tested in TestFlush.
\r