コード例 #1
0
        public IInstanceProvider Get <TImplementation>(LifeTime lifeTime, InstanceProtectionMode protectionMode,
                                                       InjectionAlertChannel alertChannel) where TImplementation : new()
        {
            IInstanceProvider instance;

            switch (lifeTime)
            {
            case LifeTime.Singleton:
                instance = new SingletonInstanceProvider <TImplementation>(protectionMode);
                break;

            case LifeTime.Transient:
                instance = new TransientInstanceProvider <TImplementation>(protectionMode);
                break;

            case LifeTime.Unknown:
                throw new ArgumentOutOfRangeException(
                          $"You can only register {LifeTime.Unknown} instances with a {nameof(Func<IInstanceProvider>)} parameter.Please check overloaded method.s");

            default:
                throw new ArgumentOutOfRangeException(nameof(lifeTime), lifeTime, null);
            }

            instance.AlertChannel = alertChannel;
            return(instance);
        }
コード例 #2
0
        public void CanAlert_ForProtectModes_returnsTrue(InstanceProtectionMode protectionMode)
        {
            const bool expected = true;
            var        sut      = GetSut(protectionMode: protectionMode);
            var        actual   = sut.CanAlert;

            Assert.That(actual, Is.EqualTo(expected));
        }
コード例 #3
0
 /// <summary>
 ///     Internal constructor with all dependencies.
 /// </summary>
 internal CustomInstanceProvider(Func <TImplementation> instanceGetter,
                                 IInjectionDetector injectionDetector,
                                 InstanceProtectionMode protectionMode,
                                 InjectionAlertChannel alertChannel,
                                 LifeTime lifeTime = LifeTime.Unknown)
     : base(lifeTime, injectionDetector, protectionMode, alertChannel)
 {
     _instanceGetter = instanceGetter ?? throw new ArgumentNullException(nameof(instanceGetter));
 }
コード例 #4
0
 public CustomInstanceProvider(Func <TImplementation> instanceGetter, InstanceProtectionMode protectionMode, LifeTime lifeTime = LifeTime.Unknown)
     : base(lifeTime, protectionMode)
 {
     if (instanceGetter == null)
     {
         throw new ArgumentNullException(nameof(instanceGetter));
     }
     _instanceGetter = instanceGetter;
 }
コード例 #5
0
 protected SafeInstanceProviderBase(LifeTime lifeTime, IInjectionDetector injectionDetector,
                                    InstanceProtectionMode protectionMode, InjectionAlertChannel alertChannel)
     : base(protectionMode)
 {
     _injectionDetector    = injectionDetector ?? throw new ArgumentNullException(nameof(injectionDetector));
     LifeTime              = lifeTime;
     AssemblyQualifiedName = typeof(TImplementation).AssemblyQualifiedName;
     AlertChannel          = alertChannel;
     ChangeProtectionMode(new ProtectionChangeContext <InstanceProtectionMode>(protectionMode));
 }
コード例 #6
0
        public void Constructor_Sets_ProtectionMode(InstanceProtectionMode mode)
        {
            //arrange
            var expected = mode;
            //act
            var sut    = GetSut(protectionMode: expected);
            var actual = sut.CurrentProtectionMode;

            //assert
            Assert.That(actual, Is.EqualTo(expected));
        }
コード例 #7
0
 private static SafeInstanceProviderBase <string> GetSut(LifeTime lifeTime = LifeTime.Unknown,
                                                         InstanceProtectionMode protectionMode = InstanceProtectionMode.NoProtection,
                                                         IInjectionDetector injectionDetector  = null,
                                                         InjectionAlertChannel alertChannel    = InjectionAlertChannel.DebugFail)
 {
     return(new TestInstanceProvider(
                alertChannel: alertChannel,
                injectionDetector: injectionDetector ?? Stubs.Get <IInjectionDetector>(),
                lifeTime: lifeTime,
                protectionMode: protectionMode));
 }
コード例 #8
0
        public void GetGeneric_SetsInitialProtectionMode(InstanceProtectionMode expected)
        {
            //arrange
            var sut = GetSut();
            Func <InstanceProviderFactoryTests> func = () => new InstanceProviderFactoryTests();
            //act
            var instance = sut.Get(func, expected, InjectionAlertChannel.DebugWrite);
            var actual   = instance.CurrentProtectionMode;

            //assert
            Assert.That(actual, Is.EqualTo(expected));
        }
コード例 #9
0
        public void Get_SetsInitialProtectionMode(InstanceProtectionMode expected)
        {
            //arrange
            var sut = GetSut();
            //act
            var instance = sut.Get <InstanceProviderFactoryTests>(LifeTime.Transient, expected,
                                                                  InjectionAlertChannel.DebugWrite);
            var actual = instance.CurrentProtectionMode;

            //assert
            Assert.That(actual, Is.EqualTo(expected));
        }
