Implements a data mapper for an unknown type. Tries to convert the value given to that type The data mapper conforms to the GOF strategy pattern DataMapper.
Inheritance: Habanero.Base.DataMappers.DataMapper
Ejemplo n.º 1
0
 public void TryParsePropValue_WhenValueNull_ShouldReturnNull()
 {
     //---------------Set up test pack-------------------
     var propMapper = new GeneralDataMapper(typeof(int));
     object returnValue;
     //---------------Execute Test ----------------------
     var result = propMapper.TryParsePropValue(null, out returnValue);
     //---------------Test Result -----------------------
     Assert.IsTrue(result);
     Assert.IsNull(returnValue);
 }
Ejemplo n.º 2
0
 public void TryParsePropValue_ShouldSetReturnValueSame_WhenValueToParseIsAlreadyCorrectType_ForReferenceType()
 {
     //---------------Set up test pack-------------------
     var dataMapper = new GeneralDataMapper(typeof (Image));
     Image valueToParse = new Bitmap(200, 200);
     object returnValue;
     //---------------Execute Test ----------------------
     var result = dataMapper.TryParsePropValue(valueToParse, out returnValue);
     //---------------Test Result -----------------------
     Assert.IsTrue(result);
     Assert.AreSame(valueToParse, returnValue);
 }
Ejemplo n.º 3
0
 public void TryParsePropValue_ShouldSetReturnValueEqual_WhenValueToParseIsAlreadyCorrectType_ForValueType()
 {
     //---------------Set up test pack-------------------
     var dataMapper = new GeneralDataMapper(typeof (int));
     int valueToParse = TestUtil.GetRandomInt();
     object returnValue;
     //---------------Execute Test ----------------------
     var result = dataMapper.TryParsePropValue(valueToParse, out returnValue);
     //---------------Test Result -----------------------
     Assert.IsTrue(result);
     Assert.AreEqual(valueToParse, returnValue);
 }      
Ejemplo n.º 4
0
 /// <summary>
 /// Returns the DataMapper for the <paramref name="targetType"/>.
 /// </summary>
 /// <param name="targetType"></param>
 /// <returns></returns>
 public IDataMapper GetDataMapper(Type targetType)
 {
     try
     {
         return _dataMappers[targetType];
     } catch (KeyNotFoundException)
     {
         var generalDataMapper = new GeneralDataMapper(targetType);
         _dataMappers.Add(targetType, generalDataMapper);
         return generalDataMapper;
     }
 }
