Skip to content

Commit

Permalink
Add more test case. (sofastack#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjboy authored Aug 14, 2018
1 parent d9d461b commit 1ba6e13
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,28 @@ private static <T> T mapToObject(Map src, Class<T> dstClazz) {
}

value = src.get(name);
if (isRequired && value == null) {
throw new NullPointerException("Field " + name + " can't be null");
}

Class fieldClazz = field.getType();
if (value != null && Collection.class.isAssignableFrom(fieldClazz)) {
Class genericType = Object.class;
try {
genericType = (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
} catch (Exception ignore) { // NOPMD
if (value == null) {
if (isRequired) {
throw new NullPointerException("Field " + name + " can't be null");
}
if (value instanceof Collection) {
value = collection2Collection((Collection) value, fieldClazz, genericType);
} else if (value.getClass().isArray()) {
value = arrayToCollection((Object[]) value, fieldClazz, genericType);
} else {
Class fieldClazz = field.getType();
if (Collection.class.isAssignableFrom(fieldClazz)) {
Class genericType = Object.class;
try {
genericType = (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
} catch (Exception ignore) { // NOPMD
}
if (value instanceof Collection) {
value = collection2Collection((Collection) value, fieldClazz, genericType);
} else if (value.getClass().isArray()) {
value = arrayToCollection(value, fieldClazz, genericType);
} else {
throw new RuntimeException("value type is not supported, type=" + value.getClass());
}
} else {
throw new RuntimeException("value type is not supported,type=" + value.getClass());
value = deserializeByType(value, fieldClazz);
}
} else {
value = deserializeByType(value, fieldClazz);
}
// 赋值
field.set(bean, value);
Expand All @@ -205,8 +207,8 @@ private static <T> T mapToObject(Map src, Class<T> dstClazz) {
return (T) bean;
}

private static <T, A> Collection<T> arrayToCollection(A[] src,
Class<? extends Collection> clazz, Class<T> genericType) {
private static <T> Collection<T> arrayToCollection(Object src,
Class<? extends Collection> clazz, Class<T> genericType) {
if (clazz.isInterface()) {
if (List.class.isAssignableFrom(clazz)) {
clazz = ArrayList.class;
Expand All @@ -215,8 +217,8 @@ private static <T, A> Collection<T> arrayToCollection(A[] src,
}
}
Collection collection = ClassUtils.newInstance(clazz);
for (int i = 0; i < src.length; ++i) {
collection.add(deserializeByType(src[i], genericType));
for (int i = 0; i < Array.getLength(src); ++i) {
collection.add(deserializeByType(Array.get(src, i), genericType));
}
return collection;
}
Expand All @@ -243,7 +245,7 @@ private static <T> T[] array2Array(Object[] src, Class<T> componentType) {
*
* @param src 原始对象
* @param clazz 期望的对象
* @param <T> 反序列化类型
* @param <T> 反序列化类型
* @return 转换后结果
*/
public static <T> T deserializeByType(Object src, Class<T> clazz) {
Expand Down Expand Up @@ -284,16 +286,21 @@ public static <T> T deserializeByType(Object src, Class<T> clazz) {
return (T) list;
}
} else if (src.getClass().isArray()) {
if (src.getClass().getComponentType().isPrimitive()) { // 基本类型数组 直接返回
return (T) src;
Class componentType = src.getClass().getComponentType();
if (componentType.isPrimitive()) {
if (Collection.class.isAssignableFrom(clazz)) {
return (T) arrayToCollection(src, (Class<? extends Collection>) clazz, Object.class);
} else {
return (T) src;
}
} else {
Object[] array = (Object[]) src;
if (clazz == Object.class) {
return (T) array2Array(array, array.getClass().getComponentType());
} else if (clazz.isArray()) {
return (T) array2Array(array, clazz.getComponentType());
} else if (Collection.class.isAssignableFrom(clazz)) {
return (T) arrayToCollection((Object[]) src, (Class<? extends Collection>) clazz, Object.class);
return (T) arrayToCollection(src, (Class<? extends Collection>) clazz, Object.class);
} else {
return (T) src;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public static int parseInt(String num, int defaultInt) {

/**
* String Long turn number.
*
* @param num The number of strings.
* @param defaultLong The default value
* @return long
Expand Down Expand Up @@ -246,9 +247,7 @@ public static <T> boolean listEquals(List<T> left, List<T> right) {
List<T> ltmp = new ArrayList<T>(left);
List<T> rtmp = new ArrayList<T>(right);
for (T t : ltmp) {
if (rtmp.contains(t)) {
rtmp.remove(t);
}
rtmp.remove(t);
}
return rtmp.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
*/
package com.alipay.sofa.rpc.common.json;

import com.alipay.sofa.rpc.common.utils.CommonUtils;
import com.alipay.sofa.rpc.common.utils.DateUtils;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -109,19 +114,46 @@ public void deserialize() throws Exception {
map4.put("key2", 222);

Map beanmap5 = new HashMap();
try {
// name is required
BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class);
Assert.fail();
} catch (Exception e) {
}
beanmap5.put("Name", "zzzzggg");
beanmap5.put("Sex", true);
beanmap5.put("age", 22);
beanmap5.put("friends", new ArrayList<TestJsonBean>());
beanmap5.put("vips", new HashSet<TestJsonBean>());
beanmap5.put("remark", "hehehe");

TestJsonBean bean = (TestJsonBean) BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class);
TestJsonBean bean = BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class);
Assert.assertEquals(beanmap5.get("Name"), bean.getName());
Assert.assertEquals(beanmap5.get("Sex"), bean.isSex());
Assert.assertEquals(beanmap5.get("age"), bean.getAge());
Assert.assertEquals(beanmap5.get("friends"), bean.getFriends());
Assert.assertEquals(beanmap5.get("Remark"), bean.getRemark());

TestJsonBean bean1 = new TestJsonBean();
beanmap5.put("friends", new TestJsonBean[] { bean1 });
bean = BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class);
Assert.assertEquals(bean1, bean.getFriends().get(0));
beanmap5.put("friends", null);
bean = BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class);
Assert.assertTrue(CommonUtils.isEmpty(bean.getFriends()));
beanmap5.put("friends", "xxxx");
try {
// invalid type
BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class);
Assert.fail();
} catch (Exception e) {
}
beanmap5.put("friends", new ArrayList<TestJsonBean>());
beanmap5.put("vips", new TestJsonBean[] { bean1 });
bean = BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class);
Assert.assertEquals(bean1, new ArrayList<TestJsonBean>(bean.getVips()).get(0));
beanmap5.put("vips", new HashSet<TestJsonBean>());

beanmap5.put(JSON.CLASS_KEY, "com.alipay.sofa.rpc.common.json.TestJsonBean");
TestJsonBean bean2 = (TestJsonBean) BeanSerializer.deserializeByType(beanmap5, Object.class);
Assert.assertEquals(beanmap5.get("Name"), bean2.getName());
Expand Down Expand Up @@ -185,6 +217,43 @@ public void deserialize() throws Exception {

}

@Test
public void testDeserializeByType() {
Assert.assertTrue(0 == BeanSerializer.deserializeByType(null, int.class));
Assert.assertTrue(0 == BeanSerializer.deserializeByType(null, long.class));
Assert.assertFalse(BeanSerializer.deserializeByType(null, boolean.class));

Assert.assertArrayEquals(new int[] { 123 }, BeanSerializer.deserializeByType(Arrays.asList(123), int[].class));
Assert.assertFalse(BeanSerializer.deserializeByType(Arrays.asList(123), String.class) instanceof String);
Assert.assertTrue(CommonUtils.listEquals(Arrays.asList(123),
BeanSerializer.deserializeByType(new int[] { 123 }, List.class)));
Assert.assertTrue(CommonUtils.listEquals(Arrays.asList("xxx"),
BeanSerializer.deserializeByType(new String[] { "xxx" }, List.class)));

Assert.assertEquals(TestJsonBean.Status.START,
BeanSerializer.deserializeByType("START", TestJsonBean.Status.class));
try {
BeanSerializer.deserializeByType(new TestJsonBean(), TestJsonBean.Status.class);
Assert.fail();
} catch (Exception e) {
}

Date now = new Date();
Assert.assertEquals(now, BeanSerializer.deserializeByType(now.getTime(), Date.class));
Assert.assertEquals(DateUtils.dateToStr(now), DateUtils.dateToStr(
BeanSerializer.deserializeByType(DateUtils.dateToStr(now), Date.class)));
try {
BeanSerializer.deserializeByType("xxxx", Date.class);
Assert.fail();
} catch (Exception e) {
}
try {
BeanSerializer.deserializeByType(new TestJsonBean(), Date.class);
Assert.fail();
} catch (Exception e) {
}
}

@Test
public void serializeMap() {
boolean error = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public void testDeserialize() {
Assert.assertEquals(((Map) json.get("k")).get("11"), "22");
}

@Test
public void testDeserializeSpecialCharter() {
String s = "{\"a\": \"\\b\\t\\n\\f\\r\\u771f\\u7684\\u5417\\uff1f\\u54c8\\u54c8\\u0068\\u0061\\u0068\\u0061\" }";
Map json = (Map) JSONSerializer.deserialize(s);
Assert.assertEquals(json.get("a"), "\b\t\n\f\r真的吗?哈哈haha");
}

@Test
public void testDeserializeWithComment() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.rpc.common.json;

import java.util.ArrayList;
import java.util.Set;

/**
*
Expand All @@ -32,6 +33,8 @@ public class TestJsonBean {
private int age;
@JSONField(skipIfNull = true)
private ArrayList<TestJsonBean> friends;
@JSONField(skipIfNull = true)
private Set<TestJsonBean> vips;
@JSONField(alias = "Remark")
private Object[] remark;

Expand Down Expand Up @@ -65,6 +68,15 @@ public void setFriends(ArrayList<TestJsonBean> friends) {
this.friends = friends;
}

public Set<TestJsonBean> getVips() {
return vips;
}

public TestJsonBean setVips(Set<TestJsonBean> vips) {
this.vips = vips;
return this;
}

public Object[] getRemark() {
return remark;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class CommonUtilsTest {

@Test
public void parseBoolean() throws Exception {
public void parseBoolean() {
Assert.assertTrue(CommonUtils.parseBoolean(null, true));
Assert.assertTrue(CommonUtils.parseBoolean("true", true));
Assert.assertFalse(CommonUtils.parseBoolean("falSE", true));
Expand All @@ -45,12 +45,12 @@ public void parseBoolean() throws Exception {
}

@Test
public void parseInts() throws Exception {
public void parseInts() {
Assert.assertArrayEquals(new int[] { 1, 2, 3 }, CommonUtils.parseInts("1,2,3", ","));
}

@Test
public void join() throws Exception {
public void join() {
Assert.assertEquals(CommonUtils.join(null, ","), "");
Assert.assertEquals(CommonUtils.join(new ArrayList(), ","), "");
List<String> s = new ArrayList<String>();
Expand Down Expand Up @@ -137,7 +137,7 @@ public void testIsNotEmpty() {
}

@Test
public void testIsTrue() throws Exception {
public void testIsTrue() {
Assert.assertTrue(CommonUtils.isTrue("true"));
Assert.assertTrue(CommonUtils.isTrue("True"));
Assert.assertFalse(CommonUtils.isTrue("111"));
Expand All @@ -150,7 +150,7 @@ public void testIsTrue() throws Exception {
}

@Test
public void testIsFalse() throws Exception {
public void testIsFalse() {
Assert.assertTrue(CommonUtils.isFalse("false"));
Assert.assertTrue(CommonUtils.isFalse("False"));
Assert.assertFalse(CommonUtils.isFalse("null"));
Expand All @@ -162,6 +162,12 @@ public void testIsFalse() throws Exception {
Assert.assertTrue(CommonUtils.isFalse(Boolean.FALSE));
}

@Test
public void testParseNum() {
Assert.assertTrue(CommonUtils.parseNum(null, 123) == 123);
Assert.assertTrue(CommonUtils.parseNum(1234, 123) == 1234);
}

@Test
public void testParseInt() {
Assert.assertEquals(CommonUtils.parseInt("", 123), 123);
Expand All @@ -171,7 +177,15 @@ public void testParseInt() {
}

@Test
public void testListEquals() throws Exception {
public void testParseLong() {
Assert.assertEquals(CommonUtils.parseLong("", 123L), 123L);
Assert.assertEquals(CommonUtils.parseLong("xxx", 123L), 123L);
Assert.assertEquals(CommonUtils.parseLong(null, 123L), 123L);
Assert.assertEquals(CommonUtils.parseLong("12345", 123L), 12345L);
}

@Test
public void testListEquals() {
List left = new ArrayList();
List right = new ArrayList();
Assert.assertTrue(CommonUtils.listEquals(null, null));
Expand Down
Loading

0 comments on commit 1ba6e13

Please sign in to comment.