Repository: apache/kafka
File: metadata/src/test/java/org/apache/kafka/metadata/storage/FormatterTest.java (Java)

Code before:
            FormatterContext formatter1 = testEnv.newFormatter();
            formatter1.formatter.setReleaseVersion(MetadataVersion.IBP_3_8_IV0);
            formatter1.formatter.setFeatureLevel("kraft.version", (short) 1);
            formatter1.formatter.setInitialControllers(DynamicVoters.
                    parse("1@localhost:8020:4znU-ou9Taa06bmEJxsjnw"));
            formatter1.formatter.setUnstableFeatureVersionsEnabled(true);
            formatter1.formatter.setHasDynamicQuorum(true);
            formatter1.formatter.run();
            assertEquals((short) 1, formatter1.formatter.featureLevels.getOrDefault("kraft.version", (short) 0));
        }
    }

    @ParameterizedTest
    @ValueSource(booleans = {false, true})
    public void testFormatWithNoInitialControllersWithOlderMetadataVersion(boolean emptyStaticVoters) throws Exception {
        try (TestEnv testEnv = new TestEnv(2)) {
            FormatterContext formatter1 = testEnv.newFormatter();
            formatter1.formatter.setReleaseVersion(MetadataVersion.IBP_3_8_IV0);
            // This MV does not support kraft.version = 1
            formatter1.formatter.setHasDynamicQuorum(emptyStaticVoters);
            formatter1.formatter.run();
            if (emptyStaticVoters) {
                assertEquals((short) 1, formatter1.formatter.featureLevels.getOrDefault("kraft.version", (short) 0));
            } else {
                assertEquals((short) 0, formatter1.formatter.featureLevels.getOrDefault("kraft.version", (short) 1));
            }
        }
    }

    private static Stream<Arguments> elrTestMetadataVersions() {
        return Stream.of(
            MetadataVersion.IBP_3_9_IV0,
            MetadataVersion.IBP_4_0_IV0,
            MetadataVersion.IBP_4_0_IV1 // ELR minimal MV
        ).map(Arguments::of);
    }

    @ParameterizedTest
    @MethodSource("elrTestMetadataVersions")
    public void testFormatElrEnabledWithMetadataVersions(MetadataVersion metadataVersion) throws Exception {
        try (TestEnv testEnv = new TestEnv(2)) {
            FormatterContext formatter1 = testEnv.newFormatter();
            formatter1.formatter.setReleaseVersion(metadataVersion);
            formatter1.formatter.setFeatureLevel(EligibleLeaderReplicasVersion.FEATURE_NAME, (short) 1);
            formatter1.formatter.setInitialControllers(DynamicVoters.
                parse("1@localhost:8020:4znU-ou9Taa06bmEJxsjnw"));
            formatter1.formatter.setHasDynamicQuorum(true);
            if (metadataVersion.isAtLeast(MetadataVersion.IBP_4_0_IV1)) {
                assertDoesNotThrow(formatter1.formatter::run);
            } else {
                assertEquals("eligible.leader.replicas.version could not be set to 1 because it depends on " +

Review comment (suggestion):
Don't you want to differentiate between missing value and the default in the test? Why not use `featureLevels.get("kraft.version")`? This comment applies to a few places in this file.

Code after:
        try (TestEnv testEnv = new TestEnv(2)) {
            FormatterContext formatter1 = testEnv.newFormatter();
            formatter1.formatter.setReleaseVersion(MetadataVersion.IBP_3_8_IV0);
            formatter1.formatter.setFeatureLevel(KRaftVersion.FEATURE_NAME, (short) 1);
            formatter1.formatter.setInitialControllers(DynamicVoters.
                    parse("1@localhost:8020:4znU-ou9Taa06bmEJxsjnw"));
            formatter1.formatter.setUnstableFeatureVersionsEnabled(true);
            formatter1.formatter.setHasDynamicQuorum(true);
            formatter1.formatter.run();
            assertEquals((short) 1, formatter1.formatter.featureLevels.get(KRaftVersion.FEATURE_NAME));
        }
    }

    @ParameterizedTest
    @ValueSource(booleans = {false, true})
    public void testFormatWithNoInitialControllersWithOlderMetadataVersion(boolean hasDynamicQuorum) throws Exception {
        try (TestEnv testEnv = new TestEnv(2)) {
            FormatterContext formatter1 = testEnv.newFormatter();
            formatter1.formatter.setReleaseVersion(MetadataVersion.IBP_3_8_IV0);
            formatter1.formatter.setHasDynamicQuorum(hasDynamicQuorum);
            formatter1.formatter.run();
            if (hasDynamicQuorum) {
                assertEquals((short) 1, formatter1.formatter.featureLevels.get(KRaftVersion.FEATURE_NAME));
            } else {
                assertEquals((short) 0, formatter1.formatter.featureLevels.get(KRaftVersion.FEATURE_NAME));
            }
        }
    }

    private static Stream<Arguments> elrTestMetadataVersions() {
        return Stream.of(
            MetadataVersion.IBP_3_9_IV0,
            MetadataVersion.IBP_4_0_IV0,
            MetadataVersion.IBP_4_0_IV1 // ELR minimal MV
        ).map(Arguments::of);
    }

    @ParameterizedTest
    @MethodSource("elrTestMetadataVersions")
    public void testFormatElrEnabledWithMetadataVersions(MetadataVersion metadataVersion) throws Exception {
        try (TestEnv testEnv = new TestEnv(2)) {
            FormatterContext formatter1 = testEnv.newFormatter();
            formatter1.formatter.setReleaseVersion(metadataVersion);
            formatter1.formatter.setFeatureLevel(EligibleLeaderReplicasVersion.FEATURE_NAME, (short) 1);
            formatter1.formatter.setInitialControllers(DynamicVoters.
                parse("1@localhost:8020:4znU-ou9Taa06bmEJxsjnw"));
            formatter1.formatter.setHasDynamicQuorum(true);
            if (metadataVersion.isAtLeast(MetadataVersion.IBP_4_0_IV1)) {
                assertDoesNotThrow(formatter1.formatter::run);
            } else {
                assertEquals("eligible.leader.replicas.version could not be set to 1 because it depends on " +