From 902e9acd777a174dcd55a00db9b262ce0237a5a9 Mon Sep 17 00:00:00 2001 From: nicolatimeus Date: Wed, 21 Jun 2023 15:57:17 +0200 Subject: [PATCH] fix: Fixed error on SQLite dp reinstallation (#4725) * fix: Fixed error on SQLite dp reinstallation Signed-off-by: Nicola Timeus * Updated tests Signed-off-by: Nicola Timeus --------- Signed-off-by: Nicola Timeus (cherry picked from commit bcc89dc5e42757d9bb7a2079b3768520963ddf86) --- .../provider/SqliteProviderActivator.java | 11 +++++-- .../provider/SqliteProviderActivatorTest.java | 33 +++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/kura/org.eclipse.kura.db.sqlite.provider/src/main/java/org/eclipse/kura/internal/db/sqlite/provider/SqliteProviderActivator.java b/kura/org.eclipse.kura.db.sqlite.provider/src/main/java/org/eclipse/kura/internal/db/sqlite/provider/SqliteProviderActivator.java index e0fa6b28251..469cb90ceb9 100644 --- a/kura/org.eclipse.kura.db.sqlite.provider/src/main/java/org/eclipse/kura/internal/db/sqlite/provider/SqliteProviderActivator.java +++ b/kura/org.eclipse.kura.db.sqlite.provider/src/main/java/org/eclipse/kura/internal/db/sqlite/provider/SqliteProviderActivator.java @@ -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 sqliteTmpDir = Optional.ofNullable(System.getProperty(SQLITE_TMPDIR_PROPERTY_KEY)); + + if (!sqliteTmpDir.isPresent() || !new File(sqliteTmpDir.get()).isDirectory()) { final Optional 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); + } } } diff --git a/kura/test/org.eclipse.kura.db.sqlite.provider.test/src/test/java/org/eclipse/kura/internal/db/sqlite/provider/SqliteProviderActivatorTest.java b/kura/test/org.eclipse.kura.db.sqlite.provider.test/src/test/java/org/eclipse/kura/internal/db/sqlite/provider/SqliteProviderActivatorTest.java index 5941293e34f..3d395d43ad3 100644 --- a/kura/test/org.eclipse.kura.db.sqlite.provider.test/src/test/java/org/eclipse/kura/internal/db/sqlite/provider/SqliteProviderActivatorTest.java +++ b/kura/test/org.eclipse.kura.db.sqlite.provider.test/src/test/java/org/eclipse/kura/internal/db/sqlite/provider/SqliteProviderActivatorTest.java @@ -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; @@ -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 = Optional.empty(); + private Optional temporaryDirectoryPath = Optional.empty(); private void givenBundleStorageAreaPath(String path) { Mockito.when(bundleContext.getDataFile("")).thenReturn(new File(path)); @@ -75,7 +89,6 @@ private void whenActivatorIsStarted() { } catch (Exception e) { this.exception = Optional.of(e); } - } private void thenSystemPropertyValueIs(final String key, final String value) { @@ -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"); + } } }