Skip to content

Commit

Permalink
[SCB-1071] update other test case
Browse files Browse the repository at this point in the history
  • Loading branch information
wujimin authored and liubao68 committed Jan 14, 2019
1 parent a27d374 commit 41e843a
Show file tree
Hide file tree
Showing 9 changed files with 976 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -72,20 +72,43 @@ public static class PojoModel {
public Map<String, Map<String, Map<String, User>>> mapMapMapUser;

public void init() {
listListUser = Arrays.asList(Arrays.asList(user));
listMapUser = Arrays.asList(Collections.singletonMap(user.name, user));
mapListUser = Collections.singletonMap("k1", Arrays.asList(user));
mapMapUser = Collections.singletonMap("k1", Collections.singletonMap(user.name, user));

listListListUser = Arrays.asList(Arrays.asList(Arrays.asList(user)));
listListMapUser = Arrays.asList(Arrays.asList(Collections.singletonMap(user.name, user)));
listMapListUser = Arrays.asList(Collections.singletonMap("k1", Arrays.asList(user)));
listMapMapUser = Arrays.asList(Collections.singletonMap("k1", Collections.singletonMap(user.name, user)));
mapListListUser = Collections.singletonMap("k1", Arrays.asList(Arrays.asList(user)));
mapListMapUser = Collections.singletonMap("k1", Arrays.asList(Collections.singletonMap(user.name, user)));
mapMapListUser = Collections.singletonMap("k1", Collections.singletonMap("k1", Arrays.asList(user)));
mapMapMapUser = Collections
.singletonMap("k1", Collections.singletonMap("k1", Collections.singletonMap(user.name, user)));
List<User> listUser = Arrays.asList(user, user);

Map<String, User> mapUser = new LinkedHashMap<>();
mapUser.put("k1", user);
mapUser.put("k2", user);

listListUser = Arrays.asList(listUser, listUser);
listMapUser = Arrays.asList(mapUser, mapUser);

mapListUser = new LinkedHashMap<>();
mapListUser.put("k1", listUser);
mapListUser.put("k2", listUser);

mapMapUser = new LinkedHashMap<>();
mapMapUser.put("k1", mapUser);
mapMapUser.put("k2", mapUser);

listListListUser = Arrays.asList(listListUser, listListUser);
listListMapUser = Arrays.asList(listMapUser, listMapUser);
listMapListUser = Arrays.asList(mapListUser, mapListUser);
listMapMapUser = Arrays.asList(mapMapUser, mapMapUser);

mapListListUser = new LinkedHashMap<>();
mapListListUser.put("k1", listListUser);
mapListListUser.put("k2", listListUser);

mapListMapUser = new LinkedHashMap<>();
mapListMapUser.put("k1", listMapUser);
mapListMapUser.put("k2", listMapUser);

mapMapListUser = new LinkedHashMap<>();
mapMapListUser.put("k1", mapListUser);
mapMapListUser.put("k2", mapListUser);

mapMapMapUser = new LinkedHashMap<>();
mapMapMapUser.put("k1", mapMapUser);
mapMapMapUser.put("k2", mapMapUser);
}
}

