PojoUtils { public static Object[] realize(Object[] objs, Class<?>[] types) { if (objs.length != types.length) { throw new IllegalArgumentException("args.length != types.length"); } Object[] dests = new Object[objs.length]; for (int i = 0; i < objs.length; i++) { dests[i] = realize(objs[i], types[i]); } return dests; }  static Object[] generalize(Object[] objs); static Object[] realize(Object[] objs, Class<?>[] types); static Object[] realize(Object[] objs, Class<?>[] types, Type[] gtypes); static Object generalize(Object pojo); static Object realize(Object pojo, Class<?> type); static Object realize(Object pojo, Class<?> type, Type genericType); static boolean isPojo(Class<?> cls); static void updatePropertyIfAbsent(Supplier<T> getterMethod, Consumer<T> setterMethod, T newValue);  }
@Test public void testMapToEnum() throws Exception { Map map = new HashMap(); map.put("name", "MONDAY"); Object o = PojoUtils.realize(map, Day.class); assertEquals(o, Day.MONDAY); }
@Test public void testMapToInterface() throws Exception { Map map = new HashMap(); map.put("content", "greeting"); map.put("from", "dubbo"); map.put("urgent", true); Object o = PojoUtils.realize(map, Message.class); Message message = (Message) o; assertThat(message.getContent(), equalTo("greeting")); assertThat(message.getFrom(), equalTo("dubbo")); assertTrue(message.isUrgent()); }
@Test public void testJsonObjectToMap() throws Exception { Method method = PojoUtilsTest.class.getMethod("setMap", Map.class); assertNotNull(method); JSONObject jsonObject = new JSONObject(); jsonObject.put("1", "test"); @SuppressWarnings("unchecked") Map<Integer, Object> value = (Map<Integer, Object>)PojoUtils.realize(jsonObject, method.getParameterTypes()[0], method.getGenericParameterTypes()[0]); method.invoke(new PojoUtilsTest(), value); assertEquals("test", value.get(1)); }
@Test public void testListJsonObjectToListMap() throws Exception { Method method = PojoUtilsTest.class.getMethod("setListMap", List.class); assertNotNull(method); JSONObject jsonObject = new JSONObject(); jsonObject.put("1", "test"); List<JSONObject> list = new ArrayList<>(1); list.add(jsonObject); @SuppressWarnings("unchecked") List<Map<Integer, Object>> result = (List<Map<Integer, Object>>)PojoUtils.realize( list, method.getParameterTypes()[0], method.getGenericParameterTypes()[0]); method.invoke(new PojoUtilsTest(), result); assertEquals("test", result.get(0).get(1)); }
@Test public void testException() throws Exception { Map map = new HashMap(); map.put("message", "dubbo exception"); Object o = PojoUtils.realize(map, RuntimeException.class); assertEquals(((Throwable) o).getMessage(), "dubbo exception"); }
@Test public void test_realize_LongPararmter_IllegalArgumentException() throws Exception { Method method = PojoUtilsTest.class.getMethod("setLong", long.class); assertNotNull(method); Object value = PojoUtils.realize("563439743927993", method.getParameterTypes()[0], method.getGenericParameterTypes()[0]); method.invoke(new PojoUtilsTest(), value); }
@Test public void test_realize_IntPararmter_IllegalArgumentException() throws Exception { Method method = PojoUtilsTest.class.getMethod("setInt", int.class); assertNotNull(method); Object value = PojoUtils.realize("123", method.getParameterTypes()[0], method.getGenericParameterTypes()[0]); method.invoke(new PojoUtilsTest(), value); }
@Test public void testRealize() throws Exception { Map<String, String> map = new LinkedHashMap<String, String>(); map.put("key", "value"); Object obj = PojoUtils.generalize(map); assertTrue(obj instanceof LinkedHashMap); Object outputObject = PojoUtils.realize(map, LinkedHashMap.class); assertTrue(outputObject instanceof LinkedHashMap); Object[] objects = PojoUtils.realize(new Object[]{map}, new Class[]{LinkedHashMap.class}); assertTrue(objects[0] instanceof LinkedHashMap); assertEquals(objects[0], outputObject); }
@Test public void testDateTimeTimestamp() throws Exception { String dateStr = "2018-09-12"; String timeStr = "10:12:33"; String dateTimeStr = "2018-09-12 10:12:33"; String[] dateFormat = new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss"}; Object date = PojoUtils.realize(dateTimeStr, Date.class, (Type) Date.class); assertEquals(Date.class, date.getClass()); assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(date)); Object time = PojoUtils.realize(dateTimeStr, java.sql.Time.class, (Type) java.sql.Time.class); assertEquals(java.sql.Time.class, time.getClass()); assertEquals(timeStr, new SimpleDateFormat(dateFormat[2]).format(time)); Object sqlDate = PojoUtils.realize(dateTimeStr, java.sql.Date.class, (Type) java.sql.Date.class); assertEquals(java.sql.Date.class, sqlDate.getClass()); assertEquals(dateStr, new SimpleDateFormat(dateFormat[1]).format(sqlDate)); Object timestamp = PojoUtils.realize(dateTimeStr, java.sql.Timestamp.class, (Type) java.sql.Timestamp.class); assertEquals(java.sql.Timestamp.class, timestamp.getClass()); assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(timestamp)); }
@Test public void testIntToBoolean() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("name", "myname"); map.put("male", 1); map.put("female", 0); PersonInfo personInfo = (PersonInfo) PojoUtils.realize(map, PersonInfo.class); assertEquals("myname", personInfo.getName()); assertTrue(personInfo.isMale()); assertFalse(personInfo.isFemale()); }
@Test public void testRealizeCollectionWithNullElement() { LinkedList<String> listStr = new LinkedList<>(); listStr.add("arrayValue"); listStr.add(null); HashSet<String> setStr = new HashSet<>(); setStr.add("setValue"); setStr.add(null); Object listResult = PojoUtils.realize(listStr, LinkedList.class); assertEquals(LinkedList.class, listResult.getClass()); assertEquals(listResult, listStr); Object setResult = PojoUtils.realize(setStr, HashSet.class); assertEquals(HashSet.class, setResult.getClass()); assertEquals(setResult, setStr); }