LogFileTest.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » UnitTests » Core » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » UnitTests » Core » LogFileTest.cs
using System;
using NUnit.Framework;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Core.Util;
using ThoughtWorks.CruiseControl.Remote;

namespace ThoughtWorks.CruiseControl.UnitTests.Core{
  [TestFixture]
  public class LogFileTest : CustomAssertion
  {
    [Test]
    public void ParseDateFromFilename()
    {
      DateTime date = new DateTime(2002, 3, 28, 13, 0, 0);
      Assert.AreEqual(date, new LogFile("log20020328130000.xml").Date);
    }
    
    [Test]
    public void VerifyFormattedDateString()
    {
      DateTime date = new DateTime(1971, 5, 14, 15, 0, 0);
      string actual = new LogFile("log19710514150000.xml").FormattedDateString;
      string expected = DateUtil.FormatDate(date);
      Assert.AreEqual(expected, actual);
    }
    
    [Test]
    public void ParseForFileFormattedDateString()
    {
      CheckDateString("19741224120000", "log19741224120000.xml");
      CheckDateString("19750101120000","log19750101120000.xml");
      CheckDateString("20020830164057","log20020830164057Lbuild.6.xml");
    }
    
    [Test]
    public void GetLatestBuildNumber()
    {
      string[] filenames = new string[]{
                         "log19710514150000.xml",
                         "log19710514150001.xml",
                         "log20020830164057Lbuild.9.xml",
                         "log19710514150002.xml",
                         "log20020830164057Lbuild.7.xml",
                         "log19710514150003.xml",
                         "log20020830164057Lbuild.6.xml",
                         "log20020830164057Lbuild.8.xml",
                         "log19710514150004.xml"
                       };
      int actual = LogFileUtil.GetLatestBuildNumber(filenames);
      Assert.AreEqual(9, actual);
    }

    [Test]
    public void GetLatestBuildNumberHandlesString()
    {
      int actual = LogFileUtil.GetLatestBuildNumber(new string[] {"log20020830164057Lbuild.v1.1.8.xml"});
      Assert.AreEqual(118, actual);
    }

    [Test]
    public void ListFiles() 
    {
      // testFilenames array must be in sorted order -- otherwise links iteration will fail
      string[] testFilenames = {"log123.xml", "log200.xml", "logfile.txt", 
                     "log20020830164057Lbuild.6.xml", "badfile.xml" };
      
      using (TempDirectory tempPath = new TempDirectory())
      {
        CreateTempFiles(tempPath, testFilenames);
        string[] fileNames = LogFileUtil.GetLogFileNames(tempPath.ToString());
        Assert.AreEqual(3,fileNames.Length);
        Assert.AreEqual(testFilenames[0],fileNames[0]);
        Assert.AreEqual(testFilenames[1],fileNames[1]);
      }
    }
    
    [Test]
    public void GetLastLogFileName()
    {
      string[] testFilenames = {"log123.xml", "log200.xml", "logfile.txt", 
                     "log20010830164057Lbuild.6.xml", 
                     "log20011230164057Lbuild.8.xml", 
                     "log20010430164057Lbuild.7.xml", 
                     "badfile.xml" };
      using (TempDirectory tempPath = new TempDirectory())
      {
        CreateTempFiles(tempPath, testFilenames);
        string logfile = LogFileUtil.GetLatestLogFileName(tempPath.ToString());
        Assert.AreEqual("log20011230164057Lbuild.8.xml", logfile);
      }
    }

    [Test]
    public void GetLastLogFileName_UnknownPath()
    {
      string logfile = LogFileUtil.GetLatestLogFileName(@"c:\non\exi\stent");
      Assert.IsNull(logfile);
    }
    
    [Test]
    public void GetLastLogFileName_EmptyFolder()
    {
      using (TempDirectory tempDir = new TempDirectory())
      {
        Assert.IsNull(LogFileUtil.GetLatestLogFileName(tempDir.ToString()));
      }
    }

    [Test]
    public void GetLastBuildDate()
    {
      string[] testFilenames = {"log123.xml", "log200.xml", "logfile.txt", 
                     "log20010830164057Lbuild.6.xml", 
                     "log20011230164057Lbuild.6.xml", 
                     "log20010430164057Lbuild.6.xml", 
                     "badfile.xml" };
      using (TempDirectory tempPath = new TempDirectory())
      {
        CreateTempFiles(tempPath, testFilenames);
        DateTime expected = new DateTime(2001,12,30,16,40,57);
        DateTime actual = LogFileUtil.GetLastBuildDate(tempPath.ToString(), new DateTime());
        Assert.AreEqual(expected, actual);
      }
    }

