| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 error handling in file I/O. | 5 // Dart test program for testing error handling in file I/O. |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
| 9 | 9 |
| 10 Directory tempDir() { | 10 Directory tempDir() { |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 file.createSync(); | 223 file.createSync(); |
| 224 var openedFile = file.openSync(FileMode.READ); | 224 var openedFile = file.openSync(FileMode.READ); |
| 225 | 225 |
| 226 // Writing to read only file should throw an exception. | 226 // Writing to read only file should throw an exception. |
| 227 Expect.throws(() => openedFile.writeByteSync(0), | 227 Expect.throws(() => openedFile.writeByteSync(0), |
| 228 (e) => checkWriteReadOnlyFileException(e)); | 228 (e) => checkWriteReadOnlyFileException(e)); |
| 229 | 229 |
| 230 openedFile.writeByte(0); | 230 openedFile.writeByte(0); |
| 231 openedFile.onError = (e) { | 231 openedFile.onError = (e) { |
| 232 checkWriteReadOnlyFileException(e); | 232 checkWriteReadOnlyFileException(e); |
| 233 p.toSendPort().send(null); | 233 openedFile.close(() => p.toSendPort().send(null)); |
| 234 }; | 234 }; |
| 235 } | 235 } |
| 236 | 236 |
| 237 testWriteListToReadOnlyFile() { | 237 testWriteListToReadOnlyFile() { |
| 238 Directory temp = tempDir(); | 238 Directory temp = tempDir(); |
| 239 ReceivePort p = new ReceivePort(); | 239 ReceivePort p = new ReceivePort(); |
| 240 p.receive((x,y) { | 240 p.receive((x,y) { |
| 241 p.close(); | 241 p.close(); |
| 242 temp.deleteRecursivelySync(); | 242 temp.deleteRecursivelySync(); |
| 243 }); | 243 }); |
| 244 | 244 |
| 245 var file = new File("${temp.path}/test_file"); | 245 var file = new File("${temp.path}/test_file"); |
| 246 file.createSync(); | 246 file.createSync(); |
| 247 var openedFile = file.openSync(FileMode.READ); | 247 var openedFile = file.openSync(FileMode.READ); |
| 248 | 248 |
| 249 List data = [0, 1, 2, 3]; | 249 List data = [0, 1, 2, 3]; |
| 250 // Writing to read only file should throw an exception. | 250 // Writing to read only file should throw an exception. |
| 251 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), | 251 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), |
| 252 (e) => checkWriteReadOnlyFileException(e)); | 252 (e) => checkWriteReadOnlyFileException(e)); |
| 253 | 253 |
| 254 openedFile.writeList(data, 0, data.length); | 254 openedFile.writeList(data, 0, data.length); |
| 255 openedFile.onError = (e) { | 255 openedFile.onError = (e) { |
| 256 checkWriteReadOnlyFileException(e); | 256 checkWriteReadOnlyFileException(e); |
| 257 p.toSendPort().send(null); | 257 openedFile.close(() => p.toSendPort().send(null)); |
| 258 }; | 258 }; |
| 259 } | 259 } |
| 260 | 260 |
| 261 testTruncateReadOnlyFile() { | 261 testTruncateReadOnlyFile() { |
| 262 Directory temp = tempDir(); | 262 Directory temp = tempDir(); |
| 263 ReceivePort p = new ReceivePort(); | 263 ReceivePort p = new ReceivePort(); |
| 264 p.receive((x,y) { | 264 p.receive((x,y) { |
| 265 p.close(); | 265 p.close(); |
| 266 temp.deleteRecursivelySync(); | 266 temp.deleteRecursivelySync(); |
| 267 }); | 267 }); |
| 268 | 268 |
| 269 var file = new File("${temp.path}/test_file"); | 269 var file = new File("${temp.path}/test_file"); |
| 270 file.createSync(); | 270 file.createSync(); |
| 271 var openedFile = file.openSync(FileMode.READ); | 271 var openedFile = file.openSync(FileMode.WRITE); |
| 272 openedFile.writeByteSync(0); |
| 273 openedFile.closeSync(); |
| 274 openedFile = file.openSync(FileMode.READ); |
| 272 | 275 |
| 273 // Truncating read only file should throw an exception. | 276 // Truncating read only file should throw an exception. |
| 274 Expect.throws(() => openedFile.truncateSync(0), | 277 Expect.throws(() => openedFile.truncateSync(0), |
| 275 (e) => checkWriteReadOnlyFileException(e)); | 278 (e) => checkWriteReadOnlyFileException(e)); |
| 276 | 279 |
| 277 openedFile.truncate(0, () => Expect.fail("Unreachable code")); | 280 openedFile.truncate(0, () => Expect.fail("Unreachable code")); |
| 278 openedFile.onError = (e) { | 281 openedFile.onError = (e) { |
| 279 checkWriteReadOnlyFileException(e); | 282 checkWriteReadOnlyFileException(e); |
| 280 p.toSendPort().send(null); | 283 openedFile.close(() => p.toSendPort().send(null)); |
| 281 }; | 284 }; |
| 282 } | 285 } |
| 283 | 286 |
| 284 bool checkFileClosedException(e) { | 287 bool checkFileClosedException(e) { |
| 285 Expect.isTrue(e is FileIOException); | 288 Expect.isTrue(e is FileIOException); |
| 286 Expect.isTrue(e.toString().indexOf("File closed") != -1); | 289 Expect.isTrue(e.toString().indexOf("File closed") != -1); |
| 287 Expect.isTrue(e.osError == null); | 290 Expect.isTrue(e.osError == null); |
| 288 return true; | 291 return true; |
| 289 } | 292 } |
| 290 | 293 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 testFullPathOnNonExistentDirectory(); | 364 testFullPathOnNonExistentDirectory(); |
| 362 testDirectoryInNonExistentDirectory(); | 365 testDirectoryInNonExistentDirectory(); |
| 363 testReadAsBytesNonExistent(); | 366 testReadAsBytesNonExistent(); |
| 364 testReadAsTextNonExistent(); | 367 testReadAsTextNonExistent(); |
| 365 testReadAsLinesNonExistent(); | 368 testReadAsLinesNonExistent(); |
| 366 testWriteByteToReadOnlyFile(); | 369 testWriteByteToReadOnlyFile(); |
| 367 testWriteListToReadOnlyFile(); | 370 testWriteListToReadOnlyFile(); |
| 368 testTruncateReadOnlyFile(); | 371 testTruncateReadOnlyFile(); |
| 369 testOperateOnClosedFile(); | 372 testOperateOnClosedFile(); |
| 370 } | 373 } |
| OLD | NEW |