Skip to content

Commit

Permalink
fix: Add Index out of bound exception handling for PropertiesFile.save()
Browse files Browse the repository at this point in the history
  • Loading branch information
bohan-amplitude committed Nov 19, 2022
1 parent 55bd9b7 commit 9e630de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions id/src/main/java/com/amplitude/id/utilities/PropertiesFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ class PropertiesFile(directory: File, key: String, prefix: String, logger: Logge
}

private fun save() {
FileOutputStream(propertiesFile).use {
underlyingProperties.store(it, null)
try {
FileOutputStream(propertiesFile).use {
underlyingProperties.store(it, null)
}
} catch (e: IndexOutOfBoundsException) {
logger?.error("Failed to save property file with path ${propertiesFile.absolutePath}, error stacktrace: ${e.stackTraceToString()}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.util.Properties

Expand Down Expand Up @@ -40,6 +41,19 @@ class PropertiesFileTest {
verify(exactly = 1) { mockedLogger.error(any()) }
}

@Test
fun `test saving properties file with exception`() {
val tempFile = File(tempFolder, "prefix-key.properties")
tempFile.createNewFile()

val propertiesFile = PropertiesFile(tempFolder!!, "key", "prefix", mockedLogger)
propertiesFile.underlyingProperties = mockedProperties
every { mockedProperties.store(any<FileOutputStream>(), null) } throws IndexOutOfBoundsException()

propertiesFile.putLong("test", 1L)
verify(exactly = 1) { mockedLogger.error(any()) }
}

@Test
fun `test loading properties file successfully`() {
val tempFile = File(tempFolder, "prefix-key.properties")
Expand Down

0 comments on commit 9e630de

Please sign in to comment.