Ejemplo n.º 5
0
        public void TryParsePropValue_ShouldParseValue_ForCustomTypeWithATypeConverter()
        {
            //---------------Set up test pack-------------------
            const string emailAddToParse = "*****@*****.**";

            var propMapper = new GeneralDataMapper(typeof(EmailAddressWithTypeConverter));
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            object parsedValue;
            var tryParsePropValue = propMapper.TryParsePropValue(emailAddToParse, out parsedValue);
            //---------------Test Result -----------------------
            Assert.IsTrue(tryParsePropValue);
            Assert.IsInstanceOf<EmailAddressWithTypeConverter>(parsedValue);
            Assert.AreEqual(emailAddToParse, ((EmailAddressWithTypeConverter)parsedValue).EmailAddress);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Returns the DataMapper for the <paramref name="targetType"/>.
 /// </summary>
 /// <param name="targetType"></param>
 /// <returns></returns>
 public IDataMapper GetDataMapper(Type targetType)
 {
     lock (this)
     {
         try
         {
             return(_dataMappers[targetType]);
         }
         catch (KeyNotFoundException)
         {
             var generalDataMapper = new GeneralDataMapper(targetType);
             _dataMappers.Add(targetType, generalDataMapper);
             return(generalDataMapper);
         }
     }
 }
Ejemplo n.º 7
0
 public void TryParsePropValue_ShouldConvert_WhenTypeCanBeConverted()
 {
     //---------------Set up test pack-------------------
     var propMapper = new GeneralDataMapper(typeof(int));
     int expectedValue = TestUtil.GetRandomInt();
     object parsedValue;
     //---------------Execute Test ----------------------
     var result = propMapper.TryParsePropValue(expectedValue.ToString(), out parsedValue);
     //---------------Test Result -----------------------
     Assert.IsTrue(result);
     Assert.AreEqual(expectedValue, parsedValue);
 }
Ejemplo n.º 8
0
        public void TryParsePropValue_ShouldThrowInvalidCastException_WhenTypeCannotBeConverted()
        {
            //---------------Set up test pack-------------------
            var propMapper = new GeneralDataMapper(typeof(ConsoleColor));
            object parsedValue;
            //---------------Execute Test ----------------------
            try
            {
                propMapper.TryParsePropValue(ConsoleKey.Z, out parsedValue);
                Assert.Fail("should fail when there's no way to convert");
                //---------------Test Result -----------------------
            }
            catch (InvalidCastException ex)
            {
                StringAssert.Contains("Invalid cast from", ex.Message);
            }

        }
Ejemplo n.º 9
0
 public void TryParsePropValue_ForEnumType_ShouldParseStringToEnumType()
 {
     //---------------Set up test pack-------------------
     var propMapper = new GeneralDataMapper(typeof(ConsoleColor));
     var expectedValue = ConsoleColor.Blue;
     string valueAsString = Enum.GetName(typeof (ConsoleColor), expectedValue);
     object parsedValue;
     //---------------Execute Test ----------------------
     var result = propMapper.TryParsePropValue(valueAsString, out parsedValue);
     //---------------Test Result -----------------------
     Assert.IsTrue(result);
     Assert.AreEqual(expectedValue, parsedValue);
 }
Ejemplo n.º 10
0
        public void Test_BOPropGeneralDataMapper_TryParseCustomProperty_StringValue()
        {
            //---------------Set up test pack-------------------
            string test = "test";
            GeneralDataMapper generalDataMapper = new GeneralDataMapper(typeof(LongText));
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            object returnValue;
            generalDataMapper.TryParsePropValue(test, out returnValue);
            //---------------Test Result -----------------------
            Assert.IsNotNull(returnValue);
            Assert.IsInstanceOf(typeof(LongText), returnValue);
            LongText longText = (LongText) returnValue;
            Assert.AreSame(test, longText.Value);
        }
Ejemplo n.º 11
0
        public void Test_BOPropGeneralDataMapper_TryParseCustomProperty_InheritedLongText()
        {
            //---------------Set up test pack-------------------
            LongText longText = new ExtendedLongText("test");
            GeneralDataMapper generalDataMapper = new GeneralDataMapper(typeof(LongText));
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            object returnValue;
            generalDataMapper.TryParsePropValue(longText, out returnValue);
            //---------------Test Result -----------------------
            Assert.IsNotNull(returnValue);
            Assert.AreSame(longText, returnValue);
        }
Ejemplo n.º 12
0
        public void Test_BOPropGeneralDataMapper_TryParseCustomProperty_StringValue()
        {
            //---------------Set up test pack-------------------
            const string test = "test";
            var generalDataMapper = new GeneralDataMapper(typeof(SimpleValueObjectStub));
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            object returnValue;
            generalDataMapper.TryParsePropValue(test, out returnValue);
            //---------------Test Result -----------------------
            Assert.IsNotNull(returnValue);
            Assert.IsInstanceOf(typeof(SimpleValueObjectStub), returnValue);
            var valueObject = (SimpleValueObjectStub) returnValue;
            Assert.AreSame(test, valueObject.ToString());
        }
Ejemplo n.º 13
0
        public void Test_BOPropGeneralDataMapper_TryParseCustomProperty_InheritedCustomProperty()
        {
            //---------------Set up test pack-------------------
            var valueObject = new SimpleValueObjectStub("test");
            var generalDataMapper = new GeneralDataMapper(typeof(CustomProperty));
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            object returnValue;
            generalDataMapper.TryParsePropValue(valueObject, out returnValue);
            //---------------Test Result -----------------------
            Assert.IsNotNull(returnValue);
            Assert.AreSame(valueObject, returnValue);
        }