Skip to content

Commit

Permalink
Clarify AccessVersion vs. FileFormat
Browse files Browse the repository at this point in the history
- Use AccessVersion with newDatabaseVersion (not FileFormat)
- Fail fast if version not present
- Parse version strings case-insensitively
- Expand unit test
  • Loading branch information
spannm committed Mar 2, 2024
1 parent 0b3e4c6 commit 89dce7f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 26 deletions.
17 changes: 10 additions & 7 deletions src/main/java/net/ucanaccess/jdbc/UcanaccessConnectionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import static net.ucanaccess.converters.Metadata.Property.*;

import io.github.spannm.jackcess.Database.FileFormat;
import net.ucanaccess.converters.Metadata;
import net.ucanaccess.converters.Metadata.Property;
import net.ucanaccess.exception.UcanaccessRuntimeException;
import net.ucanaccess.type.AccessVersion;
import net.ucanaccess.type.ColumnOrder;
import net.ucanaccess.util.Try;

import java.io.File;
import java.sql.DriverManager;
import java.util.*;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -77,19 +80,19 @@ public UcanaccessConnectionBuilder withMemory() {
return withProp(memory, true);
}

public UcanaccessConnectionBuilder withNewDatabaseVersion(FileFormat _version) {
public UcanaccessConnectionBuilder withNewDatabaseVersion(AccessVersion _version) {
return withProp(newDatabaseVersion, _version == null ? null : _version.name());
}

public UcanaccessConnectionBuilder withNewDatabaseVersion(String _version) {
FileFormat version = null;
AccessVersion accVersion = null;
if (_version != null) {
version = FileFormat.parse(_version);
if (version == null) {
accVersion = AccessVersion.parse(_version);
if (accVersion == null) {
UcanaccessRuntimeException.throwNow("Valid version required: " + _version);
}
}
return withProp(newDatabaseVersion, version);
return withProp(newDatabaseVersion, accVersion);
}

public UcanaccessConnectionBuilder withProp(Metadata.Property _prop, Object _value) {
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/net/ucanaccess/jdbc/UcanaccessDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.ucanaccess.converters.Metadata;
import net.ucanaccess.converters.Metadata.Property;
import net.ucanaccess.exception.UcanaccessRuntimeException;
import net.ucanaccess.type.AccessVersion;

import java.io.PrintWriter;
import java.io.Serializable;
Expand Down Expand Up @@ -404,13 +405,24 @@ public void setMirrorFolder(String _value) {
}

/**
* Creates a new, empty Access database in this format if the database specified by {@link #setAccessPath(String)}
* does not exist.
* Creates a new, empty Access database in the specified format
* if the database specified by {@link #setAccessPath(String)} does not exist.
*
* @param _value Valid values for this parameter are: "V2000", "V2003", "V2007", "V2010", or "V2016".
*/
public void setNewDatabaseVersion(String _value) {
setProp(newDatabaseVersion, _value);
* @param _version database versions, valid values are defined in enum {@link AccessVersion} ("V2000", "V2003", "V2007" etc.).
*/
public void setNewDatabaseVersion(String _version) {
AccessVersion accVersion = null;
if (_version != null && !_version.isEmpty()) {
accVersion = AccessVersion.parse(_version);
if (accVersion == null) {
UcanaccessRuntimeException.throwNow("Valid version required: " + _version);
}
}
setNewDatabaseVersion(accVersion);
}

public void setNewDatabaseVersion(AccessVersion _version) {
setProp(newDatabaseVersion, _version == null ? null : _version.name());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/ucanaccess/jdbc/UcanaccessDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Connection connect(String _url, Properties _props) throws SQLException {
FileFormat ff = null;
if (props.containsKey(newDatabaseVersion)) {
if (!fileDb.exists()) {
ff = FileFormat.valueOf(props.get(newDatabaseVersion).toUpperCase());
ff = FileFormat.parse(props.get(newDatabaseVersion));
}

}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/ucanaccess/type/AccessVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public static List<AccessVersion> getDefaultAccessVersion() {
return List.of(DEFAULT_ACCESS_VERSION);
}

public static AccessVersion parse(String _val) {
if (_val == null) {
public static AccessVersion parse(String _str) {
if (_str == null) {
return null;
}
String val = _val.strip().toLowerCase();
String val = _str.strip();
for (AccessVersion ver : values()) {
if (val.equals(ver.name())) {
if (val.equalsIgnoreCase(ver.name())) {
return ver;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/ucanaccess/jdbc/CreateDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void testNewDatabase(AccessVersion _accessVersion) throws Exception {
.withDbPath(fileMdb.getAbsolutePath())
.withoutUserPass()
.withImmediatelyReleaseResources()
.withNewDatabaseVersion(getFileFormat());
.withNewDatabaseVersion(getAccessVersion());
getLogger().log(Level.DEBUG, "Database url: {0}", bldr.getUrl());
UcanaccessConnection conn = bldr.build();

Expand Down
17 changes: 11 additions & 6 deletions src/test/java/net/ucanaccess/jdbc/DataSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import net.ucanaccess.exception.UcanaccessRuntimeException;
import net.ucanaccess.test.UcanaccessBaseTest;
import net.ucanaccess.type.AccessVersion;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.File;
import java.lang.System.Logger.Level;
Expand All @@ -21,12 +25,13 @@ void setNewDatabaseVersionBad() {
.isInstanceOf(UcanaccessRuntimeException.class);
}

@Test
void setNewDatabaseVersionGood() {
@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(strings = {"V2003", "v2003", "V2016"})
@NullSource
void setNewDatabaseVersionGood(String _ver) {
UcanaccessDataSource uds = new UcanaccessDataSource();
String ver = "V2003";
uds.setNewDatabaseVersion(ver);
assertEquals(ver, uds.getNewDatabaseVersion());
uds.setNewDatabaseVersion(_ver);
assertThat(uds.getNewDatabaseVersion()).isEqualToIgnoringCase(_ver);
}

@Test
Expand All @@ -51,7 +56,7 @@ void createNewDatabase() throws SQLException {

UcanaccessDataSource uds = new UcanaccessDataSource();
uds.setAccessPath(fileMdb.getAbsolutePath());
uds.setNewDatabaseVersion("V2003");
uds.setNewDatabaseVersion(AccessVersion.V2003);
uds.setImmediatelyReleaseResources(true); // so we can delete it immediately after close

try (Connection conn = uds.getConnection()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void testReloadMirror(AccessVersion _accessVersion) throws Exception {
.withDbPath(dbFile.getAbsolutePath())
.withoutUserPass()
.withMemory()
.withNewDatabaseVersion(getFileFormat())
.withNewDatabaseVersion(getAccessVersion())
.build();
UcanaccessStatement stCreate = conn.createStatement()) {
stCreate.execute("CREATE TABLE Table1 (ID COUNTER PRIMARY KEY, TextField TEXT(50))");
Expand Down

0 comments on commit 89dce7f

Please sign in to comment.