Skip to content

Commit

Permalink
test: add more unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinShunKang committed Mar 19, 2020
1 parent c6770df commit 870387b
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ public void reset() {
if (_types != null) {
_types.clear();
}

_offset = 0;
_length = 0;
}

@Override
Expand Down
134 changes: 121 additions & 13 deletions src/test/java/com/alibaba/com/caucho/hessian/io/Hessian2ReuseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,139 @@
*/
package com.alibaba.com.caucho.hessian.io;

import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
import com.alibaba.com.caucho.hessian.io.beans.*;
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class Hessian2ReuseTest {
public class Hessian2ReuseTest extends SerializeTestBase {

private static final Hessian2Input h2i = new Hessian2Input(null);

private static final Hessian2Output h2o = new Hessian2Output(null);

@SuppressWarnings("unchecked")
private static <T> T serializeAndDeserialize(T obj, Class<T> clazz) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
h2o.init(outputStream);
h2o.writeObject(obj);
h2o.flush();

h2i.init(new ByteArrayInputStream(outputStream.toByteArray()));
return (T) h2i.readObject(clazz);
}

@Test
public void testString() throws IOException {
for (int i = 0; i < 100; i++) {
String obj = "Hello, Hessian2, round:" + i;

String newObj = serializeAndDeserialize(obj, String.class);
Assert.assertEquals(obj, newObj);

String newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

@Test
public void testDate() throws IOException {
long mills = System.currentTimeMillis();
serializeAndDeserialize(new Date(mills), Date.class);
serializeAndDeserialize(new Date(mills + 1000_000), Date.class);
}

@Test
public void testBaseUser() throws IOException {
for (int i = 0; i < 100; i++) {
BaseUser obj = new BaseUser();
obj.setUserId(i * ThreadLocalRandom.current().nextInt(10000));
obj.setUserName(String.valueOf(System.currentTimeMillis()));

BaseUser newObj = serializeAndDeserialize(obj, BaseUser.class);
Assert.assertEquals(obj, newObj);

BaseUser newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

@Test
public void test() throws IOException {
Hessian2Input h2i = new Hessian2Input(null);
Hessian2Output h2o = new Hessian2Output(null);
public void testSubUser() throws IOException {
for (int i = 0; i < 100; i++) {
String str = "Hello, Hessian2, round:" + i;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
h2o.init(outputStream);
h2o.writeObject(str);
h2o.flush();
SubUser obj = new SubUser();
obj.setUserId(i * ThreadLocalRandom.current().nextInt(10000));
obj.setUserName(String.valueOf(System.currentTimeMillis()));
obj.setAgeList(Arrays.asList(12, 13, 14, 15));
obj.setSexyList(Arrays.asList(true, false));
obj.setWeightList(Arrays.asList(120D, 130D, 140D));

byte[] bytes = outputStream.toByteArray();
SubUser newObj = serializeAndDeserialize(obj, SubUser.class);
Assert.assertEquals(obj, newObj);

h2i.init(new ByteArrayInputStream(bytes));
String newStr = (String) h2i.readObject(String.class);
Assert.assertEquals(str, newStr);
SubUser newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

@Test
public void testGrandsonUser() throws IOException {
for (int i = 0; i < 100; i++) {
GrandsonUser obj = new GrandsonUser();
obj.setUserId(i * ThreadLocalRandom.current().nextInt(10000));
obj.setUserName(String.valueOf(System.currentTimeMillis()));
obj.setAgeList(Arrays.asList(12, 13, 14, 15));
obj.setSexyList(Arrays.asList(true, false));
obj.setWeightList(Arrays.asList(120D, 130D, 140D));

SubUser newObj = serializeAndDeserialize(obj, SubUser.class);
Assert.assertEquals(obj, newObj);

SubUser newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

@Test
public void testType() throws IOException {
serializeAndDeserialize(Type.Lower, Type.class);
serializeAndDeserialize(Type.Normal, Type.class);
serializeAndDeserialize(Type.High, Type.class);
}

@Test
public void testHessian2StringShortType() throws IOException {
for (int i = 0; i < 100; i++) {
Hessian2StringShortType obj = new Hessian2StringShortType();
obj.shortSet = new HashSet<>();
obj.stringShortMap = new HashMap<>();
obj.stringByteMap = new HashMap<>();
obj.stringPersonTypeMap = new HashMap<>();

obj.shortSet.add((short) i);
obj.shortSet.add((short) (i * 2));

// shortType.stringShortMap.put(String.valueOf(i), (short) i);
// shortType.stringShortMap.put(String.valueOf(i * 100), (short) (i * 100));

// shortType.stringByteMap.put(String.valueOf(i), (byte) 1);

List<Short> shorts = Arrays.asList((short) 12, (short) 4);
PersonType abc = new PersonType("ABC", 12, 128D, (short) 1, (byte) 2, shorts);
obj.stringPersonTypeMap.put("P_" + i, abc);

Hessian2StringShortType newObj = serializeAndDeserialize(obj, Hessian2StringShortType.class);
Assert.assertEquals(obj, newObj);

Hessian2StringShortType newObj2 = baseHessian2Serialize(obj);
Assert.assertEquals(newObj, newObj2);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.com.caucho.hessian.io.beans;

import java.io.Serializable;
import java.util.Objects;

/**
*/
Expand All @@ -40,4 +41,18 @@ public String getUserName() {
public void setUserName(String userName) {
this.userName = userName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BaseUser)) return false;
BaseUser user = (BaseUser) o;
return Objects.equals(userId, user.userId) &&
Objects.equals(userName, user.userName);
}

@Override
public int hashCode() {
return Objects.hash(userId, userName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.com.caucho.hessian.io.beans;

import java.io.Serializable;
import java.util.Objects;

/**
*/
Expand All @@ -32,4 +33,18 @@ public String getUserName() {
public void setUserName(String userName) {
this.userName = userName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof GrandsonUser)) return false;
if (!super.equals(o)) return false;
GrandsonUser that = (GrandsonUser) o;
return Objects.equals(userName, that.userName);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), userName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
Expand All @@ -36,4 +37,20 @@ public class Hessian2StringShortType implements Serializable {
public Hessian2StringShortType() {

}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Hessian2StringShortType)) return false;
Hessian2StringShortType that = (Hessian2StringShortType) o;
return Objects.equals(shortSet, that.shortSet) &&
Objects.equals(stringShortMap, that.stringShortMap) &&
Objects.equals(stringByteMap, that.stringByteMap) &&
Objects.equals(stringPersonTypeMap, that.stringPersonTypeMap);
}

@Override
public int hashCode() {
return Objects.hash(shortSet, stringShortMap, stringByteMap, stringPersonTypeMap);
}
}
18 changes: 18 additions & 0 deletions src/test/java/com/alibaba/com/caucho/hessian/io/beans/SubUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
*
Expand Down Expand Up @@ -62,4 +63,21 @@ public List<Boolean> getSexyList() {
public void setSexyList(List<Boolean> sexyList) {
this.sexyList = sexyList;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SubUser)) return false;
if (!super.equals(o)) return false;
SubUser subUser = (SubUser) o;
return Objects.equals(userName, subUser.userName) &&
Objects.equals(ageList, subUser.ageList) &&
Objects.equals(weightList, subUser.weightList) &&
Objects.equals(sexyList, subUser.sexyList);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), userName, ageList, weightList, sexyList);
}
}

0 comments on commit 870387b

Please sign in to comment.