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
18 protected override void SetUp()
\r
22 protected override void TearDown()
\r
26 private string _codeFileName = "StreamWriterTest.temp";
\r
27 private string _thisCodeFileName = "resources" + Path.DirectorySeparatorChar + "AFile.txt";
\r
30 public void TestCtor1() {
\r
32 bool errorThrown = false;
\r
34 StreamWriter r = new StreamWriter((Stream)null);
\r
35 } catch (ArgumentNullException) {
\r
37 } catch (Exception e) {
\r
38 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
40 Assert("null string error not thrown", errorThrown);
\r
43 bool errorThrown = false;
\r
44 FileStream f = new FileStream(_thisCodeFileName,
\r
48 StreamWriter r = new StreamWriter(f);
\r
50 } catch (ArgumentException) {
\r
52 } catch (Exception e) {
\r
53 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
56 Assert("no read error not thrown", errorThrown);
\r
59 FileStream f = new FileStream(_codeFileName,
\r
62 StreamWriter r = new StreamWriter(f);
\r
63 AssertNotNull("no stream writer", r);
\r
68 public void TestCtor2() {
\r
70 bool errorThrown = false;
\r
72 StreamWriter r = new StreamWriter("");
\r
73 } catch (ArgumentException) {
\r
75 } catch (Exception e) {
\r
76 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
78 Assert("empty string error not thrown", errorThrown);
\r
81 bool errorThrown = false;
\r
83 StreamWriter r = new StreamWriter((string)null);
\r
84 } catch (ArgumentNullException) {
\r
86 } catch (Exception e) {
\r
87 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
89 Assert("null string error not thrown", errorThrown);
\r
92 bool errorThrown = false;
\r
94 StreamWriter r = new StreamWriter("nonexistentdir/file");
\r
95 } catch (DirectoryNotFoundException) {
\r
97 } catch (Exception e) {
\r
98 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
100 Assert("dirNotFound error not thrown", errorThrown);
\r
103 bool errorThrown = false;
\r
105 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0]);
\r
106 } catch (IOException) {
\r
107 errorThrown = true;
\r
108 } catch (ArgumentException) {
\r
109 // FIXME - the spec says 'IOExc', but the
\r
110 // compiler says 'ArgExc'...
\r
111 errorThrown = true;
\r
112 } catch (Exception e) {
\r
113 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
115 Assert("1 invalid filename error not thrown", errorThrown);
\r
117 // TODO - Security/Auth exceptions
\r
119 StreamWriter r = new StreamWriter(_codeFileName);
\r
120 AssertNotNull("no stream writer", r);
\r
124 public void TestCtor3() {
\r
126 bool errorThrown = false;
\r
128 StreamWriter r = new StreamWriter("", false);
\r
129 } catch (ArgumentException) {
\r
130 errorThrown = true;
\r
131 } catch (Exception e) {
\r
132 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
134 Assert("empty string error not thrown", errorThrown);
\r
137 bool errorThrown = false;
\r
139 StreamWriter r = new StreamWriter((string)null, false);
\r
140 } catch (ArgumentNullException) {
\r
141 errorThrown = true;
\r
142 } catch (Exception e) {
\r
143 Fail ("Incorrect exception thrown at 2: " + e.ToString());
\r
145 Assert("null string error not thrown", errorThrown);
\r
148 bool errorThrown = false;
\r
150 StreamWriter r = new StreamWriter("nonexistentdir/file", false);
\r
151 } catch (DirectoryNotFoundException) {
\r
152 errorThrown = true;
\r
153 } catch (Exception e) {
\r
154 Fail ("Incorrect exception thrown at 3: " + e.ToString());
\r
156 Assert("dirNotFound error not thrown", errorThrown);
\r
159 bool errorThrown = false;
\r
161 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], false);
\r
162 } catch (IOException) {
\r
163 errorThrown = true;
\r
164 } catch (ArgumentException) {
\r
165 // FIXME - the spec says 'IOExc', but the
\r
166 // compiler says 'ArgExc'...
\r
167 errorThrown = true;
\r
168 } catch (Exception e) {
\r
169 Fail ("Incorrect exception thrown at 4: " + e.ToString());
\r
171 Assert("2 invalid filename error not thrown", errorThrown);
\r
174 StreamWriter r = new StreamWriter(_codeFileName, false);
\r
175 AssertNotNull("no stream writer", r);
\r
179 bool errorThrown = false;
\r
181 StreamWriter r = new StreamWriter("", true);
\r
182 } catch (ArgumentException) {
\r
183 errorThrown = true;
\r
184 } catch (Exception e) {
\r
185 Fail ("Incorrect exception thrown at 5: " + e.ToString());
\r
187 Assert("empty string error not thrown", errorThrown);
\r
190 bool errorThrown = false;
\r
192 StreamWriter r = new StreamWriter((string)null, true);
\r
193 } catch (ArgumentNullException) {
\r
194 errorThrown = true;
\r
195 } catch (Exception e) {
\r
196 Fail ("Incorrect exception thrown at 6: " + e.ToString());
\r
198 Assert("null string error not thrown", errorThrown);
\r
201 bool errorThrown = false;
\r
203 StreamWriter r = new StreamWriter("nonexistentdir/file", true);
\r
204 } catch (DirectoryNotFoundException) {
\r
205 errorThrown = true;
\r
206 } catch (Exception e) {
\r
207 Fail ("Incorrect exception thrown at 7: " + e.ToString());
\r
209 Assert("dirNotFound error not thrown", errorThrown);
\r
212 bool errorThrown = false;
\r
214 StreamWriter r = new StreamWriter("!$what? what? Huh? !$*#" + Path.InvalidPathChars[0], true);
\r
215 } catch (IOException) {
\r
216 errorThrown = true;
\r
217 } catch (ArgumentException) {
\r
218 // FIXME - the spec says 'IOExc', but the
\r
219 // compiler says 'ArgExc'...
\r
220 errorThrown = true;
\r
221 } catch (Exception e) {
\r
222 Fail ("Incorrect exception thrown at 8: " + e.ToString());
\r
224 Assert("3 invalid filename error not thrown", errorThrown);
\r
228 StreamWriter r = new StreamWriter(_codeFileName, true);
\r
229 AssertNotNull("no stream writer", r);
\r
231 } catch (Exception e) {
\r
232 Fail ("Unxpected exception e=" + e.ToString());
\r
237 // TODO - ctors with Encoding
\r
239 // TODO - AutoFlush
\r
240 public void TestAutoFlush() {
\r
242 MemoryStream m = new MemoryStream();
\r
243 StreamWriter w = new StreamWriter(m);
\r
244 w.AutoFlush = false;
\r
249 AssertEquals("Should be nothing before flush",
\r
252 AssertEquals("Should be something after flush",
\r
256 MemoryStream m = new MemoryStream();
\r
257 StreamWriter w = new StreamWriter(m);
\r
258 w.AutoFlush = true;
\r
263 AssertEquals("Should be something before flush",
\r
266 AssertEquals("Should be something after flush",
\r
271 public void TestBaseStream() {
\r
272 FileStream f = new FileStream(_codeFileName,
\r
275 StreamWriter r = new StreamWriter(f);
\r
276 AssertEquals("wrong base stream ", f, r.BaseStream);
\r
281 public void TestEncoding() {
\r
282 StreamWriter r = new StreamWriter(_codeFileName);
\r
283 AssertEquals("wrong encoding",
\r
284 Encoding.UTF8.GetType(), r.Encoding.GetType());
\r
288 // TODO - Close - not entirely sure how to test Close
\r
289 //public void TestClose() {
\r
291 //MemoryStream m = new MemoryStream();
\r
292 //StreamWriter w = new StreamWriter(m);
\r
293 //StreamReader r = new StreamReader(m);
\r
298 //AssertEquals("Should be nothing before close",
\r
300 //AssertEquals("Should be nothing in reader",
\r
303 //AssertEquals("Should be something after close",
\r
309 public void TestFlush() {
\r
311 bool errorThrown = false;
\r
313 FileStream f = new FileStream(_codeFileName,
\r
316 StreamWriter r = new StreamWriter(f);
\r
319 } catch (ObjectDisposedException) {
\r
320 errorThrown = true;
\r
321 } catch (Exception e) {
\r
322 Fail ("Incorrect exception thrown at 1: " + e.ToString());
\r
324 Assert("can't flush closed error not thrown", errorThrown);
\r
327 MemoryStream m = new MemoryStream();
\r
328 StreamWriter w = new StreamWriter(m);
\r
333 AssertEquals("Should be nothing before flush",
\r
336 AssertEquals("Should be something after flush",
\r
341 // TODO - Write - test errors, functionality tested in TestFlush.
\r