<commit_before>package io.sentry.android.core

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ProviderInfo
import android.os.Bundle
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.core.InvalidDsnException
import io.sentry.core.Sentry
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SentryInitProviderTest {
    private var sentryInitProvider = SentryInitProvider()

    private lateinit var context: Context

    @BeforeTest
    fun `set up`() {
        context = ApplicationProvider.getApplicationContext()
        Sentry.close()
    }

    @Test
    fun `when missing applicationId, SentryInitProvider throws`() {
        val providerInfo = ProviderInfo()

        providerInfo.authority = SentryInitProvider::class.java.name
        assertFailsWith<IllegalStateException> { sentryInitProvider.attachInfo(context, providerInfo) }
    }

    @Test
    fun `when applicationId is defined, dsn in meta-data, SDK initializes`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putString(ManifestMetadataReader.DSN_KEY, "https://key@sentry.io/123")

        sentryInitProvider.attachInfo(mockContext, providerInfo)

        assertTrue(Sentry.isEnabled())
    }

    @Test
    fun `when applicationId is defined, dsn in meta-data is empty, SDK doesnt initialize`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putString(ManifestMetadataReader.DSN_KEY, "")

        sentryInitProvider.attachInfo(mockContext, providerInfo)

        assertFalse(Sentry.isEnabled())
    }

    @Test
    fun `when applicationId is defined, dsn in meta-data is not set or null, SDK doesnt initialize`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putString(ManifestMetadataReader.DSN_KEY, null)

        sentryInitProvider.attachInfo(mockContext, providerInfo)

        assertFalse(Sentry.isEnabled())
    }

    @Test
    fun `when applicationId is defined, dsn in meta-data is invalid, SDK should throw an error`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putString(ManifestMetadataReader.DSN_KEY, "invalid dsn")

        assertFailsWith<InvalidDsnException> { sentryInitProvider.attachInfo(mockContext, providerInfo) }
    }

    @Test
    fun `when applicationId is defined, auto-init in meta-data is set to false, SDK doesnt initialize`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = BuildConfig.LIBRARY_PACKAGE_NAME + AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putBoolean(ManifestMetadataReader.AUTO_INIT, false)

        sentryInitProvider.attachInfo(mockContext, providerInfo)

        assertFalse(Sentry.isEnabled())
    }

    private fun mockMetaData(mockContext: Context, metaData: Bundle) {
        val mockPackageManager: PackageManager = mock()
        val mockApplicationInfo: ApplicationInfo = mock()

        whenever(mockContext.packageName).thenReturn("io.sentry.sample.test")
        whenever(mockContext.packageManager).thenReturn(mockPackageManager)
        whenever(mockPackageManager.getApplicationInfo(mockContext.packageName, PackageManager.GET_META_DATA)).thenReturn(mockApplicationInfo)

        mockApplicationInfo.metaData = metaData
    }

    companion object {
        private const val AUTHORITY = "io.sentry.sample.SentryInitProvider"
    }
}<commit_msg>fixed broken test (getsentry/sentry-android#33)<commit_after>package io.sentry.android.core

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ProviderInfo
import android.os.Bundle
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.core.InvalidDsnException
import io.sentry.core.Sentry
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SentryInitProviderTest {
    private var sentryInitProvider = SentryInitProvider()

    private lateinit var context: Context

    @BeforeTest
    fun `set up`() {
        context = ApplicationProvider.getApplicationContext()
        Sentry.close()
    }

    @Test
    fun `when missing applicationId, SentryInitProvider throws`() {
        val providerInfo = ProviderInfo()

        providerInfo.authority = SentryInitProvider::class.java.name
        assertFailsWith<IllegalStateException> { sentryInitProvider.attachInfo(context, providerInfo) }
    }

    @Test
    fun `when applicationId is defined, dsn in meta-data, SDK initializes`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putString(ManifestMetadataReader.DSN_KEY, "https://key@sentry.io/123")

        sentryInitProvider.attachInfo(mockContext, providerInfo)

        assertTrue(Sentry.isEnabled())
    }

    @Test
    fun `when applicationId is defined, dsn in meta-data is empty, SDK doesnt initialize`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putString(ManifestMetadataReader.DSN_KEY, "")

        sentryInitProvider.attachInfo(mockContext, providerInfo)

        assertFalse(Sentry.isEnabled())
    }

    @Test
    fun `when applicationId is defined, dsn in meta-data is not set or null, SDK doesnt initialize`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putString(ManifestMetadataReader.DSN_KEY, null)

        sentryInitProvider.attachInfo(mockContext, providerInfo)

        assertFalse(Sentry.isEnabled())
    }

    @Test
    fun `when applicationId is defined, dsn in meta-data is invalid, SDK should throw an error`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putString(ManifestMetadataReader.DSN_KEY, "invalid dsn")

        assertFailsWith<InvalidDsnException> { sentryInitProvider.attachInfo(mockContext, providerInfo) }
    }

    @Test
    fun `when applicationId is defined, auto-init in meta-data is set to false, SDK doesnt initialize`() {
        val providerInfo = ProviderInfo()

        assertFalse(Sentry.isEnabled())
        providerInfo.authority = AUTHORITY

        val mockContext: Context = mock()
        val metaData = Bundle()
        mockMetaData(mockContext, metaData)

        metaData.putBoolean(ManifestMetadataReader.AUTO_INIT, false)

        sentryInitProvider.attachInfo(mockContext, providerInfo)

        assertFalse(Sentry.isEnabled())
    }

    private fun mockMetaData(mockContext: Context, metaData: Bundle) {
        val mockPackageManager: PackageManager = mock()
        val mockApplicationInfo: ApplicationInfo = mock()

        whenever(mockContext.packageName).thenReturn("io.sentry.sample.test")
        whenever(mockContext.packageManager).thenReturn(mockPackageManager)
        whenever(mockPackageManager.getApplicationInfo(mockContext.packageName, PackageManager.GET_META_DATA)).thenReturn(mockApplicationInfo)

        mockApplicationInfo.metaData = metaData
    }

    companion object {
        private const val AUTHORITY = "io.sentry.sample.SentryInitProvider"
    }
}
{'fraction_non_alphanumeric': 0.08431140303650264, 'fraction_numerical': 0.0017228383762248303, 'mean_word_length': 3.497820823244552, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2501108', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2518, 'n_words': 546}