Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Catel - Part 4 of n: Unit testing with Catel

, 28 Jan 2011
This article explains how to write unit tests for MVVM using Catel.
Catel-04_01-unittesting.zip
src
Catel.Articles.04 - Unit testing
Models
Properties
Settings.settings
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
ViewModels
Windows
Catel.Articles.04 - Unit testing.Test
Models
Properties
UI
ViewModels
Catel.Articles.Base
Data
Attributes
Properties
Settings.settings
Resources
Images
CatenaLogic.png
Preview.png
Run.png
ShowCode.png
UI
Controls
Helpers
ViewModels
Windows
Catel.Core
Attributes
ClassDiagrams
DataObjectBase.cd
SavableDataObjectBase.cd
Collections
Helpers
ComponentModel
Data
Attributes
Exceptions
Interfaces
Diagnostics
Extensions
Helpers
Exceptions
Helpers
IO
Exceptions
IoC
LLBLGen
Log4net
Appender
Extensions
Helpers
MVVM
Commands
Interfaces
Exceptions
Services
EventArgs
Exceptions
Interfaces
ViewModels
Attributes
Interfaces
Properties
Reflection
Exceptions
Extensions
Helpers
Runtime
Serialization
Attributes
Helpers
Security
Cryptography
Helpers
Catel.Examples.Models
Properties
Catel.Examples.PersonApplication
Properties
Settings.settings
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
ViewModels
Windows
Catel.Examples.Silverlight
Properties
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
Pages
ViewModels
Windows
Catel.Examples.Silverlight.Web
Catel.Examples.Silverlight.Web.csproj.user
ClientBin
Properties
Catel.FxCop
Catel.Silverlight
Diagnostics
log4net
Core
MVVM
Commands
Services
ViewModels
Properties
Catel.Core
Catel.Windows
Reflection
Themes
Generic
Assets
Old
Windows
Controls
Data
Converters
Helpers
Helpers
Catel.Silverlight.Test
Properties
Catel.Silverlight.Test.Web
Catel.Silverlight.Test.Web.csproj.user
ClientBin
Properties
Catel.snk
Catel.Templates.WpfApplication
Properties
Settings.settings
UI
Controls
ViewModels
Windows
Catel.Templates.WpfItemTemplates
Properties
UI
Controls
ViewModels
Windows
Catel.Test
Collections
Convert
Data
Helpers
IO
MVVM
UI
ViewModels
Properties
Reflection
Runtime
Serialization
Security
Cryptography
Test References
Catel.Windows.accessor
Windows
Data
Converters
Catel.vsmdi
Catel.Windows
ClassDiagrams
ViewModelBase.cd
Collections
Extensions
Helpers
MVVM
Commands
Services
Test
UI
ViewModels
Properties
Settings.settings
Resources
Images
Add.png
ClearOutput.png
Edit.png
Error.png
Loading.gif
Preview.png
Remove.png
Save.png
TipOfTheDay.png
Warning.png
Themes
Aero
ExpressionDark
Assets
Generic
Assets
Controls
Jetpack
Assets
background.png
Old
SunnyOrange
Assets
Windows
Controls
Extensions
LinkLabel
StackGrid
Data
Converters
Helpers
Documents
Extensions
Extensions
Helpers
Input
Markup
Media
Effects
EmptyEffect
EmptyEffect.fx
EmptyEffect.ps
EmptyEffect.fx
GrayscaleEffect
GrayscaleEffect.fx
GrayscaleEffect.ps
Extensions
Imaging
Extensions
Windows
DataWindow
TipOfTheDay
Local.testsettings
Settings.StyleCop
TraceAndTestImpact.testsettings
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PropertyDataManager.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Property data manager.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Serialization;

namespace Catel.Data
{
    /// <summary>
    /// Property data manager.
    /// </summary>
    internal class PropertyDataManager
    {
        #region Variables
        /// <summary>
        /// Dictionary containing all the properties per type.
        /// </summary>
        private readonly Dictionary<Type, Dictionary<string, PropertyData>> _propertyData = new Dictionary<Type, Dictionary<string, PropertyData>>();

        /// <summary>
        /// Lock object for the <see cref="_propertyData"/> field.
        /// </summary>
        private readonly object _propertyDataLock = new object();

        /// <summary>
        /// Dictionary to provide fast xml name to property name mappings.
        /// </summary>
        private readonly Dictionary<Type, Dictionary<string, string>> _xmlNameToPropertyNameMappings = new Dictionary<Type, Dictionary<string, string>>();

        /// <summary>
        /// Dictionary to provide fast property name to xml name mappings.
        /// </summary>
        private readonly Dictionary<Type, Dictionary<string, string>> _xmlPropertyNameToXmlNameMappings = new Dictionary<Type, Dictionary<string, string>>();
        #endregion

        #region Constructor & destructor
        #endregion

        #region Properties
        #endregion

