View Javadoc

1   package com.atlassian.vcache.internal.test;
2   
3   import com.atlassian.vcache.RequestCache;
4   import com.atlassian.vcache.VCache;
5   import org.hamcrest.Matchers;
6   import org.junit.Test;
7   
8   import java.util.stream.Collectors;
9   
10  import static org.junit.Assert.assertThat;
11  
12  /**
13   * Base test class for the {@link RequestCache}.
14   */
15  public abstract class AbstractRequestCacheTest extends AbstractLocalCacheOperationsTest {
16      @Override
17      protected abstract <K, V> RequestCache<K, V> createCache(String name);
18  
19      @Test
20      public void testGetName() throws Exception {
21          final VCache namedCache = createCache("netflix");
22          assertThat(namedCache.getName(), Matchers.is("netflix"));
23      }
24  
25      @Test
26      public void detect_illegal_recursion_supplier() {
27          thrown.expect(IllegalStateException.class);
28          thrown.expectMessage("Recursive call with key: first");
29  
30          final RequestCache<String, String> cache = createCache("abc");
31  
32          cache.get("first", () ->
33                  cache.get("first", () -> "fail"));
34      }
35  
36      @Test
37      public void detect_illegal_recursion_factory() {
38          final RequestCache<String, String> cache = createCache("abc");
39  
40          thrown.expect(IllegalStateException.class);
41          thrown.expectMessage("Recursive call with key: first");
42  
43          cache.getBulk(
44                  strings1 -> {
45                      return cache.getBulk(strings2 -> {
46                          return strings2.stream().collect(Collectors.toMap(k -> k, k -> k + "-2"));
47                      }, strings1);
48                  },
49                  "first");
50      }
51  }