ProcessP4InitializerTest.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » UnitTests » Core » Sourcecontrol » Perforce » 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 » Sourcecontrol » Perforce » ProcessP4InitializerTest.cs
using System.Net;
using NMock;
using NUnit.Framework;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Core.Sourcecontrol.Perforce;
using ThoughtWorks.CruiseControl.Core.Util;

namespace ThoughtWorks.CruiseControl.UnitTests.Core.Sourcecontrol.Perforce{
  [TestFixture]
  public class ProcessP4InitializerTest
  {
    private DynamicMock processExecutorMock;
    private DynamicMock processInfoCreatorMock;
    private ProcessP4Initializer p4Initializer;

    [SetUp]
    public void Setup()
    {
      processExecutorMock = new DynamicMock(typeof(ProcessExecutor));
      processInfoCreatorMock = new DynamicMock(typeof(IP4ProcessInfoCreator));
      p4Initializer = new ProcessP4Initializer((ProcessExecutor) processExecutorMock.MockInstance,  (IP4ProcessInfoCreator) processInfoCreatorMock.MockInstance);
    }

    private void VerifyAll()
    {
      processExecutorMock.Verify();
      processInfoCreatorMock.Verify();
    }

    [Test]
    public void CreatesAClientWithGivenClientNameIfSpecified()
    {
      // Setup
      DynamicMock p4Mock = new DynamicMock(typeof(P4));
      P4 p4 = (P4) p4Mock.MockInstance;
      p4.Client = "myClient";

      p4Mock.SetupResult("ViewForSpecifications", new string[] { "//mydepot/...", "//myotherdepot/..." });

      ProcessInfo processInfo = new ProcessInfo("createclient");
      ProcessInfo processInfoWithStdInContent = new ProcessInfo("createclient");
      processInfoWithStdInContent.StandardInputContent = "Client: myClient\n\nRoot:   c:\\my\\working\\dir\n\nView:\n //mydepot/... //myClient/mydepot/...\n //myotherdepot/... //myClient/myotherdepot/...\n";

      processInfoCreatorMock.ExpectAndReturn("CreateProcessInfo", processInfo, p4, "client -i");
      processExecutorMock.ExpectAndReturn("Execute", new ProcessResult("", "", 0, false), processInfoWithStdInContent);

      // Execute
      p4Initializer.Initialize(p4, "myProject", @"c:\my\working\dir");

      // Verify
      p4Mock.Verify();
      VerifyAll();
    }

    [Test]
    public void CreatesAClientWithConstructedClientNameIfOneNotSpecifiedAndSavesClientNameInConfig()
    {
      // Setup
      P4 p4 = new P4();
      p4.View = "//mydepot/...";
      string projectName = "myProject";

      string expectedClientName = string.Format("CCNet-{0}-{1}", Dns.GetHostName(), projectName);

      ProcessInfo processInfo = new ProcessInfo("createclient");
      ProcessInfo processInfoWithStdInContent = new ProcessInfo("createclient");
      processInfoWithStdInContent.StandardInputContent = string.Format("Client: {0}\n\nRoot:   c:\\my\\working\\dir\n\nView:\n //mydepot/... //{0}/mydepot/...\n", expectedClientName);

      processInfoCreatorMock.ExpectAndReturn("CreateProcessInfo", processInfo, p4, "client -i");
      processExecutorMock.ExpectAndReturn("Execute", new ProcessResult("", "", 0, false), processInfoWithStdInContent);

      // Execute
      p4Initializer.Initialize(p4, projectName, @"c:\my\working\dir");

      // Verify
      Assert.AreEqual(expectedClientName, p4.Client);
      VerifyAll();
    }

    [Test]
    public void ShouldCheckToSeeWorkingDirectoryIsAnAbsolutePath()
    {
      P4 p4 = new P4();
      try
      {
        p4Initializer.Initialize(p4, "myProject", "thisIsNotAnAbsoluteDirectory");
        Assert.Fail("Should check for non absolute working directory");
      }
      catch (CruiseControlException e)
      {
        Assert.IsTrue(e.Message.ToLower().IndexOf("absolute path") > -1, "Should mention something about an absolute directory");
      }

      VerifyAll();
    }

    [Test]
    public void ShouldCheckViewIsValid()
    {
      P4 p4 = new P4();
      p4.View = "ThisIsNotAValidView";
      try
      {
        p4Initializer.Initialize(p4, "myProject", @"c:\my\working\dir");
        Assert.Fail("Should check for a valid view");
      }
      catch (CruiseControlException e)
      {
        Assert.IsTrue(e.Message.ToLower().IndexOf("valid view") > -1, "Should mention something about a valid view");
      }
      VerifyAll();
    }

    // CCNET-174
    [Test]
    public void ShouldAllowViewsWithSpaces()
    {
      P4 p4 = new P4();
      p4.View = @"""//blah/my path with spaces/...""";
      processInfoCreatorMock.SetupResult("CreateProcessInfo", new ProcessInfo(""), typeof(P4), typeof(string));
      processExecutorMock.SetupResult("Execute", new ProcessResult("", "", 0, false), typeof(ProcessInfo));

      p4Initializer.Initialize(p4, "myProject", @"c:\my\working\dir");
      VerifyAll();
    }

    [Test]
    public void ShouldThrowExceptionIfProcessFails()
    {
      // Setup
      P4 p4 = new P4();
      p4.View = "//mydepot/...";
      string projectName = "myProject";

      string expectedClientName = string.Format("CCNet-{0}-{1}", Dns.GetHostName(), projectName);

      ProcessInfo processInfo = new ProcessInfo("createclient");
      ProcessInfo processInfoWithStdInContent = new ProcessInfo("createclient");
      processInfoWithStdInContent.StandardInputContent = string.Format("Client: {0}\n\nRoot:   c:\\my\\working\\dir\n\nView:\n //mydepot/... //{0}/mydepot/...\n", expectedClientName);

      processInfoCreatorMock.ExpectAndReturn("CreateProcessInfo", processInfo, p4, "client -i");
      processExecutorMock.ExpectAndReturn("Execute", new ProcessResult("This is standard out", "This is standard error", 1, false), processInfoWithStdInContent);

      // Execute
      try
      {
        p4Initializer.Initialize(p4, projectName, @"c:\my\working\dir");
        Assert.Fail("Should throw an exception since process result has a non zero exit code");
      }
      catch (CruiseControlException e)
      {
        Assert.IsTrue(e.Message.IndexOf("This is standard out") > -1);
        Assert.IsTrue(e.Message.IndexOf("This is standard error") > -1);
      }

      // Verify
      Assert.AreEqual(expectedClientName, p4.Client);
      VerifyAll();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.