OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // | 4 // |
5 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:collection'; | 9 import 'dart:collection'; |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 | 700 |
701 // Tests exception handling after file was closed. | 701 // Tests exception handling after file was closed. |
702 static void testCloseException() { | 702 static void testCloseException() { |
703 bool exceptionCaught = false; | 703 bool exceptionCaught = false; |
704 bool wrongExceptionCaught = false; | 704 bool wrongExceptionCaught = false; |
705 File input = new File(tempDirectory.path + "/out_close_exception"); | 705 File input = new File(tempDirectory.path + "/out_close_exception"); |
706 RandomAccessFile openedFile = input.openSync(mode: WRITE); | 706 RandomAccessFile openedFile = input.openSync(mode: WRITE); |
707 openedFile.closeSync(); | 707 openedFile.closeSync(); |
708 try { | 708 try { |
709 openedFile.readByteSync(); | 709 openedFile.readByteSync(); |
710 } on FileException catch (ex) { | 710 } on FileSystemException catch (ex) { |
711 exceptionCaught = true; | 711 exceptionCaught = true; |
712 } on Exception catch (ex) { | 712 } on Exception catch (ex) { |
713 wrongExceptionCaught = true; | 713 wrongExceptionCaught = true; |
714 } | 714 } |
715 Expect.equals(true, exceptionCaught); | 715 Expect.equals(true, exceptionCaught); |
716 Expect.equals(true, !wrongExceptionCaught); | 716 Expect.equals(true, !wrongExceptionCaught); |
717 exceptionCaught = false; | 717 exceptionCaught = false; |
718 try { | 718 try { |
719 openedFile.writeByteSync(1); | 719 openedFile.writeByteSync(1); |
720 } on FileException catch (ex) { | 720 } on FileSystemException catch (ex) { |
721 exceptionCaught = true; | 721 exceptionCaught = true; |
722 } on Exception catch (ex) { | 722 } on Exception catch (ex) { |
723 wrongExceptionCaught = true; | 723 wrongExceptionCaught = true; |
724 } | 724 } |
725 Expect.equals(true, exceptionCaught); | 725 Expect.equals(true, exceptionCaught); |
726 Expect.equals(true, !wrongExceptionCaught); | 726 Expect.equals(true, !wrongExceptionCaught); |
727 exceptionCaught = false; | 727 exceptionCaught = false; |
728 try { | 728 try { |
729 openedFile.writeStringSync("Test"); | 729 openedFile.writeStringSync("Test"); |
730 } on FileException catch (ex) { | 730 } on FileSystemException catch (ex) { |
731 exceptionCaught = true; | 731 exceptionCaught = true; |
732 } on Exception catch (ex) { | 732 } on Exception catch (ex) { |
733 wrongExceptionCaught = true; | 733 wrongExceptionCaught = true; |
734 } | 734 } |
735 Expect.equals(true, exceptionCaught); | 735 Expect.equals(true, exceptionCaught); |
736 Expect.equals(true, !wrongExceptionCaught); | 736 Expect.equals(true, !wrongExceptionCaught); |
737 exceptionCaught = false; | 737 exceptionCaught = false; |
738 try { | 738 try { |
739 List<int> buffer = new List<int>(100); | 739 List<int> buffer = new List<int>(100); |
740 openedFile.readIntoSync(buffer, 0, 10); | 740 openedFile.readIntoSync(buffer, 0, 10); |
741 } on FileException catch (ex) { | 741 } on FileSystemException catch (ex) { |
742 exceptionCaught = true; | 742 exceptionCaught = true; |
743 } on Exception catch (ex) { | 743 } on Exception catch (ex) { |
744 wrongExceptionCaught = true; | 744 wrongExceptionCaught = true; |
745 } | 745 } |
746 Expect.equals(true, exceptionCaught); | 746 Expect.equals(true, exceptionCaught); |
747 Expect.equals(true, !wrongExceptionCaught); | 747 Expect.equals(true, !wrongExceptionCaught); |
748 exceptionCaught = false; | 748 exceptionCaught = false; |
749 try { | 749 try { |
750 List<int> buffer = new List<int>(100); | 750 List<int> buffer = new List<int>(100); |
751 openedFile.writeFromSync(buffer, 0, 10); | 751 openedFile.writeFromSync(buffer, 0, 10); |
752 } on FileException catch (ex) { | 752 } on FileSystemException catch (ex) { |
753 exceptionCaught = true; | 753 exceptionCaught = true; |
754 } on Exception catch (ex) { | 754 } on Exception catch (ex) { |
755 wrongExceptionCaught = true; | 755 wrongExceptionCaught = true; |
756 } | 756 } |
757 Expect.equals(true, exceptionCaught); | 757 Expect.equals(true, exceptionCaught); |
758 Expect.equals(true, !wrongExceptionCaught); | 758 Expect.equals(true, !wrongExceptionCaught); |
759 exceptionCaught = false; | 759 exceptionCaught = false; |
760 try { | 760 try { |
761 openedFile.positionSync(); | 761 openedFile.positionSync(); |
762 } on FileException catch (ex) { | 762 } on FileSystemException catch (ex) { |
763 exceptionCaught = true; | 763 exceptionCaught = true; |
764 } on Exception catch (ex) { | 764 } on Exception catch (ex) { |
765 wrongExceptionCaught = true; | 765 wrongExceptionCaught = true; |
766 } | 766 } |
767 Expect.equals(true, exceptionCaught); | 767 Expect.equals(true, exceptionCaught); |
768 Expect.equals(true, !wrongExceptionCaught); | 768 Expect.equals(true, !wrongExceptionCaught); |
769 exceptionCaught = false; | 769 exceptionCaught = false; |
770 try { | 770 try { |
771 openedFile.lengthSync(); | 771 openedFile.lengthSync(); |
772 } on FileException catch (ex) { | 772 } on FileSystemException catch (ex) { |
773 exceptionCaught = true; | 773 exceptionCaught = true; |
774 } on Exception catch (ex) { | 774 } on Exception catch (ex) { |
775 wrongExceptionCaught = true; | 775 wrongExceptionCaught = true; |
776 } | 776 } |
777 Expect.equals(true, exceptionCaught); | 777 Expect.equals(true, exceptionCaught); |
778 Expect.equals(true, !wrongExceptionCaught); | 778 Expect.equals(true, !wrongExceptionCaught); |
779 exceptionCaught = false; | 779 exceptionCaught = false; |
780 try { | 780 try { |
781 openedFile.flushSync(); | 781 openedFile.flushSync(); |
782 } on FileException catch (ex) { | 782 } on FileSystemException catch (ex) { |
783 exceptionCaught = true; | 783 exceptionCaught = true; |
784 } on Exception catch (ex) { | 784 } on Exception catch (ex) { |
785 wrongExceptionCaught = true; | 785 wrongExceptionCaught = true; |
786 } | 786 } |
787 Expect.equals(true, exceptionCaught); | 787 Expect.equals(true, exceptionCaught); |
788 Expect.equals(true, !wrongExceptionCaught); | 788 Expect.equals(true, !wrongExceptionCaught); |
789 input.deleteSync(); | 789 input.deleteSync(); |
790 } | 790 } |
791 | 791 |
792 // Tests stream exception handling after file was closed. | 792 // Tests stream exception handling after file was closed. |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
909 future.then((r) => Expect.fail('Directory opened as file')) | 909 future.then((r) => Expect.fail('Directory opened as file')) |
910 .catchError((e) {}); | 910 .catchError((e) {}); |
911 } | 911 } |
912 | 912 |
913 static void testOpenDirectoryAsFileSync() { | 913 static void testOpenDirectoryAsFileSync() { |
914 var f = new File('.'); | 914 var f = new File('.'); |
915 try { | 915 try { |
916 f.openSync(); | 916 f.openSync(); |
917 Expect.fail("Expected exception opening directory as file"); | 917 Expect.fail("Expected exception opening directory as file"); |
918 } catch (e) { | 918 } catch (e) { |
919 Expect.isTrue(e is FileException); | 919 Expect.isTrue(e is FileSystemException); |
920 } | 920 } |
921 } | 921 } |
922 | 922 |
923 static void testReadAsBytes() { | 923 static void testReadAsBytes() { |
924 asyncTestStarted(); | 924 asyncTestStarted(); |
925 var name = getFilename("tests/vm/data/fixed_length_file"); | 925 var name = getFilename("tests/vm/data/fixed_length_file"); |
926 var f = new File(name); | 926 var f = new File(name); |
927 f.readAsBytes().then((bytes) { | 927 f.readAsBytes().then((bytes) { |
928 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 928 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
929 Expect.equals(42, bytes.length); | 929 Expect.equals(42, bytes.length); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
998 var text = new File(name).readAsStringSync(); | 998 var text = new File(name).readAsStringSync(); |
999 Expect.isTrue(text.endsWith("42 bytes.")); | 999 Expect.isTrue(text.endsWith("42 bytes.")); |
1000 Expect.equals(42, text.length); | 1000 Expect.equals(42, text.length); |
1001 name = getDataFilename("tests/standalone/io/read_as_text.dat"); | 1001 name = getDataFilename("tests/standalone/io/read_as_text.dat"); |
1002 text = new File(name).readAsStringSync(); | 1002 text = new File(name).readAsStringSync(); |
1003 Expect.equals(6, text.length); | 1003 Expect.equals(6, text.length); |
1004 var expected = [955, 120, 46, 32, 120, 10]; | 1004 var expected = [955, 120, 46, 32, 120, 10]; |
1005 Expect.listEquals(expected, text.codeUnits); | 1005 Expect.listEquals(expected, text.codeUnits); |
1006 // First character is not ASCII. The default ASCII decoder will throw. | 1006 // First character is not ASCII. The default ASCII decoder will throw. |
1007 Expect.throws(() => new File(name).readAsStringSync(encoding: ASCII), | 1007 Expect.throws(() => new File(name).readAsStringSync(encoding: ASCII), |
1008 (e) => e is FileException); | 1008 (e) => e is FileSystemException); |
1009 // We can use an ASCII decoder that inserts the replacement character. | 1009 // We can use an ASCII decoder that inserts the replacement character. |
1010 var lenientAscii = const AsciiCodec(allowInvalid: true); | 1010 var lenientAscii = const AsciiCodec(allowInvalid: true); |
1011 text = new File(name).readAsStringSync(encoding: lenientAscii); | 1011 text = new File(name).readAsStringSync(encoding: lenientAscii); |
1012 // Default replacement character is the Unicode replacement character. | 1012 // Default replacement character is the Unicode replacement character. |
1013 expected = [UNICODE_REPLACEMENT_CHARACTER_RUNE, | 1013 expected = [UNICODE_REPLACEMENT_CHARACTER_RUNE, |
1014 UNICODE_REPLACEMENT_CHARACTER_RUNE, | 1014 UNICODE_REPLACEMENT_CHARACTER_RUNE, |
1015 120, 46, 32, 120, 10]; | 1015 120, 46, 32, 120, 10]; |
1016 Expect.listEquals(expected, text.codeUnits); | 1016 Expect.listEquals(expected, text.codeUnits); |
1017 text = new File(name).readAsStringSync(encoding: LATIN1); | 1017 text = new File(name).readAsStringSync(encoding: LATIN1); |
1018 expected = [206, 187, 120, 46, 32, 120, 10]; | 1018 expected = [206, 187, 120, 46, 32, 120, 10]; |
(...skipping 29 matching lines...) Expand all Loading... |
1048 Expect.equals(42, line.length); | 1048 Expect.equals(42, line.length); |
1049 name = getDataFilename("tests/standalone/io/readline_test1.dat"); | 1049 name = getDataFilename("tests/standalone/io/readline_test1.dat"); |
1050 lines = new File(name).readAsLinesSync(); | 1050 lines = new File(name).readAsLinesSync(); |
1051 Expect.equals(10, lines.length); | 1051 Expect.equals(10, lines.length); |
1052 } | 1052 } |
1053 | 1053 |
1054 | 1054 |
1055 static void testReadAsErrors() { | 1055 static void testReadAsErrors() { |
1056 asyncTestStarted(); | 1056 asyncTestStarted(); |
1057 var f = new File('.'); | 1057 var f = new File('.'); |
1058 Expect.throws(f.readAsBytesSync, (e) => e is FileException); | 1058 Expect.throws(f.readAsBytesSync, (e) => e is FileSystemException); |
1059 Expect.throws(f.readAsStringSync, (e) => e is FileException); | 1059 Expect.throws(f.readAsStringSync, (e) => e is FileSystemException); |
1060 Expect.throws(f.readAsLinesSync, (e) => e is FileException); | 1060 Expect.throws(f.readAsLinesSync, (e) => e is FileSystemException); |
1061 var readAsBytesFuture = f.readAsBytes(); | 1061 var readAsBytesFuture = f.readAsBytes(); |
1062 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected")) | 1062 readAsBytesFuture.then((bytes) => Expect.fail("no bytes expected")) |
1063 .catchError((e) { | 1063 .catchError((e) { |
1064 var readAsStringFuture = f.readAsString(encoding: UTF8); | 1064 var readAsStringFuture = f.readAsString(encoding: UTF8); |
1065 readAsStringFuture.then((text) => Expect.fail("no text expected")) | 1065 readAsStringFuture.then((text) => Expect.fail("no text expected")) |
1066 .catchError((e) { | 1066 .catchError((e) { |
1067 var readAsLinesFuture = f.readAsLines(encoding: UTF8); | 1067 var readAsLinesFuture = f.readAsLines(encoding: UTF8); |
1068 readAsLinesFuture.then((lines) => Expect.fail("no lines expected")) | 1068 readAsLinesFuture.then((lines) => Expect.fail("no lines expected")) |
1069 .catchError((e) { | 1069 .catchError((e) { |
1070 asyncTestDone("testReadAsLines"); | 1070 asyncTestDone("testReadAsLines"); |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1225 .then((_) => lift(Expect.isFalse)(file.exists())) | 1225 .then((_) => lift(Expect.isFalse)(file.exists())) |
1226 .then((_) => lift(Expect.isTrue)(newfile.exists())) | 1226 .then((_) => lift(Expect.isTrue)(newfile.exists())) |
1227 .then((_) => newfile.delete()) | 1227 .then((_) => newfile.delete()) |
1228 .then((_) => lift(Expect.isFalse)(newfile.exists())) | 1228 .then((_) => lift(Expect.isFalse)(newfile.exists())) |
1229 .then((_) { | 1229 .then((_) { |
1230 if (Platform.operatingSystem != "windows") { | 1230 if (Platform.operatingSystem != "windows") { |
1231 new Link(source).create(dest) | 1231 new Link(source).create(dest) |
1232 .then((_) => file.rename("xxx")) | 1232 .then((_) => file.rename("xxx")) |
1233 .then((_) { throw "Rename of broken link succeeded"; }) | 1233 .then((_) { throw "Rename of broken link succeeded"; }) |
1234 .catchError((e) { | 1234 .catchError((e) { |
1235 Expect.isTrue(e is FileException); | 1235 Expect.isTrue(e is FileSystemException); |
1236 asyncTestDone("testRename$targetExists"); | 1236 asyncTestDone("testRename$targetExists"); |
1237 }); | 1237 }); |
1238 } else { | 1238 } else { |
1239 asyncTestDone("testRename$targetExists"); | 1239 asyncTestDone("testRename$targetExists"); |
1240 } | 1240 } |
1241 }); | 1241 }); |
1242 } | 1242 } |
1243 | 1243 |
1244 static void testRenameSync({bool targetExists}) { | 1244 static void testRenameSync({bool targetExists}) { |
1245 String source = join(tempDirectory.path, 'rename_source'); | 1245 String source = join(tempDirectory.path, 'rename_source'); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1328 testLastModified(); | 1328 testLastModified(); |
1329 testDoubleAsyncOperation(); | 1329 testDoubleAsyncOperation(); |
1330 asyncEnd(); | 1330 asyncEnd(); |
1331 }); | 1331 }); |
1332 } | 1332 } |
1333 } | 1333 } |
1334 | 1334 |
1335 main() { | 1335 main() { |
1336 FileTest.testMain(); | 1336 FileTest.testMain(); |
1337 } | 1337 } |
OLD | NEW |