-
Notifications
You must be signed in to change notification settings - Fork 31
/
MethodTest.java
240 lines (187 loc) · 8.93 KB
/
MethodTest.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright 2021 Google LLC
*
* 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.jnibind.test;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.AfterClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
/**
* This class exercises rJNI (Reverse Java Native Interface) calls. All native functions will call a
* Java counterpart on MethodTestHelper which is a mock that can be used to verify expectations.
*
* <p>For the sake of simplicity, each test is separated by type and exercises an rJni call
* returning their type and passing their own parameter, the same but with parameters of their type,
* returning their type and passing multiples of their type, and lastly the same but with return
* type void.
*
* <p>Because of how signatures are generated, this provides decent coverage.
*/
@RunWith(JUnit4.class)
public class MethodTest {
static {
System.load(
System.getenv("JAVA_RUNFILES")
+ "/_main/javatests/com/jnibind/test/libmethod_test_jni.so");
}
static native void jniTearDown();
@AfterClass
public static void doShutDown() {
jniTearDown();
}
@Rule public final MockitoRule mockito = MockitoJUnit.rule();
@Mock public MethodTestHelper rJniTestHelper;
/** Void Method Tests. */
native void jniVoidMethod(MethodTestHelper mockMethodTestHelper);
native void jniVoidMethodTakesOneInt(MethodTestHelper mockMethodTestHelper, int i);
native void jniVoidMethodTakesFiveInts(
MethodTestHelper mockMethodTestHelper, int i1, int i2, int i3, int i4, int i5);
@Test
public void voidTests() {
jniVoidMethod(rJniTestHelper);
jniVoidMethodTakesOneInt(rJniTestHelper, 1);
jniVoidMethodTakesFiveInts(rJniTestHelper, 1, 2, 3, 4, 5);
verify(rJniTestHelper).voidMethod();
verify(rJniTestHelper).voidMethodTakesOneInt(1);
verify(rJniTestHelper).voidMethodTakesFiveInts(1, 2, 3, 4, 5);
}
/** Boolean Method Tests. */
native boolean jniBooleanMethod(MethodTestHelper mockMethodTestHelper);
native boolean jniBooleanMethodTakesOneBoolean(MethodTestHelper mockMethodTestHelper, boolean i);
native boolean jniBooleanMethodTakesFiveBooleans(
MethodTestHelper mockMethodTestHelper,
boolean i1,
boolean i2,
boolean i3,
boolean i4,
boolean i5);
@Test
public void booleanTests() {
when(rJniTestHelper.booleanMethod()).thenReturn(MethodTestHelper.DEFAULT_BOOLEAN);
when(rJniTestHelper.booleanMethodTakesOneBoolean(anyBoolean()))
.thenReturn(MethodTestHelper.DEFAULT_BOOLEAN);
when(rJniTestHelper.booleanMethodTakesFiveBooleans(
anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean()))
.thenReturn(MethodTestHelper.DEFAULT_BOOLEAN);
assertThat(jniBooleanMethod(rJniTestHelper)).isEqualTo(MethodTestHelper.DEFAULT_BOOLEAN);
assertThat(jniBooleanMethodTakesOneBoolean(rJniTestHelper, true))
.isEqualTo(MethodTestHelper.DEFAULT_BOOLEAN);
assertThat(jniBooleanMethodTakesFiveBooleans(rJniTestHelper, true, false, true, false, true))
.isEqualTo(MethodTestHelper.DEFAULT_BOOLEAN);
verify(rJniTestHelper).booleanMethod();
verify(rJniTestHelper).booleanMethodTakesOneBoolean(true);
verify(rJniTestHelper).booleanMethodTakesFiveBooleans(true, false, true, false, true);
}
/** Int Method Tests. */
native int jniIntMethod(MethodTestHelper mockMethodTestHelper);
native int jniIntMethodTakesOneInt(MethodTestHelper mockMethodTestHelper, int i);
native int jniIntMethodTakesFiveInts(
MethodTestHelper mockMethodTestHelper, int i1, int i2, int i3, int i4, int i5);
@Test
public void intTests() {
when(rJniTestHelper.intMethod()).thenReturn(MethodTestHelper.DEFAULT_INT);
when(rJniTestHelper.intMethodTakesOneInt(anyInt())).thenReturn(MethodTestHelper.DEFAULT_INT);
when(rJniTestHelper.intMethodTakesFiveInts(anyInt(), anyInt(), anyInt(), anyInt(), anyInt()))
.thenReturn(MethodTestHelper.DEFAULT_INT);
assertThat(jniIntMethod(rJniTestHelper)).isEqualTo(MethodTestHelper.DEFAULT_INT);
assertThat(jniIntMethodTakesOneInt(rJniTestHelper, 1)).isEqualTo(MethodTestHelper.DEFAULT_INT);
assertThat(jniIntMethodTakesFiveInts(rJniTestHelper, 1, 2, 3, 4, 5))
.isEqualTo(MethodTestHelper.DEFAULT_INT);
verify(rJniTestHelper).intMethod();
verify(rJniTestHelper).intMethodTakesOneInt(1);
verify(rJniTestHelper).intMethodTakesFiveInts(1, 2, 3, 4, 5);
}
/** Long Method Tests. */
native long jniLongMethod(MethodTestHelper mockMethodTestHelper);
native long jniLongMethodTakesOneLong(MethodTestHelper mockMethodTestHelper, long i);
native long jniLongMethodTakesFiveLongs(
MethodTestHelper mockMethodTestHelper, long i1, long i2, long i3, long i4, long i5);
@Test
public void longTests() {
when(rJniTestHelper.longMethod()).thenReturn(MethodTestHelper.DEFAULT_LONG);
when(rJniTestHelper.longMethodTakesOneLong(anyLong()))
.thenReturn(MethodTestHelper.DEFAULT_LONG);
when(rJniTestHelper.longMethodTakesFiveLongs(
anyLong(), anyLong(), anyLong(), anyLong(), anyLong()))
.thenReturn(MethodTestHelper.DEFAULT_LONG);
assertThat(jniLongMethod(rJniTestHelper)).isEqualTo(MethodTestHelper.DEFAULT_LONG);
assertThat(jniLongMethodTakesOneLong(rJniTestHelper, 1))
.isEqualTo(MethodTestHelper.DEFAULT_LONG);
assertThat(jniLongMethodTakesFiveLongs(rJniTestHelper, 1, 2, 3, 4, 5))
.isEqualTo(MethodTestHelper.DEFAULT_LONG);
verify(rJniTestHelper).longMethod();
verify(rJniTestHelper).longMethodTakesOneLong(1);
verify(rJniTestHelper).longMethodTakesFiveLongs(1, 2, 3, 4, 5);
}
/** Float Method Tests. */
native float jniFloatMethod(MethodTestHelper mockMethodTestHelper);
native float jniFloatMethodTakesOneFloat(MethodTestHelper mockMethodTestHelper, float i);
native float jniFloatMethodTakesFiveFloats(
MethodTestHelper mockMethodTestHelper, float i1, float i2, float i3, float i4, float i5);
/** Double Method Tests. */
native double jniDoubleMethod(MethodTestHelper mockMethodTestHelper);
native double jniDoubleMethodTakesOneDouble(MethodTestHelper mockMethodTestHelper, double i);
native double jniDoubleMethodTakesFiveDoubles(
MethodTestHelper mockMethodTestHelper, double i1, double i2, double i3, double i4, double i5);
@Test
public void floatTests() {
when(rJniTestHelper.floatMethod()).thenReturn(MethodTestHelper.DEFAULT_FLOAT);
when(rJniTestHelper.floatMethodTakesOneFloat(anyFloat()))
.thenReturn(MethodTestHelper.DEFAULT_FLOAT);
when(rJniTestHelper.floatMethodTakesFiveFloats(
anyFloat(), anyFloat(), anyFloat(), anyFloat(), anyFloat()))
.thenReturn(MethodTestHelper.DEFAULT_FLOAT);
assertThat(jniFloatMethod(rJniTestHelper)).isEqualTo(MethodTestHelper.DEFAULT_FLOAT);
assertThat(jniFloatMethodTakesOneFloat(rJniTestHelper, 1))
.isEqualTo(MethodTestHelper.DEFAULT_FLOAT);
assertThat(jniFloatMethodTakesFiveFloats(rJniTestHelper, 1, 2, 3, 4, 5))
.isEqualTo(MethodTestHelper.DEFAULT_FLOAT);
verify(rJniTestHelper).floatMethod();
verify(rJniTestHelper).floatMethodTakesOneFloat(1);
verify(rJniTestHelper).floatMethodTakesFiveFloats(1, 2, 3, 4, 5);
}
/** Overload Tests. */
native int jniCallFooOverload1(MethodTestHelper mockMethodTestHelper);
native int jniCallFooOverload2(MethodTestHelper mockMethodTestHelper);
native int jniCallFooOverload3(MethodTestHelper mockMethodTestHelper);
@Test
public void overloadTests() {
jniCallFooOverload1(rJniTestHelper);
jniCallFooOverload2(rJniTestHelper);
jniCallFooOverload3(rJniTestHelper);
verify(rJniTestHelper).foo();
verify(rJniTestHelper).foo(123.f);
verify(rJniTestHelper).foo(123.f, 456.f);
}
/** Overload Tests: int double disambiguation */
native void jniCallFooOverload4(MethodTestHelper mockMethodTestHelper);
@Test
public void intAndDoubleDifferentiateOnOverload() {
jniCallFooOverload4(rJniTestHelper);
verify(rJniTestHelper).intDouble(123, 456.);
verify(rJniTestHelper).intDouble(456., 123);
}
}