Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@maskedronin_jp

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

More than 1 year has passed since last update.

テストで解決。

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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
maskedronin_jp
Android
a10lab
三日坊主防止アプリ「みんチャレ」の運用会社エーテンラボ株式会社です

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?