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 CPOL
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="FileWatcher.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Available FileWatcher changes.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.IO;

namespace Catel.IO.Utils
{
    /// <summary>
    /// Available FileWatcher changes.
    /// </summary>
    public enum FileWatcherChangeType
    {
        /// <summary>
        /// A file is added.
        /// </summary>
        FileAdded,

        /// <summary>
        /// A file is deleted.
        /// </summary>
        FileDeleted,

        /// <summary>
        /// A file is renamed.
        /// </summary>
        FileRenamed,

        /// <summary>
        /// A file is changed.
        /// </summary>
        FileChanged
    }

	/// <summary>
	///	Arguments sent through when an event is fired
	/// </summary>
	public class FileWatcherEventArgs : EventArgs
	{
		#region Variables
		#endregion

		#region Constructor & destructor
		/// <summary>
		/// Initializes a new instance of the <see cref="FileWatcherEventArgs"/> class.
		/// </summary>
		/// <param name="fileName">Name of the file.</param>
		/// <param name="path">The specific path.</param>
		/// <param name="oldPath">The old path.</param>
		/// <param name="oldName">The old name.</param>
		/// <param name="changeType">Type of the change.</param>
		public FileWatcherEventArgs(string fileName, string path, string oldPath, string oldName, FileWatcherChangeType changeType)
		{
			FileName = fileName;
			Path = path;
			OldFileName = oldName;
			OldPath = oldPath;
			ChangeType = changeType;
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets the name of the file.
		/// </summary>
		/// <value>The name of the file.</value>
		public string FileName { get; private set; }

		/// <summary>
		/// Gets the old name of the file.
		/// </summary>
		/// <value>The old name of the file.</value>
		public string OldFileName { get; private set; }

		/// <summary>
		/// Gets the path.
		/// </summary>
		/// <value>The path to watch.</value>
		public string Path { get; private set; }

		/// <summary>
		/// Gets the old path.
		/// </summary>
		/// <value>The old path.</value>
		public string OldPath { get; private set; }

		/// <summary>
		/// Gets the type of the change.
		/// </summary>
		/// <value>The type of the change.</value>
		public FileWatcherChangeType ChangeType { get; private set; }
		#endregion

		#region Methods
		#endregion
	}

	/// <summary>
	/// Monitors a folder for file system changes.
	/// </summary>
	public class FileWatcher
	{
		#region Variables
        /// <summary>
        /// The path to watch.
        /// </summary>
		private string _path;

        /// <summary>
        /// The filter for the files to watch.
        /// </summary>
		private string _filter;

        /// <summary>
        /// The actual <see cref="FileSystemWatcher"/> that is wrapped by this class.
        /// </summary>
		private FileSystemWatcher _fsw;

        /// <summary>
        /// A value indicating whether subdirectories are included or not.
        /// </summary>
		private bool _includeSubdirectories;
		#endregion

		#region Delegates
		#endregion

