From 60d598f2c1d038fb1092feea2aa4712502a54e32 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 13 Nov 2011 11:15:40 -0800 Subject: [PATCH] More update tests --- .gitignore | 3 +- NzbDrone.Test.Common/TestBase.cs | 8 +- .../UpdateProviderStartTest.cs | 160 ++++++++++++------ 3 files changed, 119 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index f67b7748f..11bfc37d5 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,5 @@ NzbDrone.Web/NzbDrone.Web.Publish.xml [Bb]anners *.orig _rawPackage/ -NzbDrone.zip \ No newline at end of file +NzbDrone.zip +NzbDrone.sln.DotSettings.user* \ No newline at end of file diff --git a/NzbDrone.Test.Common/TestBase.cs b/NzbDrone.Test.Common/TestBase.cs index ba3bba103..3c919270a 100644 --- a/NzbDrone.Test.Common/TestBase.cs +++ b/NzbDrone.Test.Common/TestBase.cs @@ -24,7 +24,7 @@ namespace NzbDrone.Test.Common } [SetUp] - public virtual void TestBaseSetup() + public void TestBaseSetup() { if (Directory.Exists(TempFolder)) { @@ -36,6 +36,12 @@ namespace NzbDrone.Test.Common Mocker = new AutoMoqer(); } + [TearDown] + public void TestBaseTearDown() + { + Mocker.VerifyAllMocks(); + } + protected virtual void WithStrictMocker() { Mocker = new AutoMoqer(MockBehavior.Strict); diff --git a/NzbDrone.Update.Test/UpdateProviderStartTest.cs b/NzbDrone.Update.Test/UpdateProviderStartTest.cs index 36805695b..a83e68815 100644 --- a/NzbDrone.Update.Test/UpdateProviderStartTest.cs +++ b/NzbDrone.Update.Test/UpdateProviderStartTest.cs @@ -1,5 +1,5 @@ -using System.IO; -using AutoMoq; +using System.Collections.Generic; +using System.IO; using FizzWare.NBuilder; using Moq; using NUnit.Framework; @@ -13,8 +13,6 @@ namespace NzbDrone.Update.Test [TestFixture] class UpdateProviderStartTest : TestBase { - AutoMoqer mocker = new AutoMoqer(); - private const string UPDATE_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone\"; private const string BACKUP_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone_backup\"; private const string TARGET_FOLDER = @"C:\NzbDrone\"; @@ -25,39 +23,53 @@ namespace NzbDrone.Update.Test [SetUp] public void Setup() { - mocker = new AutoMoqer(); - - _enviromentProvider = mocker.GetMock(); + + _enviromentProvider = Mocker.GetMock(); _enviromentProvider.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); - mocker.GetMock() + Mocker.GetMock() .Setup(c => c.FolderExists(UPDATE_FOLDER)) .Returns(true); - mocker.GetMock() + Mocker.GetMock() .Setup(c => c.FolderExists(TARGET_FOLDER)) .Returns(true); } - public void WithInstalledService() + private void WithInstalledService() { - mocker.GetMock() + Mocker.GetMock() .Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)) .Returns(true); } + private void WithServiceRunning(bool state) + { + Mocker.GetMock() + .Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(state); + } + [Test] public void should_stop_nzbdrone_service_if_installed() { WithInstalledService(); //Act - mocker.Resolve().Start(TARGET_FOLDER); + Mocker.Resolve().Start(TARGET_FOLDER); //Assert - mocker.GetMock().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); - mocker.VerifyAllMocks(); + Mocker.GetMock().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); + } + + [Test] + public void should_not_stop_nzbdrone_service_if_service_isnt_installed() + { + //Act + Mocker.Resolve().Start(TARGET_FOLDER); + + //Assert + Mocker.GetMock().Verify(c => c.Stop(It.IsAny()), Times.Never()); } [Test] @@ -65,96 +77,144 @@ namespace NzbDrone.Update.Test { var proccesses = Builder.CreateListOfSize(2).Build(); - mocker.GetMock() + Mocker.GetMock() .Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName)) .Returns(proccesses); //Act - mocker.Resolve().Start(TARGET_FOLDER); + Mocker.Resolve().Start(TARGET_FOLDER); //Assert - mocker.GetMock().Verify(c => c.Kill(proccesses[0].Id), Times.Once()); - mocker.GetMock().Verify(c => c.Kill(proccesses[1].Id), Times.Once()); - mocker.VerifyAllMocks(); + Mocker.GetMock().Verify(c => c.Kill(proccesses[0].Id), Times.Once()); + Mocker.GetMock().Verify(c => c.Kill(proccesses[1].Id), Times.Once()); + } + + [Test] + public void should_not_kill_nzbdrone_process_not_running() + { + Mocker.GetMock() + .Setup(c => c.GetProcessByName(ProcessProvider.NzbDroneProccessName)) + .Returns(new List()); + + //Act + Mocker.Resolve().Start(TARGET_FOLDER); + + //Assert + Mocker.GetMock().Verify(c => c.Kill(It.IsAny()), Times.Never()); } [Test] public void should_create_backup_of_current_installation() { - var diskprovider = mocker.GetMock() + Mocker.GetMock() .Setup(c => c.CopyDirectory(TARGET_FOLDER, BACKUP_FOLDER)); - mocker.Resolve().Start(TARGET_FOLDER); - - mocker.VerifyAllMocks(); + Mocker.Resolve().Start(TARGET_FOLDER); } [Test] public void should_copy_update_package_to_target() { - var diskprovider = mocker.GetMock() + Mocker.GetMock() .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)); - mocker.Resolve().Start(TARGET_FOLDER); - - mocker.VerifyAllMocks(); + Mocker.Resolve().Start(TARGET_FOLDER); } [Test] public void should_restore_if_update_fails() { - var diskprovider = mocker.GetMock(); - diskprovider.Setup(c => c.CopyDirectory(BACKUP_FOLDER, TARGET_FOLDER)); - - diskprovider.Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)).Throws(new IOException()); + Mocker.GetMock() + .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)) + .Throws(new IOException()); - mocker.Resolve().Start(TARGET_FOLDER); + //Act + Mocker.Resolve().Start(TARGET_FOLDER); + //Assert + Mocker.GetMock() + .Verify(c => c.CopyDirectory(BACKUP_FOLDER, TARGET_FOLDER), Times.Once()); ExceptionVerification.ExcpectedFatals(1); - - mocker.VerifyAllMocks(); } [Test] public void should_restart_service_if_service_was_running() { WithInstalledService(); + WithServiceRunning(true); - var serviceProvider = mocker.GetMock(); + //Act + Mocker.Resolve().Start(TARGET_FOLDER); - serviceProvider.Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(true); + //Assert + VerifyServiceRestart(); + } + + [Test] + public void should_restart_process_if_service_was_not_running() + { + WithInstalledService(); + WithServiceRunning(false); //Act - mocker.Resolve().Start(TARGET_FOLDER); + Mocker.Resolve().Start(TARGET_FOLDER); //Assert - serviceProvider - .Verify(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); - - mocker.VerifyAllMocks(); + VerifyProcessRestart(); } [Test] - public void should_not_restart_service_if_service_was_not_running() + public void should_restart_service_if_service_was_running_and_update_fails() { WithInstalledService(); + WithServiceRunning(true); + + Mocker.GetMock() + .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)) + .Throws(new IOException()); + + //Act + Mocker.Resolve().Start(TARGET_FOLDER); + + //Assert + VerifyServiceRestart(); + ExceptionVerification.ExcpectedFatals(1); + } - var serviceProvider = mocker.GetMock(); + [Test] + public void should_restart_process_if_service_was_not_running_and_update_fails() + { + WithInstalledService(); + WithServiceRunning(false); - serviceProvider.Setup(c => c.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)) - .Returns(false); + Mocker.GetMock() + .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)) + .Throws(new IOException()); //Act - mocker.Resolve().Start(TARGET_FOLDER); + Mocker.Resolve().Start(TARGET_FOLDER); //Assert - mocker.GetMock() - .Verify(c => c.Start(TARGET_FOLDER + "nzbdrone.exe"), Times.Once()); + VerifyProcessRestart(); + ExceptionVerification.ExcpectedFatals(1); + } + + private void VerifyServiceRestart() + { + Mocker.GetMock() + .Verify(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); - serviceProvider + Mocker.GetMock() .Verify(c => c.Start(It.IsAny()), Times.Never()); + } - mocker.VerifyAllMocks(); + private void VerifyProcessRestart() + { + Mocker.GetMock() + .Verify(c => c.Start(It.IsAny()), Times.Never()); + + Mocker.GetMock() + .Verify(c => c.Start(TARGET_FOLDER + "nzbdrone.exe"), Times.Once()); } } }