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

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="LLBLGenHelper.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   LLBLGen Pro helper class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Reflection;
using log4net;

namespace Catel.LLBLGen
{
	/// <summary>
	/// LLBLGen Pro helper class.
	/// </summary>
	/// <remarks>
	/// This class extensively use reflection so no actual reference to LLBLGen is required.
	/// </remarks>
	public static class LLBLGenHelper
	{
		#region Variables
        /// <summary>
        /// The <see cref="ILog">log</see> object.
        /// </summary>
		private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
		#endregion

		/// <summary>
		/// Determines whether the specified type is an LLBLGen type (entity or collection).
		/// </summary>
		/// <param name="type">The type to check.</param>
		/// <returns>
		/// 	<c>true</c> if the specified type is an LLBLGen type; otherwise, <c>false</c>.
		/// </returns>
		/// <remarks>
		/// This method uses the <see cref="IsEntityCollection(System.Type)"/> and <see cref="IsEntity(System.Type)"/> methods.
		/// </remarks>
		public static bool IsLLBLGenType(Type type)
		{
			return IsEntity(type) || IsEntityCollection(type);
		}

		/// <summary>
		/// Determines whether the specified object is an LLBLGen type (entity or collection).
		/// </summary>
		/// <param name="entity">The entity to check.</param>
		/// <returns>
		/// 	<c>true</c> if the specified object is an LLBLGen type; otherwise, <c>false</c>.
		/// </returns>
		/// <remarks>
		/// This method uses the <see cref="IsEntityCollection(System.Type)"/> and <see cref="IsEntity(System.Type)"/> methods.
		/// </remarks>
		public static bool IsLLBLGenType(object entity)
		{
			return IsEntity(entity) || IsEntityCollection(entity);
		}

		/// <summary>
		/// Determines whether the specified type is an LLBLGen entity.
		/// </summary>
		/// <param name="type">The type to check.</param>
		/// <returns>
		/// 	<c>true</c> if the specified type is an LLBLGen entity; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsEntity(Type type)
		{
			return GetIEntityCore(type) != null;
		}

		/// <summary>
		/// Determines whether the specified object is an LLBLGen entity.
		/// </summary>
		/// <param name="entity">The entity to check.</param>
		/// <returns>
		/// 	<c>true</c> if the specified object is an LLBLGen entity; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsEntity(object entity)
		{
			return GetIEntityCore(entity) != null;
		}

		/// <summary>
		/// Determines whether the specified type is an LLBLGen entity collection.
		/// </summary>
        /// <param name="type">The typ to checke.</param>
		/// <returns>
		/// 	<c>true</c> if the specified type is an LLBLGen entity collection; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsEntityCollection(Type type)
		{
			return GetIEntityCollectionCore(type) != null;
		}

		/// <summary>
		/// Determines whether the specified object is an LLBLGen entity collection.
		/// </summary>
        /// <param name="entity">The entity to check.</param>
		/// <returns>
		/// 	<c>true</c> if the specified object is an LLBLGen entity collection; otherwise, <c>false</c>.
		/// </returns>
		public static bool IsEntityCollection(object entity)
		{
			return GetIEntityCollectionCore(entity) != null;
		}

		/// <summary>
        /// Gets the <c>IEntityCore.ValidateEntity</c> method if available.
		/// </summary>
		/// <param name="entity">The entity to get the method from.</param>
		/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
		public static MethodInfo GetValidateEntityMethod(object entity)
		{
			return GetMethod(entity, "ValidateEntity");
		}

		/// <summary>
        /// Gets the <c>IEntityCore.SaveFields(string)</c> method if available.
		/// </summary>
		/// <param name="entity">The entity to get the method from.</param>
		/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
		public static MethodInfo GetSaveFieldsMethod(object entity)
		{
			return GetMethod(entity, "SaveFields", new[] { typeof(string) });
		}

		/// <summary>
        /// Gets the <c>IEntityCore.GetDiscardSavedFieldsMethod()</c> method if available.
		/// </summary>
        /// <param name="entity">The entity to get the method from.</param>
		/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
		public static MethodInfo GetDiscardSavedFieldsMethod(object entity)
		{
			return GetMethod(entity, "DiscardSavedFields");
		}

		/// <summary>
        /// Gets the <c>IEntityCore.RollbackFields(string)</c> method if available.
		/// </summary>
        /// <param name="entity">The entity to get the method from.</param>
		/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
		public static MethodInfo GetRollbackFieldsMethod(object entity)
		{
			return GetMethod(entity, "RollbackFields", new[] { typeof(string) });
		}

		/// <summary>
		/// Gets the refetch method.
		/// </summary>
        /// <param name="entity">The entity to get the method from.</param>
		/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
		public static MethodInfo GetRefetchMethod(object entity)
		{
			return GetMethod(entity, "Refetch");
		}

