### Question:
AddressUtil { public static int getAddressCacheNegativeTtl(UnknownHostException originalException) { int negativeTtl = 10; try { negativeTtl = Integer.parseInt(Security.getProperty("networkaddress.cache.negative.ttl")); } catch (NumberFormatException exception) { log.warn("Failed to get JVM negative DNS respones cache TTL due to format problem " + "(e.g. this JVM might not have the property). " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } catch (SecurityException exception) { log.warn("Failed to get JVM negative DNS response cache TTL due to security manager. " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } if (negativeTtl == -1) { log.error( "JVM negative DNS repsonse cache TTL is set to 'forever' and host lookup failed. " + "TTL can be changed with security property " + "'networkaddress.cache.negative.ttl', see java.net.InetAddress.", originalException); throw new IllegalArgumentException(originalException); } else if (negativeTtl < 0) { log.warn("JVM specified negative DNS response cache TTL was negative (and not 'forever'). " + "Falling back to default based on Oracle JVM 1.4+ (10s)"); negativeTtl = 10; } return negativeTtl; }  static HostAndPort parseAddress(String address, boolean ignoreMissingPort); static HostAndPort parseAddress(String address, int defaultPort); static int getAddressCacheNegativeTtl(UnknownHostException originalException);  }### Answer:
@Test public void testGetNegativeTtl() { log.info("Checking that we can get the ttl on dns failures."); int expectedTtl = 20; boolean expectException = false; try { Security.setProperty("networkaddress.cache.negative.ttl", Integer.toString(expectedTtl)); } catch (SecurityException exception) { log.warn( "We can't set the DNS cache period, so we're only testing fetching the system value."); expectedTtl = 10; } try { expectedTtl = Integer.parseInt(Security.getProperty("networkaddress.cache.negative.ttl")); } catch (SecurityException exception) { log.debug("Security manager won't let us fetch the property, testing default path."); expectedTtl = 10; } catch (NumberFormatException exception) { log.debug("property isn't a number, testing default path."); expectedTtl = 10; } if (-1 == expectedTtl) { log.debug("property is set to 'forever', testing exception path"); expectException = true; } if (0 > expectedTtl) { log.debug("property is a negative value other than 'forever', testing default path."); expectedTtl = 10; } try { if (expectException) { log.info("AddressUtil is (hopefully) going to spit out an error about DNS lookups. " + "you can ignore it."); } int result = AddressUtil.getAddressCacheNegativeTtl(null); if (expectException) { fail("The JVM Security settings cache DNS failures forever. " + "In this case we expect an exception but didn't get one."); } assertEquals("Didn't get the ttl we expected", expectedTtl, result); } catch (IllegalArgumentException exception) { if (!expectException) { log.error("Got an exception when we weren't expecting.", exception); fail("We only expect to throw an IllegalArgumentException when the JVM " + "caches DNS failures forever."); } } }
@Test public void testGetNegativeTtlThrowsOnForever() { log.info("When DNS is cached forever, we should throw."); try { Security.setProperty("networkaddress.cache.negative.ttl", "-1"); } catch (SecurityException exception) { log.error("We can't set the DNS cache period, so this test is effectively ignored."); return; } try { log.info("AddressUtil is (hopefully) going to spit out an error about DNS lookups. " + "you can ignore it."); AddressUtil.getAddressCacheNegativeTtl(null); fail("The JVM Security settings cache DNS failures forever, this should cause an exception."); } catch (IllegalArgumentException exception) { } }