@Test public void testCreateCommandThrowsInvalidPacketSizeException() { LocalPingInputs localPingInputs = new LocalPingInputs.LocalPingInputsBuilder() .targetHost("10.0.0.1") .packetSize("invalid") .build(); expectedException.expect(RuntimeException.class); expectedException.expectMessage(PACKET_SIZE_SHOULD_HAVE_A_NUMERIC_VALUE); new LinuxPingCommand().createCommand(localPingInputs); }
@Override public String createCommand(LocalPingInputs localPingInputs) { StringBuilder command = new StringBuilder(); final String targetHost = localPingInputs.getTargetHost(); final String ipVersion = localPingInputs.getIpVersion(); if (isEmpty(ipVersion)) { if (InetAddressValidator.getInstance().isValidInet6Address(targetHost)) { command.append("ping6 "); } else { command.append("ping "); } } else { if (ipVersion.equals(IP_VERSION_6)) { command.append("ping6 "); } else if (ipVersion.equals(IP_VERSION_4)) { command.append("ping "); } else { throw new IllegalArgumentException(format(INVALID_ARGUMENT_IP_VERSION, ipVersion)); } } final String timeout = localPingInputs.getTimeout(); if (isNotEmpty(timeout)) { if (!isValidLong(timeout)) { throw new RuntimeException(TIMEOUT_SHOULD_HAVE_A_NUMERIC_VALUE); } Long timeoutValue = parseLong(timeout) / 1000; command.append(format("-w %s ", valueOf(timeoutValue))); } final String packetCount = localPingInputs.getPacketCount(); if (isNotEmpty(packetCount)) { if (!isValidLong(packetCount)) { throw new RuntimeException(PACKET_COUNT_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-c %s ", packetCount)); } else { command.append(format("-c %s ", DEFAULT_PACKET_COUNT)); } final String packetSize = localPingInputs.getPacketSize(); if (isNotEmpty(packetSize)) { if (!isValidInt(packetSize)) { throw new RuntimeException(PACKET_SIZE_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-s %s ", packetSize)); } command.append(localPingInputs.getTargetHost()); return command.toString(); }
LinuxPingCommand implements LocalPingCommand { @Override public String createCommand(LocalPingInputs localPingInputs) { StringBuilder command = new StringBuilder(); final String targetHost = localPingInputs.getTargetHost(); final String ipVersion = localPingInputs.getIpVersion(); if (isEmpty(ipVersion)) { if (InetAddressValidator.getInstance().isValidInet6Address(targetHost)) { command.append("ping6 "); } else { command.append("ping "); } } else { if (ipVersion.equals(IP_VERSION_6)) { command.append("ping6 "); } else if (ipVersion.equals(IP_VERSION_4)) { command.append("ping "); } else { throw new IllegalArgumentException(format(INVALID_ARGUMENT_IP_VERSION, ipVersion)); } } final String timeout = localPingInputs.getTimeout(); if (isNotEmpty(timeout)) { if (!isValidLong(timeout)) { throw new RuntimeException(TIMEOUT_SHOULD_HAVE_A_NUMERIC_VALUE); } Long timeoutValue = parseLong(timeout) / 1000; command.append(format("-w %s ", valueOf(timeoutValue))); } final String packetCount = localPingInputs.getPacketCount(); if (isNotEmpty(packetCount)) { if (!isValidLong(packetCount)) { throw new RuntimeException(PACKET_COUNT_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-c %s ", packetCount)); } else { command.append(format("-c %s ", DEFAULT_PACKET_COUNT)); } final String packetSize = localPingInputs.getPacketSize(); if (isNotEmpty(packetSize)) { if (!isValidInt(packetSize)) { throw new RuntimeException(PACKET_SIZE_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-s %s ", packetSize)); } command.append(localPingInputs.getTargetHost()); return command.toString(); } }
LinuxPingCommand implements LocalPingCommand { @Override public String createCommand(LocalPingInputs localPingInputs) { StringBuilder command = new StringBuilder(); final String targetHost = localPingInputs.getTargetHost(); final String ipVersion = localPingInputs.getIpVersion(); if (isEmpty(ipVersion)) { if (InetAddressValidator.getInstance().isValidInet6Address(targetHost)) { command.append("ping6 "); } else { command.append("ping "); } } else { if (ipVersion.equals(IP_VERSION_6)) { command.append("ping6 "); } else if (ipVersion.equals(IP_VERSION_4)) { command.append("ping "); } else { throw new IllegalArgumentException(format(INVALID_ARGUMENT_IP_VERSION, ipVersion)); } } final String timeout = localPingInputs.getTimeout(); if (isNotEmpty(timeout)) { if (!isValidLong(timeout)) { throw new RuntimeException(TIMEOUT_SHOULD_HAVE_A_NUMERIC_VALUE); } Long timeoutValue = parseLong(timeout) / 1000; command.append(format("-w %s ", valueOf(timeoutValue))); } final String packetCount = localPingInputs.getPacketCount(); if (isNotEmpty(packetCount)) { if (!isValidLong(packetCount)) { throw new RuntimeException(PACKET_COUNT_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-c %s ", packetCount)); } else { command.append(format("-c %s ", DEFAULT_PACKET_COUNT)); } final String packetSize = localPingInputs.getPacketSize(); if (isNotEmpty(packetSize)) { if (!isValidInt(packetSize)) { throw new RuntimeException(PACKET_SIZE_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-s %s ", packetSize)); } command.append(localPingInputs.getTargetHost()); return command.toString(); }  }
LinuxPingCommand implements LocalPingCommand { @Override public String createCommand(LocalPingInputs localPingInputs) { StringBuilder command = new StringBuilder(); final String targetHost = localPingInputs.getTargetHost(); final String ipVersion = localPingInputs.getIpVersion(); if (isEmpty(ipVersion)) { if (InetAddressValidator.getInstance().isValidInet6Address(targetHost)) { command.append("ping6 "); } else { command.append("ping "); } } else { if (ipVersion.equals(IP_VERSION_6)) { command.append("ping6 "); } else if (ipVersion.equals(IP_VERSION_4)) { command.append("ping "); } else { throw new IllegalArgumentException(format(INVALID_ARGUMENT_IP_VERSION, ipVersion)); } } final String timeout = localPingInputs.getTimeout(); if (isNotEmpty(timeout)) { if (!isValidLong(timeout)) { throw new RuntimeException(TIMEOUT_SHOULD_HAVE_A_NUMERIC_VALUE); } Long timeoutValue = parseLong(timeout) / 1000; command.append(format("-w %s ", valueOf(timeoutValue))); } final String packetCount = localPingInputs.getPacketCount(); if (isNotEmpty(packetCount)) { if (!isValidLong(packetCount)) { throw new RuntimeException(PACKET_COUNT_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-c %s ", packetCount)); } else { command.append(format("-c %s ", DEFAULT_PACKET_COUNT)); } final String packetSize = localPingInputs.getPacketSize(); if (isNotEmpty(packetSize)) { if (!isValidInt(packetSize)) { throw new RuntimeException(PACKET_SIZE_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-s %s ", packetSize)); } command.append(localPingInputs.getTargetHost()); return command.toString(); }  @Override String createCommand(LocalPingInputs localPingInputs); @Override Map<String, String> parseOutput(String output); }
LinuxPingCommand implements LocalPingCommand { @Override public String createCommand(LocalPingInputs localPingInputs) { StringBuilder command = new StringBuilder(); final String targetHost = localPingInputs.getTargetHost(); final String ipVersion = localPingInputs.getIpVersion(); if (isEmpty(ipVersion)) { if (InetAddressValidator.getInstance().isValidInet6Address(targetHost)) { command.append("ping6 "); } else { command.append("ping "); } } else { if (ipVersion.equals(IP_VERSION_6)) { command.append("ping6 "); } else if (ipVersion.equals(IP_VERSION_4)) { command.append("ping "); } else { throw new IllegalArgumentException(format(INVALID_ARGUMENT_IP_VERSION, ipVersion)); } } final String timeout = localPingInputs.getTimeout(); if (isNotEmpty(timeout)) { if (!isValidLong(timeout)) { throw new RuntimeException(TIMEOUT_SHOULD_HAVE_A_NUMERIC_VALUE); } Long timeoutValue = parseLong(timeout) / 1000; command.append(format("-w %s ", valueOf(timeoutValue))); } final String packetCount = localPingInputs.getPacketCount(); if (isNotEmpty(packetCount)) { if (!isValidLong(packetCount)) { throw new RuntimeException(PACKET_COUNT_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-c %s ", packetCount)); } else { command.append(format("-c %s ", DEFAULT_PACKET_COUNT)); } final String packetSize = localPingInputs.getPacketSize(); if (isNotEmpty(packetSize)) { if (!isValidInt(packetSize)) { throw new RuntimeException(PACKET_SIZE_SHOULD_HAVE_A_NUMERIC_VALUE); } command.append(format("-s %s ", packetSize)); } command.append(localPingInputs.getTargetHost()); return command.toString(); }  @Override String createCommand(LocalPingInputs localPingInputs); @Override Map<String, String> parseOutput(String output);  }