Expand Down Expand Up @@ -133,19 +156,36 @@ public void init() {
ProtoMapMapUser protoMapMapUser = new ProtoMapMapUser();
protoMapMapUser.init();

listListUser = Arrays.asList(protoListUser);
listMapUser = Arrays.asList(protoMapUser);
mapListUser = Collections.singletonMap("k1", protoListUser);
mapMapUser = Collections.singletonMap("k1", protoMapUser);

listListListUser = Arrays.asList(protoListListUser);
listListMapUser = Arrays.asList(protoListMapUser);
listMapListUser = Arrays.asList(protoMapListUser);
listMapMapUser = Arrays.asList(protoMapMapUser);
mapListListUser = Collections.singletonMap("k1", protoListListUser);
mapListMapUser = Collections.singletonMap("k1", protoListMapUser);
mapMapListUser = Collections.singletonMap("k1", protoMapListUser);
mapMapMapUser = Collections.singletonMap("k1", protoMapMapUser);
listListUser = Arrays.asList(protoListUser, protoListUser);
listMapUser = Arrays.asList(protoMapUser, protoMapUser);
mapListUser = new LinkedHashMap<>();
mapListUser.put("k1", protoListUser);
mapListUser.put("k2", protoListUser);

mapMapUser = new LinkedHashMap<>();
mapMapUser.put("k1", protoMapUser);
mapMapUser.put("k2", protoMapUser);

listListListUser = Arrays.asList(protoListListUser, protoListListUser);
listListMapUser = Arrays.asList(protoListMapUser, protoListMapUser);
listMapListUser = Arrays.asList(protoMapListUser, protoMapListUser);
listMapMapUser = Arrays.asList(protoMapMapUser, protoMapMapUser);

mapListListUser = new LinkedHashMap<>();
mapListListUser.put("k1", protoListListUser);
mapListListUser.put("k2", protoListListUser);

mapListMapUser = new LinkedHashMap<>();
mapListMapUser.put("k1", protoListMapUser);
mapListMapUser.put("k2", protoListMapUser);

mapMapListUser = new LinkedHashMap<>();
mapMapListUser.put("k1", protoMapListUser);
mapMapListUser.put("k2", protoMapListUser);

mapMapMapUser = new LinkedHashMap<>();
mapMapMapUser.put("k1", protoMapMapUser);
mapMapMapUser.put("k2", protoMapMapUser);
}
}

Expand All @@ -155,7 +195,7 @@ public static class ProtoListListUser {
public void init() {
ProtoListUser protoListUser = new ProtoListUser();
protoListUser.init();
value = Arrays.asList(protoListUser);
value = Arrays.asList(protoListUser, protoListUser);
}
}

Expand All @@ -165,7 +205,7 @@ public static class ProtoListMapUser {
public void init() {
ProtoMapUser protoMapUser = new ProtoMapUser();
protoMapUser.init();
value = Arrays.asList(protoMapUser);
value = Arrays.asList(protoMapUser, protoMapUser);
}
}

Expand All @@ -175,7 +215,9 @@ public static class ProtoMapListUser {
public void init() {
ProtoListUser protoListUser = new ProtoListUser();
protoListUser.init();
value = Collections.singletonMap("k1", protoListUser);
value = new LinkedHashMap<>();
value.put("k1", protoListUser);
value.put("k2", protoListUser);
}
}

Expand All @@ -185,36 +227,44 @@ public static class ProtoMapMapUser {
public void init() {
ProtoMapUser protoMapUser = new ProtoMapUser();
protoMapUser.init();
value = Collections.singletonMap("k1", protoMapUser);
value = new LinkedHashMap<>();
value.put("k1", protoMapUser);
value.put("k2", protoMapUser);
}
}

public static class ProtoListUser {
public List<User> value;

public void init() {
value = Arrays.asList(user);
value = Arrays.asList(user, user);
}
}

public static class ProtoMapUser {
public Map<String, User> value;

public void init() {
value = Collections.singletonMap(user.name, user);
value = new LinkedHashMap<>();
value.put("k1", user);
value.put("k2", user);
}
}

