serializer_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. import (
  21. "errors"
  22. "fmt"
  23. "testing"
  24. )
  25. type ProtocolFactory interface {
  26. GetProtocol(t TTransport) TProtocol
  27. }
  28. func compareStructs(m, m1 TestStruct) (bool, error) {
  29. switch {
  30. case m.On != m1.On:
  31. return false, errors.New("Boolean not equal")
  32. case m.B != m1.B:
  33. return false, errors.New("Byte not equal")
  34. case m.Int16 != m1.Int16:
  35. return false, errors.New("Int16 not equal")
  36. case m.Int32 != m1.Int32:
  37. return false, errors.New("Int32 not equal")
  38. case m.Int64 != m1.Int64:
  39. return false, errors.New("Int64 not equal")
  40. case m.D != m1.D:
  41. return false, errors.New("Double not equal")
  42. case m.St != m1.St:
  43. return false, errors.New("String not equal")
  44. case len(m.Bin) != len(m1.Bin):
  45. return false, errors.New("Binary size not equal")
  46. case len(m.Bin) == len(m1.Bin):
  47. for i := range m.Bin {
  48. if m.Bin[i] != m1.Bin[i] {
  49. return false, errors.New("Binary not equal")
  50. }
  51. }
  52. case len(m.StringMap) != len(m1.StringMap):
  53. return false, errors.New("StringMap size not equal")
  54. case len(m.StringList) != len(m1.StringList):
  55. return false, errors.New("StringList size not equal")
  56. case len(m.StringSet) != len(m1.StringSet):
  57. return false, errors.New("StringSet size not equal")
  58. case m.E != m1.E:
  59. return false, errors.New("TestEnum not equal")
  60. default:
  61. return true, nil
  62. }
  63. return true, nil
  64. }
  65. func ProtocolTest1(test *testing.T, pf ProtocolFactory) (bool, error) {
  66. t := NewTSerializer()
  67. t.Protocol = pf.GetProtocol(t.Transport)
  68. var m = TestStruct{}
  69. m.On = true
  70. m.B = int8(0)
  71. m.Int16 = 1
  72. m.Int32 = 2
  73. m.Int64 = 3
  74. m.D = 4.1
  75. m.St = "Test"
  76. m.Bin = make([]byte, 10)
  77. m.StringMap = make(map[string]string, 5)
  78. m.StringList = make([]string, 5)
  79. m.StringSet = make(map[string]bool, 5)
  80. m.E = 2
  81. s, err := t.WriteString(&m)
  82. if err != nil {
  83. return false, errors.New(fmt.Sprintf("Unable to Serialize struct\n\t %s", err))
  84. }
  85. t1 := NewTDeserializer()
  86. t1.Protocol = pf.GetProtocol(t1.Transport)
  87. var m1 = TestStruct{}
  88. if err = t1.ReadString(&m1, s); err != nil {
  89. return false, errors.New(fmt.Sprintf("Unable to Deserialize struct\n\t %s", err))
  90. }
  91. return compareStructs(m, m1)
  92. }
  93. func ProtocolTest2(test *testing.T, pf ProtocolFactory) (bool, error) {
  94. t := NewTSerializer()
  95. t.Protocol = pf.GetProtocol(t.Transport)
  96. var m = TestStruct{}
  97. m.On = false
  98. m.B = int8(0)
  99. m.Int16 = 1
  100. m.Int32 = 2
  101. m.Int64 = 3
  102. m.D = 4.1
  103. m.St = "Test"
  104. m.Bin = make([]byte, 10)
  105. m.StringMap = make(map[string]string, 5)
  106. m.StringList = make([]string, 5)
  107. m.StringSet = make(map[string]bool, 5)
  108. m.E = 2
  109. s, err := t.WriteString(&m)
  110. if err != nil {
  111. return false, errors.New(fmt.Sprintf("Unable to Serialize struct\n\t %s", err))
  112. }
  113. t1 := NewTDeserializer()
  114. t1.Protocol = pf.GetProtocol(t1.Transport)
  115. var m1 = TestStruct{}
  116. if err = t1.ReadString(&m1, s); err != nil {
  117. return false, errors.New(fmt.Sprintf("Unable to Deserialize struct\n\t %s", err))
  118. }
  119. return compareStructs(m, m1)
  120. }
  121. func TestSerializer(t *testing.T) {
  122. var protocol_factories map[string]ProtocolFactory
  123. protocol_factories = make(map[string]ProtocolFactory)
  124. protocol_factories["Binary"] = NewTBinaryProtocolFactoryDefault()
  125. protocol_factories["Compact"] = NewTCompactProtocolFactory()
  126. //protocol_factories["SimpleJSON"] = NewTSimpleJSONProtocolFactory() - write only, can't be read back by design
  127. protocol_factories["JSON"] = NewTJSONProtocolFactory()
  128. var tests map[string]func(*testing.T, ProtocolFactory) (bool, error)
  129. tests = make(map[string]func(*testing.T, ProtocolFactory) (bool, error))
  130. tests["Test 1"] = ProtocolTest1
  131. tests["Test 2"] = ProtocolTest2
  132. //tests["Test 3"] = ProtocolTest3 // Example of how to add additional tests
  133. for name, pf := range protocol_factories {
  134. for test, f := range tests {
  135. if s, err := f(t, pf); !s || err != nil {
  136. t.Errorf("%s Failed for %s protocol\n\t %s", test, name, err)
  137. }
  138. }
  139. }
  140. }