### Question:
CacheValidityPolicy { protected TimeValue getApparentAge(final HttpCacheEntry entry) { final Date dateValue = entry.getDate(); if (dateValue == null) { return MAX_AGE; } final long diff = entry.getResponseDate().getTime() - dateValue.getTime(); if (diff < 0L) { return TimeValue.ZERO_MILLISECONDS; } return TimeValue.ofSeconds(diff / 1000); } 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 testApparentAgeIsMaxIntIfDateHeaderNotPresent() { final Header[] headers = { new BasicHeader("Server", "MockServer/1.0") }; final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers); assertEquals(CacheValidityPolicy.MAX_AGE, impl.getApparentAge(entry)); }
@Test public void testApparentAgeIsResponseReceivedTimeLessDateHeader() { final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils .formatDate(tenSecondsAgo)) }; final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, sixSecondsAgo, headers); assertEquals(TimeValue.ofSeconds(4), impl.getApparentAge(entry)); }
@Test public void testNegativeApparentAgeIsBroughtUpToZero() { final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils .formatDate(sixSecondsAgo)) }; final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now, tenSecondsAgo, headers); assertEquals(TimeValue.ofSeconds(0), impl.getApparentAge(entry)); }