Exemplo n.º 1
0
        protected override void PostInitialize()
        {
            base.PostInitialize();
            LocalIocManager.Register <IAbpSession, CustomTestSession>(DependencyLifeStyle.Singleton);

            CustomTestSession = LocalIocManager.Resolve <CustomTestSession>();
        }
Exemplo n.º 2
0
        public void Should_Call_Module_Events_Once()
        {
            LocalIocManager.Register <IModuleFinder, MyTestModuleFinder>();

            _bootstrapper.Initialize();
            _bootstrapper.Dispose();

            var testModule    = LocalIocManager.Resolve <MyTestModule>();
            var otherModule   = LocalIocManager.Resolve <MyOtherModule>();
            var anotherModule = LocalIocManager.Resolve <MyAnotherModule>();

            testModule.PreInitializeCount.ShouldBe(1);
            testModule.InitializeCount.ShouldBe(1);
            testModule.PostInitializeCount.ShouldBe(1);
            testModule.ShutdownCount.ShouldBe(1);

            otherModule.PreInitializeCount.ShouldBe(1);
            otherModule.InitializeCount.ShouldBe(1);
            otherModule.PostInitializeCount.ShouldBe(1);
            otherModule.ShutdownCount.ShouldBe(1);
            otherModule.CallMeOnStartupCount.ShouldBe(1);

            anotherModule.PreInitializeCount.ShouldBe(1);
            anotherModule.InitializeCount.ShouldBe(1);
            anotherModule.PostInitializeCount.ShouldBe(1);
            anotherModule.ShutdownCount.ShouldBe(1);
        }
        public void Should_Set_ConnectionString_If_Configured()
        {
            new EntityFrameworkConventionalRegisterer <int, long>()
            .RegisterAssembly(
                new ConventionalRegistrationContext(
                    Assembly.GetExecutingAssembly(),
                    LocalIocManager,
                    new ConventionalRegistrationConfig()
                    ));

            //Should call default constructor since IAbpStartupConfiguration is not configured.
            var context1 = LocalIocManager.Resolve <MyDbContext>();

            context1.CalledConstructorWithConnectionString.ShouldBe(false);

            LocalIocManager.Register <IAbpStartupConfiguration, AbpStartupConfiguration>();

            //Should call default constructor since IAbpStartupConfiguration registered by IAbpStartupConfiguration.DefaultNameOrConnectionString is not set.
            var context2 = LocalIocManager.Resolve <MyDbContext>();

            context2.CalledConstructorWithConnectionString.ShouldBe(false);

            LocalIocManager.Resolve <IAbpStartupConfiguration>().DefaultNameOrConnectionString = "Server=localhost;Database=test;User=sa;Password=123";

            //Should call constructor with nameOrConnectionString since IAbpStartupConfiguration.DefaultNameOrConnectionString is set.
            var context3 = LocalIocManager.Resolve <MyDbContext>();

            context3.CalledConstructorWithConnectionString.ShouldBe(true);
        }
        public AuthorizationInterceptor_Tests()
        {
            //SUT: AuthorizationInterceptor and AuthorizeAttributeHelper
            LocalIocManager.IocContainer.Register(
                Component.For <IFeatureChecker>().Instance(Substitute.For <IFeatureChecker>())
                );
            LocalIocManager.Register <AuthorizationInterceptor>(DependencyLifeStyle.Transient);
            LocalIocManager.Register <IAuthorizationHelper, AuthorizationHelper>(DependencyLifeStyle.Transient);
            LocalIocManager.IocContainer.Register(
                Component.For <MyTestClassToBeAuthorized_Sync>().Interceptors <AuthorizationInterceptor>().LifestyleTransient(),
                Component.For <MyTestClassToBeAuthorized_Async>().Interceptors <AuthorizationInterceptor>().LifestyleTransient()
                );

            //Mock session
            var session = Substitute.For <IAbpSession>();

            session.TenantId.Returns(1);
            session.UserId.Returns(1);
            LocalIocManager.IocContainer.Register(Component.For <IAbpSession>().UsingFactoryMethod(() => session));

            //Mock permission checker
            var permissionChecker = Substitute.For <IPermissionChecker>();

            permissionChecker.IsGrantedAsync("Permission1").Returns(true);
            permissionChecker.IsGrantedAsync("Permission2").Returns(true);
            permissionChecker.IsGrantedAsync("Permission3").Returns(false); //Permission3 is not granted
            LocalIocManager.IocContainer.Register(Component.For <IPermissionChecker>().UsingFactoryMethod(() => permissionChecker));

            _syncObj  = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Sync>();
            _asyncObj = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Async>();
        }
        public void Should_Call_Initialize()
        {
            LocalIocManager.Register <MyService>(DependencyLifeStyle.Transient);
            var myService = LocalIocManager.Resolve <MyService>();

            myService.InitializeCount.ShouldBe(1);
        }
        public void Should_Fail_Circular_Constructor_Dependency()
        {
            LocalIocManager.Register <MyClass1>();
            LocalIocManager.Register <MyClass2>();
            LocalIocManager.Register <MyClass3>();

            Assert.Throws <CircularDependencyException>(() => LocalIocManager.Resolve <MyClass1>());
        }
