### Question:
IPSectionKeyGenerator implements KeyGenerator { static void initWorkerId() { InetAddress address; try { address = InetAddress.getLocalHost(); } catch (final UnknownHostException e) { throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!"); } byte[] ipAddressByteArray = address.getAddress(); long workerId = 0L; if (ipAddressByteArray.length == 4) { for (byte byteNum : ipAddressByteArray) { workerId += byteNum & 0xFF; } } else if (ipAddressByteArray.length == 16) { for (byte byteNum : ipAddressByteArray) { workerId += byteNum & 0B111111; } } else { throw new IllegalStateException("Bad LocalHost InetAddress, please check your network!"); } DefaultKeyGenerator.setWorkerId(workerId); }  @Override Number generateKey();  }### Answer:
@Test public void testIPV4() throws UnknownHostException, NoSuchFieldException, IllegalAccessException { PowerMockito.mockStatic(InetAddress.class); PowerMockito.when(InetAddress.getLocalHost()).thenReturn(ipv4Address); IPSectionKeyGenerator.initWorkerId(); Field workerIdField = DefaultKeyGenerator.class.getDeclaredField("workerId"); workerIdField.setAccessible(true); assertThat(workerIdField.getLong(DefaultKeyGenerator.class), is(362L)); }
@Test public void testIPV6() throws UnknownHostException, NoSuchFieldException, IllegalAccessException { PowerMockito.mockStatic(InetAddress.class); PowerMockito.when(InetAddress.getLocalHost()).thenReturn(ipv6Address); IPSectionKeyGenerator.initWorkerId(); Field workerIdField = DefaultKeyGenerator.class.getDeclaredField("workerId"); workerIdField.setAccessible(true); assertThat(workerIdField.getLong(DefaultKeyGenerator.class), is(1008L)); }
@Test public void testUnknownHost() throws UnknownHostException { PowerMockito.mockStatic(InetAddress.class); PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException()); exception.expect(IllegalStateException.class); exception.expectMessage("Cannot get LocalHost InetAddress, please check your network!"); IPSectionKeyGenerator.initWorkerId(); }