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

package container
import (
"fmt"
"LEIT_PM/global"
"reflect"
"testing"
)
func TestServiceInformation_New(t *testing.T) {
serviceInformation := NewServiceInformation(FactoryOfTestService)
if serviceInformation == nil {
t.Fatalf("创建失败!")
}
if serviceInformation.IsGlobal() {
t.Fatalf("目前不支持全局组件")
}
if serviceInformation.interfaceType != testServiceType {
t.Fatalf("接口类型错误")
}
}
func TestServiceInformation_New_NullError(t *testing.T) {
defer func() {
if err := recover(); err != "工厂不能为空!" {
t.Fatalf("意外错误:%s", err)
}
}()
_ = NewServiceInformation(nil)
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_New_FactoryTypeError(t *testing.T) {
defer func() {
if err := recover(); err != "工厂必须是func!" {
t.Fatalf("意外错误:%s", err)
}
}()
_ = NewServiceInformation(reflect.TypeOf(FactoryOfTestElement()))
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_New_ParameterCountError(t *testing.T) {
defer func() {
if err := recover(); err != "工厂不能有参数!" {
t.Fatalf("意外错误:%s", err)
}
}()
_ = NewServiceInformation(FactoryOfServiceTestInterfaceWithContent)
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_New_NoReturnValueError(t *testing.T) {
defer func() {
if err := recover(); err != "工厂必值需有返回值!" {
t.Fatalf("意外错误:%s", err)
}
}()
_ = NewServiceInformation(FactoryWithoutResult)
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_New_TooManyResultsError(t *testing.T) {
defer func() {
if err := recover(); err != "工厂只能有一个返回值!" {
t.Fatalf("意外错误:%s", err)
}
}()
_ = NewServiceInformation(FactoryWithTooManyResults)
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_New_ReturnValueTypeError(t *testing.T) {
defer func() {
if err := recover(); err != "工厂的返回值必需是interface!" {
t.Fatalf("意外错误:%s", err)
}
}()
_ = NewServiceInformation(FactoryOfTestElementImplement)
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_Interface(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
information.interfaceType = testServiceType
if information.Interface() != testServiceType {
t.Fatalf("获取接口类型错误")
}
}
func TestServiceInformation_RegisterMethod(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
if information.Interface() != testServiceType {
t.Fatalf("接口类型错误")
}
methodName := "String"
method := information.RegisterMethod(methodName)
if method == nil {
t.Fatalf("没找到方法")
}
if len(information.methodMapping) != 1 {
t.Fatalf("方法信息映射数量错误!")
}
storedMethod, ok := information.methodMapping[methodName]
if !ok {
t.Fatalf("没找到注册的方法!")
}
if storedMethod != method {
t.Fatalf("注册的方法不正确!")
}
}
func TestServiceInformation_RegisterMethod_EmptyMethodName(t *testing.T) {
defer func() {
if err := recover(); err != "方法名不能为空!" {
t.Fatalf("意外错误:%s", err)
}
}()
information := NewServiceInformation(FactoryOfTestService)
if information.Interface() != testServiceType {
t.Fatalf("接口类型错误")
}
methodName := ""
_ = information.RegisterMethod(methodName)
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_RegisterMethod_RepeatMethod(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
if information.Interface() != testServiceType {
t.Fatalf("接口类型错误")
}
methodName := "String"
method := information.RegisterMethod(methodName)
if method == nil {
t.Fatalf("没找到方法")
}
defer func() {
if err := recover(); err != fmt.Sprintf("方法%s已经注册!", methodName) {
t.Fatalf("意外错误:%s", err)
}
}()
_ = information.RegisterMethod("String")
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_RegisterMethod_MissingMethod(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
if information.Interface() != testServiceType {
t.Fatalf("接口类型错误")
}
methodName := "M1"
defer func() {
if err := recover(); err != fmt.Sprintf("找不到方法%s!", methodName) {
t.Fatalf("意外错误:%s", err)
}
}()
_ = information.RegisterMethod(methodName)
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_RegisterMethod_ReturnValuesCountError(t *testing.T) {
defer func() {
if err := recover(); err != fmt.Sprintf("返回值数量错误! 返回值数量: %d", 3) {
t.Fatalf("意外错误:%s", err)
}
}()
information := NewServiceInformation(FactoryOfTestService)
if information.Interface() != testServiceType {
t.Fatalf("接口类型错误")
}
methodName := "String2"
_ = information.RegisterMethod(methodName)
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_RegisterMethod_ReturnValueTypeError(t *testing.T) {
defer func() {
if err := recover(); err != fmt.Sprintf("最后一个返回值类型错误! 类型:%s", "string") {
t.Fatalf("意外错误:%s", err)
}
}()
information := NewServiceInformation(FactoryOfTestService)
if information.Interface() != testServiceType {
t.Fatalf("接口类型错误")
}
methodName := "NoError"
_ = information.RegisterMethod(methodName)
t.Fatalf("意外的没有错误!")
}
func TestServiceInformation_Method(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
methodName := "String"
method, ok := testServiceType.MethodByName(methodName)
if !ok {
t.Fatalf("意外的没找到方法:%s", methodName)
}
methodInformation := NewServiceMethodInformation(&method)
information.methodMapping[methodName] = methodInformation
if information.Method(methodName) == nil {
t.Fatalf("获取方法信息错误!")
}
}
func TestServiceInformation_Method_NullError(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
if information.Method("") != nil {
t.Fatalf("找到了空方法名的方法")
}
}
func TestServiceInformation_Method_NotFound(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
if information.Method("methodName") != nil {
t.Fatalf("找到了不存在的方法")
}
}
func TestServiceInformation_Methods(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
methodName := "String"
method, ok := testServiceType.MethodByName(methodName)
if !ok {
t.Fatalf("意外的没找到方法:%s", methodName)
}
methodInformation := NewServiceMethodInformation(&method)
information.methodMapping[methodName] = methodInformation
if len(information.Methods()) != 1 {
t.Fatalf("方法列表数量不正确!")
}
if information.Methods()[0] != methodInformation {
t.Fatalf("获取方法信息列表错误!")
}
}
func TestServiceInformation_Methods_Empty(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
if len(information.Methods()) != 0 {
t.Fatalf("方法列表意外的不为空!")
}
}
func TestServiceInformation_BuildHandler(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
if information.Interface() != testServiceType {
t.Fatalf("接口类型错误")
}
methodName := "String"
_ = information.RegisterMethod(methodName)
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.(*ServiceHandler)
if !ok {
t.Fatalf("Handler类型不正确!")
}
}
func TestServiceInformation_BuildHandler_NullCaller(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
if information.Interface() != testServiceType {
t.Fatalf("接口类型错误")
}
methodName := "String"
method := information.RegisterMethod(methodName)
userId := "userId"
user := &global.User{UserId: userId}
sessionContext, err := NewSessionContext(user, transactionHandlerFactoryMock)
if err != nil {
t.Fatalf("意外错误:%s", err.Error())
}
var callerBrokerBuilder CallerBrokerBuilder = new(TestCallerBrokerBuilderImplementWithError)
method.AppendBrokerBuilder(callerBrokerBuilder)
_, err = information.BuildHandler(sessionContext)
if err == nil {
t.Fatalf("意外的没有错误!")
}
if err.Error() != fmt.Sprintf("第2个调用器不能为空!") {
t.Fatalf("意外错误:%s", err.Error())
}
}
func TestServiceInformation_BuildHandler_NullSession(t *testing.T) {
information := NewServiceInformation(FactoryOfTestService)
if information.Interface() != testServiceType {
t.Fatalf("接口类型错误")
}
_, err := information.BuildHandler(nil)
if err == nil {
t.Fatalf("意外的没有错误!")
}
if err.Error() != "会话上下文不能为空!" {
t.Fatalf("意外错误:%s", err.Error())
}
}
func TestServiceInformation_BuildHandler_WithTooManyIn(t *testing.T) {
information := NewServiceInformation(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 TestServiceInformation_BuildHandler_WithFactoryOfNullInterface(t *testing.T) {
information := NewServiceInformation(FactoryOfNullTestService)
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 TestServiceInformation_BuildHandler_WithNoIn(t *testing.T) {
information := NewServiceInformation(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 TestServiceInformation_BuildHandler_WithReturnValue(t *testing.T) {
information := NewServiceInformation(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 TestServiceInformation_BuildHandler_WithErrorIn(t *testing.T) {
information := NewServiceInformation(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())
}
}