Repository: apache/kafka
File: clients/src/test/java/org/apache/kafka/clients/consumer/ConsumerConfigTest.java (Java)

Code before:
    }

    private void testUnsupportedConfigsWithConsumerGroupProtocol(String configName, Object value) {
        final Map<String, Object> configs = Map.of(
                ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, keyDeserializerClass,
                ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueDeserializerClass,
                ConsumerConfig.GROUP_PROTOCOL_CONFIG, GroupProtocol.CONSUMER.name(),
                configName, value
        );
        ConfigException exception = assertThrows(ConfigException.class, () -> new ConsumerConfig(configs));
        assertEquals(configName + " cannot be set when " + 
                ConsumerConfig.GROUP_PROTOCOL_CONFIG + "=" + GroupProtocol.CONSUMER.name(), exception.getMessage());
    }

    /**
     * Validates config/consumer.properties file to avoid getting out of sync with ConsumerConfig.
     */
    @Test
    public void testValidateConfigPropertiesFile() throws FileNotFoundException {
        Properties props = new Properties();
        InputStream inputStream = new FileInputStream(
                System.getProperty("user.dir") + "/../config/consumer.properties");

        try {
            props.load(inputStream);
            inputStream.close();
        } catch (IOException e) {
            fail("Failed to load config/consumer.properties file: " + e.getMessage());
        }

        ConsumerConfig config = new ConsumerConfig(props);

        for (String key : config.originals().keySet()) {
            if (!ConsumerConfig.configDef().configKeys().containsKey(key)) {
                fail("Invalid configuration key: " + key);
            }
        }
    }
}


Review comment (suggestion):
We should put the inputStream.close in the `finally` block. Same as producerConfigTest

Code after:
    private void testUnsupportedConfigsWithConsumerGroupProtocol(String configName, Object value) {
        final Map<String, Object> configs = Map.of(
                ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, keyDeserializerClass,
                ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueDeserializerClass,
                ConsumerConfig.GROUP_PROTOCOL_CONFIG, GroupProtocol.CONSUMER.name(),
                configName, value
        );
        ConfigException exception = assertThrows(ConfigException.class, () -> new ConsumerConfig(configs));
        assertEquals(configName + " cannot be set when " + 
                ConsumerConfig.GROUP_PROTOCOL_CONFIG + "=" + GroupProtocol.CONSUMER.name(), exception.getMessage());
    }

    /**
     * Validates config/consumer.properties file to avoid getting out of sync with ConsumerConfig.
     */
    @Test
    public void testValidateConfigPropertiesFile() {
        Properties props = new Properties();

        try (InputStream inputStream = new FileInputStream(System.getProperty("user.dir") + "/../config/consumer.properties")) {
            props.load(inputStream);
        } catch (Exception e) {
            fail("Failed to load config/consumer.properties file: " + e.getMessage());
        }

        ConsumerConfig config = new ConsumerConfig(props);

        for (String key : config.originals().keySet()) {
            if (!ConsumerConfig.configDef().configKeys().containsKey(key)) {
                fail("Invalid configuration key: " + key);
            }
        }
    }
}