Skip to content
Snippets Groups Projects
Commit c4ebfe98 authored by Sebastian's avatar Sebastian
Browse files

Add SettingsManager and test for it

parent 57d36af8
No related branches found
No related tags found
1 merge request!20Remove DatabaseComponent and use Database directly
...@@ -52,4 +52,17 @@ interface Database { ...@@ -52,4 +52,17 @@ interface Database {
@Throws(DbException::class) @Throws(DbException::class)
fun removeContact(txn: Connection, id: Int) fun removeContact(txn: Connection, id: Int)
/**
* Runs the given task within a transaction.
*/
@Throws(DbException::class)
fun transaction(readOnly: Boolean, task: (Connection) -> Unit)
/**
* Runs the given task within a transaction and returns the result of the
* task.
*/
@Throws(DbException::class)
fun <R> transactionWithResult(readOnly: Boolean, task: (Connection) -> R): R
} }
...@@ -461,4 +461,35 @@ abstract class JdbcDatabase(private val dbTypes: DatabaseTypes, private val cloc ...@@ -461,4 +461,35 @@ abstract class JdbcDatabase(private val dbTypes: DatabaseTypes, private val cloc
} }
} }
override fun transaction(readOnly: Boolean, task: (Connection) -> Unit) {
val txn = startTransaction()
var success = false
try {
task(txn)
success = true
} finally {
if (success) {
commitTransaction(txn)
} else {
abortTransaction(txn)
}
}
}
override fun <R> transactionWithResult(readOnly: Boolean, task: (Connection) -> R): R {
val txn = startTransaction()
var success = false
try {
val result = task(txn)
success = true
return result
} finally {
if (success) {
commitTransaction(txn)
} else {
abortTransaction(txn)
}
}
}
} }
package org.briarproject.mailbox.core.settings
import org.briarproject.mailbox.core.db.DbException
import java.sql.Connection
interface SettingsManager {
/**
* Returns all settings in the given namespace.
*/
@Throws(DbException::class)
fun getSettings(namespace: String): Settings
/**
* Returns all settings in the given namespace.
*/
@Throws(DbException::class)
fun getSettings(txn: Connection, namespace: String): Settings
/**
* Merges the given settings with any existing settings in the given
* namespace.
*/
@Throws(DbException::class)
fun mergeSettings(s: Settings, namespace: String)
}
package org.briarproject.mailbox.core.settings
import org.briarproject.mailbox.core.db.Database
import org.briarproject.mailbox.core.db.DbException
import java.sql.Connection
import javax.annotation.concurrent.Immutable
import javax.inject.Inject
@Immutable
internal class SettingsManagerImpl @Inject constructor(private val db: Database) : SettingsManager {
@Throws(DbException::class)
override fun getSettings(namespace: String): Settings {
return db.transactionWithResult(true) { txn: Connection ->
db.getSettings(txn, namespace)
}
}
@Throws(DbException::class)
override fun getSettings(txn: Connection, namespace: String): Settings {
return db.getSettings(txn, namespace)
}
@Throws(DbException::class)
override fun mergeSettings(s: Settings, namespace: String) {
db.transaction(false) { txn: Connection -> db.mergeSettings(txn, s, namespace) }
}
}
package org.briarproject.mailbox.core.settings
import dagger.Module
import dagger.Provides
import org.briarproject.mailbox.core.db.Database
@Module
class SettingsModule {
@Provides
fun provideSettingsManager(db: Database): SettingsManager {
return SettingsManagerImpl(db)
}
}
package org.briarproject.mailbox.core package org.briarproject.mailbox.core
import dagger.Component import dagger.Component
import org.briarproject.mailbox.core.db.Database
import org.briarproject.mailbox.core.lifecycle.LifecycleManager import org.briarproject.mailbox.core.lifecycle.LifecycleManager
import org.briarproject.mailbox.core.settings.SettingsManager
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
...@@ -13,4 +15,6 @@ import javax.inject.Singleton ...@@ -13,4 +15,6 @@ import javax.inject.Singleton
interface TestComponent { interface TestComponent {
fun injectCoreEagerSingletons(): CoreEagerSingletons fun injectCoreEagerSingletons(): CoreEagerSingletons
fun getLifecycleManager(): LifecycleManager fun getLifecycleManager(): LifecycleManager
fun getSettingsManager(): SettingsManager
fun getDatabase(): Database
} }
...@@ -8,6 +8,7 @@ import org.briarproject.mailbox.core.db.DatabaseConfig ...@@ -8,6 +8,7 @@ import org.briarproject.mailbox.core.db.DatabaseConfig
import org.briarproject.mailbox.core.db.DatabaseModule import org.briarproject.mailbox.core.db.DatabaseModule
import org.briarproject.mailbox.core.lifecycle.LifecycleModule import org.briarproject.mailbox.core.lifecycle.LifecycleModule
import org.briarproject.mailbox.core.server.WebServerModule import org.briarproject.mailbox.core.server.WebServerModule
import org.briarproject.mailbox.core.settings.SettingsModule
import org.briarproject.mailbox.core.system.Clock import org.briarproject.mailbox.core.system.Clock
import java.io.File import java.io.File
import javax.inject.Singleton import javax.inject.Singleton
...@@ -17,6 +18,7 @@ import javax.inject.Singleton ...@@ -17,6 +18,7 @@ import javax.inject.Singleton
LifecycleModule::class, LifecycleModule::class,
DatabaseModule::class, DatabaseModule::class,
WebServerModule::class, WebServerModule::class,
SettingsModule::class,
// no Tor module // no Tor module
] ]
) )
......
package org.briarproject.mailbox.core.settings
import org.briarproject.mailbox.core.DaggerTestComponent
import org.briarproject.mailbox.core.TestComponent
import org.briarproject.mailbox.core.TestModule
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle
import org.junit.jupiter.api.io.TempDir
import java.io.File
import kotlin.test.assertEquals
@TestInstance(Lifecycle.PER_CLASS)
class SettingsManagerTest {
private lateinit var testComponent: TestComponent
@BeforeAll
fun setUp(@TempDir tempDir: File) {
testComponent = DaggerTestComponent.builder().testModule(TestModule(tempDir)).build()
testComponent.injectCoreEagerSingletons()
testComponent.getDatabase().open(null)
}
@Test
@Throws(java.lang.Exception::class)
open fun testMergeSettings() {
val before = Settings()
before["foo"] = "bar"
before["baz"] = "bam"
val update = Settings()
update["baz"] = "qux"
val merged = Settings()
merged["foo"] = "bar"
merged["baz"] = "qux"
val sm: SettingsManager = testComponent.getSettingsManager()
// store 'before'
sm.mergeSettings(before, "namespace")
assertEquals(before, sm.getSettings("namespace"))
// merge 'update'
sm.mergeSettings(update, "namespace")
assertEquals(merged, sm.getSettings("namespace"))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment