Skip to content

Commit

Permalink
DUBBO-627 补充单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi committed Dec 26, 2012
1 parent 737dc24 commit 0e3b007
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.alibaba.dubbo.common.beanutil;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

import com.alibaba.dubbo.common.model.person.FullAddress;
import com.alibaba.dubbo.common.model.person.PersonStatus;
import com.alibaba.dubbo.common.model.person.Phone;

/**
* @author <a href="mailto:[email protected]">kimi</a>
*/
public class Bean {

private Class<?> type;

private PersonStatus status;

private Date date;

private Phone[] array;

private Collection<Phone> collection;

private Map<String, FullAddress> addresses;

public Class<?> getType() {
return type;
}

public void setType(Class<?> type) {
this.type = type;
}

public PersonStatus getStatus() {
return status;
}

public void setStatus(PersonStatus status) {
this.status = status;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public Phone[] getArray() {
return array;
}

public void setArray(Phone[] array) {
this.array = array;
}

public Collection<Phone> getCollection() {
return collection;
}

public void setCollection(Collection<Phone> collection) {
this.collection = collection;
}

public Map<String, FullAddress> getAddresses() {
return addresses;
}

public void setAddresses(Map<String, FullAddress> addresses) {
this.addresses = addresses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.junit.Test;
Expand Down Expand Up @@ -175,6 +178,91 @@ public void test_Circular_Reference() throws Exception {
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
public void testDeserializeBean() 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 beanDescriptor = JavaBeanSerializeUtil.serialize(bean, JavaBeanAccessor.METHOD);
Object deser = JavaBeanSerializeUtil.deserialize(beanDescriptor);
Assert.assertTrue(deser instanceof Bean);
Bean deserBean = (Bean)deser;
Assert.assertEquals(bean.getDate(), deserBean.getDate());
Assert.assertEquals(bean.getStatus(), deserBean.getStatus());
Assert.assertEquals(bean.getType(), deserBean.getType());
Assert.assertEquals(bean.getCollection().size(), deserBean.getCollection().size());
Assert.assertEquals(bean.getCollection().iterator().next().getClass(),
deserBean.getCollection().iterator().next().getClass());
Assert.assertEquals(bean.getAddresses().size(), deserBean.getAddresses().size());
Assert.assertEquals(bean.getAddresses().entrySet().iterator().next().getKey(),
deserBean.getAddresses().entrySet().iterator().next().getKey());
Assert.assertEquals(bean.getAddresses().entrySet().iterator().next().getValue().getClass(),
deserBean.getAddresses().entrySet().iterator().next().getValue().getClass());
}

static void assertEqualsEnum(Enum<?> expected, Object obj) {
JavaBeanDescriptor descriptor = (JavaBeanDescriptor) obj;
Assert.assertTrue(descriptor.isEnumType());
Expand Down

0 comments on commit 0e3b007

Please sign in to comment.