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

Layers Pattern in Practice

, 23 Apr 2010
Layers Pattern via a WPF project.
AdditionalDependencies.zip
BillsManager.zip
BillsManager
BillManagerUninstallAction
Properties
BillPayManager
Application.ico
BillPayManager.cd
BillPayManager.csproj.user
bin
Debug
AvalonControlsLibrary.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
WPFToolkit.dll
WPFVisifire.Charts.dll
Charting
DataGrid
icon.ico
Images
about.png
archive.png
backArrow.png
bills.png
charts.png
fwdArrow.png
icon.ico
settings.png
Properties
Themes
BillPayManager.suo
BillsBusinessLogicLib
BillsBusinessLogicLib.csproj.user
BLLBills.cd
ciumac.sergiu.pfx
Properties
BillsDalLib
BillsDalClassDiagram.cd
BillsDalLib.csproj.user
ciumac.sergiu.pfx
DALSchema
Properties
BillsEntityLib
BillEntityClassDiagram.cd
BillsEntityLib.csproj.user
ciumac.sergiu.pfx
Properties
SetupBillPayManager
SetupBillPayManager.vdproj
TestBillPayManager
Properties
TestBillPayManager.csproj.user
TestBillPayManagerManual
AddBillManualTest.mht
DeleteBill.mht
Properties
Release.zip
en-US
BillPayManager.resources.dll
ro-RO
BillPayManager.resources.dll
ReleaseSetup.zip
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)

About the Author

Ciumac Sergiu
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.
Follow on   Twitter   LinkedIn

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 23 Apr 2010
Article Copyright 2010 by Ciumac Sergiu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid