Skip to content

Commit

Permalink
Merge pull request #281 from Chans-Open-Source/feature/modify_unit_te…
Browse files Browse the repository at this point in the history
…st_module_name

Fix: modify pr #279 unit test requires module name
  • Loading branch information
AlexStocks authored Oct 11, 2021
2 parents a0c5132 + cd57270 commit 1ec5c50
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 36 deletions.
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module github.com/apache/dubbo-go-hessian2

require (
dubbo-go-hessian2-dup-struct-name-test v0.0.3
github.com/dubbogo/gost v1.9.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.4.0
)

replace dubbo-go-hessian2-dup-struct-name-test => github.com/changedenczd/dubbo-go-hessian2-dup-struct-name-test v0.0.3
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
github.com/changedenczd/dubbo-go-hessian2-dup-struct-name-test v0.0.3 h1:31bvQXq/6SUwQlr72MUSloySci2nM6Rdfc2A0cT4eGA=
github.com/changedenczd/dubbo-go-hessian2-dup-struct-name-test v0.0.3/go.mod h1:hDdmLYpL9E/pGzbUrBl5mWkibKysKDYaKVkC3CcWtcA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dubbogo/gost v1.9.0 h1:UT+dWwvLyJiDotxJERO75jB3Yxgsdy10KztR5ycxRAk=
github.com/dubbogo/gost v1.9.0/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
78 changes: 49 additions & 29 deletions dup_struct_name_test.go → hessian_test/dup_struct_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@
* limitations under the License.
*/

package hessian
package hessian_test

import (
"bufio"
"bytes"
"testing"
"time"
)

import (
dup "dubbo-go-hessian2-dup-struct-name-test"
"github.com/apache/dubbo-go-hessian2"
dupclass "github.com/apache/dubbo-go-hessian2/hessian_test/hessian_test"
)

import (
"github.com/stretchr/testify/assert"
)

const (
ExpectedErrorMsg = "reflect.Set: value of type hessian.CaseZ is not assignable to type hessian.CaseZ"
ExpectedErrorMsg = "reflect.Set: value of type hessian_test.CaseZ is not assignable to type hessian_test.CaseZ"
)

type CaseZ struct {
Expand All @@ -43,24 +45,42 @@ func (CaseZ) JavaClassName() string {
return "com.test.caseZ"
}

func doTestHessianEncodeHeader(t *testing.T, packageType hessian.PackageType, responseStatus byte, body interface{}) ([]byte, error) {
codecW := hessian.NewHessianCodec(nil)
resp, err := codecW.Write(hessian.Service{
Path: "test",
Interface: "ITest",
Version: "v1.0",
Method: "test",
Timeout: time.Second * 10,
}, hessian.DubboHeader{
SerialID: 2,
Type: packageType,
ID: 1,
ResponseStatus: responseStatus,
}, body)
assert.Nil(t, err)
return resp, err
}

func TestDupStructNameRequest(t *testing.T) {
RegisterPOJO(&dup.CaseZ{})
RegisterPOJO(&CaseZ{})
hessian.RegisterPOJO(&dupclass.CaseZ{})
hessian.RegisterPOJO(&CaseZ{})

packageType := PackageRequest
responseStatus := Zero
packageType := hessian.PackageRequest
responseStatus := hessian.Zero
var body interface{}
body = []interface{}{"a"}
resp, err := doTestHessianEncodeHeader(t, packageType, responseStatus, body)
assert.Nil(t, err)

codecR := NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))
codecR := hessian.NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))

h := &DubboHeader{}
h := &hessian.DubboHeader{}
err = codecR.ReadHeader(h)
assert.Nil(t, err)
assert.Equal(t, byte(2), h.SerialID)
assert.Equal(t, packageType, h.Type&(PackageRequest|PackageResponse|PackageHeartbeat))
assert.Equal(t, packageType, h.Type&(hessian.PackageRequest|hessian.PackageResponse|hessian.PackageHeartbeat))
assert.Equal(t, int64(1), h.ID)
assert.Equal(t, responseStatus, h.ResponseStatus)

Expand All @@ -85,13 +105,13 @@ func TestDupStructNameResponse(t *testing.T) {
err, codecR, h := doTestHeader(t, body)
assert.Nil(t, err)

decodedResponse := &Response{}
decodedResponse.RspObj = &dup.CaseZ{}
decodedResponse := &hessian.Response{}
decodedResponse.RspObj = &dupclass.CaseZ{}
err = codecR.ReadBody(decodedResponse)
assert.NotNil(t, err)
assert.Equal(t, ExpectedErrorMsg, err.Error())

decodedResponse = &Response{}
decodedResponse = &hessian.Response{}
decodedResponse.RspObj = &CaseZ{}
err = codecR.ReadBody(decodedResponse)
assert.Nil(t, err)
Expand All @@ -109,55 +129,55 @@ func TestDupStructNameResponse2(t *testing.T) {
}()

var body interface{}
body = &dup.CaseZ{Name: "TestDupStructNameResponse"}
body = &dupclass.CaseZ{Name: "TestDupStructNameResponse"}
err, codecR, h := doTestHeader(t, body)
assert.Nil(t, err)

decodedResponse := &Response{}
decodedResponse := &hessian.Response{}
decodedResponse.RspObj = &CaseZ{}
err = codecR.ReadBody(decodedResponse)
assert.NotNil(t, err)
assert.Equal(t, ExpectedErrorMsg, err.Error())

decodedResponse = &Response{}
decodedResponse.RspObj = &dup.CaseZ{}
decodedResponse = &hessian.Response{}
decodedResponse.RspObj = &dupclass.CaseZ{}
err = codecR.ReadBody(decodedResponse)
assert.Nil(t, err)

checkResponseBody(t, decodedResponse, h, body)
}

func doTestHeader(t *testing.T, body interface{}) (error, *HessianCodec, *DubboHeader) {
RegisterPOJO(&dup.CaseZ{})
RegisterPOJO(&CaseZ{})
func doTestHeader(t *testing.T, body interface{}) (error, *hessian.HessianCodec, *hessian.DubboHeader) {
hessian.RegisterPOJO(&dupclass.CaseZ{})
hessian.RegisterPOJO(&CaseZ{})

packageType := PackageResponse
responseStatus := Response_OK
packageType := hessian.PackageResponse
responseStatus := hessian.Response_OK
resp, err := doTestHessianEncodeHeader(t, packageType, responseStatus, body)
assert.Nil(t, err)

codecR := NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))
codecR := hessian.NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))

h := &DubboHeader{}
h := &hessian.DubboHeader{}
err = codecR.ReadHeader(h)
assert.Nil(t, err)

assert.Equal(t, byte(2), h.SerialID)
assert.Equal(t, packageType, h.Type&(PackageRequest|PackageResponse|PackageHeartbeat))
assert.Equal(t, packageType, h.Type&(hessian.PackageRequest|hessian.PackageResponse|hessian.PackageHeartbeat))
assert.Equal(t, int64(1), h.ID)
assert.Equal(t, responseStatus, h.ResponseStatus)
return err, codecR, h
}

func checkResponseBody(t *testing.T, decodedResponse *Response, h *DubboHeader, body interface{}) {
func checkResponseBody(t *testing.T, decodedResponse *hessian.Response, h *hessian.DubboHeader, body interface{}) {
t.Log(decodedResponse)

if h.ResponseStatus != Zero && h.ResponseStatus != Response_OK {
if h.ResponseStatus != hessian.Zero && h.ResponseStatus != hessian.Response_OK {
assert.Equal(t, "java exception:"+body.(string), decodedResponse.Exception.Error())
return
}

in, _ := EnsureInterface(UnpackPtrValue(EnsurePackValue(body)), nil)
out, _ := EnsureInterface(UnpackPtrValue(EnsurePackValue(decodedResponse.RspObj)), nil)
in, _ := hessian.EnsureInterface(hessian.UnpackPtrValue(hessian.EnsurePackValue(body)), nil)
out, _ := hessian.EnsureInterface(hessian.UnpackPtrValue(hessian.EnsurePackValue(decodedResponse.RspObj)), nil)
assert.Equal(t, in, out)
}
27 changes: 27 additions & 0 deletions hessian_test/hessian_test/dupclass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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_test

type CaseZ struct {
Name string
Age int
}

func (CaseZ) JavaClassName() string {
return "com.test.caseZz"
}
5 changes: 4 additions & 1 deletion pojo.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ var (
javaEnumType = reflect.TypeOf((*POJOEnum)(nil)).Elem()

goPkgPathWhiteListRegexp = regexp.MustCompile(`^(github\.com/apache/dubbo-go-hessian2|time)`)
goPkgPathBlackListRegexp = regexp.MustCompile(`^(github\.com/apache/dubbo-go-hessian2/hessian_test)`)
)

// struct parsing
Expand Down Expand Up @@ -254,7 +255,9 @@ func getGoName(o interface{}) string {
func combineGoName(t reflect.Type) string {
pkgPath := t.PkgPath()
goName := t.String()
if pkgPath == "" || goPkgPathWhiteListRegexp.Match([]byte(pkgPath)) {
if pkgPath == "" ||
(goPkgPathWhiteListRegexp.Match([]byte(pkgPath)) &&
!goPkgPathBlackListRegexp.Match([]byte(pkgPath))) {
return goName
}
return pkgPath + "/" + goName
Expand Down

0 comments on commit 1ec5c50

Please sign in to comment.