0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Remote Configのデフォルト値を追加し忘れる問題

Last updated at Posted at 2019-10-22

テストで解決。

import android.content.Context
import android.content.res.XmlResourceParser
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.xmlpull.v1.XmlPullParser

@RunWith(AndroidJUnit4::class)
class RemoteConfigTest {
    private lateinit var defaultXmlParser: XmlResourceParser

    @Before
    fun setUp() {
        val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
        defaultXmlParser = context.resources.getXml(R.xml.remote_config_defaults)
    }

    @Test
    fun assertAllKeysHaveDefaultValues() {
        // デフォルト値が定義されているkeyを取り出す
        val keysWithDefaults: MutableSet<String> = mutableSetOf()
        while (defaultXmlParser.eventType != XmlPullParser.END_DOCUMENT) {
            if (defaultXmlParser.eventType == XmlPullParser.START_TAG && defaultXmlParser.name == "key") {
                defaultXmlParser.next()

                keysWithDefaults.add(defaultXmlParser.text)
            }

            defaultXmlParser.next()
        }

        // デフォルト値が定義されていないkeyを取り出す
        val keysWithoutDefaults: Set<String> = RemoteConfigManager.keys() - keysWithDefaults

        // デフォルト値が定義されていないkeyがなければ終了
        if (keysWithoutDefaults.isEmpty()) return

        throw IllegalStateException(
            """keyに対応するデフォルト値が存在しない:${keysWithoutDefaults.joinToString(separator = ", ")}"""
        )
    }
}

XmlPullParserの代わりにXPathを使うともっとすっきり書けるかもしれない。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?