Skip to content

Commit e82da3a

Browse files
committed
Fix handling of primitive arrays and clean up unit tests
1 parent be92e11 commit e82da3a

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/main/java/org/medicmobile/webapp/mobile/ChtExternalApp.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,14 @@ private boolean isPrimitive(Object value) {
344344

345345
private Optional<List<?>> asPrimitiveList(Object value) {
346346
if (value != null && value.getClass().isArray()) {
347-
value = List.of(((Object[]) value));
347+
// Java utility methods are not great when mixing primitive and object arrays.
348+
// So, we manually convert any array to a List<Object>.
349+
int len = java.lang.reflect.Array.getLength(value);
350+
List<Object> list = new ArrayList<>(len);
351+
for (int i = 0; i < len; i++) {
352+
list.add(java.lang.reflect.Array.get(value, i));
353+
}
354+
value = list;
348355
}
349356
if (
350357
!(value instanceof List)

src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppHandlerTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import static android.app.Activity.RESULT_OK;
66
import static android.content.pm.PackageManager.PERMISSION_DENIED;
77
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertTrue;
89
import static org.medicmobile.webapp.mobile.EmbeddedBrowserActivity.RequestCode;
910
import static org.mockito.ArgumentMatchers.any;
1011
import static org.mockito.ArgumentMatchers.anyInt;
1112
import static org.mockito.ArgumentMatchers.anyString;
1213
import static org.mockito.ArgumentMatchers.eq;
14+
import static org.mockito.ArgumentMatchers.matches;
1315
import static org.mockito.Mockito.doNothing;
1416
import static org.mockito.Mockito.doThrow;
1517
import static org.mockito.Mockito.mock;
@@ -35,6 +37,8 @@
3537
import org.mockito.MockitoAnnotations;
3638
import org.robolectric.RobolectricTestRunner;
3739

40+
import java.util.regex.Pattern;
41+
3842
@RunWith(RobolectricTestRunner.class)
3943
public class ChtExternalAppHandlerTest {
4044
@Mock
@@ -154,18 +158,19 @@ public void processResult_withBadResultCodeAndData_logError() {
154158
//> GIVEN
155159
Intent intent = new Intent();
156160
intent.putExtra("name", "Eric");
161+
intent.putExtra("duration", 0L);
157162
ChtExternalAppHandler chtExternalAppHandler = new ChtExternalAppHandler(contextMock);
158163
String expectedMessageWarn = "ChtExternalAppHandler :: Bad result code: %s. The external app either: " +
159-
"explicitly returned this result, did not return any result or crashed during the operation. [{\"name\":\"Eric\"}]";
164+
"explicitly returned this result, did not return any result or crashed during the operation. \\[\\{\"duration\":\\d+,\"name\":\"Eric\"}]";
160165
String expectedMessageConsole = "ChtExternalAppHandler :: Bad result code: " + RESULT_CANCELED + ". The external app either: " +
161-
"explicitly returned this result, did not return any result or crashed during the operation. [{\"name\":\"Eric\"}]";
166+
"explicitly returned this result, did not return any result or crashed during the operation. \\[\\{\"duration\":\\d+,\"name\":\"Eric\"}]";
162167

163168
//> WHEN
164169
String script = chtExternalAppHandler.processResult(RESULT_CANCELED, intent);
165170

166171
//> THEN
167-
assertEquals("console.error('" + expectedMessageConsole + "')", script);
168-
medicLogMock.verify(() -> MedicLog.warn(eq(chtExternalAppHandler), eq(expectedMessageWarn), eq(RESULT_CANCELED)));
172+
assertTrue(Pattern.matches("console\\.error\\('" + expectedMessageConsole + "'\\)", script));
173+
medicLogMock.verify(() -> MedicLog.warn(eq(chtExternalAppHandler), matches(expectedMessageWarn), eq(RESULT_CANCELED)));
169174
}
170175
}
171176

src/test/java/org/medicmobile/webapp/mobile/ChtExternalAppTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,16 @@ public void processResponse_withSimpleData_buildJsonCorrectly() {
265265
intent.putExtra("a.string.array", new String[]{"some text", "another text"});
266266

267267
String expectedJsonData = "{" +
268-
"\"an.int.array\":[5,9]," +
268+
"\"an.int.array\":\"5 9\"," +
269269
"\"a.double\":2.8," +
270270
"\"a.long\":2147483649," +
271271
"\"a.string\":\"some text\"," +
272272
"\"an.int\":5," +
273-
"\"a.boolean.array\":[true,false,true]," +
273+
"\"a.boolean.array\":\"true false true\"," +
274274
"\"a.boolean\":true," +
275-
"\"a.string.array\":[\"some text\",\"another text\"]," +
276-
"\"a.long.array\":[2147483649,2147483700]," +
277-
"\"a.double.array\":[2.8,5.5]" +
275+
"\"a.string.array\":\"some_text another_text\"," +
276+
"\"a.long.array\":\"2147483649 2147483700\"," +
277+
"\"a.double.array\":\"2.8 5.5\"" +
278278
"}";
279279

280280
//> WHEN
@@ -326,7 +326,7 @@ public void processResponse_withNestedObjects_buildJsonCorrectly() {
326326
String expectedJsonData = "{" +
327327
"\"people\":[" +
328328
"{" +
329-
"\"relatives\":[\"Pepe\",\"John\",\"Matt\"]," +
329+
"\"relatives\":\"Pepe John Matt\"," +
330330
"\"name\":\"Anna\"" +
331331
"}," +
332332
"{" +
@@ -343,9 +343,9 @@ public void processResponse_withNestedObjects_buildJsonCorrectly() {
343343
"\"a.boolean\":true," +
344344
"\"more.details\":{" +
345345
"\"a.null\":null," +
346-
"\"a.boolean.array\":[true,false,true]," +
346+
"\"a.boolean.array\":\"true false true\"," +
347347
"\"a.description\":\"some awesome data\"," +
348-
"\"a.double.array\":[2.8,5.5]" +
348+
"\"a.double.array\":\"2.8 5.5\"" +
349349
"}" +
350350
"}" +
351351
"}";

0 commit comments

Comments
 (0)