IPv4ConverterLookup extends StrLookup { @Override public String lookup(String key) { StringTokenizer tokenizer = new StringTokenizer(key, "."); if(tokenizer.countTokens() != 4) { log.error("Invalid IPv4 address passed: " + key); throw new IllegalArgumentException("Invalid IPv4 address passed: " + key); } long ipAddr = 0; long[] parts = new long[4]; try { for(int i=0; i<4; i++) { parts[i] = Integer.parseInt(tokenizer.nextToken()); if(parts[i] < 0 || parts[i] > 255) throw new IllegalArgumentException("Invalid IPv4 address passed: " + key); } } catch(NumberFormatException e) { log.error("Invalid IPv4 address passed: " + key); throw new IllegalArgumentException("Invalid IPv4 address passed: " + key, e); } for(int i=0; i<4; i++) ipAddr |= parts[i] << ((3-i)*8); return Long.toString(ipAddr); }  @Override String lookup(String key);  }
@Test public void testGoodIpv4Address() { String value = lookup.lookup("192.168.4.1"); Assert.assertEquals("3232236545", value); }
@Test(expected=IllegalArgumentException.class) public void testTooLittleParts() { lookup.lookup("192.168.4,1"); }
@Test(expected=IllegalArgumentException.class) public void testPartTooLarge() { lookup.lookup("192.168.256.1"); }