Guest User

Untitled

a guest
Feb 20th, 2017
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1. import static org.assertj.core.api.Assertions.assertThat;
  2. import static org.mockito.Mockito.times;
  3. import static org.mockito.Mockito.verify;
  4. import static org.mockito.Mockito.verifyNoMoreInteractions;
  5. import static org.mockito.Mockito.when;
  6.  
  7. import java.util.List;
  8.  
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.boot.test.context.SpringBootTest;
  13. import org.springframework.boot.test.mock.mockito.MockBean;
  14. import org.springframework.cache.Cache;
  15. import org.springframework.cache.CacheManager;
  16. import org.springframework.cache.annotation.CacheEvict;
  17. import org.springframework.cache.annotation.Cacheable;
  18. import org.springframework.cache.annotation.EnableCaching;
  19. import org.springframework.cache.guava.GuavaCache;
  20. import org.springframework.cache.support.SimpleCacheManager;
  21. import org.springframework.context.annotation.Bean;
  22. import org.springframework.context.annotation.Configuration;
  23. import org.springframework.context.annotation.Import;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.test.annotation.DirtiesContext;
  26. import org.springframework.test.context.junit4.SpringRunner;
  27.  
  28. import com.google.common.cache.CacheBuilder;
  29.  
  30. @RunWith(SpringRunner.class)
  31. @SpringBootTest(classes = { IndexServiceTest.IndexCacheConfiguration.class, })
  32. @DirtiesContext
  33. public class IndexServiceTest {
  34.    
  35.     public static class Index {
  36.        
  37.         private final long timestamp;
  38.  
  39.         public Index(long timestamp) {
  40.             this.timestamp = timestamp;
  41.         }
  42.  
  43.         public long getTimestamp() {
  44.             return timestamp;
  45.         }
  46.     }
  47.  
  48.     public interface IndexCalculator {
  49.         public Index calculateIndex();
  50.  
  51.         public long getCurrentTimestamp();
  52.     }
  53.    
  54.     @Service
  55.     public static class IndexService {
  56.         @Autowired
  57.         private IndexCalculator indexCalculator;
  58.  
  59.         @Cacheable(cacheNames = "index")
  60.         @CacheEvict(cacheNames = "index", condition = "target.isObsolete(#result)")
  61.         public Index getIndex() {
  62.             System.out.println("calculating index");
  63.             return indexCalculator.calculateIndex();
  64.         }
  65.        
  66.         public boolean isObsolete(Index index) {
  67.             long indexTimestamp = index.getTimestamp();
  68.             long currentTimestamp = indexCalculator.getCurrentTimestamp();
  69.             System.out.println("indexTimestamp=" + indexTimestamp);
  70.             System.out.println("currentTimestamp=" + indexTimestamp);
  71.             if (index == null || indexTimestamp < currentTimestamp) {
  72.                 System.out.println("evicting");
  73.                 return true;
  74.             } else {
  75.                 return false;
  76.             }
  77.         }
  78.     }
  79.  
  80.     @Configuration
  81.     @EnableCaching
  82.     @Import({ IndexService.class })
  83.     public static class IndexCacheConfiguration {
  84.  
  85.         @Bean
  86.         public Cache indexCache() {
  87.             return new GuavaCache("index", CacheBuilder.newBuilder().build());
  88.         }
  89.  
  90.         @Bean
  91.         public CacheManager indexCacheManager(List<Cache> caches) {
  92.             SimpleCacheManager cacheManager = new SimpleCacheManager();
  93.             cacheManager.setCaches(caches);
  94.             return cacheManager;
  95.         }
  96.     }
  97.  
  98.     @MockBean
  99.     private IndexCalculator indexCalculator;
  100.  
  101.     @Autowired
  102.     private IndexService indexService;
  103.  
  104.     @Test
  105.     public void test() {
  106.         final Index index100 = new Index(100);
  107.         final Index index200 = new Index(200);
  108.  
  109.         when(indexCalculator.calculateIndex()).thenReturn(index100);
  110.         when(indexCalculator.getCurrentTimestamp()).thenReturn(100L);
  111.         assertThat(indexService.getIndex()).isSameAs(index100);
  112.         verify(indexCalculator).calculateIndex();
  113.         verify(indexCalculator).getCurrentTimestamp();
  114.  
  115.         when(indexCalculator.getCurrentTimestamp()).thenReturn(200L);
  116.         when(indexCalculator.calculateIndex()).thenReturn(index200);
  117.         assertThat(indexService.getIndex()).isSameAs(index100);
  118.         verify(indexCalculator, times(2)).getCurrentTimestamp();
  119.  
  120.         assertThat(indexService.getIndex()).isSameAs(index200);
  121.         verify(indexCalculator, times(2)).calculateIndex();
  122.         verify(indexCalculator, times(3)).getCurrentTimestamp();
  123.         verifyNoMoreInteractions(indexCalculator);
  124.     }
  125. }
Add Comment
Please, Sign In to add comment