JsUtil { public static String getJsObjectForFieldsValue( final Map<String, Object> jsKeyAndElementId) { final StringBuilder builder = new StringBuilder(38); builder.append('{'); for (final Entry<String, Object> entry : jsKeyAndElementId.entrySet()) { builder.append(entry.getKey()).append(":document.getElementById('") .append(entry.getValue().toString()).append("').value,"); } builder.replace(builder.length() - 1, builder.length(), "}"); return builder.toString(); } private  JsUtil(); static String getJsObjectForFieldsValue(
            final Map<String, Object> jsKeyAndElementId); static String getJsObjectForFieldsValue(
            final Map<String, Object> jsKeyAndElementId,
            final String alternativeFunction); static String getJsObjectForFieldsValue(final Set<Object> ids); static String getJsObjectForFieldsValue(final Set<Object> ids,
            final String alternativeFunction); static String getJsObjectForFieldsValue(final String... ids); static String getJsObjectForFieldsValueWithAltFun(
            final String alternativeFunction, final String... ids); static String getJsObjectForFieldsValue(final Set<Object> inputIds,
            final Set<Object> checkboxIds); static String getJsObjectForFieldsValue(final Set<Object> inputIds,
            final Set<Object> checkboxIds, final String alternativeFunction);  }
@Test public void testGetJsObjectForFieldsValue() { final Map<String, Object> jsKeyFieldIds = new LinkedHashMap<String, Object>(); jsKeyFieldIds.put("username", "uId"); jsKeyFieldIds.put("email", UUID.fromString("b2593ccc-2ab9-4cf8-818d-1f317a27a691")); jsKeyFieldIds.put("password", 555); assertEquals( "{username:document.getElementById('uId').value,email:document.getElementById('b2593ccc-2ab9-4cf8-818d-1f317a27a691').value,password:document.getElementById('555').value}", JsUtil.getJsObjectForFieldsValue(jsKeyFieldIds)); }
@Test public void testGetJsObjectForFieldsValueWithAlternativeFunction() { final Map<String, Object> jsKeyFieldIds = new LinkedHashMap<String, Object>(); jsKeyFieldIds.put("username", "uId"); jsKeyFieldIds.put("email", UUID.fromString("b2593ccc-2ab9-4cf8-818d-1f317a27a691")); jsKeyFieldIds.put("password", 555); String alternativeFunction = "gebi"; assertEquals( "{username:gebi('uId').value,email:gebi('b2593ccc-2ab9-4cf8-818d-1f317a27a691').value,password:gebi('555').value}", JsUtil.getJsObjectForFieldsValue(jsKeyFieldIds, alternativeFunction)); }
@Test public void testGetJsObjectForFieldsValueByElementIds() { final Set<Object> jsKeyFieldIds = new LinkedHashSet<Object>(); jsKeyFieldIds.add("uId"); jsKeyFieldIds .add(UUID.fromString("b2593ccc-2ab9-4cf8-818d-1f317a27a691")); jsKeyFieldIds.add(555); assertEquals( "{uId:document.getElementById('uId').value,b2593ccc-2ab9-4cf8-818d-1f317a27a691:document.getElementById('b2593ccc-2ab9-4cf8-818d-1f317a27a691').value,555:document.getElementById('555').value}", JsUtil.getJsObjectForFieldsValue(jsKeyFieldIds)); }
@Test public void testGetJsObjectForFieldsValueByElementIdsWithAltFun() { final Set<Object> jsKeyFieldIds = new LinkedHashSet<Object>(); jsKeyFieldIds.add("uId"); jsKeyFieldIds .add(UUID.fromString("b2593ccc-2ab9-4cf8-818d-1f317a27a691")); jsKeyFieldIds.add(555); assertEquals( "{uId:gebi('uId').value,b2593ccc-2ab9-4cf8-818d-1f317a27a691:gebi('b2593ccc-2ab9-4cf8-818d-1f317a27a691').value,555:gebi('555').value}", JsUtil.getJsObjectForFieldsValue(jsKeyFieldIds, "gebi")); }
@Test public void testGetJsObjectForFieldsValueByElementIdsStrings() { assertEquals( "{uId:document.getElementById('uId').value,b2593ccc-2ab9-4cf8-818d-1f317a27a691:document.getElementById('b2593ccc-2ab9-4cf8-818d-1f317a27a691').value,555:document.getElementById('555').value}", JsUtil.getJsObjectForFieldsValue("uId", "b2593ccc-2ab9-4cf8-818d-1f317a27a691", "555")); }
@Test public void testGetJsObjectForFieldsValueSetObjectSetObject() throws Exception { final Set<Object> jsInputKeyFieldIds = new LinkedHashSet<Object>(); jsInputKeyFieldIds.add("usernameInputId"); final Set<Object> jsCheckboxKeyFieldIds = new LinkedHashSet<Object>(); jsCheckboxKeyFieldIds.add("dateExpiredCheckboxId"); assertEquals( "{usernameInputId:document.getElementById('usernameInputId').value,dateExpiredCheckboxId:document.getElementById('dateExpiredCheckboxId').checked}", JsUtil.getJsObjectForFieldsValue(jsInputKeyFieldIds, jsCheckboxKeyFieldIds)); }
@Test public void testGetJsObjectForFieldsValueSetObjectSetObjectWithAltFun() throws Exception { final Set<Object> jsInputKeyFieldIds = new LinkedHashSet<Object>(); jsInputKeyFieldIds.add("usernameInputId"); final Set<Object> jsCheckboxKeyFieldIds = new LinkedHashSet<Object>(); jsCheckboxKeyFieldIds.add("dateExpiredCheckboxId"); assertEquals( "{usernameInputId:gebi('usernameInputId').value,dateExpiredCheckboxId:gebi('dateExpiredCheckboxId').checked}", JsUtil.getJsObjectForFieldsValue(jsInputKeyFieldIds, jsCheckboxKeyFieldIds, "gebi")); }