File tree Expand file tree Collapse file tree 6 files changed +47
-7
lines changed
main/java/com/segment/analytics/kotlin/android
test/java/com/segment/analytics/kotlin/android
main/java/com/segment/analytics/kotlin/core/utilities
test/kotlin/com/segment/analytics/kotlin/core/utilities Expand file tree Collapse file tree 6 files changed +47
-7
lines changed Original file line number Diff line number Diff line change @@ -20,12 +20,13 @@ class AndroidStorage(
2020 context : Context ,
2121 private val store : Store ,
2222 writeKey : String ,
23- private val ioDispatcher : CoroutineDispatcher
23+ private val ioDispatcher : CoroutineDispatcher ,
24+ directory : String? = null
2425) : Subscriber, Storage {
2526
2627 private val sharedPreferences: SharedPreferences =
2728 context.getSharedPreferences(" analytics-android-$writeKey " , Context .MODE_PRIVATE )
28- override val storageDirectory: File = context.getDir(" segment-disk-queue" , Context .MODE_PRIVATE )
29+ override val storageDirectory: File = context.getDir(directory ? : " segment-disk-queue" , Context .MODE_PRIVATE )
2930 internal val eventsFile =
3031 EventsFileManager (storageDirectory, writeKey, AndroidKVS (sharedPreferences))
3132
@@ -121,7 +122,7 @@ object AndroidStorageProvider : StorageProvider {
121122 store = store,
122123 writeKey = writeKey,
123124 ioDispatcher = ioDispatcher,
124- context = application as Context
125+ context = application as Context ,
125126 )
126127 }
127128}
Original file line number Diff line number Diff line change @@ -16,9 +16,11 @@ import kotlinx.serialization.json.*
1616import org.junit.Assert.*
1717import org.junit.Before
1818import org.junit.Test
19+ import org.junit.jupiter.api.Assertions
1920import org.junit.runner.RunWith
2021import org.robolectric.RobolectricTestRunner
2122import org.robolectric.annotation.Config
23+ import sovran.kotlin.Store
2224import java.util.*
2325
2426@RunWith(RobolectricTestRunner ::class )
@@ -147,5 +149,24 @@ class AndroidContextCollectorTests {
147149 }
148150 }
149151
152+
153+
154+ @Test
155+ fun `storage directory can be customized` () {
156+ val dir = " test"
157+ val androidStorage = AndroidStorage (
158+ appContext,
159+ Store (),
160+ " 123" ,
161+ UnconfinedTestDispatcher (),
162+ dir
163+ )
164+
165+ Assertions .assertTrue(androidStorage.storageDirectory.name.contains(dir))
166+ Assertions .assertTrue(androidStorage.eventsFile.directory.name.contains(dir))
167+ Assertions .assertTrue(androidStorage.storageDirectory.exists())
168+ Assertions .assertTrue(androidStorage.eventsFile.directory.exists())
169+ }
170+
150171 private fun JsonElement?.asString (): String? = this ?.jsonPrimitive?.content
151172}
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ import java.io.FileOutputStream
3232 * remove() will delete the file path specified
3333 */
3434class EventsFileManager (
35- private val directory : File ,
35+ val directory : File ,
3636 private val writeKey : String ,
3737 private val kvs : KVS
3838) {
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import java.util.Properties
1010 * conforming to {@link com.segment.analytics.kotlin.core.utilities.KVS} interface.
1111 * Ideal for use on JVM systems to store k-v pairs on a file.
1212 */
13- class PropertiesFile (private val directory : File , writeKey : String ) : KVS {
13+ class PropertiesFile (val directory : File , writeKey : String ) : KVS {
1414 private val underlyingProperties: Properties = Properties ()
1515 private val propertiesFileName = " analytics-kotlin-$writeKey .properties"
1616 private val propertiesFile = File (directory, propertiesFileName)
Original file line number Diff line number Diff line change @@ -19,10 +19,11 @@ import java.io.File
1919class StorageImpl (
2020 private val store : Store ,
2121 writeKey : String ,
22- private val ioDispatcher : CoroutineDispatcher
22+ private val ioDispatcher : CoroutineDispatcher ,
23+ directory : String? = null
2324) : Subscriber, Storage {
2425
25- override val storageDirectory = File (" /tmp/analytics-kotlin/$writeKey " )
26+ override val storageDirectory = File (directory ? : " /tmp/analytics-kotlin/$writeKey " )
2627 private val storageDirectoryEvents = File (storageDirectory, " events" )
2728
2829 internal val propertiesFile = PropertiesFile (storageDirectory, writeKey)
Original file line number Diff line number Diff line change @@ -161,6 +161,23 @@ internal class StorageImplTest {
161161 assertEquals(null , settings)
162162 }
163163
164+ @Test
165+ fun `storage directory can be customized` () {
166+ storage = StorageImpl (
167+ store,
168+ " 123" ,
169+ UnconfinedTestDispatcher (),
170+ " /tmp/test"
171+ )
172+
173+ assertEquals(" /tmp/test" , storage.storageDirectory.path)
174+ assertTrue(storage.eventsFile.directory.path.contains(" /tmp/test" ))
175+ assertTrue(storage.propertiesFile.directory.path.contains(" /tmp/test" ))
176+ assertTrue(storage.storageDirectory.exists())
177+ assertTrue(storage.eventsFile.directory.exists())
178+ assertTrue(storage.propertiesFile.directory.exists())
179+ }
180+
164181 @Nested
165182 inner class EventsStorage () {
166183
You can’t perform that action at this time.
0 commit comments