@SuppressWarnings("unchecked")
@Test
public void pojoModel() throws IOException {
RootSerializer pojoSerializer = modelProtoMapper.findRootSerializer("PojoModel");
RootDeserializer pojoMapDeserializer = modelProtoMapper.createRootDeserializer(Map.class, "PojoModel");
RootDeserializer pojoModelDeserializer = modelProtoMapper.createRootDeserializer(PojoModel.class, "PojoModel");

RootSerializer protoSerializer = modelProtoMapper.findRootSerializer("ProtoModel");
RootDeserializer protoMapDeserializer = modelProtoMapper.createRootDeserializer(Map.class, "ProtoModel");
RootDeserializer protoModelDeserializer = modelProtoMapper.createRootDeserializer(ProtoModel.class, "ProtoModel");
RootSerializer pojoSerializer = modelProtoMapper.createRootSerializer("PojoModel", PojoModel.class);
RootDeserializer<Map<String, Object>> pojoMapDeserializer = modelProtoMapper
.createRootDeserializer("PojoModel", Map.class);
RootDeserializer<PojoModel> pojoModelDeserializer = modelProtoMapper
.createRootDeserializer("PojoModel", PojoModel.class);

RootSerializer protoSerializer = modelProtoMapper.createRootSerializer("ProtoModel", ProtoModel.class);
RootDeserializer<Map<String, Object>> protoMapDeserializer = modelProtoMapper
.createRootDeserializer("ProtoModel", Map.class);
RootDeserializer<ProtoModel> protoModelDeserializer = modelProtoMapper
.createRootDeserializer("ProtoModel", ProtoModel.class);

PojoModel pojoModel = new PojoModel();
pojoModel.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.apache.servicecomb.foundation.protobuf.ProtoMapperFactory;
import org.apache.servicecomb.foundation.protobuf.RootDeserializer;
import org.apache.servicecomb.foundation.protobuf.RootSerializer;
import org.apache.servicecomb.foundation.protobuf.internal.model.PrimitiveArrays;
import org.apache.servicecomb.foundation.protobuf.internal.model.PrimitiveWrapperArrays;
import org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot;
import org.apache.servicecomb.foundation.protobuf.internal.model.Root;
import org.junit.Assert;
Expand All @@ -41,6 +43,18 @@ public class TestSchemaBase {

protected static RootDeserializer<Root> rootDeserializer = protoMapper.createRootDeserializer("Root", Root.class);

protected static RootSerializer primitiveArraysSerializer = protoMapper
.createRootSerializer("Root", PrimitiveArrays.class);

protected static RootDeserializer<Root> primitiveArraysDeserializer = protoMapper
.createRootDeserializer("Root", PrimitiveArrays.class);

protected static RootSerializer primitiveWrapperArraysSerializer = protoMapper
.createRootSerializer("Root", PrimitiveWrapperArrays.class);

protected static RootDeserializer<Root> primitiveWrapperArraysDeserializer = protoMapper
.createRootDeserializer("Root", PrimitiveWrapperArrays.class);

protected static RootDeserializer<Map<String, Object>> mapRootDeserializer = protoMapper
.createRootDeserializer("Root", Map.class);

Expand Down Expand Up @@ -83,6 +97,44 @@ protected void initField(String fieldName) {
}
}

protected void checkRepeatedWithPrimitive() throws Exception {
check();

check(primitiveArraysDeserializer, mapRootDeserializer, primitiveArraysSerializer, false);
String primitiveFieldName = scbMap.keySet().iterator().next();
java.lang.reflect.Field primitiveField = PrimitiveArrays.class.getDeclaredField(primitiveFieldName);
primitiveField.setAccessible(true);
Object primitiveArray = primitiveField.get(scbRoot);

check(primitiveWrapperArraysDeserializer, mapRootDeserializer, primitiveWrapperArraysSerializer, false);
String wrapperFieldName = scbMap.keySet().iterator().next();
java.lang.reflect.Field wrapperField = PrimitiveWrapperArrays.class.getDeclaredField(wrapperFieldName);
wrapperField.setAccessible(true);
Object[] array = (Object[]) wrapperField.get(scbRoot);

// dynamic strings
String[] strings = new String[array.length];
for (int idx = 0; idx < array.length; idx++) {
strings[idx] = array[idx].toString();
}
scbMap.clear();
scbMap.put(primitiveFieldName, strings);
scbMapBytes = primitiveArraysSerializer.serialize(scbMap);
Assert.assertArrayEquals(protobufBytes, scbMapBytes);

// dynamic primitive array
scbMap.clear();
scbMap.put(primitiveFieldName, primitiveArray);
scbMapBytes = primitiveArraysSerializer.serialize(scbMap);
Assert.assertArrayEquals(protobufBytes, scbMapBytes);

// dynamic array
scbMap.clear();
scbMap.put(primitiveFieldName, array);
scbMapBytes = primitiveArraysSerializer.serialize(scbMap);
Assert.assertArrayEquals(protobufBytes, scbMapBytes);
}

protected void check() throws IOException {
check(rootDeserializer, mapRootDeserializer, rootSerializer, false);
}
Expand All @@ -99,11 +151,11 @@ protected <T> void check(RootDeserializer<T> deserializer, RootDeserializer<Map<
// 2.weak type deserialize
scbMap = mapDeserializer.deserialize(protobufBytes);
// 3.weak type serialize
scbMapBytes = rootSerializer.serialize(scbMap);
scbMapBytes = serializer.serialize(scbMap);
// 4.strong type deserialize
scbRoot = deserializer.deserialize(scbMapBytes);
// 5.strong type serialize
scbRootBytes = rootSerializer.serialize(scbRoot);
scbRootBytes = serializer.serialize(scbRoot);

if (print) {
System.out.println("scbRoot bytes:" + Hex.encodeHexString(scbRootBytes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.lang.reflect.Method;

import org.apache.servicecomb.foundation.common.utils.ReflectUtils;
import org.apache.servicecomb.foundation.common.utils.bean.Getter;
import org.apache.servicecomb.foundation.common.utils.bean.IntGetter;
import org.apache.servicecomb.foundation.common.utils.bean.IntSetter;
import org.apache.servicecomb.foundation.common.utils.bean.Setter;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -100,38 +104,42 @@ public void getOrCreate() {
Assert.assertSame(Model.class, beanDescriptor.getJavaType().getRawClass());
}

@SuppressWarnings("unchecked")
@Test
public void both() throws Throwable {
public void both() {
PropertyDescriptor propertyDescriptor = beanDescriptor.getPropertyDescriptors().get("both");
propertyDescriptor.getSetter().set(model, 1);
Assert.assertEquals(1, propertyDescriptor.getGetter().get(model));
((IntSetter<Model>) propertyDescriptor.getSetter()).set(model, 1);
Assert.assertEquals(1, ((IntGetter<Model>) propertyDescriptor.getGetter()).get(model));
Assert.assertEquals(1, model.getBoth());
}

@SuppressWarnings("unchecked")
@Test
public void onlyGet() throws Throwable {
public void onlyGet() {
PropertyDescriptor propertyDescriptor = beanDescriptor.getPropertyDescriptors().get("onlyGet");
Assert.assertNull(propertyDescriptor.getSetter());

model.onlyGet(1);
Assert.assertEquals(1, propertyDescriptor.getGetter().get(model));
Assert.assertEquals(1, ((IntGetter<Model>) propertyDescriptor.getGetter()).get(model));
Assert.assertEquals(1, model.getOnlyGet());
}

@SuppressWarnings("unchecked")
@Test
public void onlySet() throws Throwable {
public void onlySet() {
PropertyDescriptor propertyDescriptor = beanDescriptor.getPropertyDescriptors().get("onlySet");
Assert.assertNull(propertyDescriptor.getGetter());

propertyDescriptor.getSetter().set(model, 1);
((IntSetter<Model>) propertyDescriptor.getSetter()).set(model, 1);
Assert.assertEquals(1, model.onlySet());
}

@SuppressWarnings("unchecked")
@Test
public void direct() throws Throwable {
public void direct() {
PropertyDescriptor propertyDescriptor = beanDescriptor.getPropertyDescriptors().get("direct");
propertyDescriptor.getSetter().set(model, 1);
Assert.assertEquals(1, propertyDescriptor.getGetter().get(model));
((Setter<Model, Integer>) propertyDescriptor.getSetter()).set(model, 1);
Assert.assertEquals(1, (int) ((Getter<Model, Integer>) propertyDescriptor.getGetter()).get(model));
Assert.assertEquals(1, model.direct);
}
}
Loading

0 comments on commit 41e843a

Please sign in to comment.