    private void CreateTempFiles(SystemPath path, string[] filenames)
    {
      foreach (string filename in filenames)
      {
        path.CreateEmptyFile(filename);
      }
    }

    [Test]
    public void GetLastBuildDate_NoDirectory()
    {
      Assert.AreEqual(new DateTime(), LogFileUtil.GetLastBuildDate(@"c:\non\exi\stent", new DateTime()));
    }

    [Test]
    public void GetLastBuildDate_NoFiles()
    {
      using (TempDirectory tempDir = new TempDirectory())
      {        
        Assert.AreEqual(new DateTime(), LogFileUtil.GetLastBuildDate(tempDir.ToString(), new DateTime()));
      }
    }

    [Test]
    public void GetLatestBuildNumberWithMissingPath()
    {
      Assert.AreEqual(0, LogFileUtil.GetLatestBuildNumber(@"c:\non\exi\stent"));
    }
    
    private void CheckDateString(string expected, string filename)
    {
      string actual = new LogFile(filename).FilenameFormattedDateString;
      Assert.AreEqual(expected, actual);
    }
    
    [Test]
    public void AttemptToCreateLogFileForFilenameWithWrongPrefix()
    {
            Assert.That(delegate { new LogFile("garbage.txt"); },
                        Throws.TypeOf<ArgumentException>().With.Property("ParamName").EqualTo("filename"));
    }
    
    [Test]
    public void AttemptToCreateLogFileForFilenameWithoutDate()
    {
      Assert.That(delegate { new LogFile("log3.xml"); },
                        Throws.TypeOf<ArgumentException>().With.Property("ParamName").EqualTo("filename"));
    }
    
    [Test]
    public void BuildSuccessful()
    {
      Assert.IsTrue(!new LogFile("log19750101120000.xml").Succeeded);
      Assert.IsTrue(new LogFile("log20020830164057Lbuild.6.xml").Succeeded);
    }
    
    [Test]
    public void CreateFileNameNoBuildNumber()
    {
      DateTime date = new DateTime(2002, 3, 28, 13, 0, 0);
      string expected = "log20020328130000.xml";
      IntegrationResult result = new IntegrationResult();
      result.StartTime = date;
      Assert.AreEqual(expected, new LogFile(result).Filename);
    }
    
    [Test]
    public void CreateFileNameWithBuildNumber()
    {
      DateTime date = new DateTime(2002, 3, 28, 13, 0, 0);
      string expected = "log20020328130000Lbuild.33.xml";
      IntegrationResult result = new IntegrationResult();
      result.StartTime = date;
      result.Label = "33";
      result.Status = IntegrationStatus.Success;
      Assert.AreEqual(expected, new LogFile(result).Filename);
    }

    [Test]
    public void CreateUrl()
    {
      string expected = "?log=log20020222120000Lbuild.0.xml";
      string actual = LogFileUtil.CreateUrl(CreateIntegrationResult(IntegrationStatus.Success, new DateTime(2002, 02, 22, 12, 00, 00)));
      Assert.AreEqual(expected, actual);
    }

    private IntegrationResult CreateIntegrationResult(IntegrationStatus status, DateTime lastModifiedDate)
    {
      IntegrationResult result = new IntegrationResult();
      result.StartTime = lastModifiedDate;
      result.Status = status;
      result.Label = "0";
      result.Modifications = new Modification[1];
      result.Modifications[0] = new Modification();
      result.Modifications[0].ModifiedTime = lastModifiedDate;
      return result;
    }

    [Test]
    public void CreateUrl_FailedBuild()
    {
      string expected = "?log=log20020222120000.xml";
      string actual = LogFileUtil.CreateUrl(CreateIntegrationResult(IntegrationStatus.Failure, new DateTime(2002, 02, 22, 12, 00, 00)));
      Assert.AreEqual(expected, actual);
    }

    [Test]
    public void CreateUrl_givenFilename()
    {
      string filename = "log20020222120000Lbuild.0.xml";
      string expected = "?log=log20020222120000Lbuild.0.xml";
      string actual = LogFileUtil.CreateUrl(filename);        
      Assert.AreEqual(expected, actual);
    }

    [Test]
    public void CreateUrlWithGivenFilenameAndProjectName()
    {
      string filename = "log20020222120000Lbuild.0.xml";
      string expected = "?log=log20020222120000Lbuild.0.xml&project=myproject";
      string actual = LogFileUtil.CreateUrl(filename, "myproject");        
      Assert.AreEqual(expected, actual);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.