Exemplo n.º 7
0
        public void HddBasedIdentifierProvider_Should_Work()
        {
            LocalIocManager.Register <IUniqueIdentifierProvider, HddBasedIdentifierProvider>();

            var hddBasedUniqueIdenfierProvider = Resolve <IUniqueIdentifierProvider>();

            hddBasedUniqueIdenfierProvider.Get().ShouldNotBeNull();
        }
Exemplo n.º 8
0
        public ObjectComparatorManager_Tests()
        {
            LocalIocManager.Register <IObjectComparator, ObjectComparatorTestClassObjectComparator>();
            LocalIocManager.Register <IObjectComparator, MyTestStringObjectComparator>();
            LocalIocManager.Register <IObjectComparatorManager, ObjectComparatorManager>();

            _objectComparatorManager = LocalIocManager.Resolve <IObjectComparatorManager>();
        }
Exemplo n.º 9
0
        public void CpuBasedIdentifierProvider_Should_Work()
        {
            LocalIocManager.Register <IUniqueIdentifierProvider, CpuBasedIdentifierProvider>();

            var sut = Resolve <IUniqueIdentifierProvider>();

            sut.Get().ShouldNotBeNull();
        }
Exemplo n.º 10
0
        public JsonAndXmlSourceMixing_Tests()
        {
            LocalIocManager.Register <ILanguageManager, LanguageManager>();
            LocalIocManager.Register <ILanguageProvider, DefaultLanguageProvider>();

            _bootstrapper = AbpBootstrapper.Create <MyLangModule>(LocalIocManager);
            _bootstrapper.Initialize();
        }
Exemplo n.º 11
0
        public void Should_Call_Dispose_Of_Transient_Dependency_When_Object_Is_Released()
        {
            LocalIocManager.Register <TestSimpleDisposableObject>(DependencyLifeStyle.Transient);
            var obj = LocalIocManager.Resolve <TestSimpleDisposableObject>();

            LocalIocManager.Release(obj);

            obj.DisposeCount.ShouldBe(1);
        }
Exemplo n.º 12
0
        public void Should_Call_Dispose_Of_Singleton_Dependency_When_IocManager_Is_Disposed()
        {
            LocalIocManager.Register <TestSimpleDisposableObject>();
            var obj = LocalIocManager.IocContainer.Resolve <TestSimpleDisposableObject>();

            LocalIocManager.Dispose();

            obj.DisposeCount.ShouldBe(1);
        }
Exemplo n.º 13
0
        public void ResolveAsDisposable_With_Constructor_Args_Should_Work()
        {
            LocalIocManager.Register <SimpleDisposableObject>(DependencyLifeStyle.Transient);

            using (var wrapper = LocalIocManager.ResolveAsDisposable <SimpleDisposableObject>(new { myData = 42 }))
            {
                wrapper.Object.MyData.ShouldBe(42);
            }
        }
Exemplo n.º 14
0
        public void UsingScope_Test_ShouldWork()
        {
            LocalIocManager.Register <TestSimpleDisposableObject>(DependencyLifeStyle.Transient);
            TestSimpleDisposableObject testSimpleObj = null;

            LocalIocManager.UsingScope(scope => { testSimpleObj = scope.Resolve <TestSimpleDisposableObject>(); });

            testSimpleObj.DisposeCount.ShouldBe(1);
        }