コード例 #10
0
        public void SetProtectionMode_From_StateProtection_To_NonStateProtection_DoesNotNotifyInnerInjectionDetector(
            InstanceProtectionMode from, InstanceProtectionMode to)
        {
            //arrange
            var detectorMock = new Mock <IInjectionDetector>();
            var sut          = GetSut(protectionMode: from,
                                      injectionDetector: detectorMock.Object);

            detectorMock.Invocations.Clear();
            //act
            sut.SetProtectionMode(to);
            //assert
            detectorMock.Verify(d => d.NotifyChanges(It.IsAny <object>()), Times.Never);
        }
コード例 #11
0
        public IInstanceProvider Get <TImplementation>(Func <TImplementation> instanceGetter,
                                                       InstanceProtectionMode protectionMode,
                                                       InjectionAlertChannel alertChannel, LifeTime lifeTime = LifeTime.Unknown)
        {
            var result = new CustomInstanceProvider <TImplementation>(
                lifeTime: lifeTime,
                instanceGetter: instanceGetter,
                protectionMode: protectionMode)
            {
                AlertChannel = alertChannel
            };

            result.SetProtectionMode(protectionMode);
            return(result);
        }
コード例 #12
0
        public void Constructor_Sets_Inner_InjectionDetectorSettings_From_ProtectionMode(
            InstanceProtectionMode protectionMode, bool scanState, bool scanCode)
        {
            //arrange
            var injectionDetector = Mock.Of <IInjectionDetector>();

            injectionDetector.ScanCode  = !scanCode;
            injectionDetector.ScanState = !scanState;
            //act
            var sut = GetSut(protectionMode: protectionMode, injectionDetector: injectionDetector);

            //assert
            Assert.That(injectionDetector.ScanState, Is.EqualTo(scanState));
            Assert.That(injectionDetector.ScanCode, Is.EqualTo(scanCode));
        }
コード例 #13
0
        public void SetProtectionMode_SwitchingProtectionMode_UpdatesProtectionModeForAllInnerInstances(
            SafeContainerProtectionMode protectionMode, InstanceProtectionMode instanceProtectionMode)
        {
            // Arrange
            var safeObjectMock    = new Mock <ISafeObject <Dictionary <string, IInstanceProvider> > >();
            var instanceProviders = new[]
            {
                new Mock <IInstanceProvider>(),
                new Mock <IInstanceProvider>(),
                new Mock <IInstanceProvider>()
            };
            var instancesToReturn = instanceProviders
                                    .Select(instance => instance.Object)
                                    .ToDictionary(provider => provider.ToString());

            safeObjectMock
            .Setup(m => m.Object)
            .Returns(instancesToReturn);
            safeObjectMock.Setup(m => m.CanAlert)
            .Returns(true);
            var safeObjectFactory = new Mock <ISafeObjectFactory>();

            safeObjectFactory.Setup(
                s => s.Get <Dictionary <string, IInstanceProvider> >(It.IsAny <IInitialSafeObjectSettings>()))
            .Returns <IInitialSafeObjectSettings>(settings => safeObjectMock.Object);
            foreach (var provider in instanceProviders)
            {
                provider.Invocations.Clear();
            }
            var otherProtectionMode = protectionMode == SafeContainerProtectionMode.FullProtection
                ? SafeContainerProtectionMode.NonProtection
                : SafeContainerProtectionMode.FullProtection;

            // Act
            var sut = GetSut(protectionMode: otherProtectionMode, safeObjectFactory: safeObjectFactory.Object);

            sut.SetProtectionMode(protectionMode);

            // Assert
            foreach (var provider in instanceProviders)
            {
                provider.Verify(
                    p => p.SetProtectionMode(
                        It.Is <InstanceProtectionMode>(mode => mode.Equals(instanceProtectionMode))),
                    Times.Once);
            }
        }