		/// <summary>
		/// Gets the name of the related entity property. For example, if <c>Product</c> is passed of an <c>Order</c> entity, this
		/// method will return <c>ProductID</c>.
		/// </summary>
		/// <param name="type">The type of the LLBLGen entity.</param>
		/// <param name="property">The property to map.</param>
		/// <returns>
		/// The name of the mapped property, or <c>null</c> if the property cannot be mapped.
		/// </returns>
		/// <remarks>
		/// For now, this method returns the property name with "ID" appended. Still waiting for feedback from LLBLGen team.
		/// TODO: actually retrieve the property name correctly
		/// </remarks>
		public static string GetRelatedEntityPropertyName(Type type, string property)
		{
			return string.Format("{0}ID", property);
		}

		/// <summary>
        /// Gets a method with the specified name of the <c>IEntityCore</c> interface.
		/// </summary>
        /// <param name="entity">The entity to get the method from.</param>
		/// <param name="methodName">Name of the method.</param>
		/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
		/// <remarks>
		/// This method only supports the retrieval of public instance methods that have no parameters.
		/// </remarks>
		public static MethodInfo GetMethod(object entity, string methodName)
		{
			return GetMethod(entity, methodName, Type.EmptyTypes);
		}

		/// <summary>
        /// Gets a method with the specified name of the <c>IEntityCore</c> interface.
		/// </summary>
		/// <param name="entity">The entity to get the method from.</param>
		/// <param name="methodName">Name of the method.</param>
		/// <param name="types">The types of the parameters.</param>
		/// <returns>
		/// 	<see cref="MethodInfo"/> or <c>null</c> if the method is not found.
		/// </returns>
		/// <remarks>
		/// This method only supports the retrieval of public instance methods.
		/// </remarks>
		public static MethodInfo GetMethod(object entity, string methodName, Type[] types)
		{
			if (entity == null)
			{
				return null;
			}

			Type type = entity.GetType();
			return type.GetMethod(methodName, types);
		}

		/// <summary>
        /// Gets the object ID of a <c>IEntityCore</c> if the object is an LLBLGen entity.
		/// </summary>
		/// <param name="entity">The entity to get the object ID from.</param>
		/// <returns>
		/// The object ID of the entity, or <c>null</c> if the object is not an entity.
		/// </returns>
		public static object GetEntityObjectID(object entity)
		{
			var entityInterface = GetIEntityCore(entity);
			if (entityInterface == null)
			{
				return null;
			}

			var propertyInfo = entityInterface.GetProperty("ObjectID");
			if (propertyInfo == null)
			{
				return null;
			}

			if (!propertyInfo.CanRead)
			{
				return null;
			}

			return propertyInfo.GetValue(entity, null);
		}

        /// <summary>
        /// Gets the <c>IEntityCore</c> interface of the type if available.
        /// </summary>
        /// <param name="type">The type to get the interface from.</param>
        /// <returns>
        /// 	<c>IEntityCore</c> or <c>null</c> if this type has no support for LLBLGen Pro.
        /// </returns>
		public static Type GetIEntityCore(Type type)
		{
			return type.GetInterface("IEntityCore", false);
		}

		/// <summary>
		/// Gets the <c>IEntityCore</c> interface of the object if available.
		/// </summary>
		/// <param name="entity">The entity to get the interface from.</param>
		/// <returns><c>IEntityCore</c> or <c>null</c> if this object has no support for LLBLGen Pro.</returns>
		public static Type GetIEntityCore(object entity)
		{
			if (entity == null)
			{
				return null;
			}

			return GetIEntityCore(entity.GetType());
		}

		/// <summary>
		/// Gets the <c>IEntityCollectionCore</c> interface of the type if available.
		/// </summary>
		/// <param name="type">The type to get the interface from.</param>
		/// <returns><c>IEntityCollectionCore</c> or <c>null</c> if this type has no support for LLBLGen Pro.</returns>
		public static Type GetIEntityCollectionCore(Type type)
		{
			return type.GetInterface("IEntityCollectionCore", false);
		}

		/// <summary>
		/// Gets the <c>IEntityCollectionCore</c> interface of the object if available.
		/// </summary>
        /// <param name="entity">The entity to get the interface from.</param>
		/// <returns><c>IEntityCollectionCore</c> or <c>null</c> if this object has no support for LLBLGen Pro.</returns>
		public static Type GetIEntityCollectionCore(object entity)
		{
			if (entity == null)
			{
				return null;
			}

			return GetIEntityCollectionCore(entity.GetType());
		}
	}
}

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)

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
Web04 | 2.8.140721.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