InetAddressMatcher implements Predicate<InetAddress> { public static InetAddressMatcher forPattern(String s) throws UnknownHostException { String hostAndMask[] = s.split("/"); checkArgument(hostAndMask.length < 3, "Invalid host specification: " + s); if (!isInetAddress(hostAndMask[0])) { checkArgument (hostAndMask.length == 1, "Invalid host specification (hostname with mask): " + s); if (s.indexOf('*') != -1 || s.indexOf('?') != -1) { return new RegexpNameMatcher(toRegExp(s)); } else { return new HostNameMatcher(s); } } InetAddress net = forString(hostAndMask[0]); if (hostAndMask.length == 2) { return new IpAddressMatcher(s, net, Integer.parseInt(hostAndMask[1])); } return new IpAddressMatcher(s, net); } protected  InetAddressMatcher(String pattern); boolean match(InetAddress addr); String getPattern(); @Override int hashCode(); @Override boolean equals(Object obj); static InetAddressMatcher forPattern(String s);  }
@Test(expected = IllegalArgumentException.class) public void testInvalidSpecWithName() throws UnknownHostException { InetAddressMatcher.forPattern("www.dcache.org/24"); }
@Test(expected = IllegalArgumentException.class) public void testInvalidSpecWithTooManySlashes() throws UnknownHostException { InetAddressMatcher.forPattern("1.1.1.1/24/25"); }
@Test(expected = IllegalArgumentException.class) public void testInvalidSpecWithNegMask() throws UnknownHostException { InetAddressMatcher.forPattern("1.1.1.1/-7"); }
@Test(expected = IllegalArgumentException.class) public void testInvalidSpecWithTooBigMaskV4() throws UnknownHostException { InetAddressMatcher.forPattern("1.1.1.1/33"); }
@Test(expected = IllegalArgumentException.class) public void testInvalidSpecWithTooBigMaskV6() throws UnknownHostException { InetAddressMatcher.forPattern("fe80::3FF:F00D:BAD:F00D/129"); }
@Test public void testForIpString() throws UnknownHostException { InetAddressMatcher ipMatcher = InetAddressMatcher.forPattern("fe80::0:0:0:0/70"); assertEquals(InetAddressMatcher.IpAddressMatcher.class, ipMatcher.getClass()); }
@Test public void testForRegexpBigString() throws UnknownHostException { InetAddressMatcher ipMatcher = InetAddressMatcher.forPattern("a*.b.c"); assertEquals(InetAddressMatcher.RegexpNameMatcher.class, ipMatcher.getClass()); }
@Test public void testForRegexpSmallString() throws UnknownHostException { InetAddressMatcher ipMatcher = InetAddressMatcher.forPattern("a.b?.c"); assertEquals(InetAddressMatcher.RegexpNameMatcher.class, ipMatcher.getClass()); }
@Test public void testForHostString() throws UnknownHostException { InetAddressMatcher ipMatcher = InetAddressMatcher.forPattern("www.google.com"); assertEquals(InetAddressMatcher.HostNameMatcher.class, ipMatcher.getClass()); }
@Test public void testIpV6SuccessfulIpNetMatching() throws UnknownHostException { InetAddressMatcher ipMatcher = InetAddressMatcher.forPattern("fe80::0:0:0:0/70"); assertTrue("Failed to match host with netmask.", ipMatcher.test(forString("fe80::3FF:F00D:BAD:F00D"))); }
@Test public void testIpV4SuccessfulIpNetMatching() throws UnknownHostException { InetAddressMatcher ipMatcher = InetAddressMatcher.forPattern("1.1.1.1/16"); assertTrue("Failed to match host with netmask.", ipMatcher.test(forString("1.1.2.3"))); }