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

Code before:
        for (Map.Entry<byte[], Integer> c : cases.entrySet()) {
            assertEquals(c.getValue().intValue(), murmur2(c.getKey()));
        }
    }

    private static String toHexString(byte[] buf) {
        StringBuilder bld = new StringBuilder();
        for (byte b : buf) {
            bld.append(String.format("%02x", b));
        }
        return bld.toString();
    }

    @Test
    public void testMurmur2Checksum() throws NoSuchAlgorithmException {
        // calculates the checksum of hashes of many different random byte arrays of variable length
        // this test detects any incompatible changes to the Murmur2 implementation with near certainty
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        int numTrials = 100;
        int maxLen = 1000;
        SplittableRandom random = new SplittableRandom(0xbd4458b165652255L);

        for (int len = 0; len <= maxLen; ++len) {
            byte[] data = new byte[len];
            for (int i = 0; i < numTrials; ++i) {
                random.nextBytes(data);
                int hash = Utils.murmur2(data);
                md.update((byte) (hash >>> 0));
                md.update((byte) (hash >>> 8));
                md.update((byte) (hash >>> 16));
                md.update((byte) (hash >>> 24));
            }
        }

        assertEquals(toHexString(md.digest()), "647f78f4a7942bf4d23d2f226458809dda799795513a0532a1ed999014f5f113");
    }

    @ParameterizedTest
    @CsvSource(value = {"PLAINTEXT", "SASL_PLAINTEXT", "SSL", "SASL_SSL"})
    public void testGetHostValid(String protocol) {
        assertEquals("mydomain.com", getHost(protocol + "://mydomain.com:8080"));
        assertEquals("MyDomain.com", getHost(protocol + "://MyDomain.com:8080"));
        assertEquals("My_Domain.com", getHost(protocol + "://My_Domain.com:8080"));
        assertEquals("::1", getHost(protocol + "://[::1]:1234"));
        assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", getHost(protocol + "://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:5678"));
        assertEquals("2001:DB8:85A3:8D3:1319:8A2E:370:7348", getHost(protocol + "://[2001:DB8:85A3:8D3:1319:8A2E:370:7348]:5678"));
        assertEquals("fe80::b1da:69ca:57f7:63d8%3", getHost(protocol + "://[fe80::b1da:69ca:57f7:63d8%3]:5678"));
        assertEquals("127.0.0.1", getHost("127.0.0.1:8000"));
        assertEquals("::1", getHost("[::1]:1234"));
    }


Review comment (refactor):
Can we just add all the `hash` values into a `long sum` variable to simplify the logic a bit?

Code after:
        }
    }

    private static String toHexString(byte[] buf) {
        StringBuilder bld = new StringBuilder();
        for (byte b : buf) {
            bld.append(String.format("%02x", b));
        }
        return bld.toString();
    }

    @Test
    public void testMurmur2Checksum() {
        // calculates the checksum of hashes of many different random byte arrays of variable length
        // this test detects any incompatible changes to the Murmur2 implementation with near certainty
        int numTrials = 100;
        int maxLen = 1000;
        long seed = 0;
        SplittableRandom random = new SplittableRandom(seed);
        long checksum = 0;

        for (int len = 0; len <= maxLen; ++len) {
            byte[] data = new byte[len];
            for (int i = 0; i < numTrials; ++i) {
                random.nextBytes(data);
                int hash = Utils.murmur2(data);
                checksum += Integer.toUnsignedLong(hash);
            }
        }

        assertEquals(0xc3b8cf7c99fcL, checksum);
    }

    @ParameterizedTest
    @CsvSource(value = {"PLAINTEXT", "SASL_PLAINTEXT", "SSL", "SASL_SSL"})
    public void testGetHostValid(String protocol) {
        assertEquals("mydomain.com", getHost(protocol + "://mydomain.com:8080"));
        assertEquals("MyDomain.com", getHost(protocol + "://MyDomain.com:8080"));
        assertEquals("My_Domain.com", getHost(protocol + "://My_Domain.com:8080"));
        assertEquals("::1", getHost(protocol + "://[::1]:1234"));
        assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", getHost(protocol + "://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:5678"));
        assertEquals("2001:DB8:85A3:8D3:1319:8A2E:370:7348", getHost(protocol + "://[2001:DB8:85A3:8D3:1319:8A2E:370:7348]:5678"));
        assertEquals("fe80::b1da:69ca:57f7:63d8%3", getHost(protocol + "://[fe80::b1da:69ca:57f7:63d8%3]:5678"));
        assertEquals("127.0.0.1", getHost("127.0.0.1:8000"));
        assertEquals("::1", getHost("[::1]:1234"));
    }

    @ParameterizedTest
    @CsvSource(value = {"PLAINTEXT", "SASL_PLAINTEXT", "SSL", "SASL_SSL"})
    public void testGetHostInvalid(String protocol) {
        assertNull(getHost(protocol + "://mydo)main.com:8080"));