C# help with testing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shani Aulakh
    New Member
    • Nov 2007
    • 44

    C# help with testing?

    Hi all,

    I have this piece of code that monitors a given folder and delete all files older that X number of days. I want to test it out on some sort of a dummy folder before i pass it to my team and they test it on real data. what do i do for that? first time doing this...Thanks for spending time!!

    Code:
    using System;
    using System.IO;
    using System.Windows.Forms;
    using System.Collections;
    
    namespace Files_ExtLib1
    {
    	/// <summary>
    	/// Summary description for FolderWatcher.
    	/// </summary>
    	public class FolderWatcher
    	{
            string m_FolderPath;
    
    		public FolderWatcher()
    		{
    			//
    			// TODO: Add constructor logic here
    			//
    		}
    
            #region Property
            public string FolderPath
            {
                get 
                {
                    return m_FolderPath;
                }
                set
                {
                    m_FolderPath = value;
                }
            }
            #endregion
    
            // Deletes the files older the days, hours, and minutes specified
            public int DeleteFilesOlderThan(int Days, int Hrs, int Mins)
            {
                string[] oldFilePaths = FilesOlderThan(Days, Hrs, Mins);
                int count = 0;
                foreach(string filePath in oldFilePaths)
                {
                     try
                     {
                        File.Delete(filePath);
                        count++;
                     }
                     catch
                     {
                        MessageBox.Show("Unable to delete " + filePath);
                     } 
                }
                // presumably number of files deleted to be returned
                return count;
    
            }
    
            // Reurns filenames having last modified time older than specified period.
            public string[] FilesOlderThan(int Days, int Hrs, int Mins)
            {
                TimeSpan ts = new TimeSpan(Days, Hrs, Mins, 0);
                ArrayList oldFilePaths = new ArrayList(); 
                // FolderPath is assumed to be path being watched
                string[] filePaths = Directory.GetFiles(FolderPath); 
                DateTime currentDate = DateTime.Now;
                foreach(string filePath in filePaths)
                {
                    DateTime lastWriteDate = File.GetLastWriteTime(filePath);
                    if ((currentDate - lastWriteDate) > ts)
                    {
                        oldFilePaths.Add(filePath);
                    }              
               }
             
               return (string[])oldFilePaths.ToArray(typeof(string));
           }
        }
    }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by Shani Aulakh
    Hi all,

    I have this piece of code that monitors a given folder and delete all files older that X number of days. I want to test it out on some sort of a dummy folder before i pass it to my team and they test it on real data. what do i do for that? first time doing this...Thanks for spending time!!
    ...
    What have you tried so far to test your code?
    Have you made Test Program to test it?
    Create a test folder and use this in your test program...your test program should call your code (each f unction) and check if the outcome was expected....if not then the code doesn't pass the test...

    Comment

    • Shani Aulakh
      New Member
      • Nov 2007
      • 44

      #3
      all i have done so far is: created a dummy folder and copied some files into it so i have a variety of last written files and i created some new files using notepad and changed laptop timing so that i have files with various creation date. now, what do i do? create a main entry point for the class 'FolderWatcher. cs' and test my methods. but how would i test the folder which i created with dummy files?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Shani Aulakh
        all i have done so far is: created a dummy folder and copied some files into it so i have a variety of last written files and i created some new files using notepad and changed laptop timing so that i have files with various creation date. now, what do i do? create a main entry point for the class 'FolderWatcher. cs' and test my methods. but how would i test the folder which i created with dummy files?
        You could manually watch it.
        Or you could write more code in your testing application to check the folder for you.
        It's up to you really.

        Comment

        • Shani Aulakh
          New Member
          • Nov 2007
          • 44

          #5
          i created the folder...but how do i test it with the class 'FolderWatcher. cs' :S i am confused....

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by Shani Aulakh
            i created the folder...but how do i test it with the class 'FolderWatcher. cs' :S i am confused....
            Create another project.

            Say, a desktop application.

            Include your FolderWatcher DLL (you will have to compile your cs to create the DLL).

            Put buttons on the form in the desktop application that are labeled DeleteFilesOlde rThan, FilesOlderThan( minutes)....

            Then in the code for these buttons create an Instance of a FolderWatcher and call the functions...you should test for invalid parameters as well as valid ones to make sure every thing's handled correctly.

            You could also look into using NUnit or some other automated black box testing tool...

            Comment

            • Shani Aulakh
              New Member
              • Nov 2007
              • 44

              #7
              okay okay will do that :) Thanks

              Comment

              • Shani Aulakh
                New Member
                • Nov 2007
                • 44

                #8
                I started a Windows Console Application project and wrote the following code in the main entry point

                Code:
                using System;
                using System.IO;
                using System.Collections.Generic;
                using System.Text;
                using Files_ExtLib1;
                
                
                namespace ConsoleApplication3
                {
                    class Program
                    {
                        static void Main(string[] args)
                        {
                            FolderWatcher fw = new FolderWatcher();
                            fw.FolderPath = @"c:\csharp"; // or whatever
                            int days = int.Parse(args[0]);
                            int hrs = int.Parse(args[1]);
                            int mins = int.Parse(args[2]);
                            fw.DeleteFilesOlderThan(days, hrs, mins);
                        }
                    }
                }
                For FolderWatcher.c s class: I started a Windows Class Library project and added the following code in it by giving it a name of 'Files_ExtLib1' the namespace i want to use.
                Code:
                using System;
                using System.IO;
                using System.Windows.Forms; // **Getting an error here ' the type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)
                using System.Collections;
                using System.Text;
                
                namespace Files_ExtLib1
                {
                    public class FolderWatcher
                    {
                        string m_FolderPath;
                
                        public FolderWatcher()
                        {
                            //
                            // TODO: Add constructor logic here
                            //
                        }
                
                        #region Property
                        public string FolderPath
                        {
                            get
                            {
                                return m_FolderPath;
                            }
                            set
                            {
                                m_FolderPath = value;
                            }
                        }
                        #endregion
                
                        /// <summary>
                        /// Deletes the files older the days, hours, and
                        /// minutes specified
                        /// </summary>
                        /// <param name="Days"></param>
                        /// <param name="Hrs"></param>
                        /// <param name="Mins"></param>
                        /// <returns>Number of files deleted</returns>
                        public int DeleteFilesOlderThan(int Days, int Hrs, int Mins)
                        {
                            string[] oldFilePaths = FilesOlderThan(Days, Hrs, Mins);
                            int count = 0;
                            foreach (string filePath in oldFilePaths)
                            {
                                try
                                {
                                    File.Delete(filePath);
                                    count++;
                                }
                                catch
                                {
                                    MessageBox.Show("Unable to delete " + filePath);
                                }
                            }
                            // presumably number of files deleted to be returned
                            return count;
                
                        }
                
                
                        /// <summary>
                        /// Reurns filenames having last modified time older than
                        /// specified period.
                        /// </summary>
                        /// <param name="Days"></param>
                        /// <param name="Hrs"></param>
                        /// <param name="Mins"></param>
                        /// <returns></returns>
                        public string[] FilesOlderThan(int Days, int Hrs, int Mins)
                        {
                            TimeSpan ts = new TimeSpan(Days, Hrs, Mins, 0);
                            ArrayList oldFilePaths = new ArrayList();
                            // FolderPath is assumed to be path being watched
                            string[] filePaths = Directory.GetFiles(FolderPath);
                            DateTime currentDate = DateTime.Now;
                            foreach (string filePath in filePaths)
                            {
                                DateTime lastWriteDate = File.GetLastWriteTime(filePath);
                                if ((currentDate - lastWriteDate) > ts)
                                {
                                    oldFilePaths.Add(filePath);
                                }
                            }
                
                            return (string[])oldFilePaths.ToArray(typeof(string));
                        }
                    }
                }
                Then i build the solution for 'Files_ExtLib1' to create a dll. den i went back to my console application and add Files_ExtLib1 in the references and in the using namespace. when i compile the application i get the following error 'IndexOutOfRang eException was unhandled'...? how do i correct this. Thanks for spending time!!!!

                Comment

                • Shani Aulakh
                  New Member
                  • Nov 2007
                  • 44

                  #9
                  Nvm......made it work......just wasn't passing arguments as command line

                  Comment

                  Working...