Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 49.1K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PropertyHelper.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Property helper class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Reflection;
using Catel.Properties;
using log4net;

namespace Catel.Reflection
{
    /// <summary>
    /// Property helper class.
    /// </summary>
    public static class PropertyHelper
    {
        #region Variables
        /// <summary>
        /// The <see cref="ILog">log</see> object.
        /// </summary>
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        #endregion

        /// <summary>
        /// Gets the property value of a specific object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="property">The property.</param>
        /// <returns>
        /// The property value or <c>null</c> if no property can be found.
        /// </returns>
        /// <exception cref="ArgumentNullException">when <paramref name="obj"/> is <c>null</c>.</exception>
        /// <exception cref="PropertyNotFoundException">when <paramref name="obj"/> is not found or not publicly available.</exception>
        /// <exception cref="CannotGetPropertyValueException">when the property value cannot be read.</exception>
        public static object GetPropertyValue(object obj, string property)
		{
			if (obj == null)
			{
				throw new ArgumentNullException("obj");
			}

			Type type = obj.GetType();
			PropertyInfo propertyInfo = type.GetProperty(property);
			if (propertyInfo == null)
			{
                Log.Error(TraceMessages.PropertyNotFoundOnObjectProbablyInvalidMapping, property, type);
			    throw new PropertyNotFoundException(property);
			}

			// Return property value if available
			if (!propertyInfo.CanRead)
			{
                Log.Error(TraceMessages.CannotReadProperty, type, property);
                throw new CannotGetPropertyValueException(property);
			}

            try
            {
                return propertyInfo.GetValue(obj, null);
            }
            catch (MethodAccessException)
            {
                Log.Error(TraceMessages.CannotReadProperty, type, property);
                throw new CannotGetPropertyValueException(property);
            }
		}

        /// <summary>
        /// Gets the property value of a specific object.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="property">The property.</param>
        /// <returns>
        /// The property value or <c>null</c> if no property can be found.
        /// </returns>
        /// <exception cref="ArgumentNullException">when <paramref name="obj"/> is <c>null</c>.</exception>
        /// <exception cref="PropertyNotFoundException">when <paramref name="obj"/> is not found or not publicly available.</exception>
        /// <exception cref="CannotGetPropertyValueException">when the property value cannot be read.</exception>
        public static TValue GetPropertyValue<TValue>(object obj, string property)
        {
            return (TValue)GetPropertyValue(obj, property);
        }

        /// <summary>
        /// Sets the property value of a specific object.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="property">The property.</param>
        /// <param name="value">The value.</param>
        /// <exception cref="ArgumentNullException">when <paramref name="obj"/> is <c>null</c>.</exception>
        /// <exception cref="PropertyNotFoundException">when <paramref name="obj"/> is not found or not publicly available.</exception>
        /// <exception cref="CannotSetPropertyValueException">when the property value cannot be written.</exception>
        public static void SetPropertyValue(object obj, string property, object value)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            Type type = obj.GetType();
            PropertyInfo propertyInfo = type.GetProperty(property);
            if (propertyInfo == null)
            {
                Log.Error(TraceMessages.PropertyNotFoundOnObjectProbablyInvalidMapping, property, type);
                throw new PropertyNotFoundException(property);
            }

            if (!propertyInfo.CanWrite)
            {
                Log.Error(TraceMessages.CannotWriteProperty, type, property);
                throw new CannotSetPropertyValueException(property);
            }

            try
            {
                propertyInfo.SetValue(obj, value, null);   
            }
            catch (MethodAccessException)
            {
                Log.Error(TraceMessages.CannotWriteProperty, type, property);
                throw new CannotSetPropertyValueException(property);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions