diff --git a/src/main/java/net/ucanaccess/console/Main.java b/src/main/java/net/ucanaccess/console/Main.java index 40e48b06..10bbd0f5 100644 --- a/src/main/java/net/ucanaccess/console/Main.java +++ b/src/main/java/net/ucanaccess/console/Main.java @@ -123,8 +123,7 @@ public static void main(String[] args) throws Exception { noMem = size > 30000000 ? ";memory=false" : ""; } - conn = DriverManager.getConnection("jdbc:ucanaccess://" + fl.getAbsolutePath() + passwordEntry + noMem, - props); + conn = DriverManager.getConnection("jdbc:ucanaccess://" + fl.getAbsolutePath() + passwordEntry + noMem, props); SQLWarning sqlw = conn.getWarnings(); while (sqlw != null) { diff --git a/src/main/java/net/ucanaccess/jdbc/UcanaccessConnectionBuilder.java b/src/main/java/net/ucanaccess/jdbc/UcanaccessConnectionBuilder.java new file mode 100644 index 00000000..f870cfbe --- /dev/null +++ b/src/main/java/net/ucanaccess/jdbc/UcanaccessConnectionBuilder.java @@ -0,0 +1,97 @@ +package net.ucanaccess.jdbc; + +import net.ucanaccess.util.Try; + +import java.sql.DriverManager; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * A build for Ucanaccess database urls and connections. + */ +public final class UcanaccessConnectionBuilder { + + private String user = "ucanaccess"; + private String password; + private String dbPath; + private boolean ignoreCase; + private long inactivityTimeout = -1; + private String columnOrder; + private Map addParms = new LinkedHashMap<>(); + + public UcanaccessConnectionBuilder withUser(String _user) { + user = _user; + return this; + } + + public UcanaccessConnectionBuilder withPassword(String _password) { + password = _password; + return this; + } + + public UcanaccessConnectionBuilder withoutUserPass() { + user = ""; + password = ""; + return this; + } + + public UcanaccessConnectionBuilder withDbPath(String _dbPath) { + dbPath = _dbPath; + return this; + } + + public UcanaccessConnectionBuilder withIgnoreCase(boolean _ignoreCase) { + ignoreCase = _ignoreCase; + return this; + } + + public UcanaccessConnectionBuilder withInactivityTimeout(long _inactivityTimeout) { + inactivityTimeout = _inactivityTimeout; + return this; + } + + public UcanaccessConnectionBuilder withColumnOrder(String _columnOrder) { + columnOrder = _columnOrder; + return this; + } + + public UcanaccessConnectionBuilder withParm(String _key, Object _value) { + Objects.requireNonNull(_key, "Key required"); + Objects.requireNonNull(_value, "Value required"); + addParms.put(_key, _value); + return this; + } + + public String buildUrl() { + Objects.requireNonNull(dbPath, "Database path required"); + + String url = UcanaccessDriver.URL_PREFIX + dbPath; + + if (ignoreCase) { + url += ";ignoreCase=" + ignoreCase; + } + if (inactivityTimeout > -1) { + url += ";inactivityTimeout=" + inactivityTimeout; + } else { + url += ";immediatelyReleaseResources=true"; + } + if (columnOrder != null) { + url += ";columnOrder=" + columnOrder; + } + if (!addParms.isEmpty()) { + url += ";" + addParms.entrySet().stream().map(e -> e.getKey() + '=' + e.getValue()).collect(Collectors.joining(";")); + } + return url; + } + + public UcanaccessConnection build() { + Try.catching(() -> Class.forName(UcanaccessDriver.class.getName())) + .orThrow(UcanaccessRuntimeException::new); + + return Try.catching(() -> DriverManager.getConnection(buildUrl(), user, password)) + .map(UcanaccessConnection.class::cast).orThrow(); + } + +} diff --git a/src/test/java/net/ucanaccess/jdbc/BigintTest.java b/src/test/java/net/ucanaccess/jdbc/BigintTest.java index 8c2675d9..c1e5128b 100644 --- a/src/test/java/net/ucanaccess/jdbc/BigintTest.java +++ b/src/test/java/net/ucanaccess/jdbc/BigintTest.java @@ -7,7 +7,6 @@ import org.junit.jupiter.params.provider.EnumSource.Mode; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; @@ -29,14 +28,15 @@ void testBigintInsert(AccessVersion _accessVersion) throws Exception { ucanaccess.close(); - String connUrl = UcanaccessDriver.URL_PREFIX + accdbPath + ";immediatelyReleaseResources=true"; - - try (Connection cnxn = DriverManager.getConnection(connUrl); - Statement st2 = cnxn.createStatement(); - ResultSet rs = st2.executeQuery("SELECT x FROM table1 WHERE entry='3 billion'")) { + try (Connection conn = buildConnection() + .withDbPath(accdbPath) + .withParm("immediatelyReleaseResources", true).build(); + Statement st2 = conn.createStatement(); + ResultSet rs = st2.executeQuery("SELECT x FROM table1 WHERE entry='3 billion'")) { rs.next(); Long actual = rs.getLong("x"); assertEquals(expected, actual); } } + } diff --git a/src/test/java/net/ucanaccess/jdbc/ColumnOrderTest.java b/src/test/java/net/ucanaccess/jdbc/ColumnOrderTest.java index 30ebf44d..0cc09054 100644 --- a/src/test/java/net/ucanaccess/jdbc/ColumnOrderTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ColumnOrderTest.java @@ -16,12 +16,17 @@ protected String getAccessPath() { return TEST_DB_DIR + "columnOrder.accdb"; } + @Override + protected UcanaccessConnectionBuilder buildConnection() { + return super.buildConnection() + .withColumnOrder("display"); + } + @ParameterizedTest(name = "[{index}] {0}") @MethodSource("net.ucanaccess.test.AccessVersion#getDefaultAccessVersion()") void testColumnOrder1(AccessVersion _accessVersion) throws Exception { init(_accessVersion); - setColumnOrder("display"); try (Connection uca = createUcanaccessConnection(); PreparedStatement ps = uca.prepareStatement("INSERT INTO t1 values (?,?,?)")) { ps.setInt(3, 3); diff --git a/src/test/java/net/ucanaccess/jdbc/ConcatNullsFalseTest.java b/src/test/java/net/ucanaccess/jdbc/ConcatNullsFalseTest.java index 41979b39..dbc870f3 100644 --- a/src/test/java/net/ucanaccess/jdbc/ConcatNullsFalseTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ConcatNullsFalseTest.java @@ -7,15 +7,16 @@ class ConcatNullsFalseTest extends UcanaccessBaseTest { - ConcatNullsFalseTest() { - appendToJdbcURL(";concatnulls=false"); - } - @Override protected String getAccessPath() { return TEST_DB_DIR + "badDb.accdb"; } + protected UcanaccessConnectionBuilder buildConnection() { + return super.buildConnection() + .withParm("concatnulls", false); + } + @ParameterizedTest(name = "[{index}] {0}") @MethodSource("net.ucanaccess.test.AccessVersion#getDefaultAccessVersion()") void testConcat(AccessVersion _accessVersion) throws Exception { diff --git a/src/test/java/net/ucanaccess/jdbc/ConcatNullsTrueTest.java b/src/test/java/net/ucanaccess/jdbc/ConcatNullsTrueTest.java index 67c8b50c..7f96b9e1 100644 --- a/src/test/java/net/ucanaccess/jdbc/ConcatNullsTrueTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ConcatNullsTrueTest.java @@ -7,18 +7,20 @@ class ConcatNullsTrueTest extends UcanaccessBaseTest { - ConcatNullsTrueTest() { - // By default, any null value will cause the function to return null. - // If the property is set false, then NULL values are replaced with empty strings. - // see: http://hsqldb.org/doc/guide/builtinfunctions-chapt.html - appendToJdbcURL(";concatnulls=true"); - } - @Override protected String getAccessPath() { return TEST_DB_DIR + "badDb.accdb"; } + @Override + protected UcanaccessConnectionBuilder buildConnection() { + // By default, any null value will cause the function to return null. + // If the property is set false, then NULL values are replaced with empty strings. + // see: http://hsqldb.org/doc/guide/builtinfunctions-chapt.html + return super.buildConnection() + .withParm("concatnulls", true); + } + @ParameterizedTest(name = "[{index}] {0}") @MethodSource("net.ucanaccess.test.AccessVersion#getDefaultAccessVersion()") void testConcat(AccessVersion _accessVersion) throws Exception { diff --git a/src/test/java/net/ucanaccess/jdbc/CreateDatabaseTest.java b/src/test/java/net/ucanaccess/jdbc/CreateDatabaseTest.java index 5ce7fb0f..67afa4e7 100644 --- a/src/test/java/net/ucanaccess/jdbc/CreateDatabaseTest.java +++ b/src/test/java/net/ucanaccess/jdbc/CreateDatabaseTest.java @@ -6,7 +6,6 @@ import org.junit.jupiter.params.provider.EnumSource; import java.io.File; -import java.sql.DriverManager; import java.sql.Statement; class CreateDatabaseTest extends UcanaccessBaseTest { @@ -19,20 +18,26 @@ void testNewDatabase(AccessVersion _accessVersion) throws Exception { File fileMdb = createTempFileName(getClass().getSimpleName()); fileMdb.deleteOnExit(); - String url = UcanaccessDriver.URL_PREFIX + fileMdb.getAbsolutePath() + ";immediatelyReleaseResources=true;newDatabaseVersion=" + getFileFormat().name(); - Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); + UcanaccessConnection conn = buildConnection() + .withDbPath(fileMdb.getAbsolutePath()) + .withUser("") + .withPassword("") + .withParm("immediatelyReleaseResources", true) + .withParm("newDatabaseVersion", getFileFormat().name()) + .build(); - UcanaccessConnection ucanaccessConnection = (UcanaccessConnection) DriverManager.getConnection(url, "", ""); - assertNotNull(ucanaccessConnection); + assertNotNull(conn); ucanaccess.close(); - ucanaccess = ucanaccessConnection; + ucanaccess = conn; getLogger().info("Database file successfully created: {}", fileMdb.getAbsolutePath()); - try (Statement st = ucanaccessConnection.createStatement()) { - st.execute("CREATE TABLE AAA (baaaa text(3) PRIMARY KEY, A long default 3, C text(4))"); - st.execute("INSERT INTO AAA(baaaa, c) VALUES ('33A','G' )"); - st.execute("INSERT INTO AAA VALUES ('33B',111,'G' )"); + + try (Statement st = conn.createStatement()) { + executeStatements(st, + "CREATE TABLE AAA (baaaa TEXT(3) PRIMARY KEY, A LONG DEFAULT 3, C TEXT(4))", + "INSERT INTO AAA(baaaa, c) VALUES ('33A','G' )", + "INSERT INTO AAA VALUES ('33B',111,'G' )"); } dumpQueryResult("SELECT * FROM AAA"); } diff --git a/src/test/java/net/ucanaccess/jdbc/ExceptionCodeTest.java b/src/test/java/net/ucanaccess/jdbc/ExceptionCodeTest.java index 90203596..763b6b34 100644 --- a/src/test/java/net/ucanaccess/jdbc/ExceptionCodeTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ExceptionCodeTest.java @@ -9,7 +9,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; -import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Locale; @@ -53,11 +52,14 @@ void testGenException(AccessVersion _accessVersion) { @ParameterizedTest(name = "[{index}] {0}") @EnumSource(value = AccessVersion.class) void testGException(AccessVersion _accessVersion) { - assertThatThrownBy(() -> DriverManager.getConnection(UcanaccessDriver.URL_PREFIX + "kuso_yaro")) - .isInstanceOf(UcanaccessSQLException.class) - .hasMessageContaining("given file does not exist") - .hasFieldOrPropertyWithValue("ErrorCode", IUcanaccessErrorCodes.UCANACCESS_GENERIC_ERROR) - .hasFieldOrPropertyWithValue("SQLState", String.valueOf(IUcanaccessErrorCodes.UCANACCESS_GENERIC_ERROR)); + assertThatThrownBy(() -> buildConnection() + .withDbPath("kuso_yaro") + .withUser("") + .build()) + .isInstanceOf(UcanaccessSQLException.class) + .hasMessageContaining("given file does not exist") + .hasFieldOrPropertyWithValue("ErrorCode", IUcanaccessErrorCodes.UCANACCESS_GENERIC_ERROR) + .hasFieldOrPropertyWithValue("SQLState", String.valueOf(IUcanaccessErrorCodes.UCANACCESS_GENERIC_ERROR)); } } diff --git a/src/test/java/net/ucanaccess/jdbc/ExternalResourcesTest.java b/src/test/java/net/ucanaccess/jdbc/ExternalResourcesTest.java index 670f8eaa..0062752a 100644 --- a/src/test/java/net/ucanaccess/jdbc/ExternalResourcesTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ExternalResourcesTest.java @@ -6,7 +6,9 @@ import org.junit.jupiter.params.provider.MethodSource; import java.io.File; -import java.sql.*; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; class ExternalResourcesTest extends UcanaccessBaseTest { @@ -15,17 +17,19 @@ class ExternalResourcesTest extends UcanaccessBaseTest { void testLinks(AccessVersion _accessVersion) throws SQLException, ClassNotFoundException { init(_accessVersion); - Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); - File main = copyResourceToTempFile(TEST_DB_DIR + "main.mdb"); File linkee1 = copyResourceToTempFile(TEST_DB_DIR + "linkee1.mdb"); File linkee2 = copyResourceToTempFile(TEST_DB_DIR + "linkee2.mdb"); - String url = UcanaccessDriver.URL_PREFIX + main.getAbsolutePath() + ";immediatelyreleaseresources=true" - + ";remap=c:\\db\\linkee1.mdb|" + linkee1.getAbsolutePath() + "&c:\\db\\linkee2.mdb|" - + linkee2.getAbsolutePath(); - getLogger().info("Database url: {}", url); - try (Connection conn = DriverManager.getConnection(url, "", ""); + UcanaccessConnectionBuilder bldr = buildConnection() + .withDbPath(main.getAbsolutePath()) + .withUser("") + .withPassword("") + .withParm("immediatelyReleaseResources", true) + .withParm("remap", "c:\\db\\linkee1.mdb|" + linkee1.getAbsolutePath() + "&c:\\db\\linkee2.mdb|" + linkee2.getAbsolutePath()); + getLogger().info("Database url: {}", bldr.buildUrl()); + + try (Connection conn = bldr.build(); Statement st = conn.createStatement()) { dumpQueryResult(() -> st.executeQuery("SELECT * FROM table1")); diff --git a/src/test/java/net/ucanaccess/jdbc/FolderTest.java b/src/test/java/net/ucanaccess/jdbc/FolderTest.java index a83f26b6..36b586b8 100644 --- a/src/test/java/net/ucanaccess/jdbc/FolderTest.java +++ b/src/test/java/net/ucanaccess/jdbc/FolderTest.java @@ -7,7 +7,6 @@ import java.io.File; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.SQLException; import java.sql.SQLWarning; @@ -23,12 +22,11 @@ void testFolderContent(AccessVersion _accessVersion) throws SQLException, ClassN return; } - Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); - File folder = new File(folderPath); for (File fl : folder.listFiles()) { - String url = UcanaccessDriver.URL_PREFIX + fl.getAbsolutePath(); - Connection conn = DriverManager.getConnection(url); + Connection conn = buildConnection() + .withDbPath(fl.getAbsolutePath()) + .build(); SQLWarning sqlw = conn.getWarnings(); getLogger().info("open {}", fl.getAbsolutePath()); while (sqlw != null) { diff --git a/src/test/java/net/ucanaccess/jdbc/MemoryTest.java b/src/test/java/net/ucanaccess/jdbc/MemoryTest.java index 48bfa2d0..07f2276a 100644 --- a/src/test/java/net/ucanaccess/jdbc/MemoryTest.java +++ b/src/test/java/net/ucanaccess/jdbc/MemoryTest.java @@ -17,9 +17,14 @@ protected void init(AccessVersion _accessVersion) throws SQLException { super.init(_accessVersion); getLogger().debug("Thread.activeCount (setup): " + Thread.activeCount()); - setInactivityTimeout(1000L); executeStatements("CREATE TABLE memm(id LONG PRIMARY KEY, A LONG, C TEXT, D TEXT)"); } + + @Override + protected UcanaccessConnectionBuilder buildConnection() { + return super.buildConnection() + .withInactivityTimeout(1000L); + } @ParameterizedTest(name = "[{index}] {0}") @MethodSource("net.ucanaccess.test.AccessVersion#getDefaultAccessVersion()") diff --git a/src/test/java/net/ucanaccess/jdbc/MultiThreadAccessTest.java b/src/test/java/net/ucanaccess/jdbc/MultiThreadAccessTest.java index f1a47253..44ffba0b 100644 --- a/src/test/java/net/ucanaccess/jdbc/MultiThreadAccessTest.java +++ b/src/test/java/net/ucanaccess/jdbc/MultiThreadAccessTest.java @@ -32,7 +32,7 @@ void afterEachTest() throws SQLException { } void crud() throws SQLException { - try (Connection conn = createUcanaccessConnection(dbPath)) { + try (Connection conn = buildConnection().withDbPath(dbPath).build()) { conn.setAutoCommit(false); Statement st = conn.createStatement(); intVal++; @@ -43,7 +43,7 @@ void crud() throws SQLException { } void crudPS() throws SQLException { - try (Connection conn = createUcanaccessConnection(dbPath)) { + try (Connection conn = buildConnection().withDbPath(dbPath).build()) { conn.setAutoCommit(false); PreparedStatement ps = conn.prepareStatement("INSERT INTO " + tableName + " (id,descr) VALUES(?, ?)"); ps.setInt(1, ++intVal); @@ -57,7 +57,7 @@ void crudPS() throws SQLException { } void crudUpdatableRS() throws SQLException { - try (Connection conn = createUcanaccessConnection(dbPath)) { + try (Connection conn = buildConnection().withDbPath(dbPath).build()) { conn.setAutoCommit(false); Statement st = conn.createStatement(); st.execute("INSERT INTO " + tableName + " (id,descr) VALUES(" + (++intVal) + " ,'" + Thread.currentThread() + "')"); @@ -89,7 +89,7 @@ void testMultiThread(AccessVersion _accessVersion) throws SQLException, IOExcept for (Thread t : threads) { Try.catching(() -> t.join()).orIgnore(); } - ucanaccess = createUcanaccessConnection(dbPath); + ucanaccess = buildConnection().withDbPath(dbPath).build(); dumpQueryResult("SELECT * FROM " + tableName + " ORDER BY id"); checkQuery("SELECT * FROM " + tableName + " ORDER BY id"); diff --git a/src/test/java/net/ucanaccess/jdbc/PasswordTest.java b/src/test/java/net/ucanaccess/jdbc/PasswordTest.java index 484f91cf..927d5f88 100644 --- a/src/test/java/net/ucanaccess/jdbc/PasswordTest.java +++ b/src/test/java/net/ucanaccess/jdbc/PasswordTest.java @@ -24,8 +24,10 @@ void testPassword(AccessVersion _accessVersion) throws Exception { .isInstanceOf(UcanaccessSQLException.class) .hasMessageContaining("Password authentication failed"); - setPassword("ucanaccess"); - try (Connection ucanaccessConnection = createUcanaccessConnection()) { + try (Connection ucanaccessConnection = new UcanaccessConnectionBuilder() + .withDbPath(getAccessTempPath()) + .withPassword("ucanaccess") + .build()) { assertNotNull(ucanaccessConnection); } } diff --git a/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java b/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java index 13569d51..e9de2598 100644 --- a/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java @@ -7,7 +7,6 @@ import java.io.File; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; @@ -24,30 +23,36 @@ void testReloadMirror(AccessVersion _accessVersion) throws Exception { mirrorFile.deleteOnExit(); // create the database - String urlCreate = UcanaccessDriver.URL_PREFIX + dbFile.getAbsolutePath() - + ";memory=true" + ";newDatabaseVersion=" + getFileFormat().name(); - - try (Connection conn = DriverManager.getConnection(urlCreate, "", ""); - Statement stCreate = conn.createStatement()) { + try (Connection conn = buildConnection() + .withDbPath(dbFile.getAbsolutePath()) + .withParm("memory", true) + .withParm("newDatabaseVersion", getFileFormat().name()) + .withoutUserPass() + .build(); + Statement stCreate = conn.createStatement()) { stCreate.execute("CREATE TABLE Table1 (ID COUNTER PRIMARY KEY, TextField TEXT(50))"); } + // create the persistent mirror - String urlMirror = - UcanaccessDriver.URL_PREFIX + dbFile.getAbsolutePath() + ";keepMirror=" + mirrorFile.getAbsolutePath(); - try (Connection conn = DriverManager.getConnection(urlMirror, "", "")) { - } + UcanaccessConnectionBuilder mirrorBuilder = buildConnection() + .withDbPath(dbFile.getAbsolutePath()) + .withParm("keepMirror", mirrorFile.getAbsolutePath()) + .withoutUserPass(); + try (Connection conn = mirrorBuilder.build()) {} // do an update without the mirror involved - String urlUpdate = UcanaccessDriver.URL_PREFIX + dbFile.getAbsolutePath() + ";memory=true"; - try (Connection conn = DriverManager.getConnection(urlUpdate, "", ""); - Statement stUpdate = conn.createStatement()) { + try (Connection conn = buildConnection() + .withDbPath(dbFile.getAbsolutePath()) + .withParm("memory", true) + .withoutUserPass().build(); + Statement stUpdate = conn.createStatement()) { stUpdate.executeUpdate("INSERT INTO Table1 (TextField) VALUES ('NewStuff')"); } - try (// now try and open the database with the (outdated) mirror - Connection conn = DriverManager.getConnection(urlMirror, "", ""); + // now try and open the database with the (outdated) mirror + try (Connection conn = mirrorBuilder.build()) { Statement stSelect = conn.createStatement(); - ResultSet rs = stSelect.executeQuery("SELECT COUNT(*) AS n FROM Table1")) { + ResultSet rs = stSelect.executeQuery("SELECT COUNT(*) AS n FROM Table1"); rs.next(); assertEquals(1, rs.getInt(1), "Unexpected record count"); } diff --git a/src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java b/src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java index 24b4fddd..ca22a369 100644 --- a/src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java +++ b/src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java @@ -8,8 +8,7 @@ import net.ucanaccess.complex.ComplexBase; import net.ucanaccess.console.Main; import net.ucanaccess.jdbc.UcanaccessConnection; -import net.ucanaccess.jdbc.UcanaccessDriver; -import net.ucanaccess.jdbc.UcanaccessRuntimeException; +import net.ucanaccess.jdbc.UcanaccessConnectionBuilder; import net.ucanaccess.util.IThrowingSupplier; import net.ucanaccess.util.Try; import org.junit.jupiter.api.AfterEach; @@ -34,18 +33,11 @@ public abstract class UcanaccessBaseTest extends AbstractBaseTest { } private File fileAccDb; - private String password = ""; private AccessVersion accessVersion; // CHECKSTYLE:OFF protected UcanaccessConnection ucanaccess; // CHECKSTYLE:ON - private String user = "ucanaccess"; private Connection verifyConnection; - private Boolean ignoreCase; - - private long inactivityTimeout = -1; - private String columnOrder; - private String append2JdbcURL = ""; protected UcanaccessBaseTest() { } @@ -56,8 +48,6 @@ protected final void setAccessVersion(AccessVersion _accessVersion) { protected void init(AccessVersion _accessVersion) throws SQLException { accessVersion = _accessVersion; - Try.catching(() -> Class.forName(UcanaccessDriver.class.getName())) - .orThrow(UcanaccessRuntimeException::new); ucanaccess = createUcanaccessConnection(); } @@ -366,38 +356,24 @@ public String getName() { return getClass().getSimpleName() + " ver " + getFileFormat(); } - protected String getPassword() { - return password; - } - - protected UcanaccessConnection createUcanaccessConnection() throws SQLException { - return createUcanaccessConnection(UcanaccessDriver.URL_PREFIX, getAccessTempPath()); - } - - protected UcanaccessConnection createUcanaccessConnection(String _dbPath) throws SQLException { - return createUcanaccessConnection(UcanaccessDriver.URL_PREFIX, _dbPath); - } - - private UcanaccessConnection createUcanaccessConnection(String _urlPrefix, String _dbPath) throws SQLException { - String dbPath = Optional.ofNullable(_dbPath).orElseGet(this::getAccessTempPath); - String url = _urlPrefix + dbPath; - if (ignoreCase != null) { - url += ";ignoreCase=" + ignoreCase; - } - if (inactivityTimeout != -1) { - url += ";inactivityTimeout=" + inactivityTimeout; - } else { - url += ";immediatelyreleaseresources=true"; - } - if (columnOrder != null) { - url += ";columnOrder=" + columnOrder; - } - url += append2JdbcURL; - return (UcanaccessConnection) DriverManager.getConnection(url, user, password); + /** + * Returns a pre-configured connection builder using {@link #getAccessTempPath()} as database path. + * @return connection builder + */ + protected final UcanaccessConnection createUcanaccessConnection() { + return buildConnection() + .withDbPath(getAccessTempPath()) + .build(); } - protected void appendToJdbcURL(String s) { - append2JdbcURL += s; + /** + * Returns a pre-configured connection builder. + * @return connection builder + */ + protected UcanaccessConnectionBuilder buildConnection() { + return new UcanaccessConnectionBuilder() + .withUser("ucanaccess") + .withPassword(""); } protected void initVerifyConnection() throws SQLException { @@ -406,34 +382,17 @@ protected void initVerifyConnection() throws SQLException { if (verifyConnection != null) { verifyConnection.close(); - verifyConnection = null; } - verifyConnection = createUcanaccessConnection(UcanaccessDriver.URL_PREFIX, tempVerifyFile.getAbsolutePath()); + verifyConnection = buildConnection().withDbPath(tempVerifyFile.getAbsolutePath()).build(); } - private boolean next(ResultSet _joRs, ResultSet _myRs) throws SQLException { - boolean b1 = _joRs.next(); - boolean b2 = _myRs.next(); + private boolean next(ResultSet _verifyResultSet, ResultSet _resultSet) throws SQLException { + boolean b1 = _verifyResultSet.next(); + boolean b2 = _resultSet.next(); assertEquals(b1, b2); return b1; } - public void setInactivityTimeout(long _inactivityTimeout) { - inactivityTimeout = _inactivityTimeout; - } - - protected void setPassword(String _password) { - password = _password; - } - - protected void setColumnOrder(String _columnOrder) { - columnOrder = _columnOrder; - } - - public void setIgnoreCase(boolean _ignoreCase) { - ignoreCase = _ignoreCase; - } - protected final void executeStatements(String... _sqls) throws SQLException { try (Statement st = ucanaccess.createStatement()) { executeStatements(st, _sqls);