Exemplo n.º 15
0
        public void IIocScopedResolver_Test_IsRegistered_ShouldWork()
        {
            LocalIocManager.Register <ISimpleDependency, SimpleDependency>(DependencyLifeStyle.Transient);

            using (var scope = LocalIocManager.CreateScope())
            {
                scope.IsRegistered <ISimpleDependency>().ShouldBe(true);
                scope.IsRegistered(typeof(ISimpleDependency)).ShouldBe(true);
            }
        }
Exemplo n.º 16
0
        public void UsingScope_Test_With_Constructor_ShouldWork()
        {
            LocalIocManager.Register <SimpleDisposableObject>(DependencyLifeStyle.Transient);

            SimpleDisposableObject simpleObj = null;

            LocalIocManager.UsingScope(scope => { simpleObj = scope.Resolve <SimpleDisposableObject>(new { myData = 40 }); });

            simpleObj.MyData.ShouldBe(40);
        }
Exemplo n.º 17
0
        protected void InitializeAbp()
        {
            LocalIocManager.Register <IAbpSession, TestAbpSession>();

            PreInitialize();

            AbpBootstrapper.Initialize();

            AbpSession = LocalIocManager.Resolve <TestAbpSession>();
        }
        public DefaultLanguageProvider_Test()
        {
            LocalIocManager.Register <ILanguageProvider, DefaultLanguageProvider>();
            var bootstrapper = AbpBootstrapper.Create <DefaultLanguageProviderLangModule>(options =>
            {
                options.IocManager = LocalIocManager;
            });

            bootstrapper.Initialize();
        }
Exemplo n.º 19
0
        public JsonAndXmlSourceMixing_Tests()
        {
            LocalIocManager.Register <IModuleFinder, MyTestModuleFinder>();

            LocalIocManager.Register <ILanguageManager, LanguageManager>();
            LocalIocManager.Register <ILanguageProvider, DefaultLanguageProvider>();

            _bootstrapper = new AbpBootstrapper(LocalIocManager);
            _bootstrapper.Initialize();
        }
        private void Initialize_Test(DependencyLifeStyle lifeStyle)
        {
            MyClass1.CreateCount = 0;
            MyClass2.CreateCount = 0;
            MyClass3.CreateCount = 0;

            LocalIocManager.Register <MyClass1>(lifeStyle);
            LocalIocManager.Register <MyClass2>(lifeStyle);
            LocalIocManager.Register <MyClass3>(lifeStyle);
        }
Exemplo n.º 21
0
        public AuthorizationInterceptor_Tests()
        {
            //SUT: AuthorizationInterceptor and AuthorizeAttributeHelper
            LocalIocManager.IocContainer.Register(
                Component.For <IFeatureChecker>().Instance(Substitute.For <IFeatureChecker>())
                );

            LocalIocManager.Register <IAuthorizationConfiguration, AuthorizationConfiguration>();
            LocalIocManager.Register <IMultiTenancyConfig, MultiTenancyConfig>();

            //AuthorizationInterceptor AbpAsyncDeterminationInterceptor<AuthorizationInterceptor> 必须都注册
            //AuthorizationInterceptor不注册时,提示AbpAsyncDeterminationInterceptor<AuthorizationInterceptor>找不到依赖
            //AbpAsyncDeterminationInterceptor<AuthorizationInterceptor>不注册时,容器报错提示未注入
            LocalIocManager.Register <AuthorizationInterceptor>(DependencyLifeStyle.Transient);
            //这个操作就相当于AbpKernelModule中的RegisterInterceptors方法
            LocalIocManager.Register <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >(DependencyLifeStyle.Transient);

            LocalIocManager.Register <IAuthorizationHelper, AuthorizationHelper>(DependencyLifeStyle.Transient);
            LocalIocManager.IocContainer.Register(
                //这里的操作有两个功能
                //这里同时对该实例( Component.For<MyTestClassToBeAuthorized_Sync>()返回的)进行两个操作
                Component.For <MyTestClassToBeAuthorized_Sync>()                               //1:将几个类添加到容器中。
                .Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >() //2:给几个类的实例添加拦截器。因为拦截器也是在IRegistration实例上操作的
                .LifestyleTransient(),                                                         //将拦截器设置为瞬时的:官方建议无特殊原因应始终使用瞬时的,避免比使用拦截器的对象生命周期更长
                Component.For <MyTestClassToBeAuthorized_Async>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient(),
                Component.For <MyTestClassToBeAllowProtected_Async>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient(),
                Component.For <MyTestClassToBeAllowProtected_Sync>().Interceptors <AbpAsyncDeterminationInterceptor <AuthorizationInterceptor> >().LifestyleTransient()
                );

            //Mock session
            var session = Substitute.For <IAbpSession>();

            session.TenantId.Returns(1);
            session.UserId.Returns(1);
            LocalIocManager.IocContainer.Register(Component.For <IAbpSession>().Instance(session));

            //Mock permission checker
            var permissionChecker = Substitute.For <IPermissionChecker>();

            permissionChecker.IsGrantedAsync("Permission1").Returns(true);
            permissionChecker.IsGrantedAsync("Permission2").Returns(true);
            permissionChecker.IsGrantedAsync("Permission3").Returns(false); //Permission3 is not granted

            permissionChecker.IsGranted("Permission1").Returns(true);
            permissionChecker.IsGranted("Permission2").Returns(true);
            permissionChecker.IsGranted("Permission3").Returns(false); //Permission3 is not granted
            //替换默认的权限校验类
            LocalIocManager.IocContainer.Register(Component.For <IPermissionChecker>().Instance(permissionChecker));

            _syncObj  = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Sync>();
            _asyncObj = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Async>();

            _syncObjForProtectedMethod  = LocalIocManager.Resolve <MyTestClassToBeAllowProtected_Sync>();
            _asyncObjForProtectedMethod = LocalIocManager.Resolve <MyTestClassToBeAllowProtected_Async>();
        }
