JavaBeanSerializeUtil { public static JavaBeanDescriptor serialize(Object obj) { JavaBeanDescriptor result = serialize(obj, JavaBeanAccessor.FIELD); return result; } 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 Class<?> name2Class(ClassLoader loader, String name);  }
@Test public void testSerialize_Primitive() throws Exception { JavaBeanDescriptor descriptor; descriptor = JavaBeanSerializeUtil.serialize(Integer.MAX_VALUE); Assert.assertTrue(descriptor.isPrimitiveType()); Assert.assertEquals(Integer.MAX_VALUE, descriptor.getPrimitiveProperty()); Date now = new Date(); descriptor = JavaBeanSerializeUtil.serialize(now); Assert.assertTrue(descriptor.isPrimitiveType()); Assert.assertEquals(now, descriptor.getPrimitiveProperty()); }
@Test public void testSerialize_Array() throws Exception { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9}; JavaBeanDescriptor descriptor = JavaBeanSerializeUtil.serialize(array, JavaBeanAccessor.METHOD); Assert.assertTrue(descriptor.isArrayType()); Assert.assertEquals(int.class.getName(), descriptor.getClassName()); for (int i = 0; i < array.length; i++) { Assert.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); Assert.assertTrue(descriptor.isArrayType()); Assert.assertEquals(Integer.class.getName(), descriptor.getClassName()); Assert.assertEquals(integers.length, descriptor.propertySize()); for(int i = 0; i < integers.length; i++) { if (integers[i] == null) { Assert.assertTrue(integers[i] == descriptor.getProperty(i)); } else { Assert.assertEquals(integers[i], ((JavaBeanDescriptor)descriptor.getProperty(i)).getPrimitiveProperty()); } } int[][] second = {{1, 2}, {3, 4}}; descriptor = JavaBeanSerializeUtil.serialize(second, JavaBeanAccessor.METHOD); Assert.assertTrue(descriptor.isArrayType()); Assert.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))); Assert.assertTrue(item.isArrayType()); Assert.assertEquals(int.class.getName(), item.getClassName()); Assert.assertEquals(second[i][j], ((JavaBeanDescriptor)item.getProperty(j)).getPrimitiveProperty()); } } BigPerson[] persons = new BigPerson[] {createBigPerson(), createBigPerson()}; descriptor = JavaBeanSerializeUtil.serialize(persons); Assert.assertTrue(descriptor.isArrayType()); Assert.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() throws Exception { 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); Assert.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"); Assert.assertTrue(descriptor == childDescriptor.getProperty("parent")); assertEqualsPrimitive(child.getName(), childDescriptor.getProperty("name")); assertEqualsPrimitive(child.getAge(), childDescriptor.getProperty("age")); }
@Test public void testBeanSerialize() throws Exception { 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); Assert.assertTrue(descriptor.isBeanType()); assertEqualsPrimitive(bean.getDate(), descriptor.getProperty("date")); assertEqualsEnum(bean.getStatus(), descriptor.getProperty("status")); Assert.assertTrue(((JavaBeanDescriptor)descriptor.getProperty("type")).isClassType()); Assert.assertEquals(Bean.class.getName(), ((JavaBeanDescriptor)descriptor.getProperty("type")).getClassNameProperty()); Assert.assertTrue(((JavaBeanDescriptor)descriptor.getProperty("array")).isArrayType()); Assert.assertEquals(0, ((JavaBeanDescriptor)descriptor.getProperty("array")).propertySize()); JavaBeanDescriptor property = (JavaBeanDescriptor)descriptor.getProperty("collection"); Assert.assertTrue(property.isCollectionType()); Assert.assertEquals(1, property.propertySize()); property = (JavaBeanDescriptor)property.getProperty(0); Assert.assertTrue(property.isBeanType()); Assert.assertEquals(Phone.class.getName(), property.getClassName()); Assert.assertEquals(0, property.propertySize()); property = (JavaBeanDescriptor)descriptor.getProperty("addresses"); Assert.assertTrue(property.isMapType()); Assert.assertEquals(bean.getAddresses().getClass().getName(), property.getClassName()); Assert.assertEquals(1, property.propertySize()); Map.Entry<Object, Object> entry = property.iterator().next(); Assert.assertTrue(((JavaBeanDescriptor)entry.getKey()).isPrimitiveType()); Assert.assertEquals("first", ((JavaBeanDescriptor)entry.getKey()).getPrimitiveProperty()); Assert.assertTrue(((JavaBeanDescriptor)entry.getValue()).isBeanType()); Assert.assertEquals(FullAddress.class.getName(), ((JavaBeanDescriptor)entry.getValue()).getClassName()); Assert.assertEquals(0, ((JavaBeanDescriptor)entry.getValue()).propertySize()); }
@Test @SuppressWarnings("unchecked") public void testSerializeJavaBeanDescriptor() throws Exception { JavaBeanDescriptor descriptor = new JavaBeanDescriptor(); JavaBeanDescriptor result = JavaBeanSerializeUtil.serialize(descriptor); Assert.assertTrue(descriptor == result); Map map = new HashMap(); map.put("first", descriptor); result = JavaBeanSerializeUtil.serialize(map); Assert.assertTrue(result.isMapType()); Assert.assertEquals(HashMap.class.getName(), result.getClassName()); Assert.assertEquals(map.size(), result.propertySize()); Object object = result.iterator().next().getValue(); Assert.assertTrue(object instanceof JavaBeanDescriptor); JavaBeanDescriptor actual = (JavaBeanDescriptor)object; Assert.assertEquals(map.get("first"), actual); }