Repository: apache/kafka
File: clients/src/test/java/org/apache/kafka/common/utils/UtilsTest.java (Java)

Code before:
        props.put("key2", "value2");

        Map<String, Object> expectedMap = new HashMap<>();
        expectedMap.put("key1", "value1");
        expectedMap.put("key2", "value2");

        assertEquals(expectedMap, Utils.castToStringObjectMap(props));
        assertEquals(Utils.propsToMap(props), Utils.castToStringObjectMap(props));
    }

    @Test
    public void testCastToStringObjectMapPropertiesNonStringKey() {
        ConfigException ce = assertThrows(ConfigException.class, () -> {
            Properties props = new Properties();
            props.put(1, "value");
            Utils.castToStringObjectMap(props);
        });
        assertTrue(ce.getMessage().contains("One or more keys is not a string."));

        ce = assertThrows(ConfigException.class, () -> {
            Properties props = new Properties();
            props.put(true, "value");
            props.put('a', "value");
            Utils.castToStringObjectMap(props);
        });
        assertTrue(ce.getMessage().contains("One or more keys is not a string."));
    }

    @Test
    public void testCloseAllQuietly() {
        AtomicReference<Throwable> exception = new AtomicReference<>();
        String msg = "you should fail";
        AtomicInteger count = new AtomicInteger(0);
        AutoCloseable c0 = () -> {
            throw new RuntimeException(msg);
        };
        AutoCloseable c1 = count::incrementAndGet;
        Utils.closeAllQuietly(exception, "test", Stream.of(c0, c1).toArray(AutoCloseable[]::new));
        assertEquals(msg, exception.get().getMessage());
        assertEquals(1, count.get());
    }

    @Test
    public void shouldAcceptValidDateFormats() throws ParseException {
        //check valid formats
        invokeGetDateTimeMethod(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"));
        invokeGetDateTimeMethod(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
        invokeGetDateTimeMethod(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"));
        invokeGetDateTimeMethod(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX"));
        invokeGetDateTimeMethod(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
    }

Review comment (suggestion):
If it possible, we can use `assertEquals("One or more keys is not a string.", ce.getMessage());`
and then we can get more accurate exception message but I am fine with current version.

Code after:
        props.put("key2", "value2");

        Map<String, Object> expectedMap = new HashMap<>();
        expectedMap.put("key1", "value1");
        expectedMap.put("key2", "value2");

        assertEquals(expectedMap, Utils.castToStringObjectMap(props));
        assertEquals(Utils.propsToMap(props), Utils.castToStringObjectMap(props));
    }

    @Test
    public void testCastToStringObjectMapPropertiesNonStringKey() {
        ConfigException ce = assertThrows(ConfigException.class, () -> {
            Properties props = new Properties();
            props.put(1, "value");
            Utils.castToStringObjectMap(props);
        });
        assertEquals("One or more keys is not a string.", ce.getMessage());

        ce = assertThrows(ConfigException.class, () -> {
            Properties props = new Properties();
            props.put(true, "value");
            props.put('a', "value");
            Utils.castToStringObjectMap(props);
        });
        assertEquals("One or more keys is not a string.", ce.getMessage());
    }

    @Test
    public void testCastToStringObjectMapPropertiesWithDefaults() {
        Properties defaultProperties = new Properties();
        defaultProperties.setProperty("DefaultKey1", "DefaultValue1");
        defaultProperties.setProperty("DefaultKey2", "DefaultValue2");

        Properties actualProperties = new Properties(defaultProperties);
        actualProperties.setProperty("ActualKey1", "ActualValue1");
        actualProperties.setProperty("ActualKey2", "ActualValue2");

        Map<String, Object> expectedMap = new HashMap<>();
        expectedMap.put("DefaultKey1", "DefaultValue1");
        expectedMap.put("DefaultKey2", "DefaultValue2");
        expectedMap.put("ActualKey1", "ActualValue1");
        expectedMap.put("ActualKey2", "ActualValue2");

        assertEquals(expectedMap, Utils.castToStringObjectMap(actualProperties));
    }

    @Test
    public void testCastToStringObjectMapPropertiesWithDefaultsAndSameKey() {
        Properties defaultProperties = new Properties();
        defaultProperties.setProperty("DefaultKey1", "DefaultValue1");