You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
8.9 KiB

  1. // Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
  2. package container
  3. import (
  4. "fmt"
  5. "reflect"
  6. "testing"
  7. )
  8. func TestDefaultInformationManager_New(t *testing.T) {
  9. defaultInformationManager := NewDefaultInformationManager()
  10. if defaultInformationManager == nil {
  11. t.Fatalf("创建失败!")
  12. }
  13. if len(defaultInformationManager.informationMapping) != 0 {
  14. t.Fatalf("组件列表数量错误!")
  15. }
  16. }
  17. func TestDefaultInformationManager_Item(t *testing.T) {
  18. informationManager := NewDefaultInformationManager()
  19. elementInformation := NewElementInformation(FactoryOfTestElement)
  20. informationManager.informationMapping[testElementType] = elementInformation
  21. if informationManager.Item(testElementType) == nil {
  22. t.Fatalf("意外未找到了组件信息!")
  23. }
  24. if informationManager.Item(testElementType) != elementInformation {
  25. t.Fatalf("获取组件信息错误!")
  26. }
  27. if informationManager.Item(testServiceType) != nil {
  28. t.Fatalf("意外找到了组件信息!")
  29. }
  30. }
  31. func TestDefaultInformationManager_Item_NullError(t *testing.T) {
  32. informationManager := NewDefaultInformationManager()
  33. information := informationManager.Item(nil)
  34. if information != nil {
  35. t.Fatal("意外的查到了数据!")
  36. }
  37. }
  38. func TestDefaultInformationManager_Items(t *testing.T) {
  39. informationManager := NewDefaultInformationManager()
  40. elementInformation := NewElementInformation(FactoryOfTestElement)
  41. informationManager.informationMapping[testElementType] = elementInformation
  42. if len(informationManager.Items()) != 1 {
  43. t.Fatalf("组件列表数量错误!")
  44. }
  45. if informationManager.Items()[0] != elementInformation {
  46. t.Fatalf("获取组件信息列表错误!")
  47. }
  48. }
  49. func TestDefaultInformationManager_RegisterElement(t *testing.T) {
  50. informationManager := NewDefaultInformationManager()
  51. informationManager.RegisterElement(FactoryOfTestElement, false)
  52. information, ok := informationManager.informationMapping[testElementType]
  53. if !ok {
  54. t.Fatalf("未找到组件!")
  55. }
  56. if information == nil {
  57. t.Fatalf("没找到信息")
  58. }
  59. elementInformation, ok := information.(*ElementInformation)
  60. if !ok {
  61. t.Fatalf("Information类型不正确!")
  62. }
  63. if testElementType != elementInformation.Interface() {
  64. t.Fatalf("接口类型不正确!")
  65. }
  66. }
  67. func TestDefaultInformationManager_RegisterGlobalElement(t *testing.T) {
  68. defer func() {
  69. if err := recover(); err != "目前不支持全局组件!" {
  70. t.Fatalf("意外错误:%s", err)
  71. }
  72. }()
  73. informationManager := NewDefaultInformationManager()
  74. informationManager.RegisterElement(FactoryOfTestElement, true)
  75. t.Fatalf("意外的没有错误!")
  76. }
  77. func TestDefaultInformationManager_RegisterElement_NullError(t *testing.T) {
  78. defer func() {
  79. if err := recover(); err != "工厂不能为空!" {
  80. t.Fatalf("意外错误:%s", err)
  81. }
  82. }()
  83. informationManager := NewDefaultInformationManager()
  84. informationManager.RegisterElement(nil, false)
  85. t.Fatalf("意外的没有错误!")
  86. }
  87. func TestDefaultInformationManager_RegisterElement_FactoryTypeError(t *testing.T) {
  88. defer func() {
  89. if err := recover(); err != "工厂必须是func!" {
  90. t.Fatalf("意外错误:%s", err)
  91. }
  92. }()
  93. informationManager := NewDefaultInformationManager()
  94. informationManager.RegisterElement(reflect.TypeOf(FactoryOfTestElement()), false)
  95. t.Fatalf("意外的没有错误!")
  96. }
  97. func TestDefaultInformationManager_RegisterElement_ParameterCountError(t *testing.T) {
  98. defer func() {
  99. if err := recover(); err != "工厂不能有参数!" {
  100. t.Fatalf("意外错误:%s", err)
  101. }
  102. }()
  103. informationManager := NewDefaultInformationManager()
  104. informationManager.RegisterElement(FactoryOfElementTestInterfaceWithContent, false)
  105. t.Fatalf("意外的没有错误!")
  106. }
  107. func TestDefaultInformationManager_RegisterElement_NoReturnValueError(t *testing.T) {
  108. defer func() {
  109. if err := recover(); err != "工厂必值需有返回值!" {
  110. t.Fatalf("意外错误:%s", err)
  111. }
  112. }()
  113. informationManager := NewDefaultInformationManager()
  114. informationManager.RegisterElement(FactoryWithoutResult, false)
  115. t.Fatalf("意外的没有错误!")
  116. }
  117. func TestDefaultInformationManager_RegisterElement_TooManyResultsError(t *testing.T) {
  118. defer func() {
  119. if err := recover(); err != "工厂只能有一个返回值!" {
  120. t.Fatalf("意外错误:%s", err)
  121. }
  122. }()
  123. informationManager := NewDefaultInformationManager()
  124. informationManager.RegisterElement(FactoryWithTooManyResults, false)
  125. t.Fatalf("意外的没有错误!")
  126. }
  127. func TestDefaultInformationManager_RegisterElement_ReturnValueTypeError(t *testing.T) {
  128. defer func() {
  129. if err := recover(); err != "工厂的返回值必需是interface!" {
  130. t.Fatalf("意外错误:%s", err)
  131. }
  132. }()
  133. informationManager := NewDefaultInformationManager()
  134. informationManager.RegisterElement(FactoryOfTestElementImplement, false)
  135. t.Fatalf("意外的没有错误!")
  136. }
  137. func TestDefaultInformationManager_RegisterElement_Repeat(t *testing.T) {
  138. defer func() {
  139. if err := recover(); err != fmt.Sprintf("组件%s接口已经注册!", testElementType.Name()) {
  140. t.Fatalf("意外错误:%s", err)
  141. }
  142. }()
  143. informationManager := NewDefaultInformationManager()
  144. informationManager.RegisterElement(FactoryOfTestElement, false)
  145. informationManager.RegisterElement(FactoryOfTestElement, false)
  146. t.Fatalf("意外的没有错误!")
  147. }
  148. func TestDefaultInformationManager_RegisterService(t *testing.T) {
  149. informationManager := NewDefaultInformationManager()
  150. serviceInformation := informationManager.RegisterService(FactoryOfTestService, false)
  151. if serviceInformation == nil {
  152. t.Fatalf("注册失败!")
  153. }
  154. information, ok := informationManager.informationMapping[testServiceType]
  155. if !ok {
  156. t.Fatalf("未找到组件!")
  157. }
  158. if information == nil {
  159. t.Fatalf("没找到信息")
  160. }
  161. serviceInformation, ok = information.(*ServiceInformation)
  162. if !ok {
  163. t.Fatalf("Information类型不正确!")
  164. }
  165. if testServiceType != serviceInformation.Interface() {
  166. t.Fatalf("接口类型不正确!")
  167. }
  168. }
  169. func TestDefaultInformationManager_RegisterGlobalService(t *testing.T) {
  170. defer func() {
  171. if err := recover(); err != "目前不支持全局组件!" {
  172. t.Fatalf("意外错误:%s", err)
  173. }
  174. }()
  175. informationManager := NewDefaultInformationManager()
  176. _ = informationManager.RegisterService(FactoryOfTestService, true)
  177. t.Fatalf("意外的没有错误!")
  178. }
  179. func TestDefaultInformationManager_RegisterService_NullError(t *testing.T) {
  180. defer func() {
  181. if err := recover(); err != "工厂不能为空!" {
  182. t.Fatalf("意外错误:%s", err)
  183. }
  184. }()
  185. informationManager := NewDefaultInformationManager()
  186. _ = informationManager.RegisterService(nil, false)
  187. t.Fatalf("意外的没有错误!")
  188. }
  189. func TestDefaultInformationManager_RegisterService_FactoryTypeError(t *testing.T) {
  190. defer func() {
  191. if err := recover(); err != "工厂必须是func!" {
  192. t.Fatalf("意外错误:%s", err)
  193. }
  194. }()
  195. informationManager := NewDefaultInformationManager()
  196. _ = informationManager.RegisterService(reflect.TypeOf(FactoryOfTestElement()), false)
  197. t.Fatalf("意外的没有错误!")
  198. }
  199. func TestDefaultInformationManager_RegisterService_ParameterCountError(t *testing.T) {
  200. defer func() {
  201. if err := recover(); err != "工厂不能有参数!" {
  202. t.Fatalf("意外错误:%s", err)
  203. }
  204. }()
  205. informationManager := NewDefaultInformationManager()
  206. _ = informationManager.RegisterService(FactoryOfServiceTestInterfaceWithContent, false)
  207. t.Fatalf("意外的没有错误!")
  208. }
  209. func TestDefaultInformationManager_RegisterService_NoReturnValueError(t *testing.T) {
  210. defer func() {
  211. if err := recover(); err != "工厂必值需有返回值!" {
  212. t.Fatalf("意外错误:%s", err)
  213. }
  214. }()
  215. informationManager := NewDefaultInformationManager()
  216. _ = informationManager.RegisterService(FactoryWithoutResult, false)
  217. t.Fatalf("意外的没有错误!")
  218. }
  219. func TestDefaultInformationManager_RegisterService_TooManyResultsError(t *testing.T) {
  220. defer func() {
  221. if err := recover(); err != "工厂只能有一个返回值!" {
  222. t.Fatalf("意外错误:%s", err)
  223. }
  224. }()
  225. informationManager := NewDefaultInformationManager()
  226. _ = informationManager.RegisterService(FactoryWithTooManyResults, false)
  227. t.Fatalf("意外的没有错误!")
  228. }
  229. func TestDefaultInformationManager_RegisterService_ReturnValueTypeError(t *testing.T) {
  230. defer func() {
  231. if err := recover(); err != "工厂的返回值必需是interface!" {
  232. t.Fatalf("意外错误:%s", err)
  233. }
  234. }()
  235. informationManager := NewDefaultInformationManager()
  236. _ = informationManager.RegisterService(FactoryOfTestElementImplement, false)
  237. t.Fatalf("意外的没有错误!")
  238. }
  239. func TestDefaultInformationManager_RegisterService_Repeat(t *testing.T) {
  240. defer func() {
  241. if err := recover(); err != fmt.Sprintf("组件%s接口已经注册!", testServiceType.Name()) {
  242. t.Fatalf("意外错误:%s", err)
  243. }
  244. }()
  245. informationManager := NewDefaultInformationManager()
  246. _ = informationManager.RegisterService(FactoryOfTestService, false)
  247. _ = informationManager.RegisterService(FactoryOfTestService, false)
  248. t.Fatalf("意外的没有错误!")
  249. }