コード例 #14
0
        public void SetProtectionMode_SwitchingProtectionMode_UpdatesProtectionModeForAllInnerInstances(
            SafeContainerProtectionMode protectionMode, InstanceProtectionMode instanceProtectionMode)
        {
            //arrange
            var safeObjectMock    = new Mock <ISafeObject <Dictionary <string, IInstanceProvider> > >();
            var instanceProvider1 = new Mock <IInstanceProvider>();
            var instanceProvider2 = new Mock <IInstanceProvider>();
            var instanceProvider3 = new Mock <IInstanceProvider>();

            safeObjectMock.Setup(m => m.Object)
            .Returns(new Dictionary <string, IInstanceProvider>()
            {
                { "a", instanceProvider1.Object },
                { "b", instanceProvider2.Object },
                { "c", instanceProvider3.Object }
            });
            safeObjectMock.Setup(m => m.CanAlert)
            .Returns(true);
            var safeObjectFactory = new Mock <ISafeObjectFactory>();

            safeObjectFactory.Setup(
                s => s.Get <Dictionary <string, IInstanceProvider> >(It.IsAny <IInitialSafeObjectSettings>()))
            .Returns <IInitialSafeObjectSettings>((settings) => safeObjectMock.Object);
            instanceProvider1.ResetCalls();
            instanceProvider2.ResetCalls();
            instanceProvider3.ResetCalls();
            var otherProtectionMode = protectionMode == SafeContainerProtectionMode.FullProtection
    ? SafeContainerProtectionMode.NonProtection
    : SafeContainerProtectionMode.FullProtection;
            //act
            var sut = GetSut(protectionMode: otherProtectionMode, safeObjectFactory: safeObjectFactory.Object);

            sut.SetProtectionMode(protectionMode);
            //assert
            instanceProvider1.Verify(
                p => p.SetProtectionMode(It.Is <InstanceProtectionMode>(mode => mode.Equals(instanceProtectionMode))),
                Times.Once);
            instanceProvider2.Verify(
                p => p.SetProtectionMode(It.Is <InstanceProtectionMode>(mode => mode.Equals(instanceProtectionMode))),
                Times.Once);
            instanceProvider3.Verify(
                p => p.SetProtectionMode(It.Is <InstanceProtectionMode>(mode => mode.Equals(instanceProtectionMode))),
                Times.Once);
        }
コード例 #15
0
        public void SetProtectionMode_From_NonStateProtection_To_StateProtection_NotifiesInnerInjectionDetector(
            InstanceProtectionMode from, InstanceProtectionMode to)
        {
            //arrange
            var detectorMock = new Mock <IInjectionDetector>();

            detectorMock.SetupProperty(d => d.ScanState);
            var sut = GetSut(protectionMode: from,
                             injectionDetector: detectorMock.Object);

            sut.Provide(); //first call to the state
            detectorMock.Invocations.Clear();
            //act
            sut.SetProtectionMode(to);
            var temp = sut.Provide(); //will trigger lazy logic

            //assert
            detectorMock.Verify(d => d.NotifyChanges(It.IsAny <object>()), Times.Once);
        }
コード例 #16
0
        public void Register_GetsInstanceProvider_With_CurrentProtectionMode(SafeContainerProtectionMode protectionMode,
                                                                             InstanceProtectionMode expected)
        {
            //arrange
            var factoryStub = new Mock <IInstanceProviderFactory>();

            factoryStub.Setup(f => f.Get <InstanceTestClass>(
                                  It.IsAny <LifeTime>(),
                                  It.IsAny <InstanceProtectionMode>(),
                                  It.IsAny <InjectionAlertChannel>())).
            Returns(Mock.Of <IInstanceProvider>());
            var sut = GetSut(providerFactory: factoryStub.Object, protectionMode: protectionMode);

            //act
            sut.Register <InstanceTestClass>();
            //assert
            factoryStub.Verify(f => f.Get <InstanceTestClass>(
                                   It.IsAny <LifeTime>(),
                                   It.Is <InstanceProtectionMode>(a => a.Equals(expected)),
                                   It.IsAny <InjectionAlertChannel>()), Times.Once);
        }
コード例 #17
0
 protected SafeInstanceProviderBase(LifeTime lifeTime, InstanceProtectionMode initialProtectionMode)
     : this(lifeTime, new InjectionDetector(), initialProtectionMode, Defaults.AlertChannel)
 {
 }
コード例 #18
0
 /// <summary>
 ///     Internal constructor with all dependencies.
 /// </summary>
 internal SingletonInstanceProvider(IInjectionDetector injectionDetector, InstanceProtectionMode protectionMode,
                                    InjectionAlertChannel alertChannel)
     : base(LifeTime.Singleton, injectionDetector, protectionMode, alertChannel)
 {
 }
コード例 #19
0
 public SingletonInstanceProvider(InstanceProtectionMode initialProtectionMode) : base(LifeTime.Singleton,
                                                                                       initialProtectionMode)
 {
 }
コード例 #20
0
 public TransientInstanceProvider(InstanceProtectionMode protectionMode)
     : base(LifeTime.Transient, protectionMode)
 {
 }
コード例 #21
0
 public TestInstanceProvider(LifeTime lifeTime, IInjectionDetector injectionDetector,
                             InstanceProtectionMode protectionMode, InjectionAlertChannel alertChannel) : base(
         lifeTime, injectionDetector, protectionMode, alertChannel)
 {
 }
コード例 #22
0
 /// <summary>
 ///     Internal constructor with all dependencies.
 /// </summary>
 internal TransientInstanceProvider(IInjectionDetector injectionDetector, InstanceProtectionMode protectionMode,
                                    InjectionAlertChannel alertChannel)
     : base(LifeTime.Transient, injectionDetector, protectionMode, alertChannel)
 {
 }