From 8fe4f2163cb23082c2c5f42061380e49ac90449f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Francoeur?= Date: Thu, 17 Jul 2025 10:12:04 -0400 Subject: [PATCH] call TursoDB::open from constructor --- .../main/java/tech/turso/core/TursoDB.java | 10 +++------ .../java/tech/turso/core/TursoDBFactory.java | 8 +------ .../java/tech/turso/core/TursoDBTest.java | 22 +------------------ 3 files changed, 5 insertions(+), 35 deletions(-) diff --git a/bindings/java/src/main/java/tech/turso/core/TursoDB.java b/bindings/java/src/main/java/tech/turso/core/TursoDB.java index 8020e685a..598b269cb 100644 --- a/bindings/java/src/main/java/tech/turso/core/TursoDB.java +++ b/bindings/java/src/main/java/tech/turso/core/TursoDB.java @@ -185,9 +185,10 @@ public final class TursoDB implements AutoCloseable { } // TODO: receive config as argument - private TursoDB(String url, String filePath) { + private TursoDB(String url, String filePath) throws SQLException { this.url = url; this.filePath = filePath; + open(0); } // TODO: add support for JNI @@ -201,16 +202,11 @@ public final class TursoDB implements AutoCloseable { return this.isOpen; } - public void open(int openFlags) throws SQLException { + private void open(int openFlags) throws SQLException { open0(filePath, openFlags); } private void open0(String filePath, int openFlags) throws SQLException { - if (isOpen) { - throw TursoExceptionUtils.buildTursoException( - TursoErrorCode.TURSO_ETC.code, "Already opened"); - } - byte[] filePathBytes = stringToUtf8ByteArray(filePath); if (filePathBytes == null) { throw TursoExceptionUtils.buildTursoException( diff --git a/bindings/java/src/main/java/tech/turso/core/TursoDBFactory.java b/bindings/java/src/main/java/tech/turso/core/TursoDBFactory.java index 2787da78b..52dea3ac8 100644 --- a/bindings/java/src/main/java/tech/turso/core/TursoDBFactory.java +++ b/bindings/java/src/main/java/tech/turso/core/TursoDBFactory.java @@ -37,13 +37,7 @@ public final class TursoDBFactory { try { return databaseHolder.computeIfAbsent( - url, - (Sneaky) - u -> { - TursoDB tursoDB = TursoDB.create(u, filePath); - tursoDB.open(0); - return tursoDB; - }); + url, (Sneaky) u -> TursoDB.create(u, filePath)); } catch (Exception e) { throw new SQLException("Error opening connection", e); } diff --git a/bindings/java/src/test/java/tech/turso/core/TursoDBTest.java b/bindings/java/src/test/java/tech/turso/core/TursoDBTest.java index 378b11cdb..f62a6c336 100644 --- a/bindings/java/src/test/java/tech/turso/core/TursoDBTest.java +++ b/bindings/java/src/test/java/tech/turso/core/TursoDBTest.java @@ -1,10 +1,8 @@ package tech.turso.core; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertFalse; -import java.sql.SQLException; import org.junit.jupiter.api.Test; import tech.turso.TestUtils; import tech.turso.TursoErrorCode; @@ -13,34 +11,16 @@ import tech.turso.exceptions.TursoException; public class TursoDBTest { @Test - void db_should_open_normally() throws Exception { + void db_should_open_and_close_normally() throws Exception { TursoDB.load(); String dbPath = TestUtils.createTempFile(); TursoDB db = TursoDB.create("jdbc:turso" + dbPath, dbPath); - db.open(0); - } - @Test - void db_should_close_normally() throws Exception { - TursoDB.load(); - String dbPath = TestUtils.createTempFile(); - TursoDB db = TursoDB.create("jdbc:turso" + dbPath, dbPath); - db.open(0); db.close(); assertFalse(db.isOpen()); } - @Test - void should_throw_exception_when_opened_twice() throws Exception { - TursoDB.load(); - String dbPath = TestUtils.createTempFile(); - TursoDB db = TursoDB.create("jdbc:turso:" + dbPath, dbPath); - db.open(0); - - assertThatThrownBy(() -> db.open(0)).isInstanceOf(SQLException.class); - } - @Test void throwJavaException_should_throw_appropriate_java_exception() throws Exception { TursoDB.load();