Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / WPF

Layers Pattern in Practice

Rate me:
Please Sign up or sign in to vote.
4.96/5 (59 votes)
23 Apr 2010CPOL25 min read 152.6K   8.1K   187  
Layers Pattern via a WPF project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using BillsDalLib;
using BillsEntityLib;
using BillsBusinessLogicLib;
using System.Diagnostics;
using System.IO;

namespace TestBillPayManager
{
    [TestFixture(Description = "Test of Logic Layer")]
    public class TestLogic
    {
        private string pathToxml = @"D:\Ahtung\Usefull Projects\WPF\BillPayManager\BillManagerConsole\bin\Debug\archive.xml";
        private string genericDuedate = "Dec 16, 2009 12:00:00 PM";

        [Test(Description = "Singleton object")]
        public void CreateSingletonObject()
        {
            IBillsManager idal1 = BillsManager.GetInstance();
            idal1.Settings = new object[2] { "archive.xml", true };
            IBillsManager idal2 = BillsManager.GetInstance();
            idal2.Settings = new object[2] { "archive.xml", true };
            Assert.AreEqual(idal1.GetHashCode(), idal2.GetHashCode());
            GC.Collect();
        }

        [Test(Description = "Insert bills into the datasource. Read inserted values. Delete inserted values")]
        public void InsertReadDeleteTest()
        {
            List<Bill> list = new List<Bill>();
            Random rand = new Random(list.GetHashCode());
            int numberOfInsertedBills = 50;
            for (int i = 0; i < numberOfInsertedBills; i++)
            {
                Bill bill = new Bill(i.ToString(), DateTime.Parse(genericDuedate), i * rand.Next(25), DateTime.Now, BillStatus.Unpaid, Guid.NewGuid());
                list.Add(bill);
            }
            Bills bills = new Bills(list);
            IBillsManager bll = BillsManager.GetInstance();   //get Datasource manager class
            bll.Settings = new object[2] { pathToxml, true };
            Bills existingBills = bll.Read(DateTime.MinValue, DateTime.MaxValue); //read Existing bills
            Trace.WriteLine(existingBills.ToList().Count());
            bll.Delete(existingBills.GetGuid());    //delete all items from the datasource
            bll.Insert(bills);  //insert items in the datasource
            Bills insertedBills = bll.Read(DateTime.MinValue, DateTime.MaxValue);   //read inserted bills
            Assert.AreEqual(numberOfInsertedBills, insertedBills.ToList().Count());
            bll.Delete(insertedBills.GetGuid());
            GC.Collect();
        }

        [Test(Description = "ReadById and Delete by Id test")]
        public void ReadByIdDeleteByIdTest()
        {
            IBillsManager bll = BillsManager.GetInstance();   //get Datasource manager class
            bll.Settings = new object[2] { pathToxml, true };
            Guid id = Guid.NewGuid();
            string payTo = "Moldtelecom";
            Bills bills = new Bills(new Bill(payTo, DateTime.Parse(genericDuedate), (decimal)125.2, DateTime.Now, BillStatus.Unpaid, id));
            bll.Insert(bills);
            Bill readBill = bll.ReadById(id);
            Assert.AreEqual(payTo, readBill.Name);
            Assert.AreEqual(DateTime.Parse(genericDuedate), readBill.DueDate);
            int deletedItems = bll.Delete(id);
            Assert.AreEqual(1, deletedItems);
            readBill = bll.ReadById(id);
            Assert.AreEqual(null, readBill);
            GC.Collect();
        }
        [Test(Description = "Update bill")]
        public void UpdateBill()
        {
            BillsManager bll = BillsManager.GetInstance();
            bll.Settings = new object[2] { pathToxml, true };
            Guid id = Guid.NewGuid();
            string payTo = "Moldtelecom";
            Bill bill = new Bill(payTo, DateTime.Parse(genericDuedate), (decimal)125.2, DateTime.Now, BillStatus.Unpaid, id);
            Bills bills = new Bills(bill);
            bll.Insert(bills);
            bill.Status = BillStatus.Paid.ToString();
            int updated = bll.Update(id, bill);
            Assert.AreEqual(1, updated);
            Bill readBill = bll.ReadById(id);
            Assert.AreEqual(bill.Name, readBill.Name);
            Assert.AreEqual(bill.Status, readBill.Status);
            GC.Collect();
        }

        [Test(Description = "Create new file test")]
        public void CreateNewFileTest()
        {
            if (File.Exists("newfile.xml"))
                File.Delete("newfile.xml");
            BillsManager ibll = BillsManager.GetInstance();
            ibll.Settings = new object[2] { "newfile.xml", true };
            ibll.Settings = new object[2] { "newnewfile.xml", true };
            Assert.AreEqual(true, File.Exists("newnewfile.xml"));
            File.Delete("newnewfile.xml");
            GC.Collect();
        }

        [Test(Description = "Read from unexisting file")]
        public void ReadFromUnexistingFile()
        {
            if (File.Exists("newfile.xml"))
                File.Delete("newfile.xml");
            IBillsManager ibll = BillsManager.GetInstance();
            ibll.Settings = new object[2] { "newfile.xml", true };
            Bills bills = ibll.Read(DateTime.MinValue, DateTime.MaxValue);
            Assert.AreEqual(0, bills.Items.Count());
        }

        [Test(Description = "Try to create a file which has a bad path")]
        [ExpectedException(typeof(BillManagerException))]
        public void TryToCreateAFileInABadPath()
        {
            if (File.Exists("newfi//le.xml"))
                File.Delete("newfi//le.xml");
            IBillsManager ibll = BillsManager.GetInstance();
            ibll.Settings = new object[2] { "newfi//le.xml", true };
        }
    }
}

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
Moldova (Republic of) Moldova (Republic of)
Interested in computer science, math, research, and everything that relates to innovation. Fan of agnostic programming, don't mind developing under any platform/framework if it explores interesting topics. In search of a better programming paradigm.

Comments and Discussions