Functions { public static String query(Object params) { if (params == null) { return ""; } Class<?> clazz = params.getClass(); if (BeanUtils.isSimpleValueType(clazz)) { return ""; } return mapToQuery(OBJECT_TO_MAP_CONVERTER.convert(params)); } private  Functions(); static String h(Object input); static String u(String value); static String br(String value); static String cut(String value, int length); static String link(String value); static String mapToQuery(Map<String, ?> map); @Deprecated static String mapToQuery(Map<String, ?> map,
            BeanWrapper beanWrapper); static String query(Object params); static String js(String value); static String hjs(String input);  }
@Test public void testQuery01() throws Exception { assertThat(Functions.query(null), is("")); assertThat(Functions.query(""), is("")); assertThat(Functions.query("aaaa"), is("")); assertThat(Functions.query(1), is("")); assertThat(Functions.query(1.0), is("")); assertThat(Functions.query(1L), is("")); assertThat(Functions.query(new Date()), is("")); assertThat(Functions.query(Locale.getDefault()), is("")); assertThat(Functions.query(new URL("http: assertThat(Functions.query(new Object[] { "1", "2", "3" }), is("")); assertThat(Functions.query(new int[] { 1, 2, 3 }), is("")); assertThat(Functions.query(Arrays.asList("a", "b", "c")), is("")); }
@Test public void testQuery02() { Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("name", "Ichiro Suzuki"); map.put("ja", "すずき いちろう"); map.put("arr", new Object[] { "xxx", "yyy" }); String query = Functions.query(map); assertThat(query, is( "name=Ichiro%20Suzuki&ja=%E3%81%99%E3%81%9A%E3%81%8D%20%E3%81%84%E3%81%A1%E3%82%8D%E3%81%86&arr%5B0%5D=xxx&arr%5B1%5D=yyy")); assertThat(Functions.query(new LinkedHashMap<String, Object>()), is( "")); }
@Test public void testQuery03() throws Exception { Person p = new Person(); p.setName("すずき いちろう"); p.setAge(10); p.setList(Arrays.asList("a", "b", "あ")); p.setDate(new SimpleDateFormat("yyyy-MM-dd").parse("2000-01-01")); String query = Functions.query(p); assertThat(query, is( "age=10&date=2000-01-01&list%5B0%5D=a&list%5B1%5D=b&list%5B2%5D=%E3%81%82&name=%E3%81%99%E3%81%9A%E3%81%8D%20%E3%81%84%E3%81%A1%E3%82%8D%E3%81%86")); assertThat(Functions.query(new Person()), is( "_age=&_date=&_list=&_name=")); }
@Test public void testQuery04_url_encoding_for_queryString() { Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("key#1", "value#1"); map.put("key[2", "value[2"); map.put("key]3", "value]3"); map.put("key&4", "value&4"); map.put("key=5", "value=5"); map.put("key+6", "value+6"); String actualQuery = Functions.query(map); StringBuilder expectedQuery = new StringBuilder(); expectedQuery.append("key%231=value%231"); expectedQuery.append("&").append("key%5B2=value%5B2"); expectedQuery.append("&").append("key%5D3=value%5D3"); expectedQuery.append("&").append("key%264=value%264"); expectedQuery.append("&").append("key%3D5=value%3D5"); expectedQuery.append("&").append("key%2B6=value%2B6"); assertThat(actualQuery, is(expectedQuery.toString())); }
@Test public void testQuery5_NestedFields() { Person p1 = new Person(); p1.setName("山田"); p1.setAge(20); p1.setDate(new LocalDate(2001, 1, 1).toDate()); Person p2 = new Person(); p2.setName("鈴木"); p2.setAge(30); p2.setDate(new LocalDate(1991, 1, 1).toDate()); MeetingRegisterForm form = new MeetingRegisterForm(); form.setMeetingId(10); form.setParticipants(Arrays.asList(p1, p2)); String q = Functions.query(form); assertThat(q, is("meetingId=10" + "&participants%5B0%5D.age=20" + "&participants%5B0%5D.date=2001-01-01" + "&_participants%5B0%5D.list=" + "&participants%5B0%5D.name=%E5%B1%B1%E7%94%B0" + "&participants%5B1%5D.age=30" + "&participants%5B1%5D.date=1991-01-01" + "&_participants%5B1%5D.list=" + "&participants%5B1%5D.name=%E9%88%B4%E6%9C%A8")); }