Skip to content

Commit

Permalink
Make storage directory customizable (#222)
Browse files Browse the repository at this point in the history
* make storage directory customizable

* add unit tests

---------

Co-authored-by: Wenxi Zeng <[email protected]>
  • Loading branch information
wenxi-zeng and Wenxi Zeng authored May 16, 2024
1 parent 515d249 commit 9103e2b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class AndroidStorage(
context: Context,
private val store: Store,
writeKey: String,
private val ioDispatcher: CoroutineDispatcher
private val ioDispatcher: CoroutineDispatcher,
directory: String? = null
) : Subscriber, Storage {

private val sharedPreferences: SharedPreferences =
context.getSharedPreferences("analytics-android-$writeKey", Context.MODE_PRIVATE)
override val storageDirectory: File = context.getDir("segment-disk-queue", Context.MODE_PRIVATE)
override val storageDirectory: File = context.getDir(directory ?: "segment-disk-queue", Context.MODE_PRIVATE)
internal val eventsFile =
EventsFileManager(storageDirectory, writeKey, AndroidKVS(sharedPreferences))

Expand Down Expand Up @@ -121,7 +122,7 @@ object AndroidStorageProvider : StorageProvider {
store = store,
writeKey = writeKey,
ioDispatcher = ioDispatcher,
context = application as Context
context = application as Context,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import kotlinx.serialization.json.*
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import sovran.kotlin.Store
import java.util.*

@RunWith(RobolectricTestRunner::class)
Expand Down Expand Up @@ -147,5 +149,24 @@ class AndroidContextCollectorTests {
}
}



@Test
fun `storage directory can be customized`() {
val dir = "test"
val androidStorage = AndroidStorage(
appContext,
Store(),
"123",
UnconfinedTestDispatcher(),
dir
)

Assertions.assertTrue(androidStorage.storageDirectory.name.contains(dir))
Assertions.assertTrue(androidStorage.eventsFile.directory.name.contains(dir))
Assertions.assertTrue(androidStorage.storageDirectory.exists())
Assertions.assertTrue(androidStorage.eventsFile.directory.exists())
}

private fun JsonElement?.asString(): String? = this?.jsonPrimitive?.content
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import java.io.FileOutputStream
* remove() will delete the file path specified
*/
class EventsFileManager(
private val directory: File,
val directory: File,
private val writeKey: String,
private val kvs: KVS
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.util.Properties
* conforming to {@link com.segment.analytics.kotlin.core.utilities.KVS} interface.
* Ideal for use on JVM systems to store k-v pairs on a file.
*/
class PropertiesFile(private val directory: File, writeKey: String) : KVS {
class PropertiesFile(val directory: File, writeKey: String) : KVS {
private val underlyingProperties: Properties = Properties()
private val propertiesFileName = "analytics-kotlin-$writeKey.properties"
private val propertiesFile = File(directory, propertiesFileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import java.io.File
class StorageImpl(
private val store: Store,
writeKey: String,
private val ioDispatcher: CoroutineDispatcher
private val ioDispatcher: CoroutineDispatcher,
directory: String? = null
) : Subscriber, Storage {

override val storageDirectory = File("/tmp/analytics-kotlin/$writeKey")
override val storageDirectory = File(directory ?: "/tmp/analytics-kotlin/$writeKey")
private val storageDirectoryEvents = File(storageDirectory, "events")

internal val propertiesFile = PropertiesFile(storageDirectory, writeKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ internal class StorageImplTest {
assertEquals(null, settings)
}

@Test
fun `storage directory can be customized`() {
storage = StorageImpl(
store,
"123",
UnconfinedTestDispatcher(),
"/tmp/test"
)

assertEquals("/tmp/test", storage.storageDirectory.path)
assertTrue(storage.eventsFile.directory.path.contains("/tmp/test"))
assertTrue(storage.propertiesFile.directory.path.contains("/tmp/test"))
assertTrue(storage.storageDirectory.exists())
assertTrue(storage.eventsFile.directory.exists())
assertTrue(storage.propertiesFile.directory.exists())
}

@Nested
inner class EventsStorage() {

Expand Down

0 comments on commit 9103e2b

Please sign in to comment.