Exemplo n.º 22
0
        private void ConfigureTokenAuth()
        {
            LocalIocManager.Register <TokenAuthConfiguration>();
            var tokenAuthConfig = LocalIocManager.Resolve <TokenAuthConfiguration>();

            tokenAuthConfig.SecurityKey        = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("SheshaTest_C421AAEE0D114E9C"));
            tokenAuthConfig.Issuer             = "SheshaTest";
            tokenAuthConfig.Audience           = "SheshaTest";
            tokenAuthConfig.SigningCredentials = new SigningCredentials(tokenAuthConfig.SecurityKey, SecurityAlgorithms.HmacSha256);
            tokenAuthConfig.Expiration         = TimeSpan.FromDays(1);
        }
Exemplo n.º 23
0
 protected void EnsureClassRegistered(Type type, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Transient)
 {
     if (!LocalIocManager.IsRegistered(type))
     {
         if (!type.IsClass || type.IsAbstract)
         {
             throw new WindException("Can not register " + type.Name + ". It should be a non-abstract class. If not, it should be registered before.");
         }
         LocalIocManager.Register(type, lifeStyle);
     }
 }
        public void Should_Inject_TestClass_For_ApplicationService()
        {
            var empty = Substitute.For <EmptyClass>();

            empty.Count = 1;

            LocalIocManager.IocContainer.Register(Component.For <EmptyClass>().Instance(empty));
            LocalIocManager.Register <IEmptyService, EmptyService>(DependencyLifeStyle.Transient);

            LocalIocManager.Resolve <IEmptyService>().Empty.ShouldNotBeNull();
            LocalIocManager.Resolve <IEmptyService>().Empty.Count.ShouldBe(1);
        }
        public DefaultUserNavigationManagerTest()
        {
            LocalIocManager.Register <IUserNavigationManager, DefaultUserNavigationManager>();
            LocalIocManager.Register <IAuthorizationService, RoleAuthorizationService>();
            LocalIocManager.Register <IUserManager, DefaultUserManager>();

            IDefaultControllerBuilderFactory factory = new MvcControllerBuilderFactory(LocalIocManager);
            var servicePrefix = TestModule.ModuleName.ToCamelCase();
            var servicesType  = LocalIocManager.IocContainer.Kernel.GetHandlers().SelectMany(t => t.Services).Where(t => t.Assembly == Assembly.GetExecutingAssembly());

            factory.ForAll <BlocksWebMvcController>(servicePrefix, servicesType).Build();
        }
Exemplo n.º 26
0
        public void ResolveAsDisposable_Should_Work()
        {
            LocalIocManager.Register <SimpleDisposableObject>(DependencyLifeStyle.Transient);

            SimpleDisposableObject simpleObj;

            using (var wrapper = LocalIocManager.ResolveAsDisposable <SimpleDisposableObject>())
            {
                wrapper.Object.ShouldNotBe(null);
                simpleObj = wrapper.Object;
            }

            simpleObj.DisposeCount.ShouldBe(1);
        }
