|
|
- package container
-
- import (
- "fmt"
- "leit.com/LAPP_CHEERSSON_BACKEND/global"
- "reflect"
- "testing"
- )
-
- func TestElementInformation_New(t *testing.T) {
-
- elementInformation := NewElementInformation(FactoryOfTestElement)
- if elementInformation == nil {
- t.Fatalf("创建失败!")
- }
- if elementInformation.IsGlobal() {
- t.Fatalf("目前不支持全局组件")
- }
- if elementInformation.interfaceType != testElementType {
- t.Fatalf("接口类型错误")
- }
- }
-
- func TestElementInformation_New_NullError(t *testing.T) {
- defer func() {
- if err := recover(); err != "工厂不能为空!" {
- t.Fatalf("意外错误:%s", err)
- }
- }()
-
- _ = NewElementInformation(nil)
- t.Fatalf("意外的没有错误!")
- }
-
- func TestElementInformation_New_FactoryTypeError(t *testing.T) {
- defer func() {
- if err := recover(); err != "工厂必须是func!" {
- t.Fatalf("意外错误:%s", err)
- }
- }()
-
- _ = NewElementInformation(reflect.TypeOf(FactoryOfTestElement()))
- t.Fatalf("意外的没有错误!")
- }
-
- func TestElementInformation_New_ParameterCountError(t *testing.T) {
- defer func() {
- if err := recover(); err != "工厂不能有参数!" {
- t.Fatalf("意外错误:%s", err)
- }
- }()
-
- _ = NewElementInformation(FactoryOfElementTestInterfaceWithContent)
- t.Fatalf("意外的没有错误!")
- }
-
- func TestElementInformation_New_NoReturnValueError(t *testing.T) {
- defer func() {
- if err := recover(); err != "工厂必值需有返回值!" {
- t.Fatalf("意外错误:%s", err)
- }
- }()
-
- _ = NewElementInformation(FactoryWithoutResult)
- t.Fatalf("意外的没有错误!")
- }
-
- func TestElementInformation_New_TooManyResultsError(t *testing.T) {
- defer func() {
- if err := recover(); err != "工厂只能有一个返回值!" {
- t.Fatalf("意外错误:%s", err)
- }
- }()
-
- _ = NewElementInformation(FactoryWithTooManyResults)
- t.Fatalf("意外的没有错误!")
- }
-
- func TestElementInformation_New_ReturnValueTypeError(t *testing.T) {
- defer func() {
- if err := recover(); err != "工厂的返回值必需是interface!" {
- t.Fatalf("意外错误:%s", err)
- }
- }()
-
- _ = NewElementInformation(FactoryOfTestElementImplement)
- t.Fatalf("意外的没有错误!")
- }
-
- func TestElementInformation_Interface(t *testing.T) {
-
- information := NewElementInformation(FactoryOfTestElement)
- information.interfaceType = testElementType
- if information.Interface() != testElementType {
- t.Fatalf("获取接口类型错误")
- }
- }
-
- func TestElementInformation_BuildHandler(t *testing.T) {
-
- information := NewElementInformation(FactoryOfTestElement)
- userId := "userId"
- user := &global.User{UserId: userId}
- sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
- if err != nil {
- t.Fatalf("意外错误:%s", err.Error())
- }
- handler, err := information.BuildHandler(sessionContext)
- if err != nil {
- t.Fatalf("意外错误:%s", err.Error())
- }
- _, ok := handler.(*ElementHandler)
- if !ok {
- t.Fatalf("Handler类型不正确!")
- }
- }
-
- func TestElementInformation_BuildHandler_NullSession(t *testing.T) {
-
- information := NewElementInformation(FactoryOfTestElement)
- if information.Interface() != testElementType {
- t.Fatalf("接口类型错误")
- }
- _, err := information.BuildHandler(nil)
- if err == nil {
- t.Fatalf("意外的没有错误!")
- }
- if err.Error() != "会话上下文不能为空!" {
- t.Fatalf("意外错误:%s", err.Error())
- }
- }
-
- func TestElementInformation_BuildHandler_WithFactoryOfNullInterface(t *testing.T) {
-
- information := NewElementInformation(FactoryOfNullTestElement)
- userId := "userId"
- user := &global.User{UserId: userId}
- sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
- if err != nil {
- t.Fatalf("意外错误:%s", err.Error())
- }
- _, err = information.BuildHandler(sessionContext)
- if err == nil {
- t.Fatalf("意外的没有错误!")
- }
- if err.Error() != fmt.Sprintf("实际值不能是空接口!") {
- t.Fatalf("意外错误:%s", err.Error())
- }
- }
-
- func TestElementInformation_BuildHandler_WithTooManyIn(t *testing.T) {
-
- information := NewElementInformation(FactoryOfTestWithTooManyInSetContext)
- userId := "userId"
- user := &global.User{UserId: userId}
- sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
- if err != nil {
- t.Fatalf("意外错误:%s", err.Error())
- }
- _, err = information.BuildHandler(sessionContext)
- if err == nil {
- t.Fatalf("意外的没有错误!")
- }
- if err.Error() != fmt.Sprintf("设置器参数数量不正确!") {
- t.Fatalf("意外错误:%s", err.Error())
- }
- }
-
- func TestElementInformation_BuildHandler_WithNoIn(t *testing.T) {
-
- information := NewElementInformation(FactoryOfTestWithNoInSetContext)
- userId := "userId"
- user := &global.User{UserId: userId}
- sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
- if err != nil {
- t.Fatalf("意外错误:%s", err.Error())
- }
- _, err = information.BuildHandler(sessionContext)
- if err == nil {
- t.Fatalf("意外的没有错误!")
- }
- if err.Error() != fmt.Sprintf("设置器参数数量不正确!") {
- t.Fatalf("意外错误:%s", err.Error())
- }
- }
-
- func TestElementInformation_BuildHandler_WithReturnValue(t *testing.T) {
-
- information := NewElementInformation(FactoryOfTestWithReturnValueSetContext)
- userId := "userId"
- user := &global.User{UserId: userId}
- sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
- if err != nil {
- t.Fatalf("意外错误:%s", err.Error())
- }
- _, err = information.BuildHandler(sessionContext)
- if err == nil {
- t.Fatalf("意外的没有错误!")
- }
- if err.Error() != fmt.Sprintf("设置器返回值数量不正确!") {
- t.Fatalf("意外错误:%s", err.Error())
- }
- }
-
- func TestElementInformation_BuildHandler_WithErrorIn(t *testing.T) {
-
- information := NewElementInformation(FactoryOfTestWithErrorInSetContext)
- userId := "userId"
- user := &global.User{UserId: userId}
- sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
- if err != nil {
- t.Fatalf("意外错误:%s", err.Error())
- }
- _, err = information.BuildHandler(sessionContext)
- if err == nil {
- t.Fatalf("意外的没有错误!")
- }
- if err.Error() != fmt.Sprintf("设置器类型不正确!") {
- t.Fatalf("意外错误:%s", err.Error())
- }
- }
|