// Copyright (C) Stichting Deltares 2017. All rights reserved. // // This file is part of the D-Soil Model application. // // The D-Soil Model application 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.Reflection; using Deltares.Geotechnics; using Deltares.Probabilistic; using Deltares.Standard; using Deltares.Standard.EventPublisher; using Deltares.Standard.Reflection; using NUnit.Framework; namespace Deltares.DSoilModel.Data.Tests { [TestFixture] public class DSoilModelTransformerTests { private bool oldIsDataEventPublishStopped; [TestFixtureSetUp] public void FixtureSetup() { oldIsDataEventPublishStopped = DataEventPublisher.IsDataEventPublishStopped; DataEventPublisher.IsDataEventPublishStopped = true; TransformerManager.Transformer = new DSoilModelTransformer(); } [TestFixtureTearDown] public void FixtureTearDown() { DataEventPublisher.IsDataEventPublishStopped = oldIsDataEventPublishStopped; } private Stochast FindAutoStochastProperty(object obj, string property) { var instance = TransformerManager.Transformer as DSoilModelTransformer; return (Stochast)instance.GetType().GetMethod("FindAutoStochastProperty", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(instance, new[] {obj, property}); } [TestCase(typeof(Soil))] [TestCase(typeof(PreConsolidationStress))] public void TestFindAutoStochastProperty(Type objType) { var obj = Activator.CreateInstance(objType); var stochastProperties = PropertyInfoSupport.GetPropertiesOfType(obj.GetType(), typeof (Stochast)); foreach (BindPropertyInfo propertyInfo in stochastProperties) { var stochast = (Stochast) propertyInfo.GetValue(obj, null); Assert.AreSame(stochast, FindAutoStochastProperty(obj, stochast.AssociatedPropertyName)); } } [Test] public void FindAutoStochastPropertyTest_ReturnsNullForUnknownProperty() { Assert.IsNull(FindAutoStochastProperty(new SosSoilLayer1D(), "Property")); Assert.IsNull(FindAutoStochastProperty(new SosSoilLayer1D(), null)); } [Test] [TestCase(typeof(Soil), "Cohesion")] [TestCase(typeof(Soil), "BeddingAngle")] [TestCase(typeof(PreConsolidationStress), "StressValue")] public void TestStochasticPropertiesUncoupled(Type objType, string propertyName) { var obj = Activator.CreateInstance(objType); var deterministicProperty = obj.GetType().GetProperty(propertyName); var stochasticProperty = FindAutoStochastProperty(obj, propertyName); // reusing method that we already test var changedValue = 123.456; deterministicProperty.SetValue(obj, changedValue, null); Assert.AreEqual(changedValue, deterministicProperty.GetValue(obj, null)); // deterministic property indeed changed Assert.AreNotEqual(changedValue, stochasticProperty.Mean); // but stochastic property remained its mean value } [Test] public void GetTransformedValueTest_ReturnsInitialValueIfNotSupported() { var transformer = new DSoilModelTransformer(); Assert.AreEqual(44, transformer.GetTransformedValue(new SosSoilLayer1D(), "Property", 44)); Assert.AreEqual(54, transformer.SetTransformedValue(new SosSoilLayer1D(), "Property", 54)); } } }