Exemplo n.º 27
0
        public void IIocScopedResolver_Test_Custom_Release_ShouldWork()
        {
            LocalIocManager.Register <ISimpleDependency, SimpleDependency>(DependencyLifeStyle.Transient);

            ISimpleDependency simpleDependency;

            using (var scope = LocalIocManager.CreateScope())
            {
                simpleDependency = scope.Resolve <ISimpleDependency>();
                scope.Release(simpleDependency);
            }

            simpleDependency.DisposeCount.ShouldBe(1);
        }
Exemplo n.º 28
0
        public AuthorizationInterceptor_Tests()
        {
            //SUT: AuthorizationInterceptor and AuthorizeAttributeHelper
            LocalIocManager.Register <AuthorizationInterceptor <int, long> >(DependencyLifeStyle.Transient);
            LocalIocManager.Register <IAuthorizeAttributeHelper <int, long>, AuthorizeAttributeHelper <int, long> >(DependencyLifeStyle.Transient);


            LocalIocManager.IocContainer.Register(
                Component.For(typeof(IAuthorizeAttributeHelper <,>)).ImplementedBy(typeof(AuthorizeAttributeHelper <,>)),
                Component.For <MyTestClassToBeAuthorized_Sync>().Interceptors <AuthorizationInterceptor <int, long> >().LifestyleTransient(),
                Component.For <MyTestClassToBeAuthorized_Async>().Interceptors <AuthorizationInterceptor <int, long> >().LifestyleTransient()
                );

            var handlers = LocalIocManager.IocContainer.Kernel.GetAssignableHandlers(typeof(object));
            var obj      = handlers.FirstOrDefault(
                x => x.ComponentModel.Implementation.IsGenericType && x.ComponentModel.Implementation.GetGenericTypeDefinition() == typeof(AuthorizationInterceptor <,>));

            var i     = obj.ComponentModel.Implementation;
            var type1 = i.GetGenericArguments()[0];
            var type2 = i.GetGenericArguments()[1];

            dynamic helper = LocalIocManager.ResolveAsDisposable(typeof(IAuthorizeAttributeHelper <,>).MakeGenericType(type1, type2));
            dynamic obj2   = helper.Object;

            var ss = Substitute.For <AbpAuthorizeAttribute>();


            //obj2.Authorize(ss);


            //helper.Object.
            //Mock session
            var session = Substitute.For <IAbpSession <int, long> >();

            session.TenantId.Returns(1);
            session.UserId.Returns(1);
            LocalIocManager.IocContainer.Register(Component.For <IAbpSession <int, long> >().UsingFactoryMethod(() => session));

            //Mock permission checker
            var permissionChecker = Substitute.For <IPermissionChecker <int, long> >();

            permissionChecker.IsGrantedAsync("Permission1").Returns(Task.FromResult(true));
            permissionChecker.IsGrantedAsync("Permission2").Returns(Task.FromResult(true));
            permissionChecker.IsGrantedAsync("Permission3").Returns(Task.FromResult(false)); //Permission3 is not granted
            LocalIocManager.IocContainer.Register(Component.For <IPermissionChecker <int, long> >().UsingFactoryMethod(() => permissionChecker));

            _syncObj  = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Sync>();
            _asyncObj = LocalIocManager.Resolve <MyTestClassToBeAuthorized_Async>();
        }
        public void Should_Inject_Session_For_ApplicationService()
        {
            var session = Substitute.For <ISharePlatformSession>();

            session.UserId.Returns("42");

            LocalIocManager.Register <MyApplicationService>();
            LocalIocManager.IocContainer.Register(
                Component.For <ISharePlatformSession>().Instance(session)
                );

            var myAppService = LocalIocManager.Resolve <MyApplicationService>();

            myAppService.TestSession();
        }
Exemplo n.º 30
0
        protected override void PreInitialize()
        {
            base.PreInitialize();

            //Fake DbConnection using Effort!
            LocalIocManager.IocContainer.Register(
                Component.For <DbConnection>()
                .UsingFactoryMethod(Effort.DbConnectionFactory.CreateTransient)
                .LifestyleSingleton()
                );

            LocalIocManager.Register <IAbpRedisConnectionProvider, AbpRedisConnectionProvider>();
            LocalIocManager.RegisterIfNot <AbpRedisCacheConfig>();
            LocalIocManager.Register <ICacheSyncService, TestCacheSyncService>();
        }