ObjectToMapConverter { private Map<String, String> convert(String prefix, Map<?, ?> value) { Map<String, String> map = new LinkedHashMap<String, String>(); for (Map.Entry<?, ?> e : value.entrySet()) { if (StringUtils.isEmpty(prefix)) { map.putAll(this.convert(e.getKey().toString(), e.getValue())); } else { map.putAll(this.convert(prefix + "[" + e.getKey() + "]", e .getValue())); } } return map; } ObjectToMapConverter(FormattingConversionService conversionService); Map<String, String> convert(Object object); Map<String, String> convert(String prefix, Object object);  }
@Test public void testConvert0_SimpleJavaBean() throws Exception { Map<String, String> map = converter.convert( new SearchUserForm0("yamada", 20)); assertThat(map.size(), is(2)); assertThat(map, hasEntry("name", "yamada")); assertThat(map, hasEntry("age", "20")); SearchUserForm0 form = new SearchUserForm0(); WebDataBinder binder = new WebDataBinder(form); binder.bind(new MutablePropertyValues(map)); assertThat(form.getName(), is("yamada")); assertThat(form.getAge(), is(20)); }
@Test public void testConvert0_With_Prefix() throws Exception { Map<String, String> map = converter.convert("pre", new SearchUserForm0("yamada", 20)); assertThat(map.size(), is(2)); assertThat(map, hasEntry("pre.name", "yamada")); assertThat(map, hasEntry("pre.age", "20")); }
@Test public void testConvert1_NestedJavaBean() throws Exception { Map<String, String> map = converter.convert( new SearchUserForm1(new SearchUserCriteriaForm1("yamada", 20), true)); assertThat(map.size(), is(3)); assertThat(map, hasEntry("criteria.name", "yamada")); assertThat(map, hasEntry("criteria.age", "20")); assertThat(map, hasEntry("rememberCriteria", "true")); SearchUserForm1 form = new SearchUserForm1(); WebDataBinder binder = new WebDataBinder(form); binder.bind(new MutablePropertyValues(map)); assertThat(form.getCriteria(), is(notNullValue())); assertThat(form.getCriteria().getName(), is("yamada")); assertThat(form.getCriteria().getAge(), is(20)); assertThat(form.isRememberCriteria(), is(true)); }
@Test public void testConvert2_ListOfJavaBean() throws Exception { Map<String, String> map = converter.convert( new BatchUpdateUserForm2(Arrays.asList( new UpdateUserCriteriaForm2("yamada", 20), new UpdateUserCriteriaForm2("tanaka", 50)), LogicalOperator2.AND)); assertThat(map.size(), is(5)); assertThat(map, hasEntry("criteria[0].name", "yamada")); assertThat(map, hasEntry("criteria[0].age", "20")); assertThat(map, hasEntry("criteria[1].name", "tanaka")); assertThat(map, hasEntry("criteria[1].age", "50")); assertThat(map, hasEntry("operator", "AND")); BatchUpdateUserForm2 form = new BatchUpdateUserForm2(); WebDataBinder binder = new WebDataBinder(form); binder.bind(new MutablePropertyValues(map)); assertThat(form.getCriteria(), is(notNullValue())); assertThat(form.getCriteria().size(), is(2)); assertThat(form.getCriteria().get(0).getName(), is("yamada")); assertThat(form.getCriteria().get(0).getAge(), is(20)); assertThat(form.getCriteria().get(1).getName(), is("tanaka")); assertThat(form.getCriteria().get(1).getAge(), is(50)); assertThat(form.getOperator(), is(LogicalOperator2.AND)); }
@Test public void testConvert3_SimpleJavaBeanAndListOfJavaBean() throws Exception { Map<String, String> map = converter.convert( new SearchAndBatchUpdateUserForm3(new SearchUserCriteriaForm3("suzuki", 30), Arrays .asList(new User3("yamada", 20), new User3("tanaka", 50)))); assertThat(map.size(), is(6)); assertThat(map, hasEntry("criteria.name", "suzuki")); assertThat(map, hasEntry("criteria.age", "30")); assertThat(map, hasEntry("users[0].name", "yamada")); assertThat(map, hasEntry("users[0].age", "20")); assertThat(map, hasEntry("users[1].name", "tanaka")); assertThat(map, hasEntry("users[1].age", "50")); SearchAndBatchUpdateUserForm3 form = new SearchAndBatchUpdateUserForm3(); WebDataBinder binder = new WebDataBinder(form); binder.bind(new MutablePropertyValues(map)); assertThat(form.getCriteria(), is(notNullValue())); assertThat(form.getCriteria().getName(), is("suzuki")); assertThat(form.getCriteria().getAge(), is(30)); assertThat(form.getUsers(), is(notNullValue())); assertThat(form.getUsers().size(), is(2)); assertThat(form.getUsers().get(0).getName(), is("yamada")); assertThat(form.getUsers().get(0).getAge(), is(20)); assertThat(form.getUsers().get(1).getName(), is("tanaka")); assertThat(form.getUsers().get(1).getAge(), is(50)); }
@Test public void testConvert4_MapOfJavaBean() throws Exception { Map<String, String> source = new LinkedHashMap<String, String>(); source.put("aaa", "111"); source.put("bbb", "222"); source.put("ccc", "333"); Map<String, String> map = converter.convert(new SearchForm4(source)); assertThat(map.size(), is(3)); assertThat(map, hasEntry("etc[aaa]", "111")); assertThat(map, hasEntry("etc[bbb]", "222")); assertThat(map, hasEntry("etc[ccc]", "333")); SearchForm4 form = new SearchForm4(); WebDataBinder binder = new WebDataBinder(form); binder.bind(new MutablePropertyValues(map)); assertThat(form.getEtc(), is(notNullValue())); assertThat(form.getEtc().size(), is(3)); assertThat(form.getEtc(), hasEntry("aaa", "111")); assertThat(form.getEtc(), hasEntry("bbb", "222")); assertThat(form.getEtc(), hasEntry("ccc", "333")); }
@Test public void testConvert5_at_DateTimeFormat() throws Exception { LocalDate date1 = new LocalDate(2015, 4, 1); LocalDate localDate1 = new LocalDate(2015, 6, 10); LocalDate date2 = new LocalDate(2015, 5, 1); LocalDate localDate2 = new LocalDate(2015, 7, 10); Map<String, String> map = converter.convert(new DateForm5(date1 .toDate(), localDate1, new DateFormItem5(date2 .toDate(), localDate2))); assertThat(map.size(), is(4)); assertThat(map, hasEntry("date", "2015-04-01")); assertThat(map, hasEntry("localDate", "2015-06-10")); assertThat(map, hasEntry("item.date", "2015-05-01")); assertThat(map, hasEntry("item.localDate", "2015-07-10")); DateForm5 form = new DateForm5(); WebDataBinder binder = new WebDataBinder(form); binder.setConversionService(new DefaultFormattingConversionService()); binder.bind(new MutablePropertyValues(map)); assertThat(form.getDate(), is(date1.toDate())); assertThat(form.getLocalDate(), is(localDate1)); assertThat(form.getItem().getDate(), is(date2.toDate())); assertThat(form.getItem().getLocalDate(), is(localDate2)); }
@Test public void testConvert6_Array() throws Exception { Map<String, String> map = converter.convert(new ArrayForm6(new int[] { 1, 2, 3 }, new double[] { 1.1, 1.2 }, new byte[] { 4, 5, 6 }, new String[] { "a", "b", "c" }, new ArrayFormItem6(new int[] { 11, 12, 13 }, new double[] { 11.1, 11.2 }, new byte[] { 14, 15, 16 }, new String[] { "d", "e", "f" }))); assertThat(map.size(), is(22)); assertThat(map, hasEntry("array1[0]", "1")); assertThat(map, hasEntry("array1[1]", "2")); assertThat(map, hasEntry("array1[2]", "3")); assertThat(map, hasEntry("array2[0]", "1.1")); assertThat(map, hasEntry("array2[1]", "1.2")); assertThat(map, hasEntry("array3[0]", "4")); assertThat(map, hasEntry("array3[1]", "5")); assertThat(map, hasEntry("array3[2]", "6")); assertThat(map, hasEntry("array4[0]", "a")); assertThat(map, hasEntry("array4[1]", "b")); assertThat(map, hasEntry("array4[2]", "c")); assertThat(map, hasEntry("item.array1[0]", "11")); assertThat(map, hasEntry("item.array1[1]", "12")); assertThat(map, hasEntry("item.array1[2]", "13")); assertThat(map, hasEntry("item.array2[0]", "11.1")); assertThat(map, hasEntry("item.array2[1]", "11.2")); assertThat(map, hasEntry("item.array3[0]", "14")); assertThat(map, hasEntry("item.array3[1]", "15")); assertThat(map, hasEntry("item.array3[2]", "16")); assertThat(map, hasEntry("item.array4[0]", "d")); assertThat(map, hasEntry("item.array4[1]", "e")); assertThat(map, hasEntry("item.array4[2]", "f")); ArrayForm6 form = new ArrayForm6(); WebDataBinder binder = new WebDataBinder(form); binder.setConversionService(new DefaultFormattingConversionService()); binder.bind(new MutablePropertyValues(map)); assertThat(form.getArray1().length, is(3)); assertThat(form.getArray1()[0], is(1)); assertThat(form.getArray1()[1], is(2)); assertThat(form.getArray1()[2], is(3)); assertThat(form.getArray2().length, is(2)); assertThat(form.getArray2()[0], is(1.1)); assertThat(form.getArray2()[1], is(1.2)); assertThat(form.getArray3().length, is(3)); assertThat(form.getArray3()[0], is((byte) 4)); assertThat(form.getArray3()[1], is((byte) 5)); assertThat(form.getArray3()[2], is((byte) 6)); assertThat(form.getArray4().length, is(3)); assertThat(form.getArray4()[0], is("a")); assertThat(form.getArray4()[1], is("b")); assertThat(form.getArray4()[2], is("c")); assertThat(form.getItem(), is(notNullValue())); assertThat(form.getItem().getArray1().length, is(3)); assertThat(form.getItem().getArray1()[0], is(11)); assertThat(form.getItem().getArray1()[1], is(12)); assertThat(form.getItem().getArray1()[2], is(13)); assertThat(form.getItem().getArray2().length, is(2)); assertThat(form.getItem().getArray2()[0], is(11.1)); assertThat(form.getItem().getArray2()[1], is(11.2)); assertThat(form.getItem().getArray3().length, is(3)); assertThat(form.getItem().getArray3()[0], is((byte) 14)); assertThat(form.getItem().getArray3()[1], is((byte) 15)); assertThat(form.getItem().getArray3()[2], is((byte) 16)); assertThat(form.getItem().getArray4().length, is(3)); assertThat(form.getItem().getArray4()[0], is("d")); assertThat(form.getItem().getArray4()[1], is("e")); assertThat(form.getItem().getArray4()[2], is("f")); }
@SuppressWarnings("unchecked") @Test public void test7_Null() { assertThat(converter.convert(null), is( (Map<String, String>) Collections.EMPTY_MAP)); }
@Test public void test8_LackingGetter() { assertThat(converter.convert(new LackingGetterForm8("aaa", "bbb")), is( Collections.singletonMap("value1", "aaa"))); }
@Test public void test9_propertiesIsNull() { NullValueForm9 form = new NullValueForm9(); NestedForm9 nestedForm = new NestedForm9(); form.setNestedForm2(nestedForm); Map<String, String> map = converter.convert(form); assertThat(map.size(), is(20)); assertThat(map, hasEntry("_string", "")); assertThat(map, hasEntry("_integer", "")); assertThat(map, hasEntry("_date", "")); assertThat(map, hasEntry("_jodaLocalDate", "")); assertThat(map, hasEntry("_customEnum", "")); assertThat(map, hasEntry("_list", "")); assertThat(map, hasEntry("_list2[0]", "")); assertThat(map, hasEntry("_map", "")); assertThat(map, hasEntry("_map2[key]", "")); assertThat(map, hasEntry("_array", "")); assertThat(map, hasEntry("_array2[0]", "")); assertThat(map, hasEntry("_nestedForm1", "")); assertThat(map, hasEntry("_nestedForm2.string", "")); assertThat(map, hasEntry("_nestedForm2.integer", "")); assertThat(map, hasEntry("_nestedForm2.list", "")); assertThat(map, hasEntry("_nestedForm2.list2[0]", "")); assertThat(map, hasEntry("_nestedForm2.map", "")); assertThat(map, hasEntry("_nestedForm2.map2[key]", "")); assertThat(map, hasEntry("_nestedForm2.array", "")); assertThat(map, hasEntry("_nestedForm2.array2[0]", "")); }
@Test public void test10_propertiesIsEmptyElement() { EmptyElementForm10 form = new EmptyElementForm10(); Map<String, String> map = converter.convert(form); assertThat(map.size(), is(4)); assertThat(map, hasEntry("list", "")); assertThat(map, hasEntry("array", "")); assertThat(map, hasEntry("nestedForm.list", "")); assertThat(map, hasEntry("nestedForm.array", "")); }
@Test public void testConvert11_Boolean() { BooleanForm11 form = new BooleanForm11(true, false, null); Map<String, String> map = converter.convert(form); assertThat(map.size(), is(3)); assertThat(map, hasEntry("bool1", "true")); assertThat(map, hasEntry("bool2", "false")); assertThat(map, hasEntry("bool3", "")); }