|
| 1 | +package io.sentry.android.core.internal.modules |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.res.AssetManager |
| 5 | +import com.nhaarman.mockitokotlin2.mock |
| 6 | +import com.nhaarman.mockitokotlin2.verify |
| 7 | +import com.nhaarman.mockitokotlin2.whenever |
| 8 | +import io.sentry.ILogger |
| 9 | +import java.io.FileNotFoundException |
| 10 | +import java.nio.charset.Charset |
| 11 | +import kotlin.test.Test |
| 12 | +import kotlin.test.assertEquals |
| 13 | +import kotlin.test.assertTrue |
| 14 | + |
| 15 | +class AssetsModulesLoaderTest { |
| 16 | + |
| 17 | + class Fixture { |
| 18 | + val context = mock<Context>() |
| 19 | + val assets = mock<AssetManager>() |
| 20 | + val logger = mock<ILogger>() |
| 21 | + |
| 22 | + fun getSut( |
| 23 | + fileName: String = "sentry-external-modules.txt", |
| 24 | + content: String? = null, |
| 25 | + throws: Boolean = false |
| 26 | + ): AssetsModulesLoader { |
| 27 | + if (content != null) { |
| 28 | + whenever(assets.open(fileName)).thenReturn( |
| 29 | + content.byteInputStream(Charset.defaultCharset()) |
| 30 | + ) |
| 31 | + } |
| 32 | + if (throws) { |
| 33 | + whenever(assets.open(fileName)).thenThrow(FileNotFoundException()) |
| 34 | + } |
| 35 | + whenever(context.assets).thenReturn(assets) |
| 36 | + return AssetsModulesLoader(context, logger) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private val fixture = Fixture() |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `reads modules from assets into map`() { |
| 44 | + val sut = fixture.getSut( |
| 45 | + content = |
| 46 | + """ |
| 47 | + com.squareup.okhttp3:okhttp:3.14.9 |
| 48 | + com.squareup.okio:okio:1.17.2 |
| 49 | + """.trimIndent() |
| 50 | + ) |
| 51 | + |
| 52 | + assertEquals( |
| 53 | + mapOf( |
| 54 | + "com.squareup.okhttp3:okhttp" to "3.14.9", |
| 55 | + "com.squareup.okio:okio" to "1.17.2" |
| 56 | + ), |
| 57 | + sut.orLoadModules |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `caches modules after first read`() { |
| 63 | + val sut = fixture.getSut( |
| 64 | + content = |
| 65 | + """ |
| 66 | + com.squareup.okhttp3:okhttp:3.14.9 |
| 67 | + com.squareup.okio:okio:1.17.2 |
| 68 | + """.trimIndent() |
| 69 | + ) |
| 70 | + |
| 71 | + // first, call method to get modules cached |
| 72 | + sut.orLoadModules |
| 73 | + |
| 74 | + // then call it second time |
| 75 | + assertEquals( |
| 76 | + mapOf( |
| 77 | + "com.squareup.okhttp3:okhttp" to "3.14.9", |
| 78 | + "com.squareup.okio:okio" to "1.17.2" |
| 79 | + ), |
| 80 | + sut.orLoadModules |
| 81 | + ) |
| 82 | + // the context only called once when there's no in-memory cache |
| 83 | + verify(fixture.context).assets |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun `when file does not exist, swallows exception and returns empty map`() { |
| 88 | + val sut = fixture.getSut(throws = true) |
| 89 | + |
| 90 | + assertTrue(sut.orLoadModules!!.isEmpty()) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `when content is malformed, swallows exception and returns empty map`() { |
| 95 | + val sut = fixture.getSut( |
| 96 | + content = |
| 97 | + """ |
| 98 | + com.squareup.okhttp3;3.14.9 |
| 99 | + """.trimIndent() |
| 100 | + ) |
| 101 | + |
| 102 | + assertTrue(sut.orLoadModules!!.isEmpty()) |
| 103 | + } |
| 104 | +} |
0 commit comments