dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo/hessian2/hessian_response_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  	"testing"
    23  )
    24  
    25  import (
    26  	hessian "github.com/apache/dubbo-go-hessian2"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func doTestReflectResponse(t *testing.T, in interface{}, out interface{}) {
    32  	err := ReflectResponse(in, out)
    33  	if err != nil {
    34  		t.Error(err)
    35  		t.FailNow()
    36  	}
    37  
    38  	result := hessian.UnpackPtrValue(reflect.ValueOf(out)).Interface()
    39  
    40  	equal := reflect.DeepEqual(in, result)
    41  	if !equal {
    42  		t.Errorf("expect [%v]: %v, but got [%v]: %v", reflect.TypeOf(in), in, reflect.TypeOf(result), result)
    43  	}
    44  }
    45  
    46  func TestReflectResponse(t *testing.T) {
    47  	var b bool
    48  	doTestReflectResponse(t, true, &b)
    49  	doTestReflectResponse(t, false, &b)
    50  
    51  	var i int
    52  	doTestReflectResponse(t, 123, &i)
    53  	doTestReflectResponse(t, 234, &i)
    54  
    55  	var i16 int16
    56  	doTestReflectResponse(t, int16(456), &i16)
    57  
    58  	var i64 int64
    59  	doTestReflectResponse(t, int64(789), &i64)
    60  
    61  	var s string
    62  	doTestReflectResponse(t, "hello world", &s)
    63  
    64  	type rr struct {
    65  		Name string
    66  		Num  int
    67  	}
    68  
    69  	var r1 rr
    70  	doTestReflectResponse(t, rr{"dubbogo", 32}, &r1)
    71  
    72  	// ------ map test -------
    73  	m1 := make(map[interface{}]interface{})
    74  	var m1r map[interface{}]interface{}
    75  	m1["hello"] = "world"
    76  	m1[1] = "go"
    77  	m1["dubbo"] = 666
    78  	doTestReflectResponse(t, m1, &m1r)
    79  
    80  	m2 := make(map[string]string)
    81  	var m2r map[string]string
    82  	m2["hello"] = "world"
    83  	m2["dubbo"] = "666"
    84  	doTestReflectResponse(t, m2, &m2r)
    85  
    86  	m3 := make(map[string]rr)
    87  	var m3r map[string]rr
    88  	m3["dubbo"] = rr{"hello", 123}
    89  	m3["go"] = rr{"world", 456}
    90  	doTestReflectResponse(t, m3, &m3r)
    91  
    92  	// ------ slice test -------
    93  	s1 := []string{"abc", "def", "hello", "world"}
    94  	var s1r []string
    95  	doTestReflectResponse(t, s1, &s1r)
    96  
    97  	s2 := []rr{{"dubbo", 666}, {"go", 999}}
    98  	var s2r []rr
    99  	doTestReflectResponse(t, s2, &s2r)
   100  
   101  	s3 := []interface{}{rr{"dubbo", 666}, 123, "hello"}
   102  	var s3r []interface{}
   103  	doTestReflectResponse(t, s3, &s3r)
   104  
   105  	// ------ interface test -------
   106  	in1 := []interface{}{rr{"dubbo", 666}, 123, "hello"}
   107  	var inr1 *interface{}
   108  	doTestReflectResponse(t, in1, reflect.New(reflect.TypeOf(inr1).Elem()).Interface())
   109  
   110  	in2 := make(map[string]rr)
   111  	var inr2 map[string]rr
   112  	m3["dubbo"] = rr{"hello", 123}
   113  	m3["go"] = rr{"world", 456}
   114  	doTestReflectResponse(t, in2, &inr2)
   115  }
   116  
   117  // separately test copy normal map to map[interface{}]interface{}
   118  func TestCopyMap(t *testing.T) {
   119  	type rr struct {
   120  		Name string
   121  		Num  int
   122  	}
   123  
   124  	m3 := make(map[string]rr)
   125  	var m3r map[interface{}]interface{}
   126  	r1 := rr{"hello", 123}
   127  	r2 := rr{"world", 456}
   128  	m3["dubbo"] = r1
   129  	m3["go"] = r2
   130  
   131  	err := ReflectResponse(m3, &m3r)
   132  	if err != nil {
   133  		t.Error(err)
   134  		t.FailNow()
   135  	}
   136  
   137  	assert.Equal(t, 2, len(m3r))
   138  
   139  	rr1, ok := m3r["dubbo"]
   140  	assert.True(t, ok)
   141  	assert.True(t, reflect.DeepEqual(r1, rr1))
   142  
   143  	rr2, ok := m3r["go"]
   144  	assert.True(t, ok)
   145  	assert.True(t, reflect.DeepEqual(r2, rr2))
   146  }
   147  
   148  // separately test copy normal slice to []interface{}
   149  func TestCopySlice(t *testing.T) {
   150  	type rr struct {
   151  		Name string
   152  		Num  int
   153  	}
   154  
   155  	r1 := rr{"hello", 123}
   156  	r2 := rr{"world", 456}
   157  
   158  	s1 := []rr{r1, r2}
   159  	var s1r []interface{}
   160  
   161  	err := ReflectResponse(s1, &s1r)
   162  	if err != nil {
   163  		t.Error(err)
   164  		t.FailNow()
   165  	}
   166  
   167  	assert.Equal(t, 2, len(s1r))
   168  	assert.True(t, reflect.DeepEqual(r1, s1r[0]))
   169  	assert.True(t, reflect.DeepEqual(r2, s1r[1]))
   170  }
   171  
   172  func TestIsSupportResponseAttachment(t *testing.T) {
   173  	is := isSupportResponseAttachment("2.X")
   174  	assert.False(t, is)
   175  
   176  	is = isSupportResponseAttachment("2.0.10")
   177  	assert.False(t, is)
   178  
   179  	is = isSupportResponseAttachment("2.5.3")
   180  	assert.False(t, is)
   181  
   182  	is = isSupportResponseAttachment("2.6.2")
   183  	assert.False(t, is)
   184  
   185  	is = isSupportResponseAttachment("1.5.5")
   186  	assert.False(t, is)
   187  
   188  	is = isSupportResponseAttachment("0.0.0")
   189  	assert.False(t, is)
   190  
   191  	is = isSupportResponseAttachment("2.0.2")
   192  	assert.True(t, is)
   193  
   194  	is = isSupportResponseAttachment("2.7.2")
   195  	assert.True(t, is)
   196  }
   197  
   198  func TestVersion2Int(t *testing.T) {
   199  	v := version2Int("2.1.3")
   200  	assert.Equal(t, 2010300, v)
   201  
   202  	v = version2Int("22.11.33")
   203  	assert.Equal(t, 22113300, v)
   204  
   205  	v = version2Int("222.111.333")
   206  	assert.Equal(t, 223143300, v)
   207  
   208  	v = version2Int("220.110.333")
   209  	assert.Equal(t, 221133300, v)
   210  
   211  	v = version2Int("229.119.333")
   212  	assert.Equal(t, 230223300, v)
   213  
   214  	v = version2Int("2222.1111.3333")
   215  	assert.Equal(t, 2233443300, v)
   216  
   217  	v = version2Int("2.11")
   218  	assert.Equal(t, 211, v)
   219  
   220  	v = version2Int("2.1.3.4")
   221  	assert.Equal(t, 2010304, v)
   222  
   223  	v = version2Int("2.1.3.4.5")
   224  	assert.Equal(t, 201030405, v)
   225  }