/
RefreshableProxyInvocationHandlerTest.java
120 lines (101 loc) · 4.5 KB
/
RefreshableProxyInvocationHandlerTest.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* (c) Copyright 2017 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.conjure.java.ext.refresh;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.common.reflect.Reflection;
import java.util.function.Function;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
public final class RefreshableProxyInvocationHandlerTest {
interface Callable {
void call();
}
@Mock
private Function<Object, Callable> supplier;
@Mock
private Callable delegate1;
@Mock
private Callable delegate2;
@BeforeEach
public void before() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testCannotConstructProxyWhenInitialRefreshableIsEmpty() throws Exception {
Refreshable<Object> refreshable = Refreshable.empty();
try {
RefreshableProxyInvocationHandler.create(refreshable, supplier);
failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
} catch (IllegalArgumentException e) {
assertThat(e)
.hasMessage("Cannot construct RefreshableProxyInvocationHandler with empty initial refreshable");
}
}
@Test
public void testUsesInitialDelegateAndUpdatesDelegatesWhenRefreshableChanges() throws Exception {
Object object1 = new Object();
Object object2 = new Object();
// Setup initial proxy to call out to delegate1, this fetches the initial delegate from the supplier.
when(supplier.apply(object1)).thenReturn(delegate1);
Refreshable<Object> refreshable = Refreshable.of(object1);
RefreshableProxyInvocationHandler<Object, Callable> handler =
RefreshableProxyInvocationHandler.create(refreshable, supplier);
verify(supplier).apply(object1);
@SuppressWarnings("ProxyNonConstantType")
Callable proxy = Reflection.newProxy(Callable.class, handler);
// First call: check that delegate 1 received call and that supplier is not invoked.
proxy.call();
verify(delegate1).call();
Mockito.verifyNoMoreInteractions(delegate1, delegate2, supplier);
// Second call: still using delegate1, not invoking the supplier.
proxy.call();
verify(delegate1, times(2)).call();
Mockito.verifyNoMoreInteractions(delegate1, delegate2, supplier);
// Third call: refresh the object and make the supplier return a new delegate2.
refreshable.set(object2);
when(supplier.apply(object2)).thenReturn(delegate2);
proxy.call();
verify(delegate2).call();
verify(supplier).apply(object2);
Mockito.verifyNoMoreInteractions(delegate1, delegate2, supplier);
// Fourth call: still using delegate2, not invoking the supplier.
proxy.call();
verify(delegate2, times(2)).call();
Mockito.verifyNoMoreInteractions(delegate1, delegate2, supplier);
}
@Test
public void testUnwrapsInvocationTargetExceptions() {
Callable throwingCallable = () -> {
throw new IllegalStateException("Whoops");
};
Refreshable<Callable> refreshable = Refreshable.of(throwingCallable);
RefreshableProxyInvocationHandler<Callable, Callable> handler =
RefreshableProxyInvocationHandler.create(refreshable, _tec -> throwingCallable);
@SuppressWarnings("ProxyNonConstantType")
Callable proxy = Reflection.newProxy(Callable.class, handler);
assertThatThrownBy(proxy::call)
.isInstanceOf(IllegalStateException.class)
.hasMessage("Whoops");
}
}