dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/dubbo/hessian2/java_class_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  	"testing"
    22  )
    23  
    24  import (
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestGetJavaName(t *testing.T) {
    29  	_, err := GetJavaName(nil)
    30  	assert.Equal(t, NilError, err)
    31  
    32  	typ, err := GetJavaName(true)
    33  	assert.Equal(t, "boolean", typ)
    34  	assert.Nil(t, err)
    35  
    36  	typ, err = GetJavaName(1)
    37  	assert.Equal(t, "long", typ)
    38  	assert.Nil(t, err)
    39  	typ, err = GetJavaName(int64(1))
    40  	assert.Equal(t, "long", typ)
    41  	assert.Nil(t, err)
    42  
    43  	typ, err = GetJavaName(int32(1))
    44  	assert.Equal(t, "int", typ)
    45  	assert.Nil(t, err)
    46  
    47  	typ, err = GetJavaName(int16(1))
    48  	assert.Equal(t, "short", typ)
    49  	assert.Nil(t, err)
    50  	typ, err = GetJavaName(int8(1))
    51  	assert.Equal(t, "short", typ)
    52  	assert.Nil(t, err)
    53  
    54  	typ, err = GetJavaName(uint(1))
    55  	assert.Equal(t, "unsigned long", typ)
    56  	assert.Nil(t, err)
    57  	typ, err = GetJavaName(uint64(1))
    58  	assert.Equal(t, "unsigned long", typ)
    59  	assert.Nil(t, err)
    60  
    61  	typ, err = GetJavaName(uint32(1))
    62  	assert.Equal(t, "unsigned int", typ)
    63  	assert.Nil(t, err)
    64  
    65  	typ, err = GetJavaName(uint16(1))
    66  	assert.Equal(t, "unsigned short", typ)
    67  	assert.Nil(t, err)
    68  	typ, err = GetJavaName(byte('a'))
    69  	assert.Equal(t, "char", typ)
    70  	assert.Nil(t, err)
    71  
    72  	typ, err = GetJavaName(float32(1.0))
    73  	assert.Equal(t, "float", typ)
    74  	assert.Nil(t, err)
    75  
    76  	typ, err = GetJavaName(1.0)
    77  	assert.Equal(t, "double", typ)
    78  	assert.Nil(t, err)
    79  
    80  	typ, err = GetJavaName("hello")
    81  	assert.Equal(t, "java.lang.String", typ)
    82  	assert.Nil(t, err)
    83  
    84  	typ, err = GetJavaName([]string{"hello"})
    85  	assert.Equal(t, "java.lang.String[]", typ)
    86  	assert.Nil(t, err)
    87  	typ, err = GetJavaName([]*mockPOJOPtr{{}})
    88  	assert.Equal(t, "org.apache.dubbo.mockPOJOPtr[]", typ)
    89  	assert.Nil(t, err)
    90  	typ, err = GetJavaName([]mockPOJO{{}})
    91  	assert.Equal(t, "org.apache.dubbo.mockPOJO[]", typ)
    92  	assert.Nil(t, err)
    93  	typ, err = GetJavaName([][]string{{"hello"}})
    94  	assert.Equal(t, "java.lang.String[][]", typ)
    95  	assert.Nil(t, err)
    96  	typ, err = GetJavaName([][]*mockPOJOPtr{{&mockPOJOPtr{}}})
    97  	assert.Equal(t, "org.apache.dubbo.mockPOJOPtr[][]", typ)
    98  	assert.Nil(t, err)
    99  	typ, err = GetJavaName([1]string{"hello"})
   100  	assert.Equal(t, "java.lang.String[]", typ)
   101  	assert.Nil(t, err)
   102  
   103  	typ, err = GetJavaName(map[string]string{"key1": "value1"})
   104  	assert.Equal(t, "java.util.Map", typ)
   105  	assert.Nil(t, err)
   106  
   107  	typ, err = GetJavaName(mockPOJO{})
   108  	assert.Equal(t, "org.apache.dubbo.mockPOJO", typ)
   109  	assert.Nil(t, err)
   110  	typ, err = GetJavaName(&mockPOJOPtr{})
   111  	assert.Equal(t, "org.apache.dubbo.mockPOJOPtr", typ)
   112  	assert.Nil(t, err)
   113  
   114  	_, err = GetJavaName(&mockNonPOJO{})
   115  	assert.Equal(t, UnexpectedTypeError, err)
   116  }
   117  
   118  type mockPOJOPtr struct {
   119  	TestField string
   120  }
   121  
   122  func (m *mockPOJOPtr) JavaClassName() string {
   123  	return "org.apache.dubbo.mockPOJOPtr"
   124  }
   125  
   126  type mockPOJO struct{}
   127  
   128  func (m mockPOJO) JavaClassName() string {
   129  	return "org.apache.dubbo.mockPOJO"
   130  }
   131  
   132  type mockNonPOJO struct{}