加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
object_test.go 11.83 KB
一键复制 编辑 原始数据 按行查看 历史
老马 提交于 2019-12-14 11:02 . first
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hessian
import (
"math"
"reflect"
"testing"
)
type Department struct {
Name string
}
func (Department) JavaClassName() string {
return "com.bdt.info.Department"
}
type WorkerInfo struct {
unexportedFiled string
Name string
Addrress string
Age int
Salary float32
Payload map[string]int32
FamilyMembers []string `hessian:"familyMembers1"`
FamilyPhoneNumber string // default convert to => familyPhoneNumber
Dpt Department
}
func (WorkerInfo) JavaClassName() string {
return "com.bdt.info.WorkerInfo"
}
func TestEncEmptyStruct(t *testing.T) {
var (
w WorkerInfo
err error
e *Encoder
d *Decoder
res interface{}
)
e = NewEncoder()
w = WorkerInfo{
Name: "Trump",
Addrress: "W,D.C.",
Age: 72,
Salary: 21000.03,
Payload: map[string]int32{"Number": 2017061118},
FamilyMembers: []string{"m1", "m2", "m3"},
FamilyPhoneNumber: "010-12345678",
// Dpt: Department{
// Name: "Adm",
// },
}
e.Encode(w)
d = NewDecoder(e.Buffer())
res, err = d.Decode()
if err != nil {
t.Errorf("Decode() = %+v", err)
}
t.Logf("decode(%v) = %v, %v\n", w, res, err)
reflect.DeepEqual(w, res)
}
func TestEncStruct(t *testing.T) {
var (
w WorkerInfo
err error
e *Encoder
d *Decoder
res interface{}
)
e = NewEncoder()
w = WorkerInfo{
Name: "Trump",
Addrress: "W,D.C.",
Age: 72,
Salary: 21000.03,
Payload: map[string]int32{"Number": 2017061118},
FamilyMembers: []string{"m1", "m2", "m3"},
FamilyPhoneNumber: "010-12345678",
Dpt: Department{
Name: "Adm",
},
unexportedFiled: "you cannot see me!",
}
wCopy := w
wCopy.unexportedFiled = ""
err = e.Encode(w)
if err != nil {
t.Errorf("Encode() = %+v", err)
}
d = NewDecoder(e.Buffer())
res, err = d.Decode()
if err != nil {
t.Errorf("Decode() = %+v", err)
}
t.Logf("decode(%+v) = %+v, %v\n", w, res, err)
w2, ok := res.(*WorkerInfo)
if !ok {
t.Fatalf("res:%T is not of type WorkerInfo", w2)
}
if !reflect.DeepEqual(wCopy, *w2) {
t.Fatalf("w != w2:\n%#v\n!=\n%#v", wCopy, w2)
}
}
type UserName struct {
FirstName string
LastName string
}
func (UserName) JavaClassName() string {
return "com.bdt.info.UserName"
}
type Person struct {
UserName
Age int32
Sex bool
}
func (Person) JavaClassName() string {
return "com.bdt.info.Person"
}
type JOB struct {
Title string
Company string
}
func (JOB) JavaClassName() string {
return "com.bdt.info.JOB"
}
type Worker struct {
Person
CurJob JOB
Jobs []JOB
}
func (Worker) JavaClassName() string {
return "com.bdt.info.Worker"
}
func TestIssue6(t *testing.T) {
name := UserName{
FirstName: "John",
LastName: "Doe",
}
person := Person{
UserName: name,
Age: 18,
Sex: true,
}
worker := &Worker{
Person: person,
CurJob: JOB{Title: "cto", Company: "facebook"},
Jobs: []JOB{
JOB{Title: "manager", Company: "google"},
JOB{Title: "ceo", Company: "microsoft"},
},
}
e := NewEncoder()
err := e.Encode(worker)
if err != nil {
t.Fatalf("encode(worker:%#v) = error:%s", worker, err)
}
bytes := e.Buffer()
d := NewDecoder(bytes)
res, err := d.Decode()
if err != nil {
t.Error(err)
t.FailNow()
}
t.Logf("type of decode object:%v", reflect.TypeOf(res))
worker2, ok := res.(*Worker)
if !ok {
t.Fatalf("res:%#v is not of type Worker", res)
}
if !reflect.DeepEqual(worker, worker2) {
t.Fatalf("worker:%#v != worker2:%#v", worker, worker2)
}
}
type A0 struct{}
func (*A0) JavaClassName() string {
return "com.caucho.hessian.test.A0"
}
type A1 struct{}
func (*A1) JavaClassName() string {
return "com.caucho.hessian.test.A1"
}
type A2 struct{}
func (*A2) JavaClassName() string {
return "com.caucho.hessian.test.A2"
}
type A3 struct{}
func (*A3) JavaClassName() string {
return "com.caucho.hessian.test.A3"
}
type A4 struct{}
func (*A4) JavaClassName() string {
return "com.caucho.hessian.test.A4"
}
type A5 struct{}
func (*A5) JavaClassName() string {
return "com.caucho.hessian.test.A5"
}
type A6 struct{}
func (*A6) JavaClassName() string {
return "com.caucho.hessian.test.A6"
}
type A7 struct{}
func (*A7) JavaClassName() string {
return "com.caucho.hessian.test.A7"
}
type A8 struct{}
func (*A8) JavaClassName() string {
return "com.caucho.hessian.test.A8"
}
type A9 struct{}
func (*A9) JavaClassName() string {
return "com.caucho.hessian.test.A9"
}
type A10 struct{}
func (*A10) JavaClassName() string {
return "com.caucho.hessian.test.A10"
}
type A11 struct{}
func (*A11) JavaClassName() string {
return "com.caucho.hessian.test.A11"
}
type A12 struct{}
func (*A12) JavaClassName() string {
return "com.caucho.hessian.test.A12"
}
type A13 struct{}
func (*A13) JavaClassName() string {
return "com.caucho.hessian.test.A13"
}
type A14 struct{}
func (*A14) JavaClassName() string {
return "com.caucho.hessian.test.A14"
}
type A15 struct{}
func (*A15) JavaClassName() string {
return "com.caucho.hessian.test.A15"
}
type A16 struct{}
func (*A16) JavaClassName() string {
return "com.caucho.hessian.test.A16"
}
type TestObjectStruct struct {
Value int32 `hessian:"_value"`
}
func (*TestObjectStruct) JavaClassName() string {
return "com.caucho.hessian.test.TestObject"
}
type TestConsStruct struct {
First string `hessian:"_first"`
Rest *TestConsStruct `hessian:"_rest"`
}
func (*TestConsStruct) JavaClassName() string {
return "com.caucho.hessian.test.TestCons"
}
func TestObject(t *testing.T) {
RegisterPOJOs(
&A0{},
&A1{},
&A2{},
&A3{},
&A4{},
&A5{},
&A6{},
&A7{},
&A8{},
&A9{},
&A10{},
&A11{},
&A12{},
&A13{},
&A14{},
&A15{},
&A16{},
&TestObjectStruct{},
&TestConsStruct{},
)
testDecodeFramework(t, "replyObject_0", &A0{})
testDecodeFramework(t, "replyObject_1", &TestObjectStruct{Value: 0})
testDecodeFramework(t, "replyObject_16", []interface{}{&A0{}, &A1{}, &A2{}, &A3{}, &A4{}, &A5{}, &A6{}, &A7{}, &A8{}, &A9{}, &A10{}, &A11{}, &A12{}, &A13{}, &A14{}, &A15{}, &A16{}})
testDecodeFramework(t, "replyObject_2", []interface{}{&TestObjectStruct{Value: 0}, &TestObjectStruct{Value: 1}})
testDecodeFramework(t, "replyObject_2b", []interface{}{&TestObjectStruct{Value: 0}, &TestObjectStruct{Value: 0}})
object := TestObjectStruct{Value: 0}
object2a := []interface{}{&object, &object}
testDecodeFramework(t, "replyObject_2a", object2a)
cons := TestConsStruct{}
cons.First = "a"
cons.Rest = &cons
testDecodeFramework(t, "replyObject_3", &cons)
}
func TestObjectEncode(t *testing.T) {
testJavaDecode(t, "argObject_0", &A0{})
testJavaDecode(t, "argObject_1", &TestObjectStruct{Value: 0})
testJavaDecode(t, "argObject_16", []interface{}{&A0{}, &A1{}, &A2{}, &A3{}, &A4{}, &A5{}, &A6{}, &A7{}, &A8{}, &A9{}, &A10{}, &A11{}, &A12{}, &A13{}, &A14{}, &A15{}, &A16{}})
testJavaDecode(t, "argObject_2", []interface{}{&TestObjectStruct{Value: 0}, &TestObjectStruct{Value: 1}})
testJavaDecode(t, "argObject_2b", []interface{}{&TestObjectStruct{Value: 0}, &TestObjectStruct{Value: 0}})
object := TestObjectStruct{Value: 0}
object2a := []interface{}{&object, &object}
testJavaDecode(t, "argObject_2a", object2a)
cons := TestConsStruct{}
cons.First = "a"
cons.Rest = &cons
testJavaDecode(t, "argObject_3", &cons)
}
type Tuple struct {
Byte int8
Short int16
Integer int32
Long int64
Double float32
B uint8
S uint16
I uint32
L uint64
D float64
}
func (t Tuple) JavaClassName() string {
return "test.tuple.Tuple"
}
func TestDecodeJavaTupleObject(t *testing.T) {
tuple := &Tuple{
Byte: 1,
Short: 1,
Integer: 1,
Long: 1,
Double: 1.23,
B: 0x01,
S: 1,
I: 1,
L: 1,
D: 1.23,
}
RegisterPOJO(tuple)
testDecodeJavaData(t, "getTheTuple", "test.tuple.TupleProviderImpl", false, tuple)
}
func TestEncodeDecodeTuple(t *testing.T) {
doTestEncodeDecodeTuple(t, &Tuple{
Byte: 1,
Short: 1,
Integer: 1,
Long: 1,
Double: 1.23,
B: 0x01,
S: 1,
I: 1,
L: 1,
D: 1.23,
})
doTestEncodeDecodeTuple(t, &Tuple{
Byte: math.MinInt8,
Short: math.MinInt16,
Integer: math.MinInt32,
Long: math.MinInt64,
Double: -99.99,
B: 0x00,
S: 0,
I: 0,
L: 0,
D: -9999.9999,
})
doTestEncodeDecodeTuple(t, &Tuple{
Byte: math.MaxInt8,
Short: math.MaxInt16,
Integer: math.MaxInt32,
Long: math.MaxInt64,
Double: math.MaxFloat32,
B: 0xFF,
S: 0xFFFF,
I: 0xFFFFFFFF,
L: 0xFFFFFFFFFFFFFFFF,
D: math.MaxFloat64,
})
}
func doTestEncodeDecodeTuple(t *testing.T, tuple *Tuple) {
e := NewEncoder()
err := e.encObject(tuple)
if err != nil {
t.Error(err)
t.FailNow()
}
d := NewDecoder(e.buffer)
decObj, err := d.Decode()
if err != nil {
t.Error(err)
t.FailNow()
}
if !reflect.DeepEqual(tuple, decObj) {
t.Errorf("expect: %v, but get: %v", tuple, decObj)
}
}
type BasePointer struct {
A *bool
}
func (t BasePointer) JavaClassName() string {
return "test.base.Base"
}
func TestBasePointer(t *testing.T) {
v := true
base := BasePointer{
A: &v,
}
doTestBasePointer(t, &base, &base)
base = BasePointer{
A: nil,
}
expectedF := false
expectedBase := BasePointer{
A: &expectedF,
}
doTestBasePointer(t, &base, &expectedBase)
}
func doTestBasePointer(t *testing.T, base *BasePointer, expected *BasePointer) {
e := NewEncoder()
err := e.encObject(base)
if err != nil {
t.Error(err)
t.FailNow()
}
d := NewDecoder(e.buffer)
decObj, err := d.Decode()
if err != nil {
t.Error(err)
t.FailNow()
}
if !reflect.DeepEqual(expected, decObj) {
t.Errorf("expect: %v, but get: %v", base, decObj)
}
}
func TestSkip(t *testing.T) {
// clear pojo
pojoRegistry = POJORegistry{
j2g: make(map[string]string),
registry: make(map[string]structInfo),
}
testDecodeFrameworkWithSkip(t, "replyObject_0", nil)
testDecodeFrameworkWithSkip(t, "replyObject_1", nil)
testDecodeFrameworkWithSkip(t, "replyObject_16", make([]interface{}, 17))
testDecodeFrameworkWithSkip(t, "replyObject_2a", make([]interface{}, 2))
testDecodeFrameworkWithSkip(t, "replyObject_3", nil)
testDecodeFrameworkWithSkip(t, "replyTypedMap_0", make(map[interface{}]interface{}))
testDecodeFrameworkWithSkip(t, "replyTypedFixedList_0", make([]string, 0))
testDecodeFrameworkWithSkip(t, "replyUntypedFixedList_0", []interface{}{})
testDecodeFrameworkWithSkip(t, "customReplyTypedFixedListHasNull", make([]Object, 3))
testDecodeFrameworkWithSkip(t, "customReplyTypedVariableListHasNull", make([]Object, 3))
testDecodeFrameworkWithSkip(t, "customReplyUntypedFixedListHasNull", make([]interface{}, 3))
testDecodeFrameworkWithSkip(t, "customReplyUntypedVariableListHasNull", make([]interface{}, 3))
testDecodeFrameworkWithSkip(t, "customReplyTypedFixedList_A0", make([]interface{}, 3))
testDecodeFrameworkWithSkip(t, "customReplyTypedVariableList_A0", make([]interface{}, 3))
testDecodeFrameworkWithSkip(t, "customReplyTypedFixedList_Test", nil)
testDecodeFrameworkWithSkip(t, "customReplyTypedFixedList_Object", make([]Object, 1))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化