### Question:
CacheValidityPolicy { public boolean isResponseFresh(final HttpCacheEntry entry, final Date now) { return getCurrentAge(entry, now).compareTo(getFreshnessLifetime(entry)) == -1; } CacheValidityPolicy(); TimeValue getCurrentAge(final HttpCacheEntry entry, final Date now); TimeValue getFreshnessLifetime(final HttpCacheEntry entry); boolean isResponseFresh(final HttpCacheEntry entry, final Date now); boolean isResponseHeuristicallyFresh(final HttpCacheEntry entry,
            final Date now, final float coefficient, final TimeValue defaultLifetime); TimeValue getHeuristicFreshnessLifetime(final HttpCacheEntry entry,
            final float coefficient, final TimeValue defaultLifetime); boolean isRevalidatable(final HttpCacheEntry entry); boolean mustRevalidate(final HttpCacheEntry entry); boolean proxyRevalidate(final HttpCacheEntry entry); boolean mayReturnStaleWhileRevalidating(final HttpCacheEntry entry, final Date now); boolean mayReturnStaleIfError(final HttpRequest request, final HttpCacheEntry entry, final Date now); boolean hasCacheControlDirective(final HttpCacheEntry entry, final String directive); TimeValue getStaleness(final HttpCacheEntry entry, final Date now); static final TimeValue MAX_AGE; }### Answer:
@Test public void testResponseIsFreshIfFreshnessLifetimeExceedsCurrentAge() { final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); impl = new CacheValidityPolicy() { @Override public TimeValue getCurrentAge(final HttpCacheEntry e, final Date d) { assertSame(entry, e); assertEquals(now, d); return TimeValue.ofSeconds(6); } @Override public TimeValue getFreshnessLifetime(final HttpCacheEntry e) { assertSame(entry, e); return TimeValue.ofSeconds(10); } }; assertTrue(impl.isResponseFresh(entry, now)); }
@Test public void testResponseIsNotFreshIfFreshnessLifetimeEqualsCurrentAge() { final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); impl = new CacheValidityPolicy() { @Override public TimeValue getCurrentAge(final HttpCacheEntry e, final Date d) { assertEquals(now, d); assertSame(entry, e); return TimeValue.ofSeconds(6); } @Override public TimeValue getFreshnessLifetime(final HttpCacheEntry e) { assertSame(entry, e); return TimeValue.ofSeconds(6); } }; assertFalse(impl.isResponseFresh(entry, now)); }
@Test public void testResponseIsNotFreshIfCurrentAgeExceedsFreshnessLifetime() { final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); impl = new CacheValidityPolicy() { @Override public TimeValue getCurrentAge(final HttpCacheEntry e, final Date d) { assertEquals(now, d); assertSame(entry, e); return TimeValue.ofSeconds(10); } @Override public TimeValue getFreshnessLifetime(final HttpCacheEntry e) { assertSame(entry, e); return TimeValue.ofSeconds(6); } }; assertFalse(impl.isResponseFresh(entry, now)); }