		#region Constructor & destructor
		/// <summary>
		/// Initializes a new instance of the <see cref="FileWatcher"/> class.
		/// </summary>
		public FileWatcher()
		{
			Path = ".";
			Filter = "*.*";
			CurrentFileName = string.Empty;
			CurrentPath = string.Empty;
			CurrentOldPath = string.Empty;
			CurrentOldFileName = string.Empty;
			IncludeSubdirectories = true;

			CreateFileWatcher();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="FileWatcher"/> class.
		/// </summary>
		/// <param name="path">The path to watch.</param>
		/// <param name="filter">The filter.</param>
		public FileWatcher(string path, string filter)
			: this()
		{
			Path = path;
			Filter = filter;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="FileWatcher"/> class.
		/// </summary>
		/// <param name="path">The path to watch.</param>
		public FileWatcher(string path)
			: this()
		{
			Path = path;
		}
		#endregion

		#region Events
		/// <summary>
		/// Occurs when something has changed.
		/// </summary>
		public event EventHandler<FileWatcherEventArgs> Changed = null;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the path to watch.
		/// </summary>
		/// <value>The path to watch.</value>
		public string Path
		{
			get
			{
				return _path;
			}

			set
			{
				_path = value;
				_fsw.Path = _path;
			}
		}

		/// <summary>
		/// Gets or sets the filter.
		/// </summary>
		/// <value>The filter.</value>
		public string Filter
		{
			get
			{
				return _filter;
			}

			set
			{
				_filter = value;
				_fsw.Filter = _filter;
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance is started.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is started; otherwise, <c>false</c>.
		/// </value>
		public bool IsStarted { get { return _fsw.EnableRaisingEvents; } }

		/// <summary>
		/// Gets the name of the current file.
		/// </summary>
		/// <value>The name of the current file.</value>
		public string CurrentFileName { get; private set; }

		/// <summary>
		/// Gets the name of the current old file.
		/// </summary>
		/// <value>The name of the current old file.</value>
		public string CurrentOldFileName { get; private set; }

		/// <summary>
		/// Gets the current path.
		/// </summary>
		/// <value>The current path.</value>
		public string CurrentPath { get; private set; }

		/// <summary>
		/// Gets the current old path.
		/// </summary>
		/// <value>The current old path.</value>
		public string CurrentOldPath { get; private set; }

		/// <summary>
		/// Gets the type of the current change.
		/// </summary>
		/// <value>The type of the current change.</value>
		public FileWatcherChangeType CurrentChangeType { get; private set; }

		/// <summary>
		/// Gets or sets a value indicating whether to include sub directories or not.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if sub directories should be included; otherwise, <c>false</c>.
		/// </value>
		public bool IncludeSubdirectories
		{
			get
			{
				return _includeSubdirectories;
			}

			set
			{
				_includeSubdirectories = value;
				if (_fsw != null)
				{
					_fsw.IncludeSubdirectories = _includeSubdirectories;
				}
			}
		}
		#endregion

		#region public methods
		/// <summary>
		///	Start the watcher for a specific folder with a specific filter.
		/// </summary>
		public void StartWatcher()
		{
			if (string.IsNullOrEmpty(_path))
			{
				return;
			}

			_fsw.Path = _path;
			_fsw.Filter = _filter;
			_fsw.IncludeSubdirectories = _includeSubdirectories;
			_fsw.EnableRaisingEvents = true;
		}

		/// <summary>
		///	Stop the folder watcher from raising events.
		/// </summary>
		public void StopWatcher()
		{
			_fsw.EnableRaisingEvents = false;
		}

		/// <summary>
		/// Creates the file watcher.
		/// </summary>
		private void CreateFileWatcher()
		{
			_fsw = new FileSystemWatcher(_path, _filter);
			_fsw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName;

			_fsw.Changed += OnChanged;
			_fsw.Created += OnCreated;
			_fsw.Deleted += OnDeleted;
			_fsw.Renamed += OnRenamed;
		}

		/// <summary>
		/// Raises the <see cref="E:Changed"/> event.
		/// </summary>
		/// <param name="e">The <see cref="Catel.IO.Utils.FileWatcherEventArgs"/> instance containing the event data.</param>
		protected virtual void OnChanged(FileWatcherEventArgs e)
		{
			if (Changed != null)
			{
				Changed(this, e);
			}
		}

		/// <summary>
		/// Called when the <see cref="FileSystemWatcher"/> has detected a change.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="args">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
		private void OnCreated(object source, FileSystemEventArgs args)
		{
			CurrentFileName = args.Name;
			CurrentPath = args.FullPath;
			CurrentOldFileName = string.Empty;
			CurrentOldPath = string.Empty;
			CurrentChangeType = FileWatcherChangeType.FileAdded;

			OnChanged(new FileWatcherEventArgs(CurrentFileName, CurrentPath, CurrentOldPath, CurrentOldFileName, FileWatcherChangeType.FileAdded));
		}

		/// <summary>
		/// Called when the <see cref="FileSystemWatcher"/> has detected a change.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="args">The <see cref="System.IO.RenamedEventArgs"/> instance containing the event data.</param>
		private void OnRenamed(object source, RenamedEventArgs args)
		{
			CurrentFileName = args.Name;
			CurrentPath = args.FullPath;
			CurrentOldFileName = args.OldFullPath;
			CurrentOldPath = args.OldName;
			CurrentChangeType = FileWatcherChangeType.FileRenamed;

			OnChanged(new FileWatcherEventArgs(CurrentFileName, CurrentPath, CurrentOldPath, CurrentOldFileName, FileWatcherChangeType.FileRenamed));
		}

		/// <summary>
		/// Called when the <see cref="FileSystemWatcher"/> has detected a change.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="args">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
		private void OnDeleted(object source, FileSystemEventArgs args)
		{
			CurrentFileName = args.Name;
			CurrentPath = args.FullPath;
			CurrentOldFileName = string.Empty;
			CurrentOldPath = string.Empty;
			CurrentChangeType = FileWatcherChangeType.FileDeleted;

			OnChanged(new FileWatcherEventArgs(CurrentFileName, CurrentPath, CurrentOldPath, CurrentOldFileName, FileWatcherChangeType.FileDeleted));
		}

		/// <summary>
		/// Called when the <see cref="FileSystemWatcher"/> has detected a change.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="args">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
		private void OnChanged(object source, FileSystemEventArgs args)
		{
			CurrentFileName = args.Name;
			CurrentPath = args.FullPath;
			CurrentOldFileName = string.Empty;
			CurrentOldPath = string.Empty;
			CurrentChangeType = FileWatcherChangeType.FileChanged;

			OnChanged(new FileWatcherEventArgs(CurrentFileName, CurrentPath, CurrentOldPath, CurrentOldFileName, FileWatcherChangeType.FileChanged));
		}
		#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 | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 28 Jan 2011
Article Copyright 2011 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid