Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import io.sentry.core.ILogger;
import io.sentry.core.SentryOptions;
import java.io.File;

class AndroidOptionsInitializer {
static void init(SentryOptions options, Context context) {
Expand All @@ -17,7 +18,17 @@ static void init(SentryOptions options, Context context, ILogger logger) {
options.setSentryClientName("sentry-android/0.0.1");

ManifestMetadataReader.applyMetadata(context, options);
createsEnvelopeDirPath(options, context);
options.addEventProcessor(new DefaultAndroidEventProcessor(context, options));
options.setSerializer(new AndroidSerializer(options.getLogger()));
}

private static void createsEnvelopeDirPath(SentryOptions options, Context context) {
File cacheDir = context.getCacheDir().getAbsoluteFile();
File envelopesDir = new File(cacheDir, "sentry-envelopes");
if (!envelopesDir.exists()) {
envelopesDir.mkdirs();
}
options.setCacheDirPath(envelopesDir.getAbsolutePath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.core.ILogger
import io.sentry.core.SentryOptions
import java.io.File
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -25,9 +27,7 @@ class AndroidOptionsInitializerTest {
@Test
fun `logger set to AndroidLogger`() {
val sentryOptions = SentryOptions()
val mockContext = mock<Context> {
on { applicationContext } doReturn context
}
val mockContext = createMockContext()

AndroidOptionsInitializer.init(sentryOptions, mockContext)
val logger = sentryOptions.javaClass.declaredFields.first { it.name == "logger" }
Expand All @@ -41,13 +41,30 @@ class AndroidOptionsInitializerTest {
@Test
fun `AndroidEventProcessor added to processors list`() {
val sentryOptions = SentryOptions()
val mockContext = mock<Context> {
on { applicationContext } doReturn context
}
val mockContext = createMockContext()
val mockLogger = mock<ILogger>()

AndroidOptionsInitializer.init(sentryOptions, mockContext, mockLogger)
val actual = sentryOptions.eventProcessors.firstOrNull { it::class == DefaultAndroidEventProcessor::class }
assertNotNull(actual)
}

@Test
fun `envelopesDir should be created at initialization`() {
val sentryOptions = SentryOptions()
val mockContext = createMockContext()
val mockLogger = mock<ILogger>()

AndroidOptionsInitializer.init(sentryOptions, mockContext, mockLogger)

assertEquals("/cache/sentry-envelopes", sentryOptions.cacheDirPath)
}

private fun createMockContext(): Context {
val mockContext = mock<Context> {
on { applicationContext } doReturn context
}
whenever(mockContext.cacheDir).thenReturn(File("/cache"))
return mockContext
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.core.InvalidDsnException
import io.sentry.core.Sentry
import java.io.File
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertFailsWith
Expand Down Expand Up @@ -46,9 +47,7 @@ class SentryInitProviderTest {
assertFalse(Sentry.isEnabled())
providerInfo.authority = AUTHORITY

val mockContext = mock<Context> {
on { applicationContext } doReturn context
}
val mockContext = createMockContext()
val metaData = Bundle()
mockMetaData(mockContext, metaData)

Expand All @@ -66,9 +65,7 @@ class SentryInitProviderTest {
assertFalse(Sentry.isEnabled())
providerInfo.authority = AUTHORITY

val mockContext = mock<Context> {
on { applicationContext } doReturn context
}
val mockContext = createMockContext()
val metaData = Bundle()
mockMetaData(mockContext, metaData)

Expand All @@ -86,9 +83,7 @@ class SentryInitProviderTest {
assertFalse(Sentry.isEnabled())
providerInfo.authority = AUTHORITY

val mockContext = mock<Context> {
on { applicationContext } doReturn context
}
val mockContext = createMockContext()
val metaData = Bundle()
mockMetaData(mockContext, metaData)

Expand All @@ -106,9 +101,7 @@ class SentryInitProviderTest {
assertFalse(Sentry.isEnabled())
providerInfo.authority = AUTHORITY

val mockContext = mock<Context> {
on { applicationContext } doReturn context
}
val mockContext = createMockContext()
val metaData = Bundle()
mockMetaData(mockContext, metaData)

Expand All @@ -124,9 +117,7 @@ class SentryInitProviderTest {
assertFalse(Sentry.isEnabled())
providerInfo.authority = AUTHORITY

val mockContext = mock<Context> {
on { applicationContext } doReturn context
}
val mockContext = createMockContext()
val metaData = Bundle()
mockMetaData(mockContext, metaData)

Expand All @@ -148,6 +139,14 @@ class SentryInitProviderTest {
mockApplicationInfo.metaData = metaData
}

private fun createMockContext(): Context {
val mockContext = mock<Context> {
on { applicationContext } doReturn context
}
whenever(mockContext.cacheDir).thenReturn(File("/cache"))
return mockContext
}

companion object {
private const val AUTHORITY = "io.sentry.sample.SentryInitProvider"
}
Expand Down
9 changes: 9 additions & 0 deletions sentry-core/src/main/java/io/sentry/core/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SentryOptions {
private ISerializer serializer;
private String sentryClientName;
private BeforeSecondCallback beforeSend;
private String cacheDirPath;

public void addEventProcessor(EventProcessor eventProcessor) {
eventProcessors.add(eventProcessor);
Expand Down Expand Up @@ -93,6 +94,14 @@ public void setBeforeSend(BeforeSecondCallback beforeSend) {
this.beforeSend = beforeSend;
}

public String getCacheDirPath() {
return cacheDirPath;
}

public void setCacheDirPath(String cacheDirPath) {
this.cacheDirPath = cacheDirPath;
}

public interface BeforeSecondCallback {
SentryEvent execute(SentryEvent event);
}
Expand Down