        #region Methods
        /// <summary>
        /// Gets the properties of a specific type.
        /// </summary>
        /// <param name="type">The type for which the properties to return.</param>
        /// <returns>Dictionary with the properties.</returns>
        public Dictionary<string, PropertyData> GetProperties(Type type)
        {
            return _propertyData.ContainsKey(type) ? _propertyData[type] : new Dictionary<string, PropertyData>();
        }

        /// <summary>
        /// Registers a property for a specific type.
        /// </summary>
        /// <param name="type">The type for which to register the property.</param>
        /// <param name="name">The name of the property.</param>
        /// <param name="propertyData">The property data.</param>
        public void RegisterProperty(Type type, string name, PropertyData propertyData)
        {
            lock (_propertyDataLock)
            {
                if (!_propertyData.ContainsKey(type))
                {
                	_propertyData.Add(type, new Dictionary<string, PropertyData>());
                }

                if (_propertyData[type].ContainsKey(name))
                {
                	throw new PropertyAlreadyRegisteredException(name, type);
                }

                _propertyData[type].Add(name, propertyData);
            }
        }

        /// <summary>
        /// Returns whether a specific property is registered.
        /// </summary>
        /// <param name="type">The type for which to check whether the property is registered.</param>
        /// <param name="name">The name of the property.</param>
        /// <returns>
        /// True if the property is registered, otherwise false.
        /// </returns>
        public bool IsPropertyRegistered(Type type, string name)
        {
            lock (_propertyDataLock)
            {
                if (_propertyData.ContainsKey(type))
                {
                    return _propertyData[type].ContainsKey(name);
                }

                return false;
            }
        }

        /// <summary>
        /// Gets the property data.
        /// </summary>
        /// <param name="type">The type for which to get the property data.</param>
        /// <param name="name">The name of the property.</param>
        /// <returns>The <see cref="PropertyData"/> of the requested property.</returns>
        /// <exception cref="PropertyNotRegisteredException">Thrown when the property is not registered.</exception>
        public PropertyData GetPropertyData(Type type, string name)
        {
            if (!IsPropertyRegistered(type, name))
            {
            	throw new PropertyNotRegisteredException(name, type);
            }

            lock (_propertyDataLock)
            {
                return _propertyData[type][name];
            }
        }

        /// <summary>
        /// Maps the name of the XML element to a property name.
        /// </summary>
        /// <param name="type">The type for which to make the xml name.</param>
        /// <param name="xmlName">Name of the XML element.</param>
        /// <returns>
        /// Name of the property that represents the xml value.
        /// </returns>
        public string MapXmlNameToPropertyName(Type type, string xmlName)
        {
            InitializeXmlPropertyMappings(type);

            return _xmlNameToPropertyNameMappings[type][xmlName];
        }

        /// <summary>
        /// Maps the name of the property name to an XML name.
        /// </summary>
        /// <param name="type">The type for which to make the xml name.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>
        /// Name of the XML element that represents the property value.
        /// </returns>
        public string MapPropertyNameToXmlName(Type type, string propertyName)
        {
            InitializeXmlPropertyMappings(type);

            return _xmlPropertyNameToXmlNameMappings[type][propertyName];
        }

        /// <summary>
        /// Initializes the XML property mappings.
        /// </summary>
        /// <param name="type">The type for which to initialize the xml mappings.</param>
        private void InitializeXmlPropertyMappings(Type type)
        {
            if (_xmlNameToPropertyNameMappings.ContainsKey(type))
            {
            	return;
            }

            _xmlNameToPropertyNameMappings.Add(type, new Dictionary<string, string>());
            _xmlPropertyNameToXmlNameMappings.Add(type, new Dictionary<string, string>());

            foreach (KeyValuePair<string, PropertyData> propertyData in GetProperties(type))
            {
                PropertyInfo propertyInfo = TypeHelper.GetPropertyInfo(type, propertyData.Key);
                XmlElementAttribute xmlElementAttribute;
                if (AttributeHelper.TryGetAttribute(propertyInfo, out xmlElementAttribute))
                {
                    _xmlNameToPropertyNameMappings[type].Add(xmlElementAttribute.ElementName, propertyData.Key);
                    _xmlPropertyNameToXmlNameMappings[type].Add(propertyData.Key, xmlElementAttribute.ElementName);
                }
                else
                {
                    _xmlNameToPropertyNameMappings[type].Add(propertyData.Key, propertyData.Key);
                    _xmlPropertyNameToXmlNameMappings[type].Add(propertyData.Key, propertyData.Key);
                }
            }
        }
        #endregion
    }
}

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)

Share

About the Author

Geert van Horrik
Software Developer CatenaLogic
Netherlands Netherlands
I am Geert van Horrik, and I have studied Computer Science in the Netherlands.
 
I love to write software using .NET (especially the combination of WPF and C#). I am also the lead developer of Catel, an open-source application development framework for WPF, Silverlight, WP7 and WinRT with the focus on MVVM.
 
I have my own company since January 1st 2007, called CatenaLogic. This company develops commercial and non-commercial software.
 
To download (or buy) applications I have written, visit my website: http://www.catenalogic.com
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 28 Jan 2011
Article Copyright 2011 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid