Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some exception decoding #62

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,4 @@ func (d *Decoder) DecodeValue() (interface{}, error) {
default:
return nil, perrors.Errorf("Invalid type: %v,>>%v<<<", string(tag), d.peek(d.len()))
}
}
}
109 changes: 109 additions & 0 deletions exception/exp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2016-2019 tianying Pan
//
// Licensed 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 exception

import (
"log"
"os"
"os/exec"
"reflect"
"testing"
)
import (
hessian "github.com/dubbogo/hessian2"
)
const (
hessianJar = "../test_hessian/target/test_hessian-1.0.0.jar"
)
type _refHolder struct {
// destinations
destinations []reflect.Value

value reflect.Value
}
func isFileExist(file string) bool {
stat, err := os.Stat(file)
if err != nil {
return false
}

return !stat.IsDir()
}

func genHessianJar() {
existFlag := isFileExist(hessianJar)
if existFlag {
return
}

cmd := exec.Command("mvn", "clean", "package")
cmd.Dir = "./test_hessian"
out, err := cmd.Output()
if err != nil {
log.Fatalf("after exec command 'mvn clean package', got error:%v, error output:%v",
err, string(out))
}
}

func getReply(method string) []byte {
genHessianJar()
cmd := exec.Command("java", "-jar", hessianJar, method)
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return out
}

func decodeResponse(method string) (interface{}, error) {
b := getReply(method)
d := hessian.NewDecoder(b)
r, e := d.Decode()
if e != nil {
return nil, e
}
return r, nil
}

func testDecodeFramework(t *testing.T, method string, expected interface{}) {
r, e := decodeResponse(method)
if e != nil {
t.Errorf("%s: decode fail with error %v", method, e)
return
}

tmp, ok := r.(*_refHolder)
if ok {
r = tmp.value.Interface()
}
if !reflect.DeepEqual(r, expected) {
t.Errorf("%s: got %v, wanted %v", method, r, expected)
}
}

func testDecodeFrameworkFunc(t *testing.T, method string, expected func(interface{})) {
r, e := decodeResponse(method)
if e != nil {
t.Errorf("%s: decode fail with error %v", method, e)
return
}

tmp, ok := r.(*_refHolder)
if ok {
r = tmp.value.Interface()
}
expected(r)
}

40 changes: 40 additions & 0 deletions exception/malformed_parameterized_type_exception.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2016-2019 tianying Pan
//
// Licensed 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 exception

import (
hessian "github.com/dubbogo/hessian2"
)
func init(){
hessian.RegisterPOJO(&MalformedParameterizedTypeException{})
}
type MalformedParameterizedTypeException struct {
SerialVersionUID int64
DetailMessage string
SuppressedExceptions []hessian.Exception
StackTrace []hessian.StackTraceElement
Cause *MalformedParameterizedTypeException
}
func (e MalformedParameterizedTypeException) Error() string {
return "MalformedParameterizedType"
}

func (MalformedParameterizedTypeException) JavaClassName() string {
return "java.lang.reflect.MalformedParameterizedTypeException"
}
func NewMalformedParameterizedTypeException(detailMessage string) *MalformedParameterizedTypeException {
return &MalformedParameterizedTypeException{DetailMessage: detailMessage}
}

26 changes: 26 additions & 0 deletions exception/malformed_parameterized_type_exception_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2016-2019 tianying Pan
//
// Licensed 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 exception

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

func TestMalformedParameterizedTypeException(t *testing.T) {
testDecodeFrameworkFunc(t, "throw_MalformedParameterizedTypeException", func(r interface{}) {
assert.Equal(t, "MalformedParameterizedType", r.(error).Error())
})
}
41 changes: 41 additions & 0 deletions exception/malformed_parameters_exception.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2016-2019 tianying Pan
//
// Licensed 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 exception


import (
hessian "github.com/dubbogo/hessian2"
)
func init(){
hessian.RegisterPOJO(&MalformedParametersException{})
}
type MalformedParametersException struct {
SerialVersionUID int64
DetailMessage string
SuppressedExceptions []hessian.Exception
StackTrace []hessian.StackTraceElement
Cause *MalformedParametersException
}
func (e MalformedParametersException) Error() string {
return e.DetailMessage
}

func (MalformedParametersException) JavaClassName() string {
return "java.lang.reflect.MalformedParametersException"
}
func NewMalformedParametersException(detailMessage string) *MalformedParametersException {
return &MalformedParametersException{DetailMessage: detailMessage}
}

25 changes: 25 additions & 0 deletions exception/malformed_parameters_exception_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2016-2019 tianying Pan
//
// Licensed 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 exception
import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestMalformedParametersException(t *testing.T) {
testDecodeFrameworkFunc(t, "throw_MalformedParametersException", func(r interface{}) {
assert.Equal(t, "MalformedParametersException", r.(error).Error())
})
}
41 changes: 41 additions & 0 deletions exception/type_not_present_exception.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2016-2019 tianying Pan
//
// Licensed 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 exception

import (
hessian "github.com/dubbogo/hessian2"
)
func init(){
hessian.RegisterPOJO(&TypeNotPresentException{})
}
type TypeNotPresentException struct {
TypeName string
SerialVersionUID int64
DetailMessage string
SuppressedExceptions []hessian.Exception
StackTrace []hessian.StackTraceElement
Cause *hessian.Throwable

}
func (e TypeNotPresentException) Error() string {
return e.DetailMessage
}

func (TypeNotPresentException) JavaClassName() string {
return "java.lang.TypeNotPresentException"
}
func NewTypeNotPresentException(typeName string,detailMessage string) *TypeNotPresentException {
return &TypeNotPresentException{TypeName: typeName,DetailMessage:detailMessage}
}
26 changes: 26 additions & 0 deletions exception/type_not_present_exception_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2016-2019 tianying Pan
//
// Licensed 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 exception

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

func TestTypeNotPresentException(t *testing.T) {
testDecodeFrameworkFunc(t, "throw_TypeNotPresentException", func(r interface{}) {
assert.Equal(t, "Type exceptiontype1 not present", r.(error).Error())
})
}
40 changes: 40 additions & 0 deletions exception/undeclared_throwable_exception.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2016-2019 tianying Pan
//
// Licensed 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 exception

import (
hessian "github.com/dubbogo/hessian2"
)
func init(){
hessian.RegisterPOJO(&UndeclaredThrowableException{})
}
type UndeclaredThrowableException struct {
SerialVersionUID int64
DetailMessage string
SuppressedExceptions []hessian.Exception
StackTrace []hessian.StackTraceElement
Cause *hessian.Throwable
UndeclaredThrowable hessian.Throwable
}
func (e UndeclaredThrowableException) Error() string {
return e.DetailMessage
}

func (UndeclaredThrowableException) JavaClassName() string {
return "java.lang.reflect.UndeclaredThrowableException"
}
func NewUndeclaredThrowableException(detailMessage string) *UndeclaredThrowableException {
return &UndeclaredThrowableException{DetailMessage: detailMessage,UndeclaredThrowable:hessian.Throwable{}}
}
26 changes: 26 additions & 0 deletions exception/undeclared_throwable_exception_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2016-2019 tianying Pan
//
// Licensed 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 exception

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

func TestUndeclaredThrowableException(t *testing.T) {
testDecodeFrameworkFunc(t, "throw_UndeclaredThrowableException", func(r interface{}) {
assert.Equal(t, "UndeclaredThrowableException", r.(error).Error())
})
}
Loading