1 package com.atlassian.vcache.internal.core;
2
3 import com.atlassian.vcache.internal.RequestContext;
4 import org.junit.Rule;
5 import org.junit.Test;
6 import org.junit.rules.ExpectedException;
7
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import static org.hamcrest.Matchers.is;
12 import static org.junit.Assert.assertThat;
13
14 public class DefaultRequestContextTest {
15 @Rule
16 public ExpectedException thrown = ExpectedException.none();
17
18 @Test
19 public void partitionIdentifier() throws Exception {
20 final RequestContext rc = new DefaultRequestContext(() -> "tenant");
21
22 assertThat(rc.partitionIdentifier(), is("tenant"));
23 }
24
25 @Test
26 public void computeIfAbsent_normal() throws Exception {
27 final RequestContext rc = new DefaultRequestContext(() -> "ignored");
28 final String missing = rc.computeIfAbsent("missing", () -> "generated");
29
30 assertThat(missing, is("generated"));
31
32 final String exists = rc.computeIfAbsent("missing", () -> "wtf");
33
34 assertThat(exists, is("generated"));
35 }
36
37 @Test
38 public void computeIfAbsent_cast_error() throws Exception {
39 final RequestContext rc = new DefaultRequestContext(() -> "ignored");
40 rc.computeIfAbsent("key", () -> "value");
41
42 thrown.expect(ClassCastException.class);
43
44 final Integer i = rc.computeIfAbsent("key", () -> 42);
45 }
46
47 @Test
48 public void verifyComputeIfAbsent() {
49
50
51
52 final int LIMIT = 64;
53 final Map<String, String> map = new HashMap<>();
54 final String KEY = "key";
55
56 map.computeIfAbsent(KEY, k -> {
57 for (int i = 0; i < LIMIT; ++i) {
58 map.put(i + "", "junk");
59 }
60 return "value";
61 });
62
63
64 assertThat(map.containsKey(KEY), is(false));
65 }
66 }