Skip to content

Commit

Permalink
SettingsImporter::getImportStreamContents throw exception when both g…
Browse files Browse the repository at this point in the history
…lobal and account setting is absent
  • Loading branch information
shamim-emon committed Jan 10, 2025
1 parent fb561e3 commit 79bfeb4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SettingsImporter internal constructor(
*
* @throws SettingsImportExportException In case of an error.
*/
@Suppress("TooGenericExceptionCaught")
@Suppress("TooGenericExceptionCaught", "ThrowsCount")
@Throws(SettingsImportExportException::class)
fun getImportStreamContents(inputStream: InputStream): ImportContents {
try {
Expand All @@ -46,7 +46,11 @@ class SettingsImporter internal constructor(
)
}

// TODO: throw exception if neither global settings nor account settings could be found
val hasAccountsWithSettings = imported.accounts.any { it.settings != null }
if (!globalSettings && !hasAccountsWithSettings) {
throw SettingsImportExportException("Neither global settings nor account settings could be found")
}

return ImportContents(globalSettings, accounts)
} catch (e: SettingsImportExportException) {
throw e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ class SettingsImporterTest : K9RobolectricTest() {
<email>[email protected]</email>
</identity>
</identities>
<settings>
<value key="a">b</value>
</settings>
</account>
</accounts>
</k9settings>
Expand Down Expand Up @@ -302,6 +305,9 @@ class SettingsImporterTest : K9RobolectricTest() {
<email>[email protected]</email>
</identity>
</identities>
<settings>
<value key="a">b</value>
</settings>
</account>
</accounts>
</k9settings>
Expand All @@ -320,4 +326,28 @@ class SettingsImporterTest : K9RobolectricTest() {
}
}
}

@Test
fun `getImportStreamContents() should throw when no setting is present in inputStream`() {
val accountUuid = UUID.randomUUID().toString()
val inputStream =
"""
<k9settings format="1" version="1">
<accounts>
<account uuid="$accountUuid">
<name></name>
<identities>
<identity>
<email>[email protected]</email>
</identity>
</identities>
</account>
</accounts>
</k9settings>
""".trimIndent().byteInputStream()

assertFailure {
settingsImporter.getImportStreamContents(inputStream)
}.isInstanceOf<SettingsImportExportException>()
}
}

0 comments on commit 79bfeb4

Please sign in to comment.