Skip to content

Commit cc7d324

Browse files
committed
java: use reflection to get private methods
1 parent 213ec63 commit cc7d324

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

java/src/main/java/com/google/openlocationcode/OpenLocationCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public OpenLocationCode(double latitude, double longitude, int codeLength) {
200200
* @param codeLength The requested number of digits.
201201
* @return The OLC for the location.
202202
*/
203-
static String encodeIntegers(long lat, long lng, int codeLength) {
203+
private static String encodeIntegers(long lat, long lng, int codeLength) {
204204
// Limit the maximum number of digits in the code.
205205
codeLength = Math.min(codeLength, MAX_DIGIT_COUNT);
206206
// Check that the code length requested is valid.

java/src/test/java/com/google/openlocationcode/EncodingTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.FileInputStream;
77
import java.io.InputStream;
88
import java.io.InputStreamReader;
9+
import java.lang.reflect.Method;
910
import java.util.ArrayList;
1011
import java.util.List;
1112

@@ -72,9 +73,14 @@ public void testEncodeFromLatLong() {
7273
}
7374

7475
@Test
75-
public void testDegreesToIntegers() {
76+
public void testDegreesToIntegers() throws Exception {
77+
// The method to test is private, we use reflection to get it to avoid changing the visibility.
78+
OpenLocationCode olcClass = new OpenLocationCode(null);
79+
Method privateMethod = OpenLocationCode.class.getDeclaredMethod("degreesToIntegers", int.class, int.class);
80+
privateMethod.setAccessible(true);
81+
7682
for (TestData testData : testDataList) {
77-
long[] got = OpenLocationCode.degreesToIntegers(testData.latitudeDegrees, testData.longitudeDegrees);
83+
long[] got = (long[]) privateMethod.invoke(olcClass, testData.latitudeDegrees, testData.longitudeDegrees);
7884
Assert.assertEquals(
7985
String.format("Latitude %f integer conversion is incorrect", testData.latitudeDegrees),
8086
testData.latitudeInteger,
@@ -87,14 +93,20 @@ public void testDegreesToIntegers() {
8793
}
8894

8995
@Test
90-
public void testEncodeIntegers() {
96+
public void testEncodeIntegers() throws Exception {
97+
// The method to test is private, we use reflection to get it to avoid changing the visibility.
98+
OpenLocationCode olcClass = new OpenLocationCode(null);
99+
Method privateMethod = OpenLocationCode.class.getDeclaredMethod("encodeIntegers", int.class, int.class);
100+
privateMethod.setAccessible(true);
101+
91102
for (TestData testData : testDataList) {
103+
String got = (String) privateMethod.invoke(olcClass, testData.latitudeInteger, testData.longitudeInteger, testData.length);
92104
Assert.assertEquals(
93105
String.format(
94106
"Latitude %d, longitude %d and length %d were wrongly encoded.",
95107
testData.latitudeInteger, testData.longitudeInteger, testData.length),
96108
testData.code,
97-
OpenLocationCode.encodeIntegers(testData.latitudeInteger, testData.longitudeInteger, testData.length));
109+
got);
98110
}
99111
}
100112
}

0 commit comments

Comments
 (0)