ScriptUtils { public static String convertToJsValue(Map<String, String> stringMap) { if (stringMap == null || stringMap.isEmpty()) { return "{}"; } String convertedValue = "{"; for (String key : stringMap.keySet()) { convertedValue = convertedValue + "\"" + key + "\":" + convertToJsValue(stringMap.get(key)) + ","; } convertedValue = StringUtils.removeEnd(convertedValue, ","); convertedValue = convertedValue + "}"; return convertedValue; }  static String translateValue(Object value); static String toJSON(Map<String, String> map); static String escapeJSONString(String jsonString); static String convertToJsValue(Map<String, String> stringMap); static String convertToJsValue(String value); static String escapeName(String name); static String convertStringListToJsArray(List<String> list); static String escapeHtml(String string); static List<String> escapeHtml(List<String> strings); static String appendScript(String script, String appendScript); static String buildFunctionCall(String functionName, Object... arguments); static String buildEventHandlerScript(String id, String eventName, String eventScript);  }
@Test public void testConvertToJSValue_function() { String jsFunction = "\n function () {alert('1 + 1 ' is 1 + 1);} \n\n"; assertEquals("function was not converted to js value as expected", jsFunction, ScriptUtils.convertToJsValue(jsFunction)); }
@Test public void testConvertToJSValue_numeric() { assertEquals("number was not converted to js value as expected", " -1 ", ScriptUtils.convertToJsValue(" -1 ")); assertEquals("number was not converted to js value as expected", "1.01 ", ScriptUtils.convertToJsValue("1.01 ")); assertEquals("string was not converted to js value as expected", "\"1.o1 \"", ScriptUtils.convertToJsValue("1.o1 ")); }
@Test public void testConvertToJSValue_mapAndArray() { assertEquals("array was not converted to js value as expected", " [-1, 4, 5] ", ScriptUtils.convertToJsValue(" [-1, 4, 5] ")); String jsMap = " {'a':1, 'b':2} \n"; assertEquals("map was not converted to js value as expected", jsMap, ScriptUtils.convertToJsValue(jsMap)); }