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
16 public class StreamWriterTest : TestCase
\r
19 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
\r
20 private string _codeFileName;
\r
21 private string _thisCodeFileName;
\r
23 public StreamWriterTest ()
\r
25 if (Directory.Exists (TempFolder))
\r
26 Directory.Delete (TempFolder, true);
\r
27 Directory.CreateDirectory (TempFolder);
\r
29 _thisCodeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
\r
30 _codeFileName = TempFolder + Path.DirectorySeparatorChar + "StreamWriterTest.temp";
\r
33 ~StreamWriterTest ()
\r
35 if (Directory.Exists (TempFolder))
\r
36 Directory.Delete (TempFolder, true);
\r
40 protected override void SetUp()
\r
42 if (!Directory.Exists (TempFolder))
\r
43 Directory.CreateDirectory (TempFolder);
\r
45 if (!File.Exists (_thisCodeFileName))
\r
46 File.Create (_thisCodeFileName).Close ();
\r
49 protected override void TearDown()
\r
55 public void TestCtor1() {
\r
57 bool errorThrown = false;
\r
59 StreamWriter r = new StreamWriter((Stream)null);
\r
60 } catch (ArgumentNullException) {
\r
62 } catch (Exception e) {
\r
63 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
65 Assert("null string error not thrown", errorThrown);
\r
68 bool errorThrown = false;
\r
69 FileStream f = new FileStream(_thisCodeFileName,
\r
73 StreamWriter r = new StreamWriter(f);
\r
75 } catch (ArgumentException) {
\r
77 } catch (Exception e) {
\r
78 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
81 Assert("no read error not thrown", errorThrown);
\r
84 FileStream f = new FileStream(_codeFileName,
\r
87 StreamWriter r = new StreamWriter(f);
\r
88 AssertNotNull("no stream writer", r);
\r
93 public void TestCtor2() {
\r
95 bool errorThrown = false;
\r
97 StreamWriter r = new StreamWriter("");
\r
98 } catch (ArgumentException) {
\r
100 } catch (Exception e) {
\r
101 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
103 Assert("empty string error not thrown", errorThrown);
\r
106 bool errorThrown = false;
\r
108 StreamWriter r = new StreamWriter((string)null);
\r
109 } catch (ArgumentNullException) {
\r
110 errorThrown = true;
\r
111 } catch (Exception e) {
\r
112 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
114 Assert("null string error not thrown", errorThrown);
\r
117 bool errorThrown = false;
\r
119 StreamWriter r = new StreamWriter("nonexistentdir/file");
\r
120 } catch (DirectoryNotFoundException) {
\r
121 errorThrown = true;
\r
122 } catch (Exception e) {
\r
123 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
125 Assert("dirNotFound error not thrown", errorThrown);
\r
128 bool errorThrown = false;
\r
130 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
\r
131 } catch (IOException) {
\r
132 errorThrown = true;
\r
133 } catch (ArgumentException) {
\r
134 // FIXME - the spec says 'IOExc', but the
\r
135 // compiler says 'ArgExc'...
\r
136 errorThrown = true;
\r
137 } catch (Exception e) {
\r
138 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
140 Assert("1 invalid filename error not thrown", errorThrown);
\r
142 // TODO - Security/Auth exceptions
\r
144 StreamWriter r = new StreamWriter(_codeFileName);
\r
145 AssertNotNull("no stream writer", r);
\r
149 public void TestCtor3() {
\r
151 bool errorThrown = false;
\r
153 StreamWriter r = new StreamWriter("", false);
\r
154 } catch (ArgumentException) {
\r
155 errorThrown = true;
\r
156 } catch (Exception e) {
\r
157 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
159 Assert("empty string error not thrown", errorThrown);
\r
162 bool errorThrown = false;
\r
164 StreamWriter r = new StreamWriter((string)null, false);
\r
165 } catch (ArgumentNullException) {
\r
166 errorThrown = true;
\r
167 } catch (Exception e) {
\r
168 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
170 Assert("null string error not thrown", errorThrown);
\r
173 bool errorThrown = false;
\r
175 StreamWriter r = new StreamWriter("nonexistentdir/file", false);
\r
176 } catch (DirectoryNotFoundException) {
\r
177 errorThrown = true;
\r
178 } catch (Exception e) {
\r
179 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
181 Assert("dirNotFound error not thrown", errorThrown);
\r
184 bool errorThrown = false;
\r
186 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
\r
187 } catch (IOException) {
\r
188 errorThrown = true;
\r
189 } catch (ArgumentException) {
\r
190 // FIXME - the spec says 'IOExc', but the
\r
191 // compiler says 'ArgExc'...
\r
192 errorThrown = true;
\r
193 } catch (Exception e) {
\r
194 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
196 Assert("2 invalid filename error not thrown", errorThrown);
\r
199 StreamWriter r = new StreamWriter(_codeFileName, false);
\r
200 AssertNotNull("no stream writer", r);
\r
204 bool errorThrown = false;
\r
206 StreamWriter r = new StreamWriter("", true);
\r
207 } catch (ArgumentException) {
\r
208 errorThrown = true;
\r
209 } catch (Exception e) {
\r
210 Fail ("Incorrect exception thrown at 5: " + e.ToString());
\r
212 Assert("empty string error not thrown", errorThrown);
\r
215 bool errorThrown = false;
\r
217 StreamWriter r = new StreamWriter((string)null, true);
\r
218 } catch (ArgumentNullException) {
\r
219 errorThrown = true;
\r
220 } catch (Exception e) {
\r
221 Fail ("Incorrect exception thrown at 6: " + e.ToString());
\r
223 Assert("null string error not thrown", errorThrown);
\r
226 bool errorThrown = false;
\r
228 StreamWriter r = new StreamWriter("nonexistentdir/file", true);
\r
229 } catch (DirectoryNotFoundException) {
\r
230 errorThrown = true;
\r
231 } catch (Exception e) {
\r
232 Fail ("Incorrect exception thrown at 7: " + e.ToString());
\r
234 Assert("dirNotFound error not thrown", errorThrown);
\r
237 bool errorThrown = false;
\r
239 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
\r
240 } catch (IOException) {
\r
241 errorThrown = true;
\r
242 } catch (ArgumentException) {
\r
243 // FIXME - the spec says 'IOExc', but the
\r
244 // compiler says 'ArgExc'...
\r
245 errorThrown = true;
\r
246 } catch (Exception e) {
\r
247 Fail ("Incorrect exception thrown at 8: " + e.ToString());
\r
249 Assert("3 invalid filename error not thrown", errorThrown);
\r
253 StreamWriter r = new StreamWriter(_codeFileName, true);
\r
254 AssertNotNull("no stream writer", r);
\r
256 } catch (Exception e) {
\r
257 Fail ("Unxpected exception e=" + e.ToString());
\r
262 // TODO - ctors with Encoding
\r
264 // TODO - AutoFlush
\r
265 public void TestAutoFlush() {
\r
267 MemoryStream m = new MemoryStream();
\r
268 StreamWriter w = new StreamWriter(m);
\r
269 w.AutoFlush = false;
\r
274 AssertEquals("Should be nothing before flush",
\r
277 AssertEquals("Should be something after flush",
\r
281 MemoryStream m = new MemoryStream();
\r
282 StreamWriter w = new StreamWriter(m);
\r
283 w.AutoFlush = true;
\r
288 AssertEquals("Should be something before flush",
\r
291 AssertEquals("Should be something after flush",
\r
296 public void TestBaseStream() {
\r
297 FileStream f = new FileStream(_codeFileName,
\r
300 StreamWriter r = new StreamWriter(f);
\r
301 AssertEquals("wrong base stream ", f, r.BaseStream);
\r
306 public void TestEncoding() {
\r
307 StreamWriter r = new StreamWriter(_codeFileName);
\r
308 AssertEquals("wrong encoding",
\r
309 Encoding.UTF8.GetType(), r.Encoding.GetType());
\r
313 // TODO - Close - not entirely sure how to test Close
\r
314 //public void TestClose() {
\r
316 //MemoryStream m = new MemoryStream();
\r
317 //StreamWriter w = new StreamWriter(m);
\r
318 //StreamReader r = new StreamReader(m);
\r
323 //AssertEquals("Should be nothing before close",
\r
325 //AssertEquals("Should be nothing in reader",
\r
328 //AssertEquals("Should be something after close",
\r
334 public void TestFlush() {
\r
336 bool errorThrown = false;
\r
338 FileStream f = new FileStream(_codeFileName,
\r
341 StreamWriter r = new StreamWriter(f);
\r
344 } catch (ObjectDisposedException) {
\r
345 errorThrown = true;
\r
346 } catch (Exception e) {
\r
347 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
349 Assert("can't flush closed error not thrown", errorThrown);
\r
352 MemoryStream m = new MemoryStream();
\r
353 StreamWriter w = new StreamWriter(m);
\r
358 AssertEquals("Should be nothing before flush",
\r
361 AssertEquals("Should be something after flush",
\r
366 // TODO - Write - test errors, functionality tested in TestFlush.
\r