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); }
internal SafeContainer( ITypeIdGenerator typeIdGenerator, IInstanceProviderFactory instanceFactory, IInstanceValidator instanceValidator, ISafeObjectFactory safeObjectFactory, SafeContainerProtectionMode protectionMode = Defaults.ContainerProtectionMode, InjectionAlertChannel alertChannel = Defaults.AlertChannel) : base(protectionMode) { if (typeIdGenerator == null) { throw new ArgumentNullException(nameof(typeIdGenerator)); } if (instanceFactory == null) { throw new ArgumentNullException(nameof(instanceFactory)); } if (instanceValidator == null) { throw new ArgumentNullException(nameof(instanceValidator)); } if (safeObjectFactory == null) { throw new ArgumentNullException(nameof(safeObjectFactory)); } _typeIdGenerator = typeIdGenerator; _instanceFactory = instanceFactory; _instanceValidator = instanceValidator; _safeObjectFactory = safeObjectFactory; _alertChannel = alertChannel; ChangeProtectionMode(new ProtectionChangeContext <SafeContainerProtectionMode>(protectionMode)); SetAlertChannelInternal(alertChannel); }
/// <summary> /// Initializes a new instance of the <see cref="InitialSafeObjectSettings"/> class. /// </summary> /// <param name="initialValue">The initial value.</param> /// <param name="isReadOnly">if set to <c>false</c> the instance will be modifiable initially.</param> /// <param name="protectionMode">The initial protection mode.</param> /// <param name="alertChannel">Gets/sets the alert channel for the inner <see cref="IInjectionDetector"/></param> public InitialSafeObjectSettings(object initialValue, bool isReadOnly, SafeObjectProtectionMode protectionMode, InjectionAlertChannel alertChannel) { InitialValue = initialValue; IsReadOnly = isReadOnly; ProtectionMode = protectionMode; AlertChannel = alertChannel; }
/// <summary> /// Internal constructor with all dependencies. /// </summary> internal InjectionDetector( IInjectionAlerter alerter, ITypeIdGenerator typeIdGenerator, IStamper <object> stateStamper, IStamper <Type> codeStamper, bool scanCode, bool scanState, InjectionAlertChannel alertChannel) { if (alerter == null) { throw new ArgumentNullException(nameof(alerter)); } if (stateStamper == null) { throw new ArgumentNullException(nameof(stateStamper)); } if (codeStamper == null) { throw new ArgumentNullException(nameof(codeStamper)); } _alerter = alerter; _typeIdGenerator = typeIdGenerator; _stateStamper = stateStamper; _codeStamper = codeStamper; _alertChannel = alertChannel; ScanCode = scanCode; ScanState = scanState; }
/// <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)); }
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)); }
private static ISafeByteFactory GetSut(SafeObjectProtectionMode innerDictionaryProtectionMode = MemoryCachedSafeByteFactory.DefaultInnerDictionaryProtection, InjectionAlertChannel alertChannel = Defaults.AlertChannel) { return(new MemoryCachedSafeByteFactory( Stubs.Get <IByteIdGenerator>(), Stubs.GetFactory <ISafeByte>(), Stubs.Get <ISafeObjectFactory>(), innerDictionaryProtectionMode )); }
public void Constructor_Sets_AlertChannel(InjectionAlertChannel alertChannel) { //arrange var expected = alertChannel; //act var sut = GetSut(alertChannel: expected); var actual = sut.AlertChannel; //assert Assert.That(actual, Is.EqualTo(expected)); }
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)); }
/// <summary> /// Initializes a new instance of the <see cref="InjectionDetector" /> class. /// </summary> /// <param name="scanCode">if set to <c>true</c> it'll scan and validate the code.</param> /// <param name="scanState">if set to <c>true</c> it'll scan and validate the state.</param> /// <param name="alertChannel">The alert channel.</param> /// <seealso cref="ScanCode" /> /// <seealso cref="ScanState" /> public InjectionDetector( bool scanCode = true, bool scanState = true, InjectionAlertChannel alertChannel = Defaults.AlertChannel) : this ( InjectionAlerter.StaticInstance, TypeIdGenerator.StaticInstance, StateStamper.StaticInstance, IlCodeStamper.StaticInstance, scanCode, scanState, alertChannel) { }
public void Constructor_Sets_InnerInjectionDetectorsAlertChannel(InjectionAlertChannel alertChannel) { // Arrange var expected = alertChannel; var sut = GetSut(channel: expected); // Act var actual = sut.AlertChannel; // Assert Assert.That(actual, Is.EqualTo(expected)); }
public void Alert_ChannelIsDebugWrite_InvokesRaiseEventAlerter(IInjectionMessage info) { //arrange const InjectionAlertChannel channel = InjectionAlertChannel.DebugWrite; var alerterMock = new Mock <IAlerter>(); var sut = GetSut(debugWriteAlerter: alerterMock.Object); //act sut.Alert(info, channel); //assert alerterMock.Verify(a => a.Alert(It.Is <IInjectionMessage>((value) => info.Equals(value)))); }
public void GetGeneric_SetsInitialInjectionAlertChannel(InjectionAlertChannel expected) { //arrange var sut = GetSut(); Func <InstanceProviderFactoryTests> func = () => new InstanceProviderFactoryTests(); //act var instance = sut.Get(func, InstanceProtectionMode.NoProtection, expected); var actual = instance.AlertChannel; //assert Assert.That(actual, Is.EqualTo(expected)); }
public void AlertChannel_AfterInitialized_CanSet() { // Arrange const InjectionAlertChannel expected = InjectionAlertChannel.DebugFail; var sut = SafeOrbitCore.Current; // Act sut.AlertChannel = expected; // Assert Assert.AreEqual(expected, sut.AlertChannel); }
public void Get_SetsInitialInjectionAlertChannel(InjectionAlertChannel expected) { //arrange var sut = GetSut(); //act var instance = sut.Get <InstanceProviderFactoryTests>(LifeTime.Transient, InstanceProtectionMode.NoProtection, expected); var actual = instance.AlertChannel; //assert Assert.That(actual, Is.EqualTo(expected)); }
public void AlertChannel_Gets_InnerInjectionDetectorChannel(InjectionAlertChannel alertChannel) { //arrange var expected = alertChannel; var injectionDetector = new Mock <IInjectionDetector>(); var sut = GetSut(injectionDetector: injectionDetector.Object); //act var temp = sut.AlertChannel; //assert injectionDetector.VerifyGet( detector => detector.AlertChannel); }
public void Alert_ChannelIsThrowException_InvokesRaiseEventAlerter(IInjectionMessage info) { // Arrange const InjectionAlertChannel channel = InjectionAlertChannel.ThrowException; var alerterMock = new Mock <IAlerter>(); var sut = GetSut(throwExceptionAlerter: alerterMock.Object); // Act sut.Alert(info, channel); // Assert alerterMock.Verify(a => a.Alert(It.Is <IInjectionMessage>(value => info.Equals(value)))); }
private void SetAlertChannelInternal(InjectionAlertChannel value) { _alertChannel = value; _typeInstancesSafe.AlertChannel = value; var dict = _typeInstancesSafe.Object; if (dict?.Values.Any(v => !v.AlertChannel.Equals(value)) == true) { _typeInstancesSafe.ApplyChanges(dic => dic? .Values .Where(c => !c.AlertChannel.Equals(value)) .ForEach(instance => instance.AlertChannel = value)); } }
public SafeObject <TestObject> GetSut( TestObject initialObject = null, bool isReadOnly = false, SafeObjectProtectionMode protectionMode = Defaults.ObjectProtectionMode, IInjectionDetector injectionDetector = null, InjectionAlertChannel channel = Defaults.AlertChannel) { return (new SafeObject <TestObject>( new InitialSafeObjectSettings( isReadOnly: isReadOnly, initialValue: initialObject, protectionMode: protectionMode, alertChannel: channel) , injectionDetector ?? Stubs.Get <IInjectionDetector>())); }
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); }
public void AlertChannel_Sets_InnerInjectionDetectorChannel(InjectionAlertChannel alertChannel) { // Arrange var expected = alertChannel; var injectionDetector = new Mock <IInjectionDetector>(); var sut = GetSut(injectionDetector: injectionDetector.Object); // Act sut.AlertChannel = expected; // Assert injectionDetector.VerifySet( detector => detector.AlertChannel = expected ); }
public void AlertChannel_Gets_InnerInjectionDetectorChannel(InjectionAlertChannel alertChannel) { //arrange var expected = alertChannel; var injectionDetector = new Mock <IInjectionDetector>(); injectionDetector.SetupGet(detector => detector.AlertChannel) .Returns(expected); var sut = GetSut(injectionDetector: injectionDetector.Object); //act var actual = sut.AlertChannel; //assert Assert.AreEqual(expected, actual); }
private ISafeObject <Dictionary <string, IInstanceProvider> > CreateInnerDictionary( SafeContainerProtectionMode protectionMode, InjectionAlertChannel alertChannel, Dictionary <string, IInstanceProvider> initialObject = null) { var innerDictionaryProtection = GetInnerDictionaryObjectProtectionMode(protectionMode); var settings = new InitialSafeObjectSettings ( protectionMode: innerDictionaryProtection, isReadOnly: false, initialValue: initialObject, alertChannel: alertChannel ); var result = _safeObjectFactory.Get <Dictionary <string, IInstanceProvider> >(settings); return(result); }
private static ISafeContainer GetSut( SafeContainerProtectionMode protectionMode = SafeContainerProtectionMode.FullProtection , IInstanceProviderFactory providerFactory = null, IInstanceValidator instanceValidator = null, ISafeObjectFactory safeObjectFactory = null, InjectionAlertChannel alertChannel = Defaults.AlertChannel ) { return(new SafeContainer( Stubs.Get <ITypeIdGenerator>(), providerFactory ?? Stubs.Get <IInstanceProviderFactory>(), instanceValidator ?? Stubs.Get <IInstanceValidator>(), safeObjectFactory ?? Stubs.Get <ISafeObjectFactory>(), protectionMode, alertChannel )); }
public void AlertChannel_VerifiedInstance_ChangeItself() { // Arrange const InjectionAlertChannel expected = InjectionAlertChannel.DebugFail; var sut = GetSut(); sut.Register <IInstanceTestClass, InstanceTestClass>(LifeTime.Singleton); sut.Verify(); // Act sut.AlertChannel = expected; // Assert var actual = sut.AlertChannel; Assert.AreEqual(expected, actual); }
public void Constructor_AlertChannel_SetsToInnerSafeObject(InjectionAlertChannel alertChannel) { //arrange var safeObjectMock = new Mock <ISafeObject <Dictionary <string, IInstanceProvider> > >(); safeObjectMock.Setup(m => m.Object).Returns(new Dictionary <string, IInstanceProvider>()); var safeObjectFactory = new Mock <ISafeObjectFactory>(); safeObjectFactory.Setup( s => s.Get <Dictionary <string, IInstanceProvider> >(It.IsAny <IInitialSafeObjectSettings>())) .Returns <IInitialSafeObjectSettings>((settings) => safeObjectMock.Object); //act var sut = GetSut(alertChannel: alertChannel, safeObjectFactory: safeObjectFactory.Object); //assert safeObjectMock.VerifySet(o => o.AlertChannel = alertChannel); }
public void InjectionAlertChannel_WhenAlerting_UsedForInternalAlerter(InjectionAlertChannel expected) { //arrange var alerterMock = new Mock <IInjectionAlerter>(); var sut = GetSut( protectState: true, protectCode: true, isStateValid: false, isCodeValid: false, alerter: alerterMock.Object); var testObject = new object(); sut.AlertChannel = expected; //act sut.NotifyChanges(testObject); sut.AlertUnnotifiedChanges(testObject); //assert alerterMock.Verify((alerter) => alerter.Alert( It.IsAny <IInjectionMessage>(), It.Is <InjectionAlertChannel>((m) => m.Equals(expected)))); }
public IAlerter Get(InjectionAlertChannel channel) { switch (channel) { case InjectionAlertChannel.RaiseEvent: return(new RaiseEventAlerter(SafeOrbitCore.Current)); case InjectionAlertChannel.ThrowException: return(new ThrowExceptionAlerter()); case InjectionAlertChannel.DebugFail: return(new DebugFailAlerter()); case InjectionAlertChannel.DebugWrite: return(new DebugWriteAlerter()); default: throw new ArgumentOutOfRangeException(nameof(channel), channel, null); } }
public void AlertChannel_Sets_InnerSafeObjectAlertChannel(InjectionAlertChannel alertChannel) { //arrange var expected = alertChannel; var safeObjectMock = new Mock <ISafeObject <Dictionary <string, IInstanceProvider> > >(); safeObjectMock.Setup(m => m.Object).Returns(new Dictionary <string, IInstanceProvider>()); var safeObjectFactory = new Mock <ISafeObjectFactory>(); safeObjectFactory.Setup( s => s.Get <Dictionary <string, IInstanceProvider> >(It.IsAny <IInitialSafeObjectSettings>())) .Returns <IInitialSafeObjectSettings>((settings) => safeObjectMock.Object); var sut = GetSut(safeObjectFactory: safeObjectFactory.Object); //act sut.AlertChannel = alertChannel; //assert safeObjectMock.VerifySet( detector => detector.AlertChannel = expected ); }
public void Register_GetsInstanceProvider_With_CurrentAlertChannel(InjectionAlertChannel alertChannel) { //arrange var expected = alertChannel; 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, alertChannel: expected); //act sut.Register <InstanceTestClass>(); //assert factoryStub.Verify(f => f.Get <InstanceTestClass>( It.IsAny <LifeTime>(), It.IsAny <InstanceProtectionMode>(), It.Is <InjectionAlertChannel>(a => a.Equals(expected))), Times.Once); }