Click here to Skip to main content
15,885,091 members
Articles / Web Development / ASP.NET

Email Templates: A generic solution to handle template files

Rate me:
Please Sign up or sign in to vote.
4.91/5 (44 votes)
4 Nov 2004CPOL7 min read 173.6K   1.3K   133  
A simple, user-friendly and flexible framework that takes care of all your email template files - once and for all.
using System;
using System.Configuration;
using System.IO;
using System.Globalization;
using System.Web.Mail;

using NUnit.Framework;

using Evolve.Util.MailTemplates;
using Evolve.Util.MailTemplates.Xml;

namespace Evolve.MailTemplates.Tests
{
  /// <summary>
  /// Validates proper parsing of the
  /// </summary>
  [TestFixture]
  public class TestConfiguration
  {
    public TestConfiguration()
    {
    }


    /// <summary>
    /// Tests proper deserialization of the sample config file.
    /// </summary>
    [Test]
    public void TestConfigFileReading()
    {
      //load the configuration file...
      string configFile = ConfigurationSettings.AppSettings["Evolve.TemplateConfiguration"];
      TemplateConfiguration configuration = ConfigFileParser.ParseConfiguration(configFile);

      //we should have 3 template groups
      Assert.AreEqual(3, configuration.TemplateGroups.Count);

      //check if the full path of the base folder is stored
      DirectoryInfo dir = new DirectoryInfo(configuration.BaseDirectory);
      Assert.AreEqual(dir.FullName, configuration.BaseDirectory);

      TemplateGroupElement group = configuration.GetGroup("newsletters");
      Assert.AreEqual("newsletters", group.TemplateGroupId);
      Assert.AreEqual("codeproject@evolvesoftware.ch", group.SenderAddress);
      Assert.AreEqual(2, group.Templates.Count);
  

      //check second group
      group = configuration.GetGroup("greetingcard");
      Assert.AreEqual(1, group.Templates.Count);
      Assert.AreEqual(MailFormat.Html, group.MailFormat);
    }



    [Test]
    public void TestTemplateAccess()
    {
      TemplateHandler handler = TemplateHandler.Instance;
      MailTemplate template1 = handler["greetingcard", new CultureInfo("en")];
      MailTemplate template2 = handler["greetingcard", new CultureInfo("en")];
  
      //we need to get different instances
      Assert.IsFalse(MailTemplate.ReferenceEquals(template1, template2));

      //format the subject
      template1.Subject = String.Format(template1.Subject, "Philipp");
      Assert.AreEqual(template1.Subject, "Philipp sent you a greeting card!");
    }


    /// <summary>
    /// Tests whether properly localized templates are being returned.
    /// </summary>
    [Test]
    public void TestLocalization()
    {
      TemplateHandler handler = TemplateHandler.Instance;

      MailTemplate template = handler["newsletters", new CultureInfo("en")];      
      //the locale should not be "en" as it's the default
      Assert.AreEqual(CultureInfo.InvariantCulture, template.Locale, "default template without invaraint culture");
      
      //get the german template
      template = handler["newsletters", new CultureInfo("de")];
      Assert.AreEqual(new CultureInfo("de"), template.Locale);
      Assert.AreEqual("Newsletter Registrierung Evolve Software", template.Subject);

      //if I submit an subculture of the available "de" locale, this one should be returned
      template = handler["newsletters", new CultureInfo("de-ch")];
      Assert.AreEqual(new CultureInfo("de"), template.Locale, "wrong parent returned for culture ch-de");

      //if I submit an unknown culture, the default should be returned
      template = handler["newsletters", new CultureInfo("fr-ch")];
      Assert.AreEqual(CultureInfo.InvariantCulture, template.Locale, "did not get default template for culture fr-ch");
      //if I submit an unknown culture, the default should be returned
      template = handler["newsletters", new CultureInfo("fr")];
      Assert.AreEqual(CultureInfo.InvariantCulture, template.Locale, "did not get default template for culture fr");      


      template = handler["newsletters"];
      Assert.AreEqual(CultureInfo.InvariantCulture, template.Locale, "could not retrieve default template");
      Assert.AreEqual("Evolve Newsletter Subscription", template.Subject);


      //check group that has no default
      try
      {
        template = handler["greetingcard"];
        Assert.Fail("No exception thrown when accessing default template");
      }
      catch (ArgumentException expected) { }

      
      //request valid locale
      template = handler["greetingcard", new CultureInfo("en")];
      Assert.AreEqual(new CultureInfo("en"), template.Locale);

      
      //request unavailable locale
      try
      {
        template = handler["greetingcard", new CultureInfo("fr")];
        Assert.Fail("No exception thrown when requesting unknown locale");
      }
      catch (ArgumentException expected) { }



    }

  }
}

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
Architect I'm a gun for hire
Switzerland Switzerland
Philipp is an independent software engineer with great love for all things .NET.
He lives in Winterthur, Switzerland and his home on the web is at http://www.hardcodet.net.

Comments and Discussions