Index: Ringtoets/Common/src/Ringtoets.Common.Forms/Observers/FailureMechanismResultObserver.cs =================================================================== diff -u --- Ringtoets/Common/src/Ringtoets.Common.Forms/Observers/FailureMechanismResultObserver.cs (revision 0) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/Observers/FailureMechanismResultObserver.cs (revision 7c1d0fc0849778e055f4f4120dd2b6fcdfd673b3) @@ -0,0 +1,88 @@ +// Copyright (C) Stichting Deltares 2017. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using Core.Common.Base; +using Ringtoets.Common.Data.FailureMechanism; + +namespace Ringtoets.Common.Forms.Observers +{ + /// + /// Class that observes all objects in an related to + /// its section results. + /// + /// The type of the failure mechanism to observe. + /// The type of the section results in the failure mechanism. + public class FailureMechanismResultObserver : Observable, IDisposable + where TFailureMechanism : IFailureMechanism, IHasSectionResults + where TSectionResult : FailureMechanismSectionResult + { + private readonly Observer failureMechanismObserver; + private readonly Observer failureMechanismSectionResultObserver; + private readonly RecursiveObserver, TSectionResult> failureMechanismSectionResultsObserver; + + /// + /// Creates a new instance of . + /// + /// The to observe. + /// Thrown when is null. + public FailureMechanismResultObserver(TFailureMechanism failureMechanism) + { + if (failureMechanism == null) + { + throw new ArgumentNullException(nameof(failureMechanism)); + } + + failureMechanismObserver = new Observer(NotifyObservers) + { + Observable = failureMechanism + }; + + failureMechanismSectionResultObserver = new Observer(NotifyObservers) + { + Observable = failureMechanism.SectionResults + }; + + failureMechanismSectionResultsObserver = new RecursiveObserver, TSectionResult>( + NotifyObservers, + sr => sr) + { + Observable = failureMechanism.SectionResults + }; + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + failureMechanismObserver.Dispose(); + failureMechanismSectionResultObserver.Dispose(); + failureMechanismSectionResultsObserver.Dispose(); + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.Forms/Ringtoets.Common.Forms.csproj =================================================================== diff -u -r07bb209b7475f9a60cea565c982c47e0562f6daf -r7c1d0fc0849778e055f4f4120dd2b6fcdfd673b3 --- Ringtoets/Common/src/Ringtoets.Common.Forms/Ringtoets.Common.Forms.csproj (.../Ringtoets.Common.Forms.csproj) (revision 07bb209b7475f9a60cea565c982c47e0562f6daf) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/Ringtoets.Common.Forms.csproj (.../Ringtoets.Common.Forms.csproj) (revision 7c1d0fc0849778e055f4f4120dd2b6fcdfd673b3) @@ -57,6 +57,7 @@ + Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Observers/FailureMechanismResultObserverTest.cs =================================================================== diff -u --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Observers/FailureMechanismResultObserverTest.cs (revision 0) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Observers/FailureMechanismResultObserverTest.cs (revision 7c1d0fc0849778e055f4f4120dd2b6fcdfd673b3) @@ -0,0 +1,129 @@ +// Copyright (C) Stichting Deltares 2017. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Linq; +using Core.Common.Base; +using NUnit.Framework; +using Rhino.Mocks; +using Ringtoets.Common.Data.FailureMechanism; +using Ringtoets.Common.Data.TestUtil; +using Ringtoets.Common.Forms.Observers; + +namespace Ringtoets.Common.Forms.Test.Observers +{ + [TestFixture] + public class FailureMechanismResultObserverTest + { + [Test] + public void Constructor_FailureMechanismNull_ThrowsArgumentNullException() + { + // Call + TestDelegate test = () => new FailureMechanismResultObserver(null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("failureMechanism", exception.ParamName); + } + + [Test] + public void Constructor_WithFailureMechanism_ExpectedProperties() + { + // Call + using (var resultObserver = new FailureMechanismResultObserver(new TestFailureMechanism())) + { + // Assert + Assert.IsInstanceOf(resultObserver); + Assert.IsInstanceOf(resultObserver); + } + } + + [Test] + public void GivenFailureMechanismResultObserverWithAttachedObserver_WhenFailureMechanismNotifiesObservers_ThenFailureMechanismResultObserverNotified() + { + // Given + var failureMechanism = new TestFailureMechanism(); + + using (var resultObserver = new FailureMechanismResultObserver(failureMechanism)) + { + var mocks = new MockRepository(); + var observer = mocks.StrictMock(); + observer.Expect(o => o.UpdateObserver()); + mocks.ReplayAll(); + + resultObserver.Attach(observer); + + // When + failureMechanism.NotifyObservers(); + + // Then + mocks.VerifyAll(); + } + } + + [Test] + public void GivenFailureMechanismResultObserverWithAttachedObserver_WhenFailureMechanismSectionResultCollectionNotifiesObservers_ThenFailureMechanismResultObserverNotified() + { + // Given + var failureMechanism = new TestFailureMechanism(); + + using (var resultObserver = new FailureMechanismResultObserver(failureMechanism)) + { + var mocks = new MockRepository(); + var observer = mocks.StrictMock(); + observer.Expect(o => o.UpdateObserver()); + mocks.ReplayAll(); + + resultObserver.Attach(observer); + + // When + failureMechanism.SectionResults.NotifyObservers(); + + // Then + mocks.VerifyAll(); + } + } + + [Test] + public void GivenFailureMechanismResultObserverWithAttachedObserver_WhenFailureMechanismSectionResultNotifiesObservers_ThenFailureMechanismResultObserverNotified() + { + // Given + var failureMechanism = new TestFailureMechanism(); + failureMechanism.AddSection(FailureMechanismSectionTestFactory.CreateFailureMechanismSection()); + + using (var resultObserver = new FailureMechanismResultObserver(failureMechanism)) + { + var mocks = new MockRepository(); + var observer = mocks.StrictMock(); + observer.Expect(o => o.UpdateObserver()); + mocks.ReplayAll(); + + resultObserver.Attach(observer); + + // When + failureMechanism.SectionResults.Single().NotifyObservers(); + + // Then + mocks.VerifyAll(); + } + } + } +} \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Ringtoets.Common.Forms.Test.csproj =================================================================== diff -u -r84223425c4ab8eec2440f512b5b06bea8937f8f9 -r7c1d0fc0849778e055f4f4120dd2b6fcdfd673b3 --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Ringtoets.Common.Forms.Test.csproj (.../Ringtoets.Common.Forms.Test.csproj) (revision 84223425c4ab8eec2440f512b5b06bea8937f8f9) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/Ringtoets.Common.Forms.Test.csproj (.../Ringtoets.Common.Forms.Test.csproj) (revision 7c1d0fc0849778e055f4f4120dd2b6fcdfd673b3) @@ -64,6 +64,7 @@ +