苏州瑞玛APS项目web后台
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.

415 lines
12 KiB

  1. package container
  2. import (
  3. "fmt"
  4. "leit.com/LAPP_CHEERSSON_BACKEND/global"
  5. "reflect"
  6. "testing"
  7. )
  8. func TestServiceInformation_New(t *testing.T) {
  9. serviceInformation := NewServiceInformation(FactoryOfTestService)
  10. if serviceInformation == nil {
  11. t.Fatalf("创建失败!")
  12. }
  13. if serviceInformation.IsGlobal() {
  14. t.Fatalf("目前不支持全局组件")
  15. }
  16. if serviceInformation.interfaceType != testServiceType {
  17. t.Fatalf("接口类型错误")
  18. }
  19. }
  20. func TestServiceInformation_New_NullError(t *testing.T) {
  21. defer func() {
  22. if err := recover(); err != "工厂不能为空!" {
  23. t.Fatalf("意外错误:%s", err)
  24. }
  25. }()
  26. _ = NewServiceInformation(nil)
  27. t.Fatalf("意外的没有错误!")
  28. }
  29. func TestServiceInformation_New_FactoryTypeError(t *testing.T) {
  30. defer func() {
  31. if err := recover(); err != "工厂必须是func!" {
  32. t.Fatalf("意外错误:%s", err)
  33. }
  34. }()
  35. _ = NewServiceInformation(reflect.TypeOf(FactoryOfTestElement()))
  36. t.Fatalf("意外的没有错误!")
  37. }
  38. func TestServiceInformation_New_ParameterCountError(t *testing.T) {
  39. defer func() {
  40. if err := recover(); err != "工厂不能有参数!" {
  41. t.Fatalf("意外错误:%s", err)
  42. }
  43. }()
  44. _ = NewServiceInformation(FactoryOfServiceTestInterfaceWithContent)
  45. t.Fatalf("意外的没有错误!")
  46. }
  47. func TestServiceInformation_New_NoReturnValueError(t *testing.T) {
  48. defer func() {
  49. if err := recover(); err != "工厂必值需有返回值!" {
  50. t.Fatalf("意外错误:%s", err)
  51. }
  52. }()
  53. _ = NewServiceInformation(FactoryWithoutResult)
  54. t.Fatalf("意外的没有错误!")
  55. }
  56. func TestServiceInformation_New_TooManyResultsError(t *testing.T) {
  57. defer func() {
  58. if err := recover(); err != "工厂只能有一个返回值!" {
  59. t.Fatalf("意外错误:%s", err)
  60. }
  61. }()
  62. _ = NewServiceInformation(FactoryWithTooManyResults)
  63. t.Fatalf("意外的没有错误!")
  64. }
  65. func TestServiceInformation_New_ReturnValueTypeError(t *testing.T) {
  66. defer func() {
  67. if err := recover(); err != "工厂的返回值必需是interface!" {
  68. t.Fatalf("意外错误:%s", err)
  69. }
  70. }()
  71. _ = NewServiceInformation(FactoryOfTestElementImplement)
  72. t.Fatalf("意外的没有错误!")
  73. }
  74. func TestServiceInformation_Interface(t *testing.T) {
  75. information := NewServiceInformation(FactoryOfTestService)
  76. information.interfaceType = testServiceType
  77. if information.Interface() != testServiceType {
  78. t.Fatalf("获取接口类型错误")
  79. }
  80. }
  81. func TestServiceInformation_RegisterMethod(t *testing.T) {
  82. information := NewServiceInformation(FactoryOfTestService)
  83. if information.Interface() != testServiceType {
  84. t.Fatalf("接口类型错误")
  85. }
  86. methodName := "String"
  87. method := information.RegisterMethod(methodName)
  88. if method == nil {
  89. t.Fatalf("没找到方法")
  90. }
  91. if len(information.methodMapping) != 1 {
  92. t.Fatalf("方法信息映射数量错误!")
  93. }
  94. storedMethod, ok := information.methodMapping[methodName]
  95. if !ok {
  96. t.Fatalf("没找到注册的方法!")
  97. }
  98. if storedMethod != method {
  99. t.Fatalf("注册的方法不正确!")
  100. }
  101. }
  102. func TestServiceInformation_RegisterMethod_EmptyMethodName(t *testing.T) {
  103. defer func() {
  104. if err := recover(); err != "方法名不能为空!" {
  105. t.Fatalf("意外错误:%s", err)
  106. }
  107. }()
  108. information := NewServiceInformation(FactoryOfTestService)
  109. if information.Interface() != testServiceType {
  110. t.Fatalf("接口类型错误")
  111. }
  112. methodName := ""
  113. _ = information.RegisterMethod(methodName)
  114. t.Fatalf("意外的没有错误!")
  115. }
  116. func TestServiceInformation_RegisterMethod_RepeatMethod(t *testing.T) {
  117. information := NewServiceInformation(FactoryOfTestService)
  118. if information.Interface() != testServiceType {
  119. t.Fatalf("接口类型错误")
  120. }
  121. methodName := "String"
  122. method := information.RegisterMethod(methodName)
  123. if method == nil {
  124. t.Fatalf("没找到方法")
  125. }
  126. defer func() {
  127. if err := recover(); err != fmt.Sprintf("方法%s已经注册!", methodName) {
  128. t.Fatalf("意外错误:%s", err)
  129. }
  130. }()
  131. _ = information.RegisterMethod("String")
  132. t.Fatalf("意外的没有错误!")
  133. }
  134. func TestServiceInformation_RegisterMethod_MissingMethod(t *testing.T) {
  135. information := NewServiceInformation(FactoryOfTestService)
  136. if information.Interface() != testServiceType {
  137. t.Fatalf("接口类型错误")
  138. }
  139. methodName := "M1"
  140. defer func() {
  141. if err := recover(); err != fmt.Sprintf("找不到方法%s!", methodName) {
  142. t.Fatalf("意外错误:%s", err)
  143. }
  144. }()
  145. _ = information.RegisterMethod(methodName)
  146. t.Fatalf("意外的没有错误!")
  147. }
  148. func TestServiceInformation_RegisterMethod_ReturnValuesCountError(t *testing.T) {
  149. defer func() {
  150. if err := recover(); err != fmt.Sprintf("返回值数量错误! 返回值数量: %d", 3) {
  151. t.Fatalf("意外错误:%s", err)
  152. }
  153. }()
  154. information := NewServiceInformation(FactoryOfTestService)
  155. if information.Interface() != testServiceType {
  156. t.Fatalf("接口类型错误")
  157. }
  158. methodName := "String2"
  159. _ = information.RegisterMethod(methodName)
  160. t.Fatalf("意外的没有错误!")
  161. }
  162. func TestServiceInformation_RegisterMethod_ReturnValueTypeError(t *testing.T) {
  163. defer func() {
  164. if err := recover(); err != fmt.Sprintf("最后一个返回值类型错误! 类型:%s", "string") {
  165. t.Fatalf("意外错误:%s", err)
  166. }
  167. }()
  168. information := NewServiceInformation(FactoryOfTestService)
  169. if information.Interface() != testServiceType {
  170. t.Fatalf("接口类型错误")
  171. }
  172. methodName := "NoError"
  173. _ = information.RegisterMethod(methodName)
  174. t.Fatalf("意外的没有错误!")
  175. }
  176. func TestServiceInformation_Method(t *testing.T) {
  177. information := NewServiceInformation(FactoryOfTestService)
  178. methodName := "String"
  179. method, ok := testServiceType.MethodByName(methodName)
  180. if !ok {
  181. t.Fatalf("意外的没找到方法:%s", methodName)
  182. }
  183. methodInformation := NewServiceMethodInformation(&method)
  184. information.methodMapping[methodName] = methodInformation
  185. if information.Method(methodName) == nil {
  186. t.Fatalf("获取方法信息错误!")
  187. }
  188. }
  189. func TestServiceInformation_Method_NullError(t *testing.T) {
  190. information := NewServiceInformation(FactoryOfTestService)
  191. if information.Method("") != nil {
  192. t.Fatalf("找到了空方法名的方法")
  193. }
  194. }
  195. func TestServiceInformation_Method_NotFound(t *testing.T) {
  196. information := NewServiceInformation(FactoryOfTestService)
  197. if information.Method("methodName") != nil {
  198. t.Fatalf("找到了不存在的方法")
  199. }
  200. }
  201. func TestServiceInformation_Methods(t *testing.T) {
  202. information := NewServiceInformation(FactoryOfTestService)
  203. methodName := "String"
  204. method, ok := testServiceType.MethodByName(methodName)
  205. if !ok {
  206. t.Fatalf("意外的没找到方法:%s", methodName)
  207. }
  208. methodInformation := NewServiceMethodInformation(&method)
  209. information.methodMapping[methodName] = methodInformation
  210. if len(information.Methods()) != 1 {
  211. t.Fatalf("方法列表数量不正确!")
  212. }
  213. if information.Methods()[0] != methodInformation {
  214. t.Fatalf("获取方法信息列表错误!")
  215. }
  216. }
  217. func TestServiceInformation_Methods_Empty(t *testing.T) {
  218. information := NewServiceInformation(FactoryOfTestService)
  219. if len(information.Methods()) != 0 {
  220. t.Fatalf("方法列表意外的不为空!")
  221. }
  222. }
  223. func TestServiceInformation_BuildHandler(t *testing.T) {
  224. information := NewServiceInformation(FactoryOfTestService)
  225. if information.Interface() != testServiceType {
  226. t.Fatalf("接口类型错误")
  227. }
  228. methodName := "String"
  229. _ = information.RegisterMethod(methodName)
  230. userId := "userId"
  231. user := &global.User{UserId: userId}
  232. sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
  233. if err != nil {
  234. t.Fatalf("意外错误:%s", err.Error())
  235. }
  236. handler, err := information.BuildHandler(sessionContext)
  237. if err != nil {
  238. t.Fatalf("意外错误:%s", err.Error())
  239. }
  240. _, ok := handler.(*ServiceHandler)
  241. if !ok {
  242. t.Fatalf("Handler类型不正确!")
  243. }
  244. }
  245. func TestServiceInformation_BuildHandler_NullCaller(t *testing.T) {
  246. information := NewServiceInformation(FactoryOfTestService)
  247. if information.Interface() != testServiceType {
  248. t.Fatalf("接口类型错误")
  249. }
  250. methodName := "String"
  251. method := information.RegisterMethod(methodName)
  252. userId := "userId"
  253. user := &global.User{UserId: userId}
  254. sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
  255. if err != nil {
  256. t.Fatalf("意外错误:%s", err.Error())
  257. }
  258. var callerBrokerBuilder CallerBrokerBuilder = new(TestCallerBrokerBuilderImplementWithError)
  259. method.AppendBrokerBuilder(callerBrokerBuilder)
  260. _, err = information.BuildHandler(sessionContext)
  261. if err == nil {
  262. t.Fatalf("意外的没有错误!")
  263. }
  264. if err.Error() != fmt.Sprintf("第2个调用器不能为空!") {
  265. t.Fatalf("意外错误:%s", err.Error())
  266. }
  267. }
  268. func TestServiceInformation_BuildHandler_NullSession(t *testing.T) {
  269. information := NewServiceInformation(FactoryOfTestService)
  270. if information.Interface() != testServiceType {
  271. t.Fatalf("接口类型错误")
  272. }
  273. _, err := information.BuildHandler(nil)
  274. if err == nil {
  275. t.Fatalf("意外的没有错误!")
  276. }
  277. if err.Error() != "会话上下文不能为空!" {
  278. t.Fatalf("意外错误:%s", err.Error())
  279. }
  280. }
  281. func TestServiceInformation_BuildHandler_WithTooManyIn(t *testing.T) {
  282. information := NewServiceInformation(FactoryOfTestWithTooManyInSetContext)
  283. userId := "userId"
  284. user := &global.User{UserId: userId}
  285. sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
  286. if err != nil {
  287. t.Fatalf("意外错误:%s", err.Error())
  288. }
  289. _, err = information.BuildHandler(sessionContext)
  290. if err == nil {
  291. t.Fatalf("意外的没有错误!")
  292. }
  293. if err.Error() != fmt.Sprintf("设置器参数数量不正确!") {
  294. t.Fatalf("意外错误:%s", err.Error())
  295. }
  296. }
  297. func TestServiceInformation_BuildHandler_WithFactoryOfNullInterface(t *testing.T) {
  298. information := NewServiceInformation(FactoryOfNullTestService)
  299. userId := "userId"
  300. user := &global.User{UserId: userId}
  301. sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
  302. if err != nil {
  303. t.Fatalf("意外错误:%s", err.Error())
  304. }
  305. _, err = information.BuildHandler(sessionContext)
  306. if err == nil {
  307. t.Fatalf("意外的没有错误!")
  308. }
  309. if err.Error() != fmt.Sprintf("实际值不能是空接口!") {
  310. t.Fatalf("意外错误:%s", err.Error())
  311. }
  312. }
  313. func TestServiceInformation_BuildHandler_WithNoIn(t *testing.T) {
  314. information := NewServiceInformation(FactoryOfTestWithNoInSetContext)
  315. userId := "userId"
  316. user := &global.User{UserId: userId}
  317. sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
  318. if err != nil {
  319. t.Fatalf("意外错误:%s", err.Error())
  320. }
  321. _, err = information.BuildHandler(sessionContext)
  322. if err == nil {
  323. t.Fatalf("意外的没有错误!")
  324. }
  325. if err.Error() != fmt.Sprintf("设置器参数数量不正确!") {
  326. t.Fatalf("意外错误:%s", err.Error())
  327. }
  328. }
  329. func TestServiceInformation_BuildHandler_WithReturnValue(t *testing.T) {
  330. information := NewServiceInformation(FactoryOfTestWithReturnValueSetContext)
  331. userId := "userId"
  332. user := &global.User{UserId: userId}
  333. sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
  334. if err != nil {
  335. t.Fatalf("意外错误:%s", err.Error())
  336. }
  337. _, err = information.BuildHandler(sessionContext)
  338. if err == nil {
  339. t.Fatalf("意外的没有错误!")
  340. }
  341. if err.Error() != fmt.Sprintf("设置器返回值数量不正确!") {
  342. t.Fatalf("意外错误:%s", err.Error())
  343. }
  344. }
  345. func TestServiceInformation_BuildHandler_WithErrorIn(t *testing.T) {
  346. information := NewServiceInformation(FactoryOfTestWithErrorInSetContext)
  347. userId := "userId"
  348. user := &global.User{UserId: userId}
  349. sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
  350. if err != nil {
  351. t.Fatalf("意外错误:%s", err.Error())
  352. }
  353. _, err = information.BuildHandler(sessionContext)
  354. if err == nil {
  355. t.Fatalf("意外的没有错误!")
  356. }
  357. if err.Error() != fmt.Sprintf("设置器类型不正确!") {
  358. t.Fatalf("意外错误:%s", err.Error())
  359. }
  360. }