From 9103e2b316dc5408a7aa07806b804a2e93a99b24 Mon Sep 17 00:00:00 2001 From: Wenxi Zeng Date: Thu, 16 May 2024 16:08:16 -0500 Subject: [PATCH] Make storage directory customizable (#222) * make storage directory customizable * add unit tests --------- Co-authored-by: Wenxi Zeng --- .../analytics/kotlin/android/Storage.kt | 7 ++++--- .../android/AndroidContextCollectorTests.kt | 21 +++++++++++++++++++ .../core/utilities/EventsFileManager.kt | 2 +- .../kotlin/core/utilities/PropertiesFile.kt | 2 +- .../kotlin/core/utilities/StorageImpl.kt | 5 +++-- .../kotlin/core/utilities/StorageImplTest.kt | 17 +++++++++++++++ 6 files changed, 47 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt b/android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt index 26250113..32fd9f4b 100644 --- a/android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt +++ b/android/src/main/java/com/segment/analytics/kotlin/android/Storage.kt @@ -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)) @@ -121,7 +122,7 @@ object AndroidStorageProvider : StorageProvider { store = store, writeKey = writeKey, ioDispatcher = ioDispatcher, - context = application as Context + context = application as Context, ) } } \ No newline at end of file diff --git a/android/src/test/java/com/segment/analytics/kotlin/android/AndroidContextCollectorTests.kt b/android/src/test/java/com/segment/analytics/kotlin/android/AndroidContextCollectorTests.kt index 96f0708d..df6c0d45 100644 --- a/android/src/test/java/com/segment/analytics/kotlin/android/AndroidContextCollectorTests.kt +++ b/android/src/test/java/com/segment/analytics/kotlin/android/AndroidContextCollectorTests.kt @@ -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) @@ -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 } \ No newline at end of file diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt index 196e9dbd..f70d87b4 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/EventsFileManager.kt @@ -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 ) { diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/PropertiesFile.kt b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/PropertiesFile.kt index 2b75e1e5..88638a5c 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/PropertiesFile.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/PropertiesFile.kt @@ -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) diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt index a03cb354..dc093ca0 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/utilities/StorageImpl.kt @@ -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) diff --git a/core/src/test/kotlin/com/segment/analytics/kotlin/core/utilities/StorageImplTest.kt b/core/src/test/kotlin/com/segment/analytics/kotlin/core/utilities/StorageImplTest.kt index d3b7c4c6..9bd1d275 100644 --- a/core/src/test/kotlin/com/segment/analytics/kotlin/core/utilities/StorageImplTest.kt +++ b/core/src/test/kotlin/com/segment/analytics/kotlin/core/utilities/StorageImplTest.kt @@ -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() {