@Test public void testIPAddressFormatValidation() { assertFalse("Invalid characters in IP address", NetworkAddress.isValidIPAddress("10.10.1.a")); assertFalse("Invalid characters in IP address", NetworkAddress.isValidIPAddress("10.10.1.10, 10.10.1.10")); assertFalse("Invalid characters in IP address", NetworkAddress.isValidIPAddress("10.10.1.10 10.10.1.10")); assertFalse("Invalid characters in IP address", NetworkAddress.isValidIPAddress("10.10.1.10/10.10.1.10")); assertFalse("Out of range values in IP address", NetworkAddress.isValidIPAddress("10.10.1.-1")); assertFalse("Out of range values in IP address", NetworkAddress.isValidIPAddress("10.258.1.10")); assertFalse("Null IP address", NetworkAddress.isValidIPAddress(null)); assertFalse("Empty String IP address", NetworkAddress.isValidIPAddress("")); assertTrue("Valid IP address", NetworkAddress.isValidIPAddress("192.168.10.1")); }
public static boolean isValidIPAddress(String networkAddress) { if (networkAddress != null) { String[] split = networkAddress.split("\\."); if (split.length == 4) { int[] octets = new int[4]; for (int i=0; i<4; i++) { try { octets[i] = Integer.parseInt(split[i]); } catch (NumberFormatException e) { return false; } if (octets[i] < 0 || octets[i] > 255) { return false; } } return true; } } return false; }
NetworkAddress { public static boolean isValidIPAddress(String networkAddress) { if (networkAddress != null) { String[] split = networkAddress.split("\\."); if (split.length == 4) { int[] octets = new int[4]; for (int i=0; i<4; i++) { try { octets[i] = Integer.parseInt(split[i]); } catch (NumberFormatException e) { return false; } if (octets[i] < 0 || octets[i] > 255) { return false; } } return true; } } return false; } }
NetworkAddress { public static boolean isValidIPAddress(String networkAddress) { if (networkAddress != null) { String[] split = networkAddress.split("\\."); if (split.length == 4) { int[] octets = new int[4]; for (int i=0; i<4; i++) { try { octets[i] = Integer.parseInt(split[i]); } catch (NumberFormatException e) { return false; } if (octets[i] < 0 || octets[i] > 255) { return false; } } return true; } } return false; } private  NetworkAddress(byte[] network, byte[] netmask); }
NetworkAddress { public static boolean isValidIPAddress(String networkAddress) { if (networkAddress != null) { String[] split = networkAddress.split("\\."); if (split.length == 4) { int[] octets = new int[4]; for (int i=0; i<4; i++) { try { octets[i] = Integer.parseInt(split[i]); } catch (NumberFormatException e) { return false; } if (octets[i] < 0 || octets[i] > 255) { return false; } } return true; } } return false; } private  NetworkAddress(byte[] network, byte[] netmask); @Override String toString(); boolean isAddressInNetwork(String address); static NetworkAddress parse(String networkAddress); static NetworkAddress parseBlock(String networkAddress); static boolean isValidIPAddress(String networkAddress); }
NetworkAddress { public static boolean isValidIPAddress(String networkAddress) { if (networkAddress != null) { String[] split = networkAddress.split("\\."); if (split.length == 4) { int[] octets = new int[4]; for (int i=0; i<4; i++) { try { octets[i] = Integer.parseInt(split[i]); } catch (NumberFormatException e) { return false; } if (octets[i] < 0 || octets[i] > 255) { return false; } } return true; } } return false; } private  NetworkAddress(byte[] network, byte[] netmask); @Override String toString(); boolean isAddressInNetwork(String address); static NetworkAddress parse(String networkAddress); static NetworkAddress parseBlock(String networkAddress); static boolean isValidIPAddress(String networkAddress);  }