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
6 files changed +47
-7
lines changed Original file line number Diff line number Diff line change @@ -20,12 +20,13 @@ class AndroidStorage(
20
20
context : Context ,
21
21
private val store : Store ,
22
22
writeKey : String ,
23
- private val ioDispatcher : CoroutineDispatcher
23
+ private val ioDispatcher : CoroutineDispatcher ,
24
+ directory : String? = null
24
25
) : Subscriber, Storage {
25
26
26
27
private val sharedPreferences: SharedPreferences =
27
28
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 )
29
30
internal val eventsFile =
30
31
EventsFileManager (storageDirectory, writeKey, AndroidKVS (sharedPreferences))
31
32
@@ -121,7 +122,7 @@ object AndroidStorageProvider : StorageProvider {
121
122
store = store,
122
123
writeKey = writeKey,
123
124
ioDispatcher = ioDispatcher,
124
- context = application as Context
125
+ context = application as Context ,
125
126
)
126
127
}
127
128
}
Original file line number Diff line number Diff line change @@ -16,9 +16,11 @@ import kotlinx.serialization.json.*
16
16
import org.junit.Assert.*
17
17
import org.junit.Before
18
18
import org.junit.Test
19
+ import org.junit.jupiter.api.Assertions
19
20
import org.junit.runner.RunWith
20
21
import org.robolectric.RobolectricTestRunner
21
22
import org.robolectric.annotation.Config
23
+ import sovran.kotlin.Store
22
24
import java.util.*
23
25
24
26
@RunWith(RobolectricTestRunner ::class )
@@ -147,5 +149,24 @@ class AndroidContextCollectorTests {
147
149
}
148
150
}
149
151
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
+
150
171
private fun JsonElement?.asString (): String? = this ?.jsonPrimitive?.content
151
172
}
Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ import java.io.FileOutputStream
32
32
* remove() will delete the file path specified
33
33
*/
34
34
class EventsFileManager (
35
- private val directory : File ,
35
+ val directory : File ,
36
36
private val writeKey : String ,
37
37
private val kvs : KVS
38
38
) {
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import java.util.Properties
10
10
* conforming to {@link com.segment.analytics.kotlin.core.utilities.KVS} interface.
11
11
* Ideal for use on JVM systems to store k-v pairs on a file.
12
12
*/
13
- class PropertiesFile (private val directory : File , writeKey : String ) : KVS {
13
+ class PropertiesFile (val directory : File , writeKey : String ) : KVS {
14
14
private val underlyingProperties: Properties = Properties ()
15
15
private val propertiesFileName = " analytics-kotlin-$writeKey .properties"
16
16
private val propertiesFile = File (directory, propertiesFileName)
Original file line number Diff line number Diff line change @@ -19,10 +19,11 @@ import java.io.File
19
19
class StorageImpl (
20
20
private val store : Store ,
21
21
writeKey : String ,
22
- private val ioDispatcher : CoroutineDispatcher
22
+ private val ioDispatcher : CoroutineDispatcher ,
23
+ directory : String? = null
23
24
) : Subscriber, Storage {
24
25
25
- override val storageDirectory = File (" /tmp/analytics-kotlin/$writeKey " )
26
+ override val storageDirectory = File (directory ? : " /tmp/analytics-kotlin/$writeKey " )
26
27
private val storageDirectoryEvents = File (storageDirectory, " events" )
27
28
28
29
internal val propertiesFile = PropertiesFile (storageDirectory, writeKey)
Original file line number Diff line number Diff line change @@ -161,6 +161,23 @@ internal class StorageImplTest {
161
161
assertEquals(null , settings)
162
162
}
163
163
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
+
164
181
@Nested
165
182
inner class EventsStorage () {
166
183
You can’t perform that action at this time.
0 commit comments