|
6 | 6 | import java.io.FileOutputStream; |
7 | 7 | import java.io.IOException; |
8 | 8 | import java.io.InputStream; |
| 9 | +import java.nio.file.AccessDeniedException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.Paths; |
9 | 13 | import java.sql.*; |
10 | 14 | import java.util.ArrayList; |
11 | 15 | import java.util.Properties; |
|
14 | 18 | import java.util.concurrent.Executors; |
15 | 19 | import java.util.concurrent.Future; |
16 | 20 | import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.condition.DisabledOnOs; |
| 22 | +import org.junit.jupiter.api.condition.OS; |
17 | 23 | import org.junit.jupiter.api.io.TempDir; |
18 | 24 | import org.sqlite.SQLiteConfig.JournalMode; |
19 | 25 | import org.sqlite.SQLiteConfig.Pragma; |
@@ -555,4 +561,49 @@ public void settingsBeforeDbCreationConfig() throws Exception { |
555 | 561 | } |
556 | 562 | } |
557 | 563 | } |
| 564 | + |
| 565 | + @Test |
| 566 | + public void openNonExistingFileNoCreate() { |
| 567 | + Path nonExisting = Paths.get("non_existing.db").toAbsolutePath(); |
| 568 | + assertThat(Files.exists(nonExisting)).isFalse(); |
| 569 | + SQLiteConfig cfg = new SQLiteConfig(); |
| 570 | + cfg.resetOpenMode(SQLiteOpenMode.CREATE); |
| 571 | + assertThatExceptionOfType(SQLiteException.class) |
| 572 | + .isThrownBy( |
| 573 | + () -> { |
| 574 | + @SuppressWarnings({"resource", "unused"}) |
| 575 | + Connection _c = cfg.createConnection("jdbc:sqlite:" + nonExisting); |
| 576 | + }) |
| 577 | + .satisfies( |
| 578 | + e -> |
| 579 | + assertThat(e.getResultCode()) |
| 580 | + .isEqualTo(SQLiteErrorCode.SQLITE_CANTOPEN)); |
| 581 | + assertThat(Files.exists(nonExisting)).isFalse(); |
| 582 | + } |
| 583 | + |
| 584 | + @DisabledOnOs(OS.WINDOWS) // File.setReadOnly doesn't seem to work here |
| 585 | + @Test |
| 586 | + public void openNonExistingFileInReadOnlyDirectory(@TempDir Path tmpDir) { |
| 587 | + assertThat(tmpDir.toFile().setReadOnly()).isTrue(); |
| 588 | + assertThat(Files.exists(tmpDir)).isTrue(); |
| 589 | + Path nonExisting = tmpDir.resolve("non_existing.db").toAbsolutePath(); |
| 590 | + assertThatThrownBy(() -> Files.createFile(nonExisting)) |
| 591 | + .isInstanceOf(AccessDeniedException.class); |
| 592 | + assertThat(Files.exists(nonExisting)).isFalse(); |
| 593 | + SQLiteConfig cfg = new SQLiteConfig(); |
| 594 | + assertThatExceptionOfType(SQLiteException.class) |
| 595 | + .isThrownBy( |
| 596 | + () -> { |
| 597 | + @SuppressWarnings({"resource", "unused"}) |
| 598 | + Connection _c = cfg.createConnection("jdbc:sqlite:" + nonExisting); |
| 599 | + }) |
| 600 | + .satisfies( |
| 601 | + e -> |
| 602 | + // It would be nice, if the native error code were more specific on |
| 603 | + // why the file can't be |
| 604 | + // opened, but this is what we get: |
| 605 | + assertThat(e.getResultCode()) |
| 606 | + .isEqualTo(SQLiteErrorCode.SQLITE_CANTOPEN)); |
| 607 | + assertThat(Files.exists(nonExisting)).isFalse(); |
| 608 | + } |
558 | 609 | } |
0 commit comments