|
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; |
@@ -409,4 +415,49 @@ public void overrideURIPragmaValuesWithProperties() throws Exception { |
409 | 415 | stat.close(); |
410 | 416 | conn.close(); |
411 | 417 | } |
| 418 | + |
| 419 | + @Test |
| 420 | + public void openNonExistingFileNoCreate() { |
| 421 | + Path nonExisting = Paths.get("non_existing.db").toAbsolutePath(); |
| 422 | + assertThat(Files.exists(nonExisting)).isFalse(); |
| 423 | + SQLiteConfig cfg = new SQLiteConfig(); |
| 424 | + cfg.resetOpenMode(SQLiteOpenMode.CREATE); |
| 425 | + assertThatExceptionOfType(SQLiteException.class) |
| 426 | + .isThrownBy( |
| 427 | + () -> { |
| 428 | + @SuppressWarnings({"resource", "unused"}) |
| 429 | + Connection _c = cfg.createConnection("jdbc:sqlite:" + nonExisting); |
| 430 | + }) |
| 431 | + .satisfies( |
| 432 | + e -> |
| 433 | + assertThat(e.getResultCode()) |
| 434 | + .isEqualTo(SQLiteErrorCode.SQLITE_CANTOPEN)); |
| 435 | + assertThat(Files.exists(nonExisting)).isFalse(); |
| 436 | + } |
| 437 | + |
| 438 | + @DisabledOnOs(OS.WINDOWS) // File.setReadOnly doesn't seem to work here |
| 439 | + @Test |
| 440 | + public void openNonExistingFileInReadOnlyDirectory(@TempDir Path tmpDir) { |
| 441 | + assertThat(tmpDir.toFile().setReadOnly()).isTrue(); |
| 442 | + assertThat(Files.exists(tmpDir)).isTrue(); |
| 443 | + Path nonExisting = tmpDir.resolve("non_existing.db").toAbsolutePath(); |
| 444 | + assertThatThrownBy(() -> Files.createFile(nonExisting)) |
| 445 | + .isInstanceOf(AccessDeniedException.class); |
| 446 | + assertThat(Files.exists(nonExisting)).isFalse(); |
| 447 | + SQLiteConfig cfg = new SQLiteConfig(); |
| 448 | + assertThatExceptionOfType(SQLiteException.class) |
| 449 | + .isThrownBy( |
| 450 | + () -> { |
| 451 | + @SuppressWarnings({"resource", "unused"}) |
| 452 | + Connection _c = cfg.createConnection("jdbc:sqlite:" + nonExisting); |
| 453 | + }) |
| 454 | + .satisfies( |
| 455 | + e -> |
| 456 | + // It would be nice, if the native error code were more specific on |
| 457 | + // why the file can't be |
| 458 | + // opened, but this is what we get: |
| 459 | + assertThat(e.getResultCode()) |
| 460 | + .isEqualTo(SQLiteErrorCode.SQLITE_CANTOPEN)); |
| 461 | + assertThat(Files.exists(nonExisting)).isFalse(); |
| 462 | + } |
412 | 463 | } |
0 commit comments