JavaBeanSerializeUtil { public static JavaBeanDescriptor serialize(Object obj) { return serialize(obj, JavaBeanAccessor.FIELD); } private  JavaBeanSerializeUtil(); static JavaBeanDescriptor serialize(Object obj); static JavaBeanDescriptor serialize(Object obj, JavaBeanAccessor accessor); static Object deserialize(JavaBeanDescriptor beanDescriptor); static Object deserialize(JavaBeanDescriptor beanDescriptor, ClassLoader loader); static Object getConstructorArg(Class<?> cl); static Class<?> name2Class(ClassLoader loader, String name);  }
@Test public void testSerialize_Primitive() { JavaBeanDescriptor descriptor; descriptor = JavaBeanSerializeUtil.serialize(Integer.MAX_VALUE); Assertions.assertTrue(descriptor.isPrimitiveType()); Assertions.assertEquals(Integer.MAX_VALUE, descriptor.getPrimitiveProperty()); Date now = new Date(); descriptor = JavaBeanSerializeUtil.serialize(now); Assertions.assertTrue(descriptor.isPrimitiveType()); Assertions.assertEquals(now, descriptor.getPrimitiveProperty()); }
@Test public void testSerialize_Primitive_NUll() { JavaBeanDescriptor descriptor; descriptor = JavaBeanSerializeUtil.serialize(null); Assertions.assertNull(descriptor); }
@Test public void testSerialize_Array() { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; JavaBeanDescriptor descriptor = JavaBeanSerializeUtil.serialize(array, JavaBeanAccessor.METHOD); Assertions.assertTrue(descriptor.isArrayType()); Assertions.assertEquals(int.class.getName(), descriptor.getClassName()); for (int i = 0; i < array.length; i++) { Assertions.assertEquals(array[i], ((JavaBeanDescriptor) descriptor.getProperty(i)).getPrimitiveProperty()); } Integer[] integers = new Integer[]{1, 2, 3, 4, null, null, null}; descriptor = JavaBeanSerializeUtil.serialize(integers, JavaBeanAccessor.METHOD); Assertions.assertTrue(descriptor.isArrayType()); Assertions.assertEquals(Integer.class.getName(), descriptor.getClassName()); Assertions.assertEquals(integers.length, descriptor.propertySize()); for (int i = 0; i < integers.length; i++) { if (integers[i] == null) { Assertions.assertSame(integers[i], descriptor.getProperty(i)); } else { Assertions.assertEquals(integers[i], ((JavaBeanDescriptor) descriptor.getProperty(i)) .getPrimitiveProperty()); } } int[][] second = {{1, 2}, {3, 4}}; descriptor = JavaBeanSerializeUtil.serialize(second, JavaBeanAccessor.METHOD); Assertions.assertTrue(descriptor.isArrayType()); Assertions.assertEquals(int[].class.getName(), descriptor.getClassName()); for (int i = 0; i < second.length; i++) { for (int j = 0; j < second[i].length; j++) { JavaBeanDescriptor item = (((JavaBeanDescriptor) descriptor.getProperty(i))); Assertions.assertTrue(item.isArrayType()); Assertions.assertEquals(int.class.getName(), item.getClassName()); Assertions.assertEquals(second[i][j], ((JavaBeanDescriptor) item.getProperty(j)).getPrimitiveProperty()); } } BigPerson[] persons = new BigPerson[]{createBigPerson(), createBigPerson()}; descriptor = JavaBeanSerializeUtil.serialize(persons); Assertions.assertTrue(descriptor.isArrayType()); Assertions.assertEquals(BigPerson.class.getName(), descriptor.getClassName()); for (int i = 0; i < persons.length; i++) { assertEqualsBigPerson(persons[i], descriptor.getProperty(i)); } }
@Test public void test_Circular_Reference() { PojoUtilsTest.Parent parent = new PojoUtilsTest.Parent(); parent.setAge(Integer.MAX_VALUE); parent.setEmail("a@b"); parent.setName("zhangsan"); PojoUtilsTest.Child child = new PojoUtilsTest.Child(); child.setAge(100); child.setName("lisi"); child.setParent(parent); parent.setChild(child); JavaBeanDescriptor descriptor = JavaBeanSerializeUtil.serialize(parent, JavaBeanAccessor.METHOD); Assertions.assertTrue(descriptor.isBeanType()); assertEqualsPrimitive(parent.getAge(), descriptor.getProperty("age")); assertEqualsPrimitive(parent.getName(), descriptor.getProperty("name")); assertEqualsPrimitive(parent.getEmail(), descriptor.getProperty("email")); JavaBeanDescriptor childDescriptor = (JavaBeanDescriptor) descriptor.getProperty("child"); Assertions.assertSame(descriptor, childDescriptor.getProperty("parent")); assertEqualsPrimitive(child.getName(), childDescriptor.getProperty("name")); assertEqualsPrimitive(child.getAge(), childDescriptor.getProperty("age")); }
@Test public void testBeanSerialize() { Bean bean = new Bean(); bean.setDate(new Date()); bean.setStatus(PersonStatus.ENABLED); bean.setType(Bean.class); bean.setArray(new Phone[]{}); Collection<Phone> collection = new ArrayList<Phone>(); bean.setCollection(collection); Phone phone = new Phone(); collection.add(phone); Map<String, FullAddress> map = new HashMap<String, FullAddress>(); FullAddress address = new FullAddress(); map.put("first", address); bean.setAddresses(map); JavaBeanDescriptor descriptor = JavaBeanSerializeUtil.serialize(bean, JavaBeanAccessor.METHOD); Assertions.assertTrue(descriptor.isBeanType()); assertEqualsPrimitive(bean.getDate(), descriptor.getProperty("date")); assertEqualsEnum(bean.getStatus(), descriptor.getProperty("status")); Assertions.assertTrue(((JavaBeanDescriptor) descriptor.getProperty("type")).isClassType()); Assertions.assertEquals(Bean.class.getName(), ((JavaBeanDescriptor) descriptor.getProperty("type")) .getClassNameProperty()); Assertions.assertTrue(((JavaBeanDescriptor) descriptor.getProperty("array")).isArrayType()); Assertions.assertEquals(0, ((JavaBeanDescriptor) descriptor.getProperty("array")).propertySize()); JavaBeanDescriptor property = (JavaBeanDescriptor) descriptor.getProperty("collection"); Assertions.assertTrue(property.isCollectionType()); Assertions.assertEquals(1, property.propertySize()); property = (JavaBeanDescriptor) property.getProperty(0); Assertions.assertTrue(property.isBeanType()); Assertions.assertEquals(Phone.class.getName(), property.getClassName()); Assertions.assertEquals(0, property.propertySize()); property = (JavaBeanDescriptor) descriptor.getProperty("addresses"); Assertions.assertTrue(property.isMapType()); Assertions.assertEquals(bean.getAddresses().getClass().getName(), property.getClassName()); Assertions.assertEquals(1, property.propertySize()); Map.Entry<Object, Object> entry = property.iterator().next(); Assertions.assertTrue(((JavaBeanDescriptor) entry.getKey()).isPrimitiveType()); Assertions.assertEquals("first", ((JavaBeanDescriptor) entry.getKey()).getPrimitiveProperty()); Assertions.assertTrue(((JavaBeanDescriptor) entry.getValue()).isBeanType()); Assertions.assertEquals(FullAddress.class.getName(), ((JavaBeanDescriptor) entry.getValue()).getClassName()); Assertions.assertEquals(0, ((JavaBeanDescriptor) entry.getValue()).propertySize()); }
@Test @SuppressWarnings("unchecked") public void testSerializeJavaBeanDescriptor() { JavaBeanDescriptor descriptor = new JavaBeanDescriptor(); JavaBeanDescriptor result = JavaBeanSerializeUtil.serialize(descriptor); Assertions.assertSame(descriptor, result); Map map = new HashMap(); map.put("first", descriptor); result = JavaBeanSerializeUtil.serialize(map); Assertions.assertTrue(result.isMapType()); Assertions.assertEquals(HashMap.class.getName(), result.getClassName()); Assertions.assertEquals(map.size(), result.propertySize()); Object object = result.iterator().next().getValue(); Assertions.assertTrue(object instanceof JavaBeanDescriptor); JavaBeanDescriptor actual = (JavaBeanDescriptor) object; Assertions.assertEquals(map.get("first"), actual); }