Skip to content

Commit

Permalink
Refactor test with linked access databases
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed May 3, 2024
1 parent edac486 commit c84d46a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
18 changes: 4 additions & 14 deletions src/main/java/net/ucanaccess/jdbc/UcanaccessDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.sql.*;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

public final class UcanaccessDriver implements Driver {

Expand Down Expand Up @@ -144,7 +145,9 @@ public Connection connect(String _url, Properties _props) throws SQLException {
dbRef.setPreventReloading(Boolean.parseBoolean(props.get(preventReloading)));
}
if (props.containsKey(reMap)) {
dbRef.setExternalResourcesMapping(toMap(props.get(reMap)));
Map<String, String> map = Arrays.stream(props.get(reMap).split("&")).map(s -> s.split("\\|")).filter(arr -> arr.length == 2)
.collect(Collectors.toMap(k1 -> k1[0], v1 -> v1[1], (v1, v2) -> v1, LinkedHashMap::new));
dbRef.setExternalResourcesMapping(map);
}
if (props.containsKey(supportsAccessLike)) {
SQLConverter.setSupportsAccessLike(Boolean.parseBoolean(props.get(supportsAccessLike)));
Expand Down Expand Up @@ -247,19 +250,6 @@ private Integer validateLobScale(String _property) {
return null;
}

private Map<String, String> toMap(String property) {
Map<String, String> hm = new HashMap<>();
StringTokenizer st = new StringTokenizer(property, "&");
while (st.hasMoreTokens()) {
String entry = st.nextToken();
if (!entry.contains("|")) {
continue;
}
hm.put(entry.substring(0, entry.indexOf('|')).toLowerCase(), entry.substring(entry.indexOf('|') + 1));
}
return hm;
}

@Override
public int getMajorVersion() {
return 0;
Expand Down
20 changes: 13 additions & 7 deletions src/test/java/net/ucanaccess/jdbc/ExternalResourcesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,32 @@
import net.ucanaccess.type.AccessVersion;
import org.junit.jupiter.params.ParameterizedTest;

import java.io.File;
import java.lang.System.Logger.Level;
import java.sql.SQLException;

class ExternalResourcesTest extends UcanaccessBaseTest {

@Override
protected String getAccessPath() {
return getTestDbDir() + "linked.mdb";
}

@ParameterizedTest(name = "[{index}] {0}")
@AccessDefaultVersionSource
void testLinks(AccessVersion _accessVersion) throws SQLException {
init(_accessVersion);
setAccessVersion(_accessVersion);

File main = copyResourceToTempFile(getTestDbDir() + "main.mdb");
File linkee1 = copyResourceToTempFile(getTestDbDir() + "linkee1.mdb");
File linkee2 = copyResourceToTempFile(getTestDbDir() + "linkee2.mdb");
String dbLinked = getAccessTempPath();
String dbLinkee1 = copyResourceToTempFile(getTestDbDir() + "linkee1.mdb").getAbsolutePath();
String dbLinkee2 = copyResourceToTempFile(getTestDbDir() + "linkee2.mdb").getAbsolutePath();
String origPath = "c:\\db\\";

UcanaccessConnectionBuilder bldr = buildConnection()
.withDbPath(main.getAbsolutePath())
.withDbPath(dbLinked)
.withoutUserPass()
.withImmediatelyReleaseResources()
.withProp(Property.reMap, "c:\\db\\linkee1.mdb|" + linkee1.getAbsolutePath() + "&c:\\db\\linkee2.mdb|" + linkee2.getAbsolutePath());
.withProp(Property.reMap, origPath + "linkee1.mdb" + '|' + dbLinkee1
+ '&' + origPath + "linkee2.mdb" + '|' + dbLinkee2);
getLogger().log(Level.DEBUG, "Database url: {0}", bldr.getUrl());

try (UcanaccessConnection conn = bldr.build();
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/net/ucanaccess/test/UcanaccessBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,12 @@ void createNewDatabase(FileFormat _fileFormat, File _dbFile) {
}

protected File copyResourceToTempFile(String _resourcePath) {
File resourceFile = new File(_resourcePath);

try (InputStream is = getClass().getClassLoader().getResourceAsStream(_resourcePath)) {
if (is == null) {
getLogger().log(Level.WARNING, "Resource {0} not found in classpath", _resourcePath);
return null;
}
File tempFile = createTempFile(resourceFile.getName().replace('.', '_'));
File tempFile = createTempFile(new File(_resourcePath).getName().replace('.', '_'));
getLogger().log(Level.DEBUG, "Copying resource {0} to {1}", _resourcePath, tempFile.getAbsolutePath());
return copyFile(is, tempFile);
} catch (IOException _ex) {
Expand Down
File renamed without changes.

0 comments on commit c84d46a

Please sign in to comment.