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 469cb90ceb9..a645f885fa9 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 @@ -42,7 +42,7 @@ public void start(final BundleContext context) throws Exception { @Override public void stop(final BundleContext context) throws Exception { if (locationChanged) { - System.setProperty(SQLITE_TMPDIR_PROPERTY_KEY, null); + System.clearProperty(SQLITE_TMPDIR_PROPERTY_KEY); } } 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 3d395d43ad3..d182aebb637 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 @@ -27,6 +27,7 @@ public class SqliteProviderActivatorTest { @Test public void shouldSetSqliteTempDirIfUnset() { + givenNoSystemPropertyValue("org.sqlite.tmpdir"); givenBundleStorageAreaPath("/tmp/foo"); whenActivatorIsStarted(); @@ -37,6 +38,7 @@ public void shouldSetSqliteTempDirIfUnset() { @Test public void shouldNotSetSqliteTempDirIfBundleStorageAreaIsNotAvailable() { + givenNoSystemPropertyValue("org.sqlite.tmpdir"); givenNoBundleStorageArea(); whenActivatorIsStarted(); @@ -67,9 +69,34 @@ public void shouldNotChangeSqliteTempDirIfSetAndExisting() { thenSystemPropertyValueIs("org.sqlite.tmpdir", temporaryDirectoryPath()); } + @Test + public void shouldClearSqliteTempDirOnStopIfChanged() { + givenNoSystemPropertyValue("org.sqlite.tmpdir"); + givenBundleStorageAreaPath("/tmp/foo"); + givenStartedActivator(); + + whenActivatorIsStopped(); + + thenNoExceptionIsThrown(); + thenSystemPropertyValueIs("org.sqlite.tmpdir", null); + } + + @Test + public void shouldNotClearSqliteTempDirOnStopIfNotChanged() { + givenSystemProperty("org.sqlite.tmpdir", temporaryDirectoryPath()); + givenBundleStorageAreaPath("/tmp/foo"); + givenStartedActivator(); + + whenActivatorIsStopped(); + + 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 SqliteProviderActivator activator = new SqliteProviderActivator(); private void givenBundleStorageAreaPath(String path) { Mockito.when(bundleContext.getDataFile("")).thenReturn(new File(path)); @@ -83,9 +110,28 @@ private void givenSystemProperty(final String key, final String value) { System.setProperty(key, value); } + private void givenNoSystemPropertyValue(final String key) { + System.clearProperty(key); + } + + private void givenStartedActivator() { + whenActivatorIsStarted(); + thenNoExceptionIsThrown(); + } + private void whenActivatorIsStarted() { try { - new SqliteProviderActivator().start(bundleContext); + this.exception = Optional.empty(); + activator.start(bundleContext); + } catch (Exception e) { + this.exception = Optional.of(e); + } + } + + private void whenActivatorIsStopped() { + try { + this.exception = Optional.empty(); + activator.stop(bundleContext); } catch (Exception e) { this.exception = Optional.of(e); }