Skip to content

Commit

Permalink
add unit test for java wrapper object
Browse files Browse the repository at this point in the history
  • Loading branch information
tiltwind committed May 5, 2023
1 parent 25f3ccf commit f73a225
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
10 changes: 10 additions & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,16 @@ func SetValueToPtrDest(dest reflect.Value, v reflect.Value) {
vv := v.Float()
dest.Set(reflect.ValueOf(&vv))
return
case _typeOfRunePtr:
if v.Kind() == reflect.String {
vv := Rune(v.String()[0])
dest.Set(reflect.ValueOf(&vv))
return
}

vv := Rune(v.Int())
dest.Set(reflect.ValueOf(&vv))
return
default:
dest.Set(v)
}
Expand Down
4 changes: 3 additions & 1 deletion java_lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ func init() {
type Rune rune

var (
_typeOfRune = reflect.TypeOf(Rune(0))
_varRune = Rune(0)
_typeOfRune = reflect.TypeOf(_varRune)
_typeOfRunePtr = reflect.TypeOf(&_varRune)
)
49 changes: 49 additions & 0 deletions java_lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,52 @@ func TestDecodeJavaCharacterArray(t *testing.T) {
t.Logf("%T %+v", got, got)
assert.Equal(t, arr, got)
}

type JavaLangObjectHolder struct {
FieldInteger *int32 `json:"fieldInteger"`
FieldLong *int64 `json:"fieldLong"`
FieldBoolean *bool `json:"fieldBoolean"`
FieldShort *int16 `json:"fieldShort"`
FieldByte *int8 `json:"fieldByte"`
FieldFloat *float32 `json:"fieldFloat"`
FieldDouble *float64 `json:"fieldDouble"`
FieldCharacter *Rune `json:"fieldCharacter"`
}

func (h JavaLangObjectHolder) JavaClassName() string {
return "test.model.JavaLangObjectHolder"
}

func TestDecodeJavaLangObjectHolder(t *testing.T) {
var a int32 = 123
var b int64 = 456
var c = true
var d int16 = 789
var e int8 = 12
var f float32 = 3.45
var g = 6.78
var h Rune = 'A'

obj := &JavaLangObjectHolder{
FieldInteger: &a,
FieldLong: &b,
FieldBoolean: &c,
FieldShort: &d,
FieldByte: &e,
FieldFloat: &f,
FieldDouble: &g,
FieldCharacter: &h,
}

RegisterPOJO(obj)

got, err := decodeJavaResponse(`customReplyJavaLangObjectHolder`, ``, false)
assert.NoError(t, err)
t.Logf("customReplyJavaLangObjectHolder: %T %+v", got, got)
assert.Equal(t, obj, got)

got, err = decodeJavaResponse(`customReplyJavaLangObjectHolderForNull`, ``, false)
assert.NoError(t, err)
t.Logf("customReplyJavaLangObjectHolderForNull: %T %+v", got, got)
assert.Equal(t, &JavaLangObjectHolder{}, got)
}
24 changes: 24 additions & 0 deletions test_hessian/src/main/java/test/TestCustomReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import test.generic.Response;
import test.model.CustomMap;
import test.model.DateDemo;
import test.model.JavaLangObjectHolder;
import test.model.User;

import java.io.OutputStream;
Expand Down Expand Up @@ -797,6 +798,29 @@ public void customReplyEnumVariableList() throws Exception {
output.writeObject(enumList.toArray(new Locale.Category[enumList.size()]));
output.flush();
}

public void customReplyJavaLangObjectHolder() throws Exception {
JavaLangObjectHolder holder = new JavaLangObjectHolder();

holder.setFieldInteger(123);
holder.setFieldLong(456L);
holder.setFieldBoolean(true);
holder.setFieldShort((short) 789);
holder.setFieldByte((byte) 12);
holder.setFieldFloat(3.45f);
holder.setFieldDouble(6.78);
holder.setFieldCharacter('A');

output.writeObject(holder);
output.flush();
}

public void customReplyJavaLangObjectHolderForNull() throws Exception {
// all fields are default null.
JavaLangObjectHolder holder = new JavaLangObjectHolder();
output.writeObject(holder);
output.flush();
}
}

interface Leg {
Expand Down
98 changes: 98 additions & 0 deletions test_hessian/src/main/java/test/model/JavaLangObjectHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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 test.model;

import java.io.Serializable;

/**
* @author tiltwind
*/
public class JavaLangObjectHolder implements Serializable {
private Integer fieldInteger;
private Long fieldLong;
private Boolean fieldBoolean;
private Double fieldDouble;
private Float fieldFloat;
private Short fieldShort;
private Byte fieldByte;
private Character fieldCharacter;

public Integer getFieldInteger() {
return fieldInteger;
}

public void setFieldInteger(Integer fieldInteger) {
this.fieldInteger = fieldInteger;
}

public Long getFieldLong() {
return fieldLong;
}

public void setFieldLong(Long fieldLong) {
this.fieldLong = fieldLong;
}

public Boolean getFieldBoolean() {
return fieldBoolean;
}

public void setFieldBoolean(Boolean fieldBoolean) {
this.fieldBoolean = fieldBoolean;
}

public Double getFieldDouble() {
return fieldDouble;
}

public void setFieldDouble(Double fieldDouble) {
this.fieldDouble = fieldDouble;
}

public Float getFieldFloat() {
return fieldFloat;
}

public void setFieldFloat(Float fieldFloat) {
this.fieldFloat = fieldFloat;
}

public Short getFieldShort() {
return fieldShort;
}

public void setFieldShort(Short fieldShort) {
this.fieldShort = fieldShort;
}

public Byte getFieldByte() {
return fieldByte;
}

public void setFieldByte(Byte fieldByte) {
this.fieldByte = fieldByte;
}

public Character getFieldCharacter() {
return fieldCharacter;
}

public void setFieldCharacter(Character fieldCharacter) {
this.fieldCharacter = fieldCharacter;
}
}

0 comments on commit f73a225

Please sign in to comment.