Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed error on SQLite dp reinstallation #4725

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,28 @@ public class SqliteProviderActivator implements BundleActivator {

private static final String SQLITE_TMPDIR_PROPERTY_KEY = "org.sqlite.tmpdir";

private boolean locationChanged = false;

@Override
public void start(final BundleContext context) throws Exception {
if (System.getProperty(SQLITE_TMPDIR_PROPERTY_KEY) == null) {
final Optional<String> sqliteTmpDir = Optional.ofNullable(System.getProperty(SQLITE_TMPDIR_PROPERTY_KEY));

if (!sqliteTmpDir.isPresent() || !new File(sqliteTmpDir.get()).isDirectory()) {

final Optional<File> bundleStorageAreaLocation = Optional.ofNullable(context.getDataFile(""));

if (bundleStorageAreaLocation.isPresent()) {
System.setProperty(SQLITE_TMPDIR_PROPERTY_KEY, bundleStorageAreaLocation.get().getAbsolutePath());
locationChanged = true;
}
}
}

@Override
public void stop(final BundleContext context) throws Exception {
// nothing to do
if (locationChanged) {
System.setProperty(SQLITE_TMPDIR_PROPERTY_KEY, null);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
package org.eclipse.kura.internal.db.sqlite.provider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;
import java.nio.file.Files;
import java.util.Optional;

import org.junit.Test;
Expand Down Expand Up @@ -44,18 +46,30 @@ public void shouldNotSetSqliteTempDirIfBundleStorageAreaIsNotAvailable() {
}

@Test
public void shouldNotChangeSqliteTempDirIfAlreadySet() {
public void shouldChangeSqliteTempDirIfSetButNonExisting() {
givenSystemProperty("org.sqlite.tmpdir", "bar");
givenBundleStorageAreaPath("/tmp/foo");

whenActivatorIsStarted();

thenNoExceptionIsThrown();
thenSystemPropertyValueIs("org.sqlite.tmpdir", "bar");
thenSystemPropertyValueIs("org.sqlite.tmpdir", "/tmp/foo");
}

@Test
public void shouldNotChangeSqliteTempDirIfSetAndExisting() {
givenSystemProperty("org.sqlite.tmpdir", temporaryDirectoryPath());
givenBundleStorageAreaPath("/tmp/foo");

whenActivatorIsStarted();

thenNoExceptionIsThrown();
thenSystemPropertyValueIs("org.sqlite.tmpdir", temporaryDirectoryPath());
}

private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
private Optional<Exception> exception = Optional.empty();
private Optional<String> temporaryDirectoryPath = Optional.empty();

private void givenBundleStorageAreaPath(String path) {
Mockito.when(bundleContext.getDataFile("")).thenReturn(new File(path));
Expand All @@ -75,7 +89,6 @@ private void whenActivatorIsStarted() {
} catch (Exception e) {
this.exception = Optional.of(e);
}

}

private void thenSystemPropertyValueIs(final String key, final String value) {
Expand All @@ -84,6 +97,20 @@ private void thenSystemPropertyValueIs(final String key, final String value) {

private void thenNoExceptionIsThrown() {
assertEquals(Optional.empty(), this.exception);
}

private String temporaryDirectoryPath() {
if (temporaryDirectoryPath.isPresent()) {
return temporaryDirectoryPath.get();
}

try {
final String newPath = Files.createTempDirectory(null).toFile().getAbsolutePath();
this.temporaryDirectoryPath = Optional.of(newPath);
return newPath;
} catch (final Exception e) {
fail("Cannot create temporary directory");
throw new IllegalStateException("unreachable");
}
}
}