Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: tests/standalone/src/io/FileTest.dart

Issue 10252020: test rename overhaul: step 12 - standalone (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 //
5 // Dart test program for testing file I/O.
6
7 #import("dart:io");
8 #import("dart:isolate");
9
10 class MyListOfOneElement implements List {
11 int _value;
12 MyListOfOneElement(this._value);
13 int get length() => 1;
14 operator [](int index) => _value;
15 }
16
17 class FileTest {
18 static Directory tempDirectory;
19 static int numLiveAsyncTests = 0;
20
21 static void asyncTestStarted() { ++numLiveAsyncTests; }
22 static void asyncTestDone(String name) {
23 --numLiveAsyncTests;
24 if (numLiveAsyncTests == 0) {
25 deleteTempDirectory();
26 }
27 }
28
29 static void createTempDirectory(Function doNext) {
30 tempDirectory = new Directory('');
31 tempDirectory.onError = (e) {
32 Expect.fail("Failed creating temporary directory");
33 };
34 tempDirectory.createTemp(doNext);
35 }
36
37 static void deleteTempDirectory() {
38 tempDirectory.deleteRecursivelySync();
39 }
40
41 // Test for file read functionality.
42 static void testReadStream() {
43 // Read a file and check part of it's contents.
44 String filename = getFilename("bin/file_test.cc");
45 File file = new File(filename);
46 InputStream input = file.openInputStream();
47 input.onData = () {
48 List<int> buffer = new List<int>(42);
49 int bytesRead = input.readInto(buffer, 0, 12);
50 Expect.equals(12, bytesRead);
51 bytesRead = input.readInto(buffer, 12, 30);
52 input.close();
53 Expect.equals(30, bytesRead);
54 Expect.equals(47, buffer[0]); // represents '/' in the file.
55 Expect.equals(47, buffer[1]); // represents '/' in the file.
56 Expect.equals(32, buffer[2]); // represents ' ' in the file.
57 Expect.equals(67, buffer[3]); // represents 'C' in the file.
58 Expect.equals(111, buffer[4]); // represents 'o' in the file.
59 Expect.equals(112, buffer[5]); // represents 'p' in the file.
60 Expect.equals(121, buffer[6]); // represents 'y' in the file.
61 Expect.equals(114, buffer[7]); // represents 'r' in the file.
62 Expect.equals(105, buffer[8]); // represents 'i' in the file.
63 Expect.equals(103, buffer[9]); // represents 'g' in the file.
64 Expect.equals(104, buffer[10]); // represents 'h' in the file.
65 Expect.equals(116, buffer[11]); // represents 't' in the file.
66 };
67 }
68
69 // Test for file read and write functionality.
70 static void testReadWriteStream() {
71 asyncTestStarted();
72
73 // Read a file.
74 String inFilename = getFilename("tests/vm/data/fixed_length_file");
75 File file;
76 InputStream input;
77 int bytesRead;
78
79 // Test reading all using readInto.
80 file = new File(inFilename);
81 input = file.openInputStream();
82 input.onData = () {
83 List<int> buffer1 = new List<int>(42);
84 bytesRead = input.readInto(buffer1, 0, 42);
85 Expect.equals(42, bytesRead);
86 Expect.isTrue(input.closed);
87
88 // Test reading all using readInto and read.
89 file = new File(inFilename);
90 input = file.openInputStream();
91 input.onData = () {
92 bytesRead = input.readInto(buffer1, 0, 21);
93 Expect.equals(21, bytesRead);
94 buffer1 = input.read();
95 Expect.equals(21, buffer1.length);
96 Expect.isTrue(input.closed);
97
98 // Test reading all using read and readInto.
99 file = new File(inFilename);
100 input = file.openInputStream();
101 input.onData = () {
102 buffer1 = input.read(21);
103 Expect.equals(21, buffer1.length);
104 bytesRead = input.readInto(buffer1, 0, 21);
105 Expect.equals(21, bytesRead);
106 Expect.isTrue(input.closed);
107
108 // Test reading all using read.
109 file = new File(inFilename);
110 input = file.openInputStream();
111 input.onData = () {
112 buffer1 = input.read();
113 Expect.equals(42, buffer1.length);
114 Expect.isTrue(input.closed);
115
116 // Write the contents of the file just read into another file.
117 String outFilename = tempDirectory.path + "/out_read_write_stream";
118 file = new File(outFilename);
119 OutputStream output = file.openOutputStream();
120 bool writeDone = output.writeFrom(buffer1, 0, 42);
121 Expect.equals(false, writeDone);
122 output.onNoPendingWrites = () {
123 output.close();
124 output.onClosed = () {
125 // Now read the contents of the file just written.
126 List<int> buffer2 = new List<int>(42);
127 file = new File(outFilename);
128 input = file.openInputStream();
129 input.onData = () {
130 bytesRead = input.readInto(buffer2, 0, 42);
131 Expect.equals(42, bytesRead);
132 // Now compare the two buffers to check if they are identical.
133 for (int i = 0; i < buffer1.length; i++) {
134 Expect.equals(buffer1[i], buffer2[i]);
135 }
136 };
137 input.onClosed = () {
138 // Delete the output file.
139 file.deleteSync();
140 Expect.isFalse(file.existsSync());
141 asyncTestDone("testReadWriteStream");
142 };
143 };
144 };
145 };
146 };
147 };
148 };
149 }
150
151 static void testRead() {
152 // Read a file and check part of it's contents.
153 String filename = getFilename("bin/file_test.cc");
154 File file = new File(filename);
155 file.onError = (e) {
156 Expect.fail("No errors expected : $e");
157 };
158 file.open(FileMode.READ, (RandomAccessFile file) {
159 List<int> buffer = new List<int>(10);
160 file.readList(buffer, 0, 5, (bytes_read) {
161 Expect.equals(5, bytes_read);
162 file.readList(buffer, 5, 5, (bytes_read) {
163 Expect.equals(5, bytes_read);
164 Expect.equals(47, buffer[0]); // represents '/' in the file.
165 Expect.equals(47, buffer[1]); // represents '/' in the file.
166 Expect.equals(32, buffer[2]); // represents ' ' in the file.
167 Expect.equals(67, buffer[3]); // represents 'C' in the file.
168 Expect.equals(111, buffer[4]); // represents 'o' in the file.
169 Expect.equals(112, buffer[5]); // represents 'p' in the file.
170 Expect.equals(121, buffer[6]); // represents 'y' in the file.
171 Expect.equals(114, buffer[7]); // represents 'r' in the file.
172 Expect.equals(105, buffer[8]); // represents 'i' in the file.
173 Expect.equals(103, buffer[9]); // represents 'g' in the file.
174 file.close(() => null);
175 });
176 });
177 });
178 }
179
180 static void testReadSync() {
181 // Read a file and check part of it's contents.
182 String filename = getFilename("bin/file_test.cc");
183 RandomAccessFile file = (new File(filename)).openSync();
184 List<int> buffer = new List<int>(42);
185 int bytes_read = 0;
186 bytes_read = file.readListSync(buffer, 0, 12);
187 Expect.equals(12, bytes_read);
188 bytes_read = file.readListSync(buffer, 12, 30);
189 Expect.equals(30, bytes_read);
190 Expect.equals(47, buffer[0]); // represents '/' in the file.
191 Expect.equals(47, buffer[1]); // represents '/' in the file.
192 Expect.equals(32, buffer[2]); // represents ' ' in the file.
193 Expect.equals(67, buffer[3]); // represents 'C' in the file.
194 Expect.equals(111, buffer[4]); // represents 'o' in the file.
195 Expect.equals(112, buffer[5]); // represents 'p' in the file.
196 Expect.equals(121, buffer[6]); // represents 'y' in the file.
197 Expect.equals(114, buffer[7]); // represents 'r' in the file.
198 Expect.equals(105, buffer[8]); // represents 'i' in the file.
199 Expect.equals(103, buffer[9]); // represents 'g' in the file.
200 Expect.equals(104, buffer[10]); // represents 'h' in the file.
201 Expect.equals(116, buffer[11]); // represents 't' in the file.
202 }
203
204 // Test for file read and write functionality.
205 static void testReadWrite() {
206 // Read a file.
207 String inFilename = getFilename("tests/vm/data/fixed_length_file");
208 final File file = new File(inFilename);
209 file.onError = (e) {
210 Expect.fail("No errors expected : $e");
211 };
212 file.open(FileMode.READ, (RandomAccessFile openedFile) {
213 openedFile.onError = (s) {
214 Expect.fail("No errors expected : $s");
215 };
216 List<int> buffer1 = new List<int>(42);
217 openedFile.readList(buffer1, 0, 42, (bytes_read) {
218 Expect.equals(42, bytes_read);
219 openedFile.close(() {
220 // Write the contents of the file just read into another file.
221 String outFilename = tempDirectory.path + "/out_read_write";
222 final File file2 = new File(outFilename);
223 file2.onError = (e) {
224 Expect.fail("No errors expected : $e");
225 };
226 file2.create(() {
227 file2.fullPath((s) {
228 Expect.isTrue(new File(s).existsSync());
229 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') {
230 Expect.fail("Not a full path");
231 }
232 file2.open(FileMode.WRITE, (RandomAccessFile openedFile2) {
233 openedFile2.onError = (s) {
234 Expect.fail("No errors expected : $s");
235 };
236 openedFile2.writeList(buffer1, 0, bytes_read);
237 openedFile2.onNoPendingWrites = () {
238 openedFile2.close(() {
239 List<int> buffer2 = new List<int>(bytes_read);
240 final File file3 = new File(outFilename);
241 file3.onError = (e) {
242 Expect.fail("No errors expected : $e");
243 };
244 file3.open(FileMode.READ, (RandomAccessFile openedFile3) {
245 openedFile3.onError = (s) {
246 Expect.fail("No errors expected : $s");
247 };
248 openedFile3.readList(buffer2, 0, 42, (bytes_read) {
249 Expect.equals(42, bytes_read);
250 openedFile3.close(() {
251 // Now compare the two buffers to check if they
252 // are identical.
253 Expect.equals(buffer1.length, buffer2.length);
254 for (int i = 0; i < buffer1.length; i++) {
255 Expect.equals(buffer1[i], buffer2[i]);
256 }
257 // Delete the output file.
258 final file4 = file3;
259 file4.delete(() {
260 file4.exists((exists) {
261 Expect.isFalse(exists);
262 asyncTestDone("testReadWrite");
263 });
264 });
265 });
266 });
267 });
268 });
269 };
270 });
271 });
272 });
273 });
274 });
275 });
276 asyncTestStarted();
277 }
278
279 static void testWriteAppend() {
280 String content = "foobar";
281 String filename = tempDirectory.path + "/write_append";
282 File file = new File(filename);
283 file.createSync();
284 Expect.isTrue(new File(filename).existsSync());
285 List<int> buffer = content.charCodes();
286 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
287 openedFile.writeListSync(buffer, 0, buffer.length);
288 openedFile.closeSync();
289 // Reopen the file in write mode to ensure that we overwrite the content.
290 openedFile = (new File(filename)).openSync(FileMode.WRITE);
291 openedFile.writeListSync(buffer, 0, buffer.length);
292 Expect.equals(content.length, openedFile.lengthSync());
293 openedFile.closeSync();
294 // Open the file in append mode and ensure that we do not overwrite
295 // the existing content.
296 openedFile = (new File(filename)).openSync(FileMode.APPEND);
297 openedFile.writeListSync(buffer, 0, buffer.length);
298 Expect.equals(content.length * 2, openedFile.lengthSync());
299 openedFile.closeSync();
300 file.deleteSync();
301 }
302
303 static void testOutputStreamWriteAppend() {
304 String content = "foobar";
305 String filename = tempDirectory.path + "/outstream_write_append";
306 File file = new File(filename);
307 file.createSync();
308 List<int> buffer = content.charCodes();
309 OutputStream outStream = file.openOutputStream();
310 outStream.write(buffer);
311 outStream.onNoPendingWrites = () {
312 outStream.close();
313 outStream.onClosed = () {
314 File file2 = new File(filename);
315 OutputStream appendingOutput =
316 file2.openOutputStream(FileMode.APPEND);
317 appendingOutput.write(buffer);
318 appendingOutput.onNoPendingWrites = () {
319 appendingOutput.close();
320 appendingOutput.onClosed = () {
321 File file3 = new File(filename);
322 file3.open(FileMode.READ, (RandomAccessFile openedFile) {
323 openedFile.length((int length) {
324 Expect.equals(content.length * 2, length);
325 openedFile.close(() {
326 file3.delete(() {
327 asyncTestDone("testOutputStreamWriteAppend");
328 });
329 });
330 });
331 });
332 };
333 };
334 };
335 };
336 asyncTestStarted();
337 }
338
339 // Test for file read and write functionality.
340 static void testOutputStreamWriteString() {
341 String content = "foobar";
342 String filename = tempDirectory.path + "/outstream_write_string";
343 File file = new File(filename);
344 file.createSync();
345 List<int> buffer = content.charCodes();
346 OutputStream outStream = file.openOutputStream();
347 outStream.writeString("abcdABCD");
348 outStream.writeString("abcdABCD", Encoding.UTF_8);
349 outStream.writeString("abcdABCD", Encoding.ISO_8859_1);
350 outStream.writeString("abcdABCD", Encoding.ASCII);
351 outStream.writeString("æøå", Encoding.UTF_8);
352 outStream.onNoPendingWrites = () {
353 outStream.close();
354 outStream.onClosed = () {
355 RandomAccessFile raf = file.openSync();
356 Expect.equals(38, raf.lengthSync());
357 };
358 };
359 asyncTestStarted();
360 }
361
362
363 static void testReadWriteSync() {
364 // Read a file.
365 String inFilename = getFilename("tests/vm/data/fixed_length_file");
366 RandomAccessFile file = (new File(inFilename)).openSync();
367 List<int> buffer1 = new List<int>(42);
368 int bytes_read = 0;
369 int bytes_written = 0;
370 bytes_read = file.readListSync(buffer1, 0, 42);
371 Expect.equals(42, bytes_read);
372 file.closeSync();
373 // Write the contents of the file just read into another file.
374 String outFilename = tempDirectory.path + "/out_read_write_sync";
375 File outFile = new File(outFilename);
376 outFile.createSync();
377 String path = outFile.fullPathSync();
378 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') {
379 Expect.fail("Not a full path");
380 }
381 Expect.isTrue(new File(path).existsSync());
382 RandomAccessFile openedFile = outFile.openSync(FileMode.WRITE);
383 openedFile.writeListSync(buffer1, 0, bytes_read);
384 openedFile.closeSync();
385 // Now read the contents of the file just written.
386 List<int> buffer2 = new List<int>(bytes_read);
387 openedFile = (new File(outFilename)).openSync();
388 bytes_read = openedFile.readListSync(buffer2, 0, 42);
389 Expect.equals(42, bytes_read);
390 openedFile.closeSync();
391 // Now compare the two buffers to check if they are identical.
392 Expect.equals(buffer1.length, buffer2.length);
393 for (int i = 0; i < buffer1.length; i++) {
394 Expect.equals(buffer1[i], buffer2[i]);
395 }
396 // Delete the output file.
397 outFile.deleteSync();
398 Expect.isFalse(outFile.existsSync());
399 }
400
401 static void testReadEmptyFileSync() {
402 String fileName = tempDirectory.path + "/empty_file_sync";
403 File file = new File(fileName);
404 file.createSync();
405 RandomAccessFile openedFile = file.openSync();
406 Expect.equals(-1, openedFile.readByteSync());
407 openedFile.closeSync();
408 file.deleteSync();
409 }
410
411 static void testReadEmptyFile() {
412 String fileName = tempDirectory.path + "/empty_file";
413 File file = new File(fileName);
414 file.onError = (e) {
415 Expect.fail("No errors expected : $e");
416 };
417 asyncTestStarted();
418 file.create(() {
419 file.open(FileMode.READ, (RandomAccessFile openedFile) {
420 openedFile.readByte((int byte) {
421 Expect.equals(-1, byte);
422 });
423 openedFile.onError = (e) {
424 Expect.isTrue(e is FileIOException);
425 openedFile.close(() {
426 file.delete(() {
427 asyncTestDone("testReadEmptyFile");
428 });
429 });
430 };
431 });
432 });
433 }
434
435 // Test for file write of different types of lists.
436 static void testWriteVariousLists() {
437 asyncTestStarted();
438 final String fileName = "${tempDirectory.path}/testWriteVariousLists";
439 final File file = new File(fileName);
440 file.onError = (e) => Expect.fail("No errors expected : $e");
441 file.create(() {
442 file.open(FileMode.WRITE, (RandomAccessFile openedFile) {
443 // Write bytes from 0 to 7.
444 openedFile.writeList([0], 0, 1);
445 openedFile.writeList(const [1], 0, 1);
446 openedFile.writeList(new MyListOfOneElement(2), 0, 1);
447 var x = 12345678901234567890123456789012345678901234567890;
448 var y = 12345678901234567890123456789012345678901234567893;
449 openedFile.writeList([y - x], 0, 1);
450 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104.
451 openedFile.writeList(const [261], 0, 1);
452 openedFile.writeList(new MyListOfOneElement(262), 0, 1);
453 x = 12345678901234567890123456789012345678901234567890;
454 y = 12345678901234567890123456789012345678901234568153;
455 openedFile.writeList([y - x], 0, 1);
456
457 openedFile.onError = (e) => Expect.fail("No errors expected : $e");
458 openedFile.onNoPendingWrites = () {
459 openedFile.close(() {
460 // Check the written bytes.
461 final File file2 = new File(fileName);
462 var openedFile2 = file2.openSync();
463 var length = openedFile2.lengthSync();
464 Expect.equals(8, length);
465 List data = new List(length);
466 openedFile2.readListSync(data, 0, length);
467 for (var i = 0; i < data.length; i++) {
468 Expect.equals(i, data[i]);
469 }
470 openedFile2.closeSync();
471 file2.deleteSync();
472 asyncTestDone("testWriteVariousLists");
473 });
474 };
475 });
476 });
477 }
478
479 static void testDirectory() {
480 asyncTestStarted();
481
482 // Port to verify that the test completes.
483 var port = new ReceivePort();
484 port.receive((message, replyTo) {
485 port.close();
486 Expect.equals(1, message);
487 asyncTestDone("testDirectory");
488 });
489
490 var tempDir = tempDirectory.path;
491 var file = new File("${tempDir}/testDirectory");
492 var errors = 0;
493 file.directory((d) => Expect.fail("non-existing file"));
494 file.onError = (e) {
495 file.onError = (e) => Expect.fail("no error expected");
496 file.create(() {
497 file.directory((Directory d) {
498 d.onError = (s) => Expect.fail("no error expected");
499 d.exists((exists) {
500 Expect.isTrue(exists);
501 Expect.isTrue(d.path.endsWith(tempDir));
502 file.delete(() {
503 var file_dir = new File(".");
504 file_dir.directory((d) => Expect.fail("non-existing file"));
505 file_dir.onError = (e) {
506 var file_dir = new File(tempDir);
507 file_dir.directory((d) => Expect.fail("non-existing file"));
508 file_dir.onError = (e) => port.toSendPort().send(1);
509 };
510 });
511 });
512 });
513 });
514 };
515 }
516
517 static void testDirectorySync() {
518 var tempDir = tempDirectory.path;
519 var file = new File("${tempDir}/testDirectorySync");
520 // Non-existing file should throw exception.
521 Expect.throws(file.directorySync, (e) { return e is FileIOException; });
522 file.createSync();
523 // Check that the path of the returned directory is the temp directory.
524 Directory d = file.directorySync();
525 Expect.isTrue(d.existsSync());
526 Expect.isTrue(d.path.endsWith(tempDir));
527 file.deleteSync();
528 // Directories should throw exception.
529 var file_dir = new File(".");
530 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
531 file_dir = new File(tempDir);
532 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; });
533 }
534
535 // Test for file length functionality.
536 static void testLength() {
537 String filename = getFilename("tests/vm/data/fixed_length_file");
538 File file = new File(filename);
539 RandomAccessFile openedFile = file.openSync();
540 openedFile.onError = (e) => Expect.fail("No errors expected");
541 file.onError = (e) => Expect.fail("No errors expected");
542 openedFile.length((length) {
543 Expect.equals(42, length);
544 openedFile.close(() => null);
545 });
546 file.length((length) {
547 Expect.equals(42, length);
548 });
549 }
550
551 static void testLengthSync() {
552 String filename = getFilename("tests/vm/data/fixed_length_file");
553 File file = new File(filename);
554 RandomAccessFile openedFile = file.openSync();
555 Expect.equals(42, file.lengthSync());
556 Expect.equals(42, openedFile.lengthSync());
557 openedFile.closeSync();
558 }
559
560 // Test for file position functionality.
561 static void testPosition() {
562 String filename = getFilename("tests/vm/data/fixed_length_file");
563 RandomAccessFile input = (new File(filename)).openSync();
564 input.onError = (e) => Expect.fail("No errors expected");
565 input.position((position) {
566 Expect.equals(0, position);
567 List<int> buffer = new List<int>(100);
568 input.readList(buffer, 0, 12, (bytes_read) {
569 input.position((position) {
570 Expect.equals(12, position);
571 input.readList(buffer, 12, 6, (bytes_read) {
572 input.position((position) {
573 Expect.equals(18, position);
574 input.setPosition(8, () {
575 input.position((position) {
576 Expect.equals(8, position);
577 input.close(() => null);
578 });
579 });
580 });
581 });
582 });
583 });
584 });
585 }
586
587 static void testPositionSync() {
588 String filename = getFilename("tests/vm/data/fixed_length_file");
589 RandomAccessFile input = (new File(filename)).openSync();
590 Expect.equals(0, input.positionSync());
591 List<int> buffer = new List<int>(100);
592 input.readListSync(buffer, 0, 12);
593 Expect.equals(12, input.positionSync());
594 input.readListSync(buffer, 12, 6);
595 Expect.equals(18, input.positionSync());
596 input.setPositionSync(8);
597 Expect.equals(8, input.positionSync());
598 input.closeSync();
599 }
600
601 static void testTruncate() {
602 File file = new File(tempDirectory.path + "/out_truncate");
603 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
604 file.onError = (e) => Expect.fail("No errors expected: $e");
605 file.open(FileMode.WRITE, (RandomAccessFile openedFile) {
606 openedFile.writeList(buffer, 0, 10);
607 openedFile.onNoPendingWrites = () {
608 openedFile.length((length) {
609 Expect.equals(10, length);
610 openedFile.truncate(5, () {
611 openedFile.length((length) {
612 Expect.equals(5, length);
613 openedFile.close(() {
614 file.delete(() {
615 file.exists((exists) {
616 Expect.isFalse(exists);
617 asyncTestDone("testTruncate");
618 });
619 });
620 });
621 });
622 });
623 });
624 };
625 });
626 asyncTestStarted();
627 }
628
629 static void testTruncateSync() {
630 File file = new File(tempDirectory.path + "/out_truncate_sync");
631 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
632 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
633 openedFile.writeListSync(buffer, 0, 10);
634 Expect.equals(10, openedFile.lengthSync());
635 openedFile.truncateSync(5);
636 Expect.equals(5, openedFile.lengthSync());
637 openedFile.closeSync();
638 file.deleteSync();
639 Expect.isFalse(file.existsSync());
640 }
641
642 // Tests exception handling after file was closed.
643 static void testCloseException() {
644 bool exceptionCaught = false;
645 bool wrongExceptionCaught = false;
646 File input = new File(tempDirectory.path + "/out_close_exception");
647 RandomAccessFile openedFile = input.openSync(FileMode.WRITE);
648 openedFile.closeSync();
649 try {
650 openedFile.readByteSync();
651 } catch (FileIOException ex) {
652 exceptionCaught = true;
653 } catch (Exception ex) {
654 wrongExceptionCaught = true;
655 }
656 Expect.equals(true, exceptionCaught);
657 Expect.equals(true, !wrongExceptionCaught);
658 exceptionCaught = false;
659 try {
660 openedFile.writeByteSync(1);
661 } catch (FileIOException ex) {
662 exceptionCaught = true;
663 } catch (Exception ex) {
664 wrongExceptionCaught = true;
665 }
666 Expect.equals(true, exceptionCaught);
667 Expect.equals(true, !wrongExceptionCaught);
668 exceptionCaught = false;
669 try {
670 openedFile.writeStringSync("Test");
671 } catch (FileIOException ex) {
672 exceptionCaught = true;
673 } catch (Exception ex) {
674 wrongExceptionCaught = true;
675 }
676 Expect.equals(true, exceptionCaught);
677 Expect.equals(true, !wrongExceptionCaught);
678 exceptionCaught = false;
679 try {
680 List<int> buffer = new List<int>(100);
681 openedFile.readListSync(buffer, 0, 10);
682 } catch (FileIOException ex) {
683 exceptionCaught = true;
684 } catch (Exception ex) {
685 wrongExceptionCaught = true;
686 }
687 Expect.equals(true, exceptionCaught);
688 Expect.equals(true, !wrongExceptionCaught);
689 exceptionCaught = false;
690 try {
691 List<int> buffer = new List<int>(100);
692 openedFile.writeListSync(buffer, 0, 10);
693 } catch (FileIOException ex) {
694 exceptionCaught = true;
695 } catch (Exception ex) {
696 wrongExceptionCaught = true;
697 }
698 Expect.equals(true, exceptionCaught);
699 Expect.equals(true, !wrongExceptionCaught);
700 exceptionCaught = false;
701 try {
702 openedFile.positionSync();
703 } catch (FileIOException ex) {
704 exceptionCaught = true;
705 } catch (Exception ex) {
706 wrongExceptionCaught = true;
707 }
708 Expect.equals(true, exceptionCaught);
709 Expect.equals(true, !wrongExceptionCaught);
710 exceptionCaught = false;
711 try {
712 openedFile.lengthSync();
713 } catch (FileIOException ex) {
714 exceptionCaught = true;
715 } catch (Exception ex) {
716 wrongExceptionCaught = true;
717 }
718 Expect.equals(true, exceptionCaught);
719 Expect.equals(true, !wrongExceptionCaught);
720 exceptionCaught = false;
721 try {
722 openedFile.flushSync();
723 } catch (FileIOException ex) {
724 exceptionCaught = true;
725 } catch (Exception ex) {
726 wrongExceptionCaught = true;
727 }
728 Expect.equals(true, exceptionCaught);
729 Expect.equals(true, !wrongExceptionCaught);
730 input.deleteSync();
731 }
732
733 // Tests stream exception handling after file was closed.
734 static void testCloseExceptionStream() {
735 asyncTestStarted();
736 List<int> buffer = new List<int>(42);
737 File file = new File(tempDirectory.path + "/out_close_exception_stream");
738 file.createSync();
739 InputStream input = file.openInputStream();
740 input.onClosed = () {
741 Expect.isTrue(input.closed);
742 Expect.isNull(input.readInto(buffer, 0, 12));
743 OutputStream output = file.openOutputStream();
744 output.close();
745 Expect.throws(() => output.writeFrom(buffer, 0, 12));
746 output.onClosed = () {
747 file.deleteSync();
748 asyncTestDone("testCloseExceptionStream");
749 };
750 };
751 }
752
753 // Tests buffer out of bounds exception.
754 static void testBufferOutOfBoundsException() {
755 bool exceptionCaught = false;
756 bool wrongExceptionCaught = false;
757 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds");
758 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
759 try {
760 List<int> buffer = new List<int>(10);
761 openedFile.readListSync(buffer, 0, 12);
762 } catch (IndexOutOfRangeException ex) {
763 exceptionCaught = true;
764 } catch (Exception ex) {
765 wrongExceptionCaught = true;
766 }
767 Expect.equals(true, exceptionCaught);
768 Expect.equals(true, !wrongExceptionCaught);
769 exceptionCaught = false;
770 try {
771 List<int> buffer = new List<int>(10);
772 openedFile.readListSync(buffer, 6, 6);
773 } catch (IndexOutOfRangeException ex) {
774 exceptionCaught = true;
775 } catch (Exception ex) {
776 wrongExceptionCaught = true;
777 }
778 Expect.equals(true, exceptionCaught);
779 Expect.equals(true, !wrongExceptionCaught);
780 exceptionCaught = false;
781 try {
782 List<int> buffer = new List<int>(10);
783 openedFile.readListSync(buffer, -1, 1);
784 } catch (IndexOutOfRangeException ex) {
785 exceptionCaught = true;
786 } catch (Exception ex) {
787 wrongExceptionCaught = true;
788 }
789 Expect.equals(true, exceptionCaught);
790 Expect.equals(true, !wrongExceptionCaught);
791 exceptionCaught = false;
792 try {
793 List<int> buffer = new List<int>(10);
794 openedFile.readListSync(buffer, 0, -1);
795 } catch (IndexOutOfRangeException ex) {
796 exceptionCaught = true;
797 } catch (Exception ex) {
798 wrongExceptionCaught = true;
799 }
800 Expect.equals(true, exceptionCaught);
801 Expect.equals(true, !wrongExceptionCaught);
802 exceptionCaught = false;
803 try {
804 List<int> buffer = new List<int>(10);
805 openedFile.writeListSync(buffer, 0, 12);
806 } catch (IndexOutOfRangeException ex) {
807 exceptionCaught = true;
808 } catch (Exception ex) {
809 wrongExceptionCaught = true;
810 }
811 Expect.equals(true, exceptionCaught);
812 Expect.equals(true, !wrongExceptionCaught);
813 exceptionCaught = false;
814 try {
815 List<int> buffer = new List<int>(10);
816 openedFile.writeListSync(buffer, 6, 6);
817 } catch (IndexOutOfRangeException ex) {
818 exceptionCaught = true;
819 } catch (Exception ex) {
820 wrongExceptionCaught = true;
821 }
822 Expect.equals(true, exceptionCaught);
823 Expect.equals(true, !wrongExceptionCaught);
824 exceptionCaught = false;
825 try {
826 List<int> buffer = new List<int>(10);
827 openedFile.writeListSync(buffer, -1, 1);
828 } catch (IndexOutOfRangeException ex) {
829 exceptionCaught = true;
830 } catch (Exception ex) {
831 wrongExceptionCaught = true;
832 }
833 Expect.equals(true, exceptionCaught);
834 Expect.equals(true, !wrongExceptionCaught);
835 exceptionCaught = false;
836 try {
837 List<int> buffer = new List<int>(10);
838 openedFile.writeListSync(buffer, 0, -1);
839 } catch (IndexOutOfRangeException ex) {
840 exceptionCaught = true;
841 } catch (Exception ex) {
842 wrongExceptionCaught = true;
843 }
844 Expect.equals(true, exceptionCaught);
845 Expect.equals(true, !wrongExceptionCaught);
846 openedFile.closeSync();
847 file.deleteSync();
848 }
849
850 static void testOpenDirectoryAsFile() {
851 var f = new File('.');
852 f.open(FileMode.READ, (r) => Expect.fail('Directory opened as file'));
853 f.onError = (e) => null;
854 }
855
856 static void testOpenDirectoryAsFileSync() {
857 var f = new File('.');
858 try {
859 f.openSync();
860 Expect.fail("Expected exception opening directory as file");
861 } catch (var e) {
862 Expect.isTrue(e is FileIOException);
863 }
864 }
865
866 static void testReadAsBytes() {
867 var port = new ReceivePort();
868 port.receive((result, replyTo) {
869 port.close();
870 Expect.equals(42, result);
871 });
872 var name = getFilename("tests/vm/data/fixed_length_file");
873 var f = new File(name);
874 f.readAsBytes((bytes) {
875 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
876 port.toSendPort().send(bytes.length);
877 });
878 f.onError = (e) => Expect.fail("No errors expected: $e");
879 }
880
881 static void testReadAsBytesSync() {
882 var name = getFilename("tests/vm/data/fixed_length_file");
883 var bytes = new File(name).readAsBytesSync();
884 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes."));
885 Expect.equals(bytes.length, 42);
886 }
887
888 static void testReadAsText() {
889 var port = new ReceivePort();
890 port.receive((result, replyTo) {
891 port.close();
892 Expect.equals(1, result);
893 });
894 var name = getFilename("tests/vm/data/fixed_length_file");
895 var f = new File(name);
896 f.readAsText(Encoding.UTF_8, (text) {
897 Expect.isTrue(text.endsWith("42 bytes."));
898 Expect.equals(42, text.length);
899 var name = getDataFilename("tests/standalone/src/io/read_as_text.dat");
900 var f = new File(name);
901 f.onError = (e) => Expect.fail("No errors expected: $e");
902 f.readAsText(Encoding.UTF_8, (text) {
903 Expect.equals(6, text.length);
904 var expected = [955, 120, 46, 32, 120, 10];
905 Expect.listEquals(expected, text.charCodes());
906 f.readAsText(Encoding.ISO_8859_1, (text) {
907 Expect.equals(7, text.length);
908 var expected = [206, 187, 120, 46, 32, 120, 10];
909 Expect.listEquals(expected, text.charCodes());
910 f.onError = (e) {
911 port.toSendPort().send(1);
912 };
913 f.readAsText(Encoding.ASCII, (text) {
914 Expect.fail("Non-ascii char should cause error");
915 });
916 });
917 });
918 });
919 f.onError = (e) {
920 Expect.fail("No errors expected: $e");
921 };
922 }
923
924 static void testReadAsTextSync() {
925 var name = getFilename("tests/vm/data/fixed_length_file");
926 var text = new File(name).readAsTextSync();
927 Expect.isTrue(text.endsWith("42 bytes."));
928 Expect.equals(42, text.length);
929 name = getDataFilename("tests/standalone/src/io/read_as_text.dat");
930 text = new File(name).readAsTextSync();
931 Expect.equals(6, text.length);
932 var expected = [955, 120, 46, 32, 120, 10];
933 Expect.listEquals(expected, text.charCodes());
934 Expect.throws(() { new File(name).readAsTextSync(Encoding.ASCII); });
935 text = new File(name).readAsTextSync(Encoding.ISO_8859_1);
936 expected = [206, 187, 120, 46, 32, 120, 10];
937 Expect.equals(7, text.length);
938 Expect.listEquals(expected, text.charCodes());
939 }
940
941 static void testReadAsLines() {
942 var port = new ReceivePort();
943 port.receive((result, replyTo) {
944 port.close();
945 Expect.equals(42, result);
946 });
947 var name = getFilename("tests/vm/data/fixed_length_file");
948 var f = new File(name);
949 f.readAsLines(Encoding.UTF_8, (lines) {
950 Expect.equals(1, lines.length);
951 var line = lines[0];
952 Expect.isTrue(line.endsWith("42 bytes."));
953 port.toSendPort().send(line.length);
954 });
955 f.onError = (e) => Expect.fail("No errors expected: $e");
956 }
957
958 static void testReadAsLinesSync() {
959 var name = getFilename("tests/vm/data/fixed_length_file");
960 var lines = new File(name).readAsLinesSync();
961 Expect.equals(1, lines.length);
962 var line = lines[0];
963 Expect.isTrue(line.endsWith("42 bytes."));
964 Expect.equals(42, line.length);
965 name = getDataFilename("tests/standalone/src/io/readline_test1.dat");
966 lines = new File(name).readAsLinesSync();
967 Expect.equals(10, lines.length);
968 }
969
970
971 static void testReadAsErrors() {
972 var port = new ReceivePort();
973 port.receive((message, _) {
974 port.close();
975 Expect.equals(1, message);
976 });
977 var f = new File('.');
978 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException);
979 Expect.throws(f.readAsTextSync, (e) => e is FileIOException);
980 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException);
981 f.readAsBytes((bytes) => Expect.fail("no bytes expected"));
982 f.onError = (e) {
983 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected"));
984 f.onError = (e) {
985 f.readAsLines(Encoding.UTF_8,
986 (lines) => Expect.fail("no lines expected"));
987 f.onError = (e) => port.toSendPort().send(1);
988 };
989 };
990 }
991
992 // Test that opens the same file for writing then for appending to test
993 // that the file is not truncated when opened for appending.
994 static void testAppend() {
995 var file = new File('${tempDirectory.path}/out_append');
996 file.open(FileMode.WRITE, (openedFile) {
997 openedFile.writeString("asdf");
998 openedFile.onNoPendingWrites = () {
999 openedFile.close(() {
1000 file.open(FileMode.APPEND, (openedFile) {
1001 openedFile.length((length) {
1002 Expect.equals(4, length);
1003 openedFile.writeString("asdf");
1004 openedFile.onNoPendingWrites = () {
1005 openedFile.length((length) {
1006 Expect.equals(8, length);
1007 openedFile.close(() {
1008 file.delete(() {
1009 file.exists((exists) {
1010 Expect.isFalse(exists);
1011 asyncTestDone("testAppend");
1012 });
1013 });
1014 });
1015 });
1016 };
1017 });
1018 });
1019 });
1020 };
1021 });
1022 asyncTestStarted();
1023 }
1024
1025 static void testAppendSync() {
1026 var file = new File('${tempDirectory.path}/out_append_sync');
1027 var openedFile = file.openSync(FileMode.WRITE);
1028 openedFile.writeStringSync("asdf");
1029 Expect.equals(4, openedFile.lengthSync());
1030 openedFile.closeSync();
1031 openedFile = file.openSync(FileMode.WRITE);
1032 openedFile.setPositionSync(4);
1033 openedFile.writeStringSync("asdf");
1034 Expect.equals(8, openedFile.lengthSync());
1035 openedFile.closeSync();
1036 file.deleteSync();
1037 Expect.isFalse(file.existsSync());
1038 }
1039
1040 // Helper method to be able to run the test from the runtime
1041 // directory, or the top directory.
1042 static String getFilename(String path) =>
1043 new File(path).existsSync() ? path : 'runtime/' + path;
1044
1045 static String getDataFilename(String path) =>
1046 new File(path).existsSync() ? path : '../' + path;
1047
1048 // Main test entrypoint.
1049 static testMain() {
1050 testRead();
1051 testReadSync();
1052 testReadStream();
1053 testLength();
1054 testLengthSync();
1055 testPosition();
1056 testPositionSync();
1057 testOpenDirectoryAsFile();
1058 testOpenDirectoryAsFileSync();
1059 testReadAsBytes();
1060 testReadAsBytesSync();
1061 testReadAsText();
1062 testReadAsTextSync();
1063 testReadAsLines();
1064 testReadAsLinesSync();
1065 testReadAsErrors();
1066
1067 createTempDirectory(() {
1068 testReadWrite();
1069 testReadWriteSync();
1070 testReadWriteStream();
1071 testReadEmptyFileSync();
1072 testReadEmptyFile();
1073 testTruncate();
1074 testTruncateSync();
1075 testCloseException();
1076 testCloseExceptionStream();
1077 testBufferOutOfBoundsException();
1078 testAppend();
1079 testAppendSync();
1080 testWriteAppend();
1081 testOutputStreamWriteAppend();
1082 testOutputStreamWriteString();
1083 testWriteVariousLists();
1084 testDirectory();
1085 testDirectorySync();
1086 });
1087 }
1088 }
1089
1090 main() {
1091 FileTest.testMain();
1092 }
OLDNEW
« no previous file with comments | « tests/standalone/src/io/FileOutputStreamTest.dart ('k') | tests/standalone/src/io/HttpClientTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698