-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
535 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...droid-core/src/main/java/io/sentry/android/core/internal/modules/AssetsModulesLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.sentry.android.core.internal.modules; | ||
|
||
import android.content.Context; | ||
import io.sentry.ILogger; | ||
import io.sentry.SentryLevel; | ||
import io.sentry.internal.modules.ModulesLoader; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@ApiStatus.Internal | ||
public final class AssetsModulesLoader extends ModulesLoader { | ||
|
||
private final @NotNull Context context; | ||
|
||
public AssetsModulesLoader(final @NotNull Context context, final @NotNull ILogger logger) { | ||
super(logger); | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
protected Map<String, String> loadModules() { | ||
final Map<String, String> modules = new TreeMap<>(); | ||
|
||
try { | ||
final InputStream stream = context.getAssets().open(EXTERNAL_MODULES_FILENAME); | ||
return parseStream(stream); | ||
} catch (FileNotFoundException e) { | ||
logger.log(SentryLevel.INFO, "%s file was not found.", EXTERNAL_MODULES_FILENAME); | ||
} catch (IOException e) { | ||
logger.log(SentryLevel.ERROR, "Error extracting modules.", e); | ||
} | ||
return modules; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...oid-core/src/test/java/io/sentry/android/core/internal/modules/AssetsModulesLoaderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package io.sentry.android.core.internal.modules | ||
|
||
import android.content.Context | ||
import android.content.res.AssetManager | ||
import com.nhaarman.mockitokotlin2.mock | ||
import com.nhaarman.mockitokotlin2.verify | ||
import com.nhaarman.mockitokotlin2.whenever | ||
import io.sentry.ILogger | ||
import java.io.FileNotFoundException | ||
import java.nio.charset.Charset | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class AssetsModulesLoaderTest { | ||
|
||
class Fixture { | ||
val context = mock<Context>() | ||
val assets = mock<AssetManager>() | ||
val logger = mock<ILogger>() | ||
|
||
fun getSut( | ||
fileName: String = "sentry-external-modules.txt", | ||
content: String? = null, | ||
throws: Boolean = false | ||
): AssetsModulesLoader { | ||
if (content != null) { | ||
whenever(assets.open(fileName)).thenReturn( | ||
content.byteInputStream(Charset.defaultCharset()) | ||
) | ||
} | ||
if (throws) { | ||
whenever(assets.open(fileName)).thenThrow(FileNotFoundException()) | ||
} | ||
whenever(context.assets).thenReturn(assets) | ||
return AssetsModulesLoader(context, logger) | ||
} | ||
} | ||
|
||
private val fixture = Fixture() | ||
|
||
@Test | ||
fun `reads modules from assets into map`() { | ||
val sut = fixture.getSut( | ||
content = | ||
""" | ||
com.squareup.okhttp3:okhttp:3.14.9 | ||
com.squareup.okio:okio:1.17.2 | ||
""".trimIndent() | ||
) | ||
|
||
assertEquals( | ||
mapOf( | ||
"com.squareup.okhttp3:okhttp" to "3.14.9", | ||
"com.squareup.okio:okio" to "1.17.2" | ||
), | ||
sut.orLoadModules | ||
) | ||
} | ||
|
||
@Test | ||
fun `caches modules after first read`() { | ||
val sut = fixture.getSut( | ||
content = | ||
""" | ||
com.squareup.okhttp3:okhttp:3.14.9 | ||
com.squareup.okio:okio:1.17.2 | ||
""".trimIndent() | ||
) | ||
|
||
// first, call method to get modules cached | ||
sut.orLoadModules | ||
|
||
// then call it second time | ||
assertEquals( | ||
mapOf( | ||
"com.squareup.okhttp3:okhttp" to "3.14.9", | ||
"com.squareup.okio:okio" to "1.17.2" | ||
), | ||
sut.orLoadModules | ||
) | ||
// the context only called once when there's no in-memory cache | ||
verify(fixture.context).assets | ||
} | ||
|
||
@Test | ||
fun `when file does not exist, swallows exception and returns empty map`() { | ||
val sut = fixture.getSut(throws = true) | ||
|
||
assertTrue(sut.orLoadModules!!.isEmpty()) | ||
} | ||
|
||
@Test | ||
fun `when content is malformed, swallows exception and returns empty map`() { | ||
val sut = fixture.getSut( | ||
content = | ||
""" | ||
com.squareup.okhttp3;3.14.9 | ||
""".trimIndent() | ||
) | ||
|
||
assertTrue(sut.orLoadModules!!.isEmpty()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
sentry/src/main/java/io/sentry/internal/modules/IModulesLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.sentry.internal.modules; | ||
|
||
import java.util.Map; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
@ApiStatus.Internal | ||
public interface IModulesLoader { | ||
@Nullable | ||
Map<String, String> getOrLoadModules(); | ||
} |
Oops, something went wrong.