public void ShouldReturnCalculatedWorkingDirectoryIfOneIsNotSet()
        {
            // Setup
            project.Name = "myProject";

            // Execute & Verify
            Assert.AreEqual(
                Path.Combine(executionEnvironment.GetDefaultProgramDataFolder(ApplicationType.Server),
                             Path.Combine("myProject", "WorkingDirectory")),
                project.WorkingDirectory);
        }
        public void LoadShouldThrowExceptionIfStateFileDoesNotExist()
        {
            Expect.Call(executionEnvironment.GetDefaultProgramDataFolder(ApplicationType.Server)).IgnoreArguments().Constraints(Rhino.Mocks.Constraints.Is.NotNull()).Return(applicationDataPath);
            Expect.Call(delegate { fileSystem.EnsureFolderExists(applicationDataPath); });
            Expect.Call(fileSystem.Load(null)).IgnoreArguments().Constraints(Rhino.Mocks.Constraints.Is.NotNull()).Throw(new FileNotFoundException());
            mocks.ReplayAll();

            state              = new FileStateManager(fileSystem, executionEnvironment);
            result             = IntegrationResultMother.CreateSuccessful();
            result.ProjectName = ProjectName;

            Assert.That(delegate { state.LoadState(ProjectName); },
                        Throws.TypeOf <CruiseControlException>().With.Property("InnerException").TypeOf <FileNotFoundException>());
        }
        public void SaveToNonExistingFolder()
        {
            string newDirectory = Directory.GetCurrentDirectory() + "\\NewDirectory";

            Assert.IsFalse(Directory.Exists(newDirectory), "The test directory should not exist");

            Expect.Call(executionEnvironment.GetDefaultProgramDataFolder(ApplicationType.Server)).IgnoreArguments().
            Constraints(Rhino.Mocks.Constraints.Is.NotNull()).Return(applicationDataPath);
            Expect.Call(delegate { fileSystem.EnsureGivenFolderExists(newDirectory); });
            Expect.Call(delegate { fileSystem.AtomicSave(string.Empty, string.Empty); }).IgnoreArguments().Constraints(
                Rhino.Mocks.Constraints.Is.NotNull(), Rhino.Mocks.Constraints.Is.Anything());
            mocks.ReplayAll();

            state = new FileStateManager(fileSystem, executionEnvironment);
            state.StateFileDirectory = newDirectory;
            result             = IntegrationResultMother.CreateSuccessful();
            result.ProjectName = "my project";
            state.SaveState(result);
        }
        protected void SetUp()
        {
            projectSerializerMock = new DynamicMock(typeof(IProjectSerializer));

            integratorMock1 = new DynamicMock(typeof(IProjectIntegrator));
            integratorMock2 = new DynamicMock(typeof(IProjectIntegrator));
            integratorMock3 = new DynamicMock(typeof(IProjectIntegrator));
            integrator1     = (IProjectIntegrator)integratorMock1.MockInstance;
            integrator2     = (IProjectIntegrator)integratorMock2.MockInstance;
            integrator3     = (IProjectIntegrator)integratorMock3.MockInstance;
            integratorMock1.SetupResult("Name", "Project 1");
            integratorMock2.SetupResult("Name", "Project 2");
            integratorMock3.SetupResult("Name", "Project 3");

            fileSystem           = mocks.DynamicMock <IFileSystem>();
            executionEnvironment = mocks.DynamicMock <IExecutionEnvironment>();

            SetupResult.For(executionEnvironment.IsRunningOnWindows).Return(true);
            SetupResult.For(executionEnvironment.GetDefaultProgramDataFolder(ApplicationType.Server)).Return(applicationDataPath);
            SetupResult.For(fileSystem.DirectoryExists(applicationDataPath)).Return(true);
            mocks.ReplayAll();

            integrationQueue = null;             // We have no way of injecting currently.

            configuration = new Configuration();
            project1      = new Project();
            project1.Name = "Project 1";
            integratorMock1.SetupResult("Project", project1);

            project2      = new Project();
            project2.Name = "Project 2";
            integratorMock2.SetupResult("Project", project1);

            mockProject = new DynamicMock(typeof(IProject));
            mockProject.SetupResult("Name", "Project 3");
            mockProject.SetupResult("QueueName", "Project 3");
            mockProject.SetupResult("QueueName", "Project 3");
            mockProjectInstance = (IProject)mockProject.MockInstance;
            mockProject.SetupResult("Name", "Project 3");
            mockProject.SetupResult("StartupMode", ProjectStartupMode.UseLastState);
            integratorMock3.SetupResult("Project", mockProjectInstance);

            configuration.AddProject(project1);
            configuration.AddProject(project2);
            configuration.AddProject(mockProjectInstance);

            integratorList = new ProjectIntegratorList();
            integratorList.Add(integrator1);
            integratorList.Add(integrator2);
            integratorList.Add(integrator3);

            configServiceMock = new DynamicMock(typeof(IConfigurationService));
            configServiceMock.ExpectAndReturn("Load", configuration);

            projectIntegratorListFactoryMock = new DynamicMock(typeof(IProjectIntegratorListFactory));
            projectIntegratorListFactoryMock.ExpectAndReturn("CreateProjectIntegrators", integratorList, configuration.Projects, integrationQueue);

            stateManagerMock = new DynamicMock(typeof(IProjectStateManager));
            stateManagerMock.SetupResult("CheckIfProjectCanStart", true, typeof(string));

            server = new CruiseServer((IConfigurationService)configServiceMock.MockInstance,
                                      (IProjectIntegratorListFactory)projectIntegratorListFactoryMock.MockInstance,
                                      (IProjectSerializer)projectSerializerMock.MockInstance,
                                      (IProjectStateManager)stateManagerMock.MockInstance,
                                      fileSystem,
                                      executionEnvironment,
                                      null);
        }
		protected void SetUp()
		{
			projectSerializerMock = new DynamicMock(typeof (IProjectSerializer));

			integratorMock1 = new DynamicMock(typeof (IProjectIntegrator));
			integratorMock2 = new DynamicMock(typeof (IProjectIntegrator));
			integratorMock3 = new DynamicMock(typeof (IProjectIntegrator));
			integrator1 = (IProjectIntegrator) integratorMock1.MockInstance;
			integrator2 = (IProjectIntegrator) integratorMock2.MockInstance;
			integrator3 = (IProjectIntegrator) integratorMock3.MockInstance;
            integratorMock1.SetupResult("Name", "Project 1");
			integratorMock2.SetupResult("Name", "Project 2");
			integratorMock3.SetupResult("Name", "Project 3");

			fileSystem = mocks.DynamicMock<IFileSystem>();
			executionEnvironment = mocks.DynamicMock<IExecutionEnvironment>();

			SetupResult.For(executionEnvironment.IsRunningOnWindows).Return(true);
			SetupResult.For(executionEnvironment.GetDefaultProgramDataFolder(ApplicationType.Server)).Return(applicationDataPath);
			SetupResult.For(fileSystem.DirectoryExists(applicationDataPath)).Return(true);
			mocks.ReplayAll();

			integrationQueue = null; // We have no way of injecting currently.

			configuration = new Configuration();
			project1 = new Project();
			project1.Name = "Project 1";
            integratorMock1.SetupResult("Project", project1);
			
			project2 = new Project();
			project2.Name = "Project 2";
            integratorMock2.SetupResult("Project", project1);

			mockProject = new DynamicMock(typeof(IProject));
			mockProject.SetupResult("Name", "Project 3");
            mockProject.SetupResult("QueueName", "Project 3");
            mockProject.SetupResult("QueueName", "Project 3");
            mockProjectInstance = (IProject)mockProject.MockInstance;
			mockProject.SetupResult("Name", "Project 3");
            mockProject.SetupResult("StartupMode", ProjectStartupMode.UseLastState);
            integratorMock3.SetupResult("Project", mockProjectInstance);

			configuration.AddProject(project1);
			configuration.AddProject(project2);
			configuration.AddProject(mockProjectInstance);

			integratorList = new ProjectIntegratorList();
			integratorList.Add(integrator1);
			integratorList.Add(integrator2);
			integratorList.Add(integrator3);
            
			configServiceMock = new DynamicMock(typeof (IConfigurationService));
			configServiceMock.ExpectAndReturn("Load", configuration);

			projectIntegratorListFactoryMock = new DynamicMock(typeof (IProjectIntegratorListFactory));
			projectIntegratorListFactoryMock.ExpectAndReturn("CreateProjectIntegrators", integratorList, configuration.Projects, integrationQueue);

            stateManagerMock = new DynamicMock(typeof(IProjectStateManager));
            stateManagerMock.SetupResult("CheckIfProjectCanStart", true, typeof(string));

			server = new CruiseServer((IConfigurationService) configServiceMock.MockInstance,
			                          (IProjectIntegratorListFactory) projectIntegratorListFactoryMock.MockInstance,
			                          (IProjectSerializer) projectSerializerMock.MockInstance,
                                      (IProjectStateManager)stateManagerMock.MockInstance,
									  fileSystem,
									  executionEnvironment,
                                      null);
		}
        public void DefaultConstructorSetsPersistanceLocation()
        {
            // This is an indirect test - if the correct location is set, then the FileExists call
            // will be valid
            Expect.Call(executionEnvironment.GetDefaultProgramDataFolder(ApplicationType.Server)).IgnoreArguments().Constraints(Rhino.Mocks.Constraints.Is.NotNull()).Return(applicationDataPath);
            Expect.Call(delegate { fileSystem.EnsureFolderExists(applicationDataPath); }).IgnoreArguments().Constraints(Rhino.Mocks.Constraints.Is.NotNull());
            Expect.Call(fileSystem.FileExists(persistanceFilePath)).Return(false);
            mocks.ReplayAll();

            stateManager = new XmlProjectStateManager(fileSystem, executionEnvironment);
            stateManager.CheckIfProjectCanStart("Project");
            mocks.VerifyAll();
        }