Repository navigation
Expand file tree
/
Copy pathFrameLocalTest.java
More file actions
83 lines (64 loc) · 1.88 KB
/
Copy pathFrameLocalTest.java
File metadata and controls
83 lines (64 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package org.bk.ass;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
class FrameLocalTest {
@Test
public void shouldReturnSuppliedValue() {
// GIVEN
FrameLocal<Integer> frameLocal = new FrameLocal<>(() -> 1, () -> 2);
// WHEN
Integer value = frameLocal.get();
// THEN
assertThat(value).isEqualTo(2);
}
@Test
public void shouldNotCallInitializerTwicePerFrame() {
// GIVEN
CountingSupplier<Integer> countingSupplier = new CountingSupplier<>(() -> 2);
FrameLocal<Integer> frameLocal = new FrameLocal<>(() -> 1, countingSupplier);
frameLocal.get();
// WHEN
frameLocal.get();
// THEN
assertThat(countingSupplier.calls).isEqualTo(1);
}
@Test
public void shouldReturnSuppliedValueForMultipleInvocations() {
// GIVEN
FrameLocal<Integer> frameLocal = new FrameLocal<>(() -> 1, () -> 3);
frameLocal.get();
// WHEN
Integer value = frameLocal.get();
// THEN
assertThat(value).isEqualTo(3);
}
@Test
public void shouldRetrieveNewValueForNewFrame() {
// GIVEN
AtomicInteger frame = new AtomicInteger(1);
AtomicInteger value = new AtomicInteger(1337);
FrameLocal<Integer> frameLocal = new FrameLocal<>(frame::get, value::get);
Integer firstValue = frameLocal.get();
frame.set(2);
value.set(815);
// WHEN
Integer secondValue = frameLocal.get();
// THEN
assertThat(firstValue).isEqualTo(1337);
assertThat(secondValue).isEqualTo(815);
}
private static class CountingSupplier<T> implements Supplier<T> {
private final Supplier<T> delegate;
int calls;
private CountingSupplier(Supplier<T> delegate) {
this.delegate = delegate;
}
@Override
public T get() {
calls++;
return delegate.get();
}
}
}