Click here to Skip to main content
15,885,435 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 48.9K   572   11  
This article explains how to write unit tests for MVVM using Catel.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;

namespace Ricciolo.StylesExplorer.MarkupReflection
{
    public class BamlAssembly : MarshalByRefObject
    {
        private readonly string _filePath;
        private Assembly _assembly;
        private BamlFileList _bamlFile;

        public BamlAssembly(Assembly assembly)
        {
            _assembly = assembly;
            _filePath = assembly.CodeBase;

            ReadBaml();
        }

        public BamlAssembly(string filePath)
        {
            this._filePath = Path.GetFullPath(filePath);
            this._assembly = Assembly.LoadFile(this.FilePath);
            if (String.Compare(this.Assembly.CodeBase, this.FilePath, true) != 0)
                throw new ArgumentException("Cannot load filePath because Assembly is already loaded", "filePath");

            ReadBaml();
        }

        private void ReadBaml()
        {
            // Get available names
            string[] resources = this.Assembly.GetManifestResourceNames();
            foreach (string res in resources)
            {
                // Solo le risorse
                if (String.Compare(Path.GetExtension(res), ".resources", true) != 0) continue;

                // Get stream
                using (Stream stream = this.Assembly.GetManifestResourceStream(res))
                {
                    try
                    {
                        ResourceReader reader = new ResourceReader(stream);
                        foreach (DictionaryEntry entry in reader)
                        {
                            if (String.Compare(Path.GetExtension(entry.Key.ToString()), ".baml", true) == 0 && entry.Value is Stream)
                            {
                                BamlFile bm = new BamlFile(GetAssemblyResourceUri(entry.Key.ToString()), (Stream)entry.Value);
                                this.BamlFiles.Add(bm);
                            }
                        }
                    }
                    catch (ArgumentException)
                    {}
                }
            }
        }

        private Uri GetAssemblyResourceUri(string resourceName)
        {
            AssemblyName asm = this.Assembly.GetName();
            byte[] data = asm.GetPublicKeyToken();
            StringBuilder token = new StringBuilder(data.Length * 2);
            for (int x = 0; x < data.Length; x++)
            {
                token.Append(data[x].ToString("x", System.Globalization.CultureInfo.InvariantCulture));
            }

            return new Uri(String.Format(@"{0};V{1};{2};component\{3}", asm.Name, asm.Version, token, Path.ChangeExtension(resourceName, ".xaml")), UriKind.RelativeOrAbsolute);
        }

        public string FilePath
        {
            get { return _filePath; }
        }

        public Assembly Assembly
        {
            get { return _assembly; }
        }

        public BamlFileList BamlFiles
        {
            get
            {
                if (_bamlFile == null)
                    _bamlFile = new BamlFileList();
                 return _bamlFile;
            }
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
    }

    [Serializable()]
    public class BamlFileList : Collection<BamlFile>
    {}

}

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