Skip to content

Commit

Permalink
fix: Fixed error on SQLite dp reinstallation (#4725)
Browse files Browse the repository at this point in the history
* fix: Fixed error on SQLite dp reinstallation

Signed-off-by: Nicola Timeus <[email protected]>

* Updated tests

Signed-off-by: Nicola Timeus <[email protected]>

---------

Signed-off-by: Nicola Timeus <[email protected]>
  • Loading branch information
nicolatimeus authored Jun 21, 2023
1 parent c3a3bfe commit bcc89dc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
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");
}
}
}

0 comments on commit bcc89dc

Please sign in to comment.