### Question:
JavaScriptMapFactory { public static Map<String, Object> javaScriptObjectToMap(NativeObject nativeObj) { if (nativeObj == null) { return null; } final Map<String, Object> toReturn = new HashMap<String, Object>(nativeObj.getIds().length); for (Object key : nativeObj.getIds()) { final Object value; if (key instanceof String) { value = nativeObj.get((String)key, nativeObj); } else if (key instanceof Integer) { value = nativeObj.get(((Integer)key).intValue(), nativeObj); } else { throw new IllegalArgumentException("Invalid JavaScript object representation."); } if (value != null) { toReturn.put(key.toString(), value); } } return toReturn; }  static Map<String, Object> javaScriptObjectToMap(NativeObject nativeObj);  }### Answer:
@Test public void testValidObjectConvertsSuccessfully() { final String myJavaScriptObject = "({number:1, word:'up'})"; loadNativeObjectFromJavaScript(myJavaScriptObject); myNativeObject.put("rock", myNativeObject, "on"); myNativeObject.put(0, myNativeObject, "first"); myNativeObject.put(0, myNativeObject, "second"); Map<String, Object> mapRepresentation = JavaScriptMapFactory.javaScriptObjectToMap(myNativeObject); assertTrue(mapRepresentation.get("word").equals("up")); assertTrue(mapRepresentation.get("rock").equals("on")); assertTrue(mapRepresentation.get("0").equals("second")); assertTrue((Double) mapRepresentation.get("number") == 1); }
@Test public void testNullNativeObjectIsNull() { Map<String, Object> mapRepresentation = JavaScriptMapFactory.javaScriptObjectToMap(null); assertNull(mapRepresentation); }
@Test public void testEmptyObjectIsEmptyMap() { final String myJavaScriptObject = "({})"; loadNativeObjectFromJavaScript(myJavaScriptObject); Map<String, Object> mapRepresentation = JavaScriptMapFactory.javaScriptObjectToMap(myNativeObject); assertTrue(mapRepresentation.isEmpty()); }
@Test (expectedExceptions = IllegalArgumentException.class) public void testInvalidObjectKeyThrowsIllegalArgument() { NativeObject mockNativeObject = mock(NativeObject.class); Object[] listOfItems = new Object[1]; listOfItems[0] = new Object(); when(mockNativeObject.getIds()).thenReturn(listOfItems); Map<String, Object> mapRepresentation = JavaScriptMapFactory.javaScriptObjectToMap(mockNativeObject); }