dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo/hessian2/hessian_request_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package hessian2
    19  
    20  import (
    21  	"reflect"
    22  	"strconv"
    23  	"testing"
    24  	"time"
    25  )
    26  
    27  import (
    28  	hessian "github.com/apache/dubbo-go-hessian2"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  type TestEnumGender hessian.JavaEnum
    34  
    35  const (
    36  	MAN hessian.JavaEnum = iota
    37  	WOMAN
    38  )
    39  
    40  var genderName = map[hessian.JavaEnum]string{
    41  	MAN:   "MAN",
    42  	WOMAN: "WOMAN",
    43  }
    44  
    45  var genderValue = map[string]hessian.JavaEnum{
    46  	"MAN":   MAN,
    47  	"WOMAN": WOMAN,
    48  }
    49  
    50  func (g TestEnumGender) JavaClassName() string {
    51  	return "com.ikurento.test.TestEnumGender"
    52  }
    53  
    54  func (g TestEnumGender) String() string {
    55  	s, ok := genderName[hessian.JavaEnum(g)]
    56  	if ok {
    57  		return s
    58  	}
    59  
    60  	return strconv.Itoa(int(g))
    61  }
    62  
    63  func (g TestEnumGender) EnumValue(s string) hessian.JavaEnum {
    64  	v, ok := genderValue[s]
    65  	if ok {
    66  		return v
    67  	}
    68  
    69  	return hessian.InvalidJavaEnum
    70  }
    71  
    72  func TestPackRequest(t *testing.T) {
    73  	bytes, err := packRequest(Service{
    74  		Path:      "test",
    75  		Interface: "ITest",
    76  		Version:   "v1.0",
    77  		Method:    "test",
    78  		Timeout:   time.Second * 10,
    79  	}, DubboHeader{
    80  		SerialID: 0,
    81  		Type:     PackageRequest,
    82  		ID:       123,
    83  	}, []interface{}{1, 2})
    84  
    85  	assert.Nil(t, err)
    86  
    87  	if bytes != nil {
    88  		t.Logf("pack request: %s", string(bytes))
    89  	}
    90  }
    91  
    92  func TestGetArgsTypeList(t *testing.T) {
    93  	type Test struct{}
    94  	str, err := getArgsTypeList([]interface{}{nil, 1, []int{2}, true, []bool{false}, "a", []string{"b"}, Test{}, &Test{}, []Test{}, map[string]Test{}, TestEnumGender(MAN)})
    95  	assert.NoError(t, err)
    96  	assert.Equal(t, "VJ[JZ[ZLjava/lang/String;[Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;Ljava/util/Map;Lcom/ikurento/test/TestEnumGender;", str)
    97  }
    98  
    99  func TestDescRegex(t *testing.T) {
   100  	results := DescRegex.FindAllString("Ljava/lang/String;", -1)
   101  	assert.Equal(t, 1, len(results))
   102  	assert.Equal(t, "Ljava/lang/String;", results[0])
   103  
   104  	results = DescRegex.FindAllString("Ljava/lang/String;I", -1)
   105  	assert.Equal(t, 2, len(results))
   106  	assert.Equal(t, "Ljava/lang/String;", results[0])
   107  	assert.Equal(t, "I", results[1])
   108  
   109  	results = DescRegex.FindAllString("ILjava/lang/String;", -1)
   110  	assert.Equal(t, 2, len(results))
   111  	assert.Equal(t, "I", results[0])
   112  	assert.Equal(t, "Ljava/lang/String;", results[1])
   113  
   114  	results = DescRegex.FindAllString("ILjava/lang/String;IZ", -1)
   115  	assert.Equal(t, 4, len(results))
   116  	assert.Equal(t, "I", results[0])
   117  	assert.Equal(t, "Ljava/lang/String;", results[1])
   118  	assert.Equal(t, "I", results[2])
   119  	assert.Equal(t, "Z", results[3])
   120  
   121  	results = DescRegex.FindAllString("[Ljava/lang/String;[I", -1)
   122  	assert.Equal(t, 2, len(results))
   123  	assert.Equal(t, "[Ljava/lang/String;", results[0])
   124  	assert.Equal(t, "[I", results[1])
   125  }
   126  
   127  func TestIssue192(t *testing.T) {
   128  	type args struct {
   129  		origin map[interface{}]interface{}
   130  	}
   131  	tests := []struct {
   132  		name string
   133  		args args
   134  		want map[string]interface{}
   135  	}{
   136  		{
   137  			name: "not null",
   138  			args: args{
   139  				origin: map[interface{}]interface{}{
   140  					"1": nil,
   141  					"2": "3",
   142  					"":  "",
   143  				},
   144  			},
   145  			want: map[string]interface{}{
   146  				"1": "",
   147  				"2": "3",
   148  				"":  "",
   149  			},
   150  		},
   151  	}
   152  	for _, tt := range tests {
   153  		t.Run(tt.name, func(t *testing.T) {
   154  			if got := ToMapStringInterface(tt.args.origin); !reflect.DeepEqual(got, tt.want) {
   155  				t.Errorf("ToMapStringString() = %v, want %v", got, tt.want)
   156  			}
   157  		})
   158  	}
   159  }