View Javadoc

1   package com.atlassian.vcache.internal.core;
2   
3   import org.junit.Rule;
4   import org.junit.Test;
5   import org.junit.rules.ExpectedException;
6   
7   import java.util.Arrays;
8   
9   @SuppressWarnings("EmptyTryBlock")
10  public class RecursionDetectorTest {
11      @Rule
12      public ExpectedException thrown = ExpectedException.none();
13  
14      @Test
15      public void guardOn_pass() {
16          final RecursionDetector<String> rd = new RecursionDetector<>();
17  
18          try (RecursionDetector.Guard<String> g1 = rd.guardOn("a")) {
19              try (RecursionDetector.Guard<String> g2 = rd.guardOn("ab")) {
20                  try (RecursionDetector.Guard<String> g3 = rd.guardOn("abc")) {
21                  }
22              }
23          }
24  
25          try (RecursionDetector.Guard<String> g1 = rd.guardOn("a")) {
26          }
27      }
28  
29      @Test
30      public void guardOn_detect() {
31          thrown.expect(IllegalStateException.class);
32          thrown.expectMessage("Recursive call with key: a");
33  
34          final RecursionDetector<String> rd = new RecursionDetector<>();
35  
36          try (RecursionDetector.Guard<String> g1 = rd.guardOn("a")) {
37              try (RecursionDetector.Guard<String> g2 = rd.guardOn("ab")) {
38                  try (RecursionDetector.Guard<String> g3 = rd.guardOn(new String("a"))) {
39                  }
40              }
41          }
42      }
43  
44      @Test
45      public void guardOn_keys_pass() throws Exception {
46          final RecursionDetector<String> rd = new RecursionDetector<>();
47  
48          try (RecursionDetector.Guard<String> g1 = rd.guardOn(Arrays.asList("a", "ab"))) {
49              try (RecursionDetector.Guard<String> g2 = rd.guardOn("abc")) {
50                  try (RecursionDetector.Guard<String> g3 = rd.guardOn(Arrays.asList("abcd", "z"))) {
51                  }
52              }
53          }
54  
55          try (RecursionDetector.Guard<String> g1 = rd.guardOn("a")) {
56          }
57  
58      }
59  
60      @Test
61      public void guardOn_keys_detect() throws Exception {
62          thrown.expect(IllegalStateException.class);
63          thrown.expectMessage("Recursive call with key: a");
64  
65          final RecursionDetector<String> rd = new RecursionDetector<>();
66  
67          try (RecursionDetector.Guard<String> g1 = rd.guardOn(Arrays.asList("a", "ab"))) {
68              try (RecursionDetector.Guard<String> g2 = rd.guardOn("abc")) {
69                  try (RecursionDetector.Guard<String> g3 = rd.guardOn(Arrays.asList("a", "z"))) {
70                  }
71              }
72          }
73      }
74  }