WebCache { public CacheControl getCacheControlFor( CacheStrategy cacheStrategy ) { final CacheControl cacheControl; if ( RESPECT_SYSTEM_SETTING == cacheStrategy ) { cacheStrategy = (CacheStrategy) systemSettingManager.getSystemSetting( CACHE_STRATEGY ); } final boolean cacheStrategyHasExpirationTimeSet = cacheStrategy != null && cacheStrategy != NO_CACHE; if ( cacheStrategyHasExpirationTimeSet ) { cacheControl = maxAge( cacheStrategy.toSeconds(), SECONDS ); setCacheabilityFor( cacheControl ); } else { cacheControl = noCache(); } return cacheControl; } WebCache( final SystemSettingManager systemSettingManager,
        final AnalyticsCacheSettings analyticsCacheSettings ); CacheControl getCacheControlFor( CacheStrategy cacheStrategy ); CacheControl getCacheControlFor( final Date latestEndDate ); boolean isProgressiveCachingEnabled();  }
@Test public void testGetCacheControlForWhenCacheStrategyIsNoCache() { final CacheStrategy theCacheStrategy = NO_CACHE; final CacheControl expectedCacheControl = noCache(); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetCacheControlForWhenCacheStrategyIsRespectSystemSetting() { final CacheStrategy theInputCacheStrategy = RESPECT_SYSTEM_SETTING; final CacheStrategy theCacheStrategySet = CACHE_5_MINUTES; final CacheControl expectedCacheControl = stubPublicCacheControl( theCacheStrategySet ); when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); when( systemSettingManager.getSystemSetting( CACHE_STRATEGY ) ).thenReturn( theCacheStrategySet ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theInputCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetAnalyticsCacheControlForWhenTimeToLiveIsZero() { final long zeroTimeToLive = 0; final Date aDate = new Date(); final CacheControl expectedCacheControl = noCache(); when( analyticsCacheSettings.progressiveExpirationTimeOrDefault( aDate ) ).thenReturn( zeroTimeToLive ); final CacheControl actualCacheControl = webCache.getCacheControlFor( aDate ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetAnalyticsCacheControlForWhenTimeToLiveIsNegative() { final long zeroTimeToLive = -1; final Date aDate = new Date(); final CacheControl expectedCacheControl = noCache(); when( analyticsCacheSettings.progressiveExpirationTimeOrDefault( aDate ) ).thenReturn( zeroTimeToLive ); final CacheControl actualCacheControl = webCache.getCacheControlFor( aDate ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetAnalyticsCacheControlForWhenTimeToLiveIsPositive() { final long positiveTimeToLive = 60; final Date aDate = new Date(); final CacheControl expectedCacheControl = stubPublicCacheControl ( positiveTimeToLive ); final Cacheability setCacheability = PUBLIC; when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( setCacheability ); when( analyticsCacheSettings.progressiveExpirationTimeOrDefault( aDate ) ).thenReturn( positiveTimeToLive ); final CacheControl actualCacheControl = webCache.getCacheControlFor( aDate ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test( expected = UnsupportedOperationException.class ) public void testGetCacheControlForWhenCacheStrategyIsRespectSystemSettingNotUsedInObjectBasis() { final CacheStrategy theInputCacheStrategy = RESPECT_SYSTEM_SETTING; final CacheStrategy theCacheStrategySet = RESPECT_SYSTEM_SETTING; when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); when( systemSettingManager.getSystemSetting( CACHE_STRATEGY ) ).thenReturn( theCacheStrategySet ); webCache.getCacheControlFor( theInputCacheStrategy ); fail( "Should not reach here. Exception was expected: " ); }
@Test public void testGetCacheControlForWhenCacheStrategyIsCache1Minute() { final CacheStrategy theCacheStrategy = CACHE_1_MINUTE; final CacheControl expectedCacheControl = stubPublicCacheControl( theCacheStrategy ); when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetCacheControlForWhenCacheStrategyIsCache5Minutes() { final CacheStrategy theCacheStrategy = CACHE_5_MINUTES; final CacheControl expectedCacheControl = stubPublicCacheControl( theCacheStrategy ); when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetCacheControlForWhenCacheStrategyIsCache10Minutes() { final CacheStrategy theCacheStrategy = CACHE_10_MINUTES; final CacheControl expectedCacheControl = stubPublicCacheControl( theCacheStrategy ); when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetCacheControlForWhenCacheStrategyIsCache15Minutes() { final CacheStrategy theCacheStrategy = CACHE_15_MINUTES; final CacheControl expectedCacheControl = stubPublicCacheControl( theCacheStrategy ); when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetCacheControlForWhenCacheStrategyIsCache30Minutes() { final CacheStrategy theCacheStrategy = CACHE_30_MINUTES; final CacheControl expectedCacheControl = stubPublicCacheControl( theCacheStrategy ); when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetCacheControlForWhenCacheStrategyIsCache1Hour() { final CacheStrategy theCacheStrategy = CACHE_1_HOUR; final CacheControl expectedCacheControl = stubPublicCacheControl( theCacheStrategy ); when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetCacheControlForWhenCacheStrategyIsCache2Weeks() { final CacheStrategy theCacheStrategy = CACHE_TWO_WEEKS; final CacheControl expectedCacheControl = stubPublicCacheControl( theCacheStrategy ); when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testGetCacheControlForWhenCacheStrategyIsCache6AMTomorrow() { final CacheStrategy theCacheStrategy = CACHE_6AM_TOMORROW; final CacheControl expectedCacheControl = stubPublicCacheControl( theCacheStrategy ); when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( PUBLIC ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString(), is( expectedCacheControl.toString() ) ); }
@Test public void testSetCacheabilityWhenCacheabilityIsSetToPublic() { final CacheStrategy theCacheStrategy = CACHE_5_MINUTES; final Cacheability setCacheability = PUBLIC; when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( setCacheability ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString().toLowerCase(), containsString( "public" ) ); }
@Test public void testSetCacheabilityWhenCacheabilityIsSetToPrivate() { final CacheStrategy theCacheStrategy = CACHE_5_MINUTES; final Cacheability setCacheability = PRIVATE; when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( setCacheability ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString().toLowerCase(), containsString( "private" ) ); }
@Test public void testSetCacheabilityWhenCacheabilityIsSetToNull() { final CacheStrategy theCacheStrategy = CACHE_5_MINUTES; final Cacheability nullCacheability = null; when( systemSettingManager.getSystemSetting( CACHEABILITY ) ).thenReturn( nullCacheability ); final CacheControl actualCacheControl = webCache.getCacheControlFor( theCacheStrategy ); assertThat( actualCacheControl.toString().toLowerCase(), not( containsString( "private" ) ) ); assertThat( actualCacheControl.toString().toLowerCase(), not( containsString( "public" ) ) ); }