Skip to content

Commit

Permalink
fix: Fixed NPE on sqlite activator stop (#4773)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicola Timeus <[email protected]>
  • Loading branch information
nicolatimeus authored Jul 20, 2023
1 parent 096b2b9 commit a92d199
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class SqliteProviderActivatorTest {

@Test
public void shouldSetSqliteTempDirIfUnset() {
givenNoSystemPropertyValue("org.sqlite.tmpdir");
givenBundleStorageAreaPath("/tmp/foo");

whenActivatorIsStarted();
Expand All @@ -37,6 +38,7 @@ public void shouldSetSqliteTempDirIfUnset() {

@Test
public void shouldNotSetSqliteTempDirIfBundleStorageAreaIsNotAvailable() {
givenNoSystemPropertyValue("org.sqlite.tmpdir");
givenNoBundleStorageArea();

whenActivatorIsStarted();
Expand Down Expand Up @@ -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> exception = Optional.empty();
private Optional<String> temporaryDirectoryPath = Optional.empty();
private SqliteProviderActivator activator = new SqliteProviderActivator();

private void givenBundleStorageAreaPath(String path) {
Mockito.when(bundleContext.getDataFile("")).thenReturn(new File(path));
Expand All @@ -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);
}
Expand Down

0 comments on commit a92d199

Please sign in to comment.