From 633f0b619731b3a5b647b718b95e005f0234203c Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Wed, 2 Nov 2011 22:04:14 -0700 Subject: [PATCH] PathProvider. visit us for all of your pathing needs. --- NzbDrone.App.Test/Fixtures.cs | 2 +- .../EnviromentProviderTest.cs | 8 -- NzbDrone.Common.Test/Fixtures.cs | 2 +- NzbDrone.Common/EnviromentProvider.cs | 35 +----- NzbDrone.Common/NzbDrone.Common.csproj | 1 + NzbDrone.Common/PathProvider.cs | 111 ++++++++++++++++++ NzbDrone.Core.Test/Framework/Fixtures.cs | 36 +++--- NzbDrone.Core.Test/Framework/TestBase.cs | 49 +++++++- .../JobTests/BannerDownloadJobTest.cs | 23 ++-- .../ProviderTests/ConfigFileProviderTest.cs | 53 ++++----- .../ProviderTests/TvDbProviderTest.cs | 30 ++--- .../PreformUpdateFixture.cs | 12 +- NzbDrone.Core/CentralDispatch.cs | 12 +- NzbDrone.Core/Datastore/Connection.cs | 37 ++++-- .../Instrumentation/LogConfiguration.cs | 12 +- .../Providers/Core/ConfigFileProvider.cs | 21 ++-- .../Providers/Jobs/BannerDownloadJob.cs | 12 +- NzbDrone.Core/Providers/TvDbProvider.cs | 8 +- NzbDrone.Core/Providers/UpdateProvider.cs | 12 +- .../{LoggingFixtures.cs => LoggingTest.cs} | 6 +- .../NzbDrone.Test.Common.csproj | 2 +- NzbDrone.Update.Test/Fixtures.cs | 2 +- NzbDrone.Web/Global.asax.cs | 11 +- config.xml | 2 + 24 files changed, 313 insertions(+), 186 deletions(-) create mode 100644 NzbDrone.Common/PathProvider.cs rename NzbDrone.Test.Common/{LoggingFixtures.cs => LoggingTest.cs} (88%) create mode 100644 config.xml diff --git a/NzbDrone.App.Test/Fixtures.cs b/NzbDrone.App.Test/Fixtures.cs index b3a5640d5..4089933e0 100644 --- a/NzbDrone.App.Test/Fixtures.cs +++ b/NzbDrone.App.Test/Fixtures.cs @@ -3,6 +3,6 @@ using NUnit.Framework; using NzbDrone.Test.Common; [SetUpFixture] -public class Fixtures : LoggingFixtures +public class Fixtures : LoggingTest { } diff --git a/NzbDrone.Common.Test/EnviromentProviderTest.cs b/NzbDrone.Common.Test/EnviromentProviderTest.cs index b4d7d6c3d..68da7c761 100644 --- a/NzbDrone.Common.Test/EnviromentProviderTest.cs +++ b/NzbDrone.Common.Test/EnviromentProviderTest.cs @@ -17,14 +17,6 @@ namespace NzbDrone.Common.Test enviromentController.IsUserInteractive.Should().BeTrue(); } - [Test] - public void Log_path_should_not_be_empty() - { - enviromentController.LogPath.Should().NotBeBlank(); - Path.IsPathRooted(enviromentController.LogPath).Should().BeTrue("Path is not rooted"); - - } - [Test] public void StartupPath_should_not_be_empty() { diff --git a/NzbDrone.Common.Test/Fixtures.cs b/NzbDrone.Common.Test/Fixtures.cs index b3a5640d5..4089933e0 100644 --- a/NzbDrone.Common.Test/Fixtures.cs +++ b/NzbDrone.Common.Test/Fixtures.cs @@ -3,6 +3,6 @@ using NUnit.Framework; using NzbDrone.Test.Common; [SetUpFixture] -public class Fixtures : LoggingFixtures +public class Fixtures : LoggingTest { } diff --git a/NzbDrone.Common/EnviromentProvider.cs b/NzbDrone.Common/EnviromentProvider.cs index fe7b78451..6ff76153f 100644 --- a/NzbDrone.Common/EnviromentProvider.cs +++ b/NzbDrone.Common/EnviromentProvider.cs @@ -32,11 +32,6 @@ namespace NzbDrone.Common } } - public virtual String LogPath - { - get { return Environment.CurrentDirectory; } - } - public virtual bool IsUserInteractive { get { return Environment.UserInteractive; } @@ -68,26 +63,7 @@ namespace NzbDrone.Common } } - public virtual string WebRoot - { - get - { - return Path.Combine(ApplicationPath, "NzbDrone.Web"); - } - } - - public virtual string AppDataPath - { - get - { - var path = Path.Combine(WebRoot, "App_Data"); - if (!Directory.Exists(path)) - Directory.CreateDirectory(path); - - return path; - } - } - + public virtual string StartUpPath { get @@ -111,15 +87,6 @@ namespace NzbDrone.Common } - - public virtual String TempPath - { - get - { - return Path.GetTempPath(); - } - } - private static bool ContainsIIS(DirectoryInfo dir) { return dir.GetDirectories(IIS_FOLDER_NAME).Length != 0; diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 4297ab830..04df7f468 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -50,6 +50,7 @@ + diff --git a/NzbDrone.Common/PathProvider.cs b/NzbDrone.Common/PathProvider.cs new file mode 100644 index 000000000..9925e72b5 --- /dev/null +++ b/NzbDrone.Common/PathProvider.cs @@ -0,0 +1,111 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; + +namespace NzbDrone.Common +{ + public class PathProvider + { + + private const string WEB_FOLDER = "NzbDrone.Web"; + private const string APP_DATA = "App_Data"; + + private const string LOG_CONFIG_FILE = "log.config"; + private const string APP_CONFIG_FILE = "config.xml"; + + private const string NZBDRONE_DB_FILE = "nzbdrone.sdf"; + private const string LOG_DB_FILE = "log.sdf"; + + public const string UPDATE_SANDBOX_FOLDER_NAME = "nzbdrone_update"; + + private readonly string _applicationPath; + + + public PathProvider(EnviromentProvider enviromentProvider) + { + _applicationPath = enviromentProvider.ApplicationPath; + } + + public PathProvider() + { + + } + + public virtual String LogPath + { + get { return Environment.CurrentDirectory; } + } + + public virtual string WebRoot + { + get + { + return Path.Combine(_applicationPath, WEB_FOLDER); + } + } + + public virtual string AppData + { + get + { + var path = Path.Combine(WebRoot, APP_DATA); + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + + return path; + } + } + + public virtual string NzbDronoeDbFile + { + get + { + + return Path.Combine(AppData, NZBDRONE_DB_FILE); + } + } + + public virtual string LogDbFile + { + get + { + + return Path.Combine(AppData, LOG_DB_FILE); + } + } + + public virtual String SystemTemp + { + get + { + return Path.GetTempPath(); + } + } + + public string LogConfigFile + { + get { return Path.Combine(WebRoot, LOG_CONFIG_FILE); } + } + + public string AppConfigFile + { + get { return Path.Combine(_applicationPath, APP_CONFIG_FILE); } + } + + public string BannerPath + { + get { return Path.Combine(WebRoot, "Content", "Images", "Banners"); } + } + + public string CacheFolder + { + get { return Path.Combine(AppData, "Cache"); } + } + + public string UpdateSandboxFolder + { + get { return Path.Combine(SystemTemp, UPDATE_SANDBOX_FOLDER_NAME); } + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core.Test/Framework/Fixtures.cs b/NzbDrone.Core.Test/Framework/Fixtures.cs index b5e8a4e52..a1fa89703 100644 --- a/NzbDrone.Core.Test/Framework/Fixtures.cs +++ b/NzbDrone.Core.Test/Framework/Fixtures.cs @@ -4,22 +4,22 @@ using NUnit.Framework; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; -[SetUpFixture] -public class Fixtures : LoggingFixtures -{ - [SetUp] - public void SetUp() - { - var oldDbFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sdf", SearchOption.AllDirectories); - foreach (var file in oldDbFiles) - { - try - { - File.Delete(file); - } - catch { } - } +//[SetUpFixture] +//public class Fixtures : LoggingFixtures +//{ +// //[SetUp] +// //public void SetUp() +// //{ +// // var oldDbFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sdf", SearchOption.AllDirectories); +// // foreach (var file in oldDbFiles) +// // { +// // try +// // { +// // File.Delete(file); +// // } +// // catch { } +// // } - MockLib.CreateDataBaseTemplate(); - } -} +// // MockLib.CreateDataBaseTemplate(); +// //} +//} diff --git a/NzbDrone.Core.Test/Framework/TestBase.cs b/NzbDrone.Core.Test/Framework/TestBase.cs index 344c1efd1..427b01c5c 100644 --- a/NzbDrone.Core.Test/Framework/TestBase.cs +++ b/NzbDrone.Core.Test/Framework/TestBase.cs @@ -1,13 +1,47 @@ using System.IO; +using AutoMoq; using NUnit.Framework; +using Ninject; +using NzbDrone.Common; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.Framework { - public class TestBase + public class TestBase : LoggingTest // ReSharper disable InconsistentNaming { + static TestBase() + { + InitLogging(); + + var oldDbFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sdf", SearchOption.AllDirectories); + foreach (var file in oldDbFiles) + { + try + { + File.Delete(file); + } + catch { } + } + + MockLib.CreateDataBaseTemplate(); + } + + protected StandardKernel LiveKernel = null; + protected AutoMoqer Mocker = null; + + protected string VirtualPath + { + get + { + var virtualPath = Path.Combine(TempFolder, "VirtualNzbDrone"); + if (!Directory.Exists(virtualPath)) Directory.CreateDirectory(virtualPath); + + return virtualPath; + } + } + [SetUp] public virtual void SetupBase() { @@ -18,6 +52,9 @@ namespace NzbDrone.Core.Test.Framework } Directory.CreateDirectory(TempFolder); + + LiveKernel = new StandardKernel(); + Mocker = new AutoMoqer(); } [TearDown] @@ -27,6 +64,16 @@ namespace NzbDrone.Core.Test.Framework } + protected void WithTempAsStartUpPath() + { + Mocker.GetMock() + .SetupGet(c => c.ApplicationPath) + .Returns(VirtualPath); + + Mocker.Resolve(); + } + + protected string TempFolder { get { return Path.Combine(Directory.GetCurrentDirectory(), "temp"); } diff --git a/NzbDrone.Core.Test/JobTests/BannerDownloadJobTest.cs b/NzbDrone.Core.Test/JobTests/BannerDownloadJobTest.cs index 58a678bdd..59b50e506 100644 --- a/NzbDrone.Core.Test/JobTests/BannerDownloadJobTest.cs +++ b/NzbDrone.Core.Test/JobTests/BannerDownloadJobTest.cs @@ -92,7 +92,8 @@ namespace NzbDrone.Core.Test.JobTests var fakeSeries = Builder.CreateListOfSize(10) .Build(); - var path = Path.Combine(new EnviromentProvider().WebRoot, "Content", "Images", "Banners"); + + var pathProvider = Mocker.Resolve(); var mocker = new AutoMoqer(MockBehavior.Strict); mocker.Resolve(); @@ -104,42 +105,42 @@ namespace NzbDrone.Core.Test.JobTests .Returns(fakeSeries); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "1.jpg"))) + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "1.jpg"))) .Throws(new WebException()); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "2.jpg"))); + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "2.jpg"))); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "3.jpg"))) + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "3.jpg"))) .Throws(new WebException()); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "4.jpg"))); + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "4.jpg"))); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "5.jpg"))) + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "5.jpg"))) .Throws(new WebException()); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "6.jpg"))); + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "6.jpg"))); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "7.jpg"))) + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "7.jpg"))) .Throws(new WebException()); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "8.jpg"))); + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "8.jpg"))); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "9.jpg"))) + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "9.jpg"))) .Throws(new WebException()); mocker.GetMock() - .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(path, "10.jpg"))); + .Setup(s => s.DownloadFile(It.IsAny(), Path.Combine(pathProvider.BannerPath, "10.jpg"))); mocker.GetMock() diff --git a/NzbDrone.Core.Test/ProviderTests/ConfigFileProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/ConfigFileProviderTest.cs index 704fc9857..6b56ff532 100644 --- a/NzbDrone.Core.Test/ProviderTests/ConfigFileProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/ConfigFileProviderTest.cs @@ -2,6 +2,7 @@ using AutoMoq; using FluentAssertions; using NUnit.Framework; +using NzbDrone.Common; using NzbDrone.Core.Model; using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Test.Framework; @@ -15,14 +16,15 @@ namespace NzbDrone.Core.Test.ProviderTests [SetUp] public void SetUp() { + WithTempAsStartUpPath(); + //Reset config file - var mocker = new AutoMoqer(); - var configFile = mocker.Resolve().ConfigFile; + var configFile = Mocker.Resolve().AppConfigFile; if (File.Exists(configFile)) File.Delete(configFile); - mocker.Resolve().CreateDefaultConfigFile(); + Mocker.Resolve().CreateDefaultConfigFile(); } [Test] @@ -31,10 +33,8 @@ namespace NzbDrone.Core.Test.ProviderTests const string key = "Port"; const string value = "8989"; - var mocker = new AutoMoqer(); - //Act - var result = mocker.Resolve().GetValue(key, value); + var result = Mocker.Resolve().GetValue(key, value); //Assert result.Should().Be(value); @@ -46,10 +46,8 @@ namespace NzbDrone.Core.Test.ProviderTests const string key = "Port"; const int value = 8989; - var mocker = new AutoMoqer(); - //Act - var result = mocker.Resolve().GetValueInt(key, value); + var result = Mocker.Resolve().GetValueInt(key, value); //Assert result.Should().Be(value); @@ -64,7 +62,7 @@ namespace NzbDrone.Core.Test.ProviderTests var mocker = new AutoMoqer(); //Act - var result = mocker.Resolve().GetValueBoolean(key, value); + var result = Mocker.Resolve().GetValueBoolean(key, value); //Assert result.Should().BeTrue(); @@ -76,7 +74,7 @@ namespace NzbDrone.Core.Test.ProviderTests var mocker = new AutoMoqer(); //Act - var result = mocker.Resolve().LaunchBrowser; + var result = Mocker.Resolve().LaunchBrowser; //Assert result.Should().Be(true); @@ -87,10 +85,8 @@ namespace NzbDrone.Core.Test.ProviderTests { const int value = 8989; - var mocker = new AutoMoqer(); - //Act - var result = mocker.Resolve().Port; + var result = Mocker.Resolve().Port; //Assert result.Should().Be(value); @@ -102,13 +98,11 @@ namespace NzbDrone.Core.Test.ProviderTests const string key = "LaunchBrowser"; const bool value = false; - var mocker = new AutoMoqer(); - //Act - mocker.Resolve().SetValue(key, value); + Mocker.Resolve().SetValue(key, value); //Assert - var result = mocker.Resolve().LaunchBrowser; + var result = Mocker.Resolve().LaunchBrowser; result.Should().Be(value); } @@ -118,13 +112,11 @@ namespace NzbDrone.Core.Test.ProviderTests const string key = "Port"; const int value = 12345; - var mocker = new AutoMoqer(); - //Act - mocker.Resolve().SetValue(key, value); + Mocker.Resolve().SetValue(key, value); //Assert - var result = mocker.Resolve().Port; + var result = Mocker.Resolve().Port; result.Should().Be(value); } @@ -134,10 +126,8 @@ namespace NzbDrone.Core.Test.ProviderTests const string key = "Hello"; const string value = "World"; - var mocker = new AutoMoqer(); - //Act - var result = mocker.Resolve().GetValue(key, value); + var result = Mocker.Resolve().GetValue(key, value); //Assert result.Should().Be(value); @@ -149,10 +139,8 @@ namespace NzbDrone.Core.Test.ProviderTests const string key = "Hello"; const string value = "World"; - var mocker = new AutoMoqer(); - //Act - var result = mocker.Resolve().GetValue(key, value, "Universe"); + var result = Mocker.Resolve().GetValue(key, value, "Universe"); //Assert result.Should().Be(value); @@ -161,10 +149,9 @@ namespace NzbDrone.Core.Test.ProviderTests [Test] public void GetAuthenticationType_No_Existing_Value() { - var mocker = new AutoMoqer(); //Act - var result = mocker.Resolve().AuthenticationType; + var result = Mocker.Resolve().AuthenticationType; //Assert result.Should().Be(AuthenticationType.Anonymous); @@ -173,11 +160,11 @@ namespace NzbDrone.Core.Test.ProviderTests [Test] public void GetAuthenticationType_Windows() { - var mocker = new AutoMoqer(); - mocker.Resolve().SetValue("AuthenticationType", 1); + + Mocker.Resolve().SetValue("AuthenticationType", 1); //Act - var result = mocker.Resolve().AuthenticationType; + var result = Mocker.Resolve().AuthenticationType; //Assert result.Should().Be(AuthenticationType.Windows); diff --git a/NzbDrone.Core.Test/ProviderTests/TvDbProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/TvDbProviderTest.cs index a7e570261..cb894ebf6 100644 --- a/NzbDrone.Core.Test/ProviderTests/TvDbProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/TvDbProviderTest.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using FluentAssertions; using NUnit.Framework; +using Ninject; using NzbDrone.Common; using NzbDrone.Core.Providers; using NzbDrone.Core.Test.Framework; @@ -16,30 +17,31 @@ namespace NzbDrone.Core.Test.ProviderTests // ReSharper disable InconsistentNaming public class TvDbProviderTest : TestBase { + private TvDbProvider tvDbProvider; + + [SetUp] + public void Setup() + { + tvDbProvider = LiveKernel.Get(); + } + [TestCase("The Simpsons")] [TestCase("Family Guy")] [TestCase("South Park")] public void successful_search(string title) { - var result = new TvDbProvider(new EnviromentProvider()).SearchSeries(title); + var result = tvDbProvider.SearchSeries(title); result.Should().NotBeEmpty(); result[0].SeriesName.Should().Be(title); } - - - - [Test] public void no_search_result() { - //setup - var tvdbProvider = new TvDbProvider(new EnviromentProvider()); - //act - var result = tvdbProvider.SearchSeries(Guid.NewGuid().ToString()); + var result = tvDbProvider.SearchSeries(Guid.NewGuid().ToString()); //assert result.Should().BeEmpty(); @@ -49,11 +51,8 @@ namespace NzbDrone.Core.Test.ProviderTests [Test] public void none_unique_season_episode_number() { - //setup - var tvdbProvider = new TvDbProvider(new EnviromentProvider()); - //act - var result = tvdbProvider.GetSeries(75978, true);//Family guy + var result = tvDbProvider.GetSeries(75978, true);//Family guy //Asserts that when episodes are grouped by Season/Episode each group contains maximum of //one item. @@ -65,11 +64,8 @@ namespace NzbDrone.Core.Test.ProviderTests [Test] public void American_dad_fix() { - //setup - var tvdbProvider = new TvDbProvider(new EnviromentProvider()); - //act - var result = tvdbProvider.GetSeries(73141, true); + var result = tvDbProvider.GetSeries(73141, true); var seasonsNumbers = result.Episodes.Select(e => e.SeasonNumber) .Distinct().ToList(); diff --git a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/PreformUpdateFixture.cs b/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/PreformUpdateFixture.cs index 68e28f688..a6d09601d 100644 --- a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/PreformUpdateFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/PreformUpdateFixture.cs @@ -22,8 +22,8 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests public void setup() { _mocker = new AutoMoqer(MockBehavior.Strict); - _mocker.GetMock() - .SetupGet(c => c.TempPath).Returns(TempFolder); + _mocker.GetMock() + .SetupGet(c => c.SystemTemp).Returns(TempFolder); } @@ -39,11 +39,11 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests }; _mocker.GetMock().Setup( - c => c.DownloadFile(updatePackage.Url, Path.Combine(TempFolder, UpdateProvider.SandboxFolderName ,updatePackage.FileName))); + c => c.DownloadFile(updatePackage.Url, Path.Combine(TempFolder, PathProvider.UPDATE_SANDBOX_FOLDER_NAME, updatePackage.FileName))); _mocker.GetMock().Setup( - c => c.ExtractArchive(Path.Combine(TempFolder, UpdateProvider.SandboxFolderName, updatePackage.FileName), - Path.Combine(TempFolder, UpdateProvider.SandboxFolderName))); + c => c.ExtractArchive(Path.Combine(TempFolder, PathProvider.UPDATE_SANDBOX_FOLDER_NAME, updatePackage.FileName), + Path.Combine(TempFolder, PathProvider.UPDATE_SANDBOX_FOLDER_NAME))); _mocker.Resolve().PreformUpdate(updatePackage); } @@ -52,7 +52,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests public void Should_download_and_extract_to_temp_folder() { - var updateSubFolder = new DirectoryInfo(Path.Combine(TempFolder, UpdateProvider.SandboxFolderName)); + var updateSubFolder = new DirectoryInfo(Path.Combine(TempFolder, PathProvider.UPDATE_SANDBOX_FOLDER_NAME)); var updatePackage = new UpdatePackage { diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index 37a3fb385..4d805a29e 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -39,7 +39,9 @@ namespace NzbDrone.Core { BindKernel(); - MigrationsHelper.Run(Connection.MainConnectionString, true); + var mainConnectionString = _kernel.Get().MainConnectionString; + + MigrationsHelper.Run(mainConnectionString, true); LogConfiguration.RegisterDatabaseLogger(_kernel.Get()); @@ -59,9 +61,11 @@ namespace NzbDrone.Core Logger.Debug("Binding Ninject's Kernel"); _kernel = new StandardKernel(); - _kernel.Bind().ToMethod(c => Connection.GetPetaPocoDb(Connection.MainConnectionString)).InTransientScope(); - _kernel.Bind().ToMethod(c => Connection.GetPetaPocoDb(Connection.LogConnectionString, false)).WhenInjectedInto().InSingletonScope(); - _kernel.Bind().ToMethod(c => Connection.GetPetaPocoDb(Connection.LogConnectionString)).WhenInjectedInto().InSingletonScope(); + var connection = _kernel.Get(); + + _kernel.Bind().ToMethod(c => connection.GetMainPetaPocoDb()).InTransientScope(); + _kernel.Bind().ToMethod(c => connection.GetLogPetaPocoDb(false)).WhenInjectedInto().InSingletonScope(); + _kernel.Bind().ToMethod(c => connection.GetLogPetaPocoDb()).WhenInjectedInto().InSingletonScope(); _kernel.Bind().ToSelf().InSingletonScope(); } diff --git a/NzbDrone.Core/Datastore/Connection.cs b/NzbDrone.Core/Datastore/Connection.cs index 7c05d377f..8dfc38859 100644 --- a/NzbDrone.Core/Datastore/Connection.cs +++ b/NzbDrone.Core/Datastore/Connection.cs @@ -10,38 +10,51 @@ using PetaPoco; namespace NzbDrone.Core.Datastore { - public static class Connection + public class Connection { - private static EnviromentProvider _enviromentProvider = new EnviromentProvider(); + private readonly PathProvider _pathProvider; - static Connection() + public Connection(PathProvider pathProvider) { - Database.Mapper = new CustomeMapper(); + _pathProvider = pathProvider; } - - public static string GetConnectionString(string path) + static Connection() { - //return String.Format("Data Source={0};Version=3;Cache Size=30000;Pooling=true;Default Timeout=2", path); - return String.Format("Data Source={0}", path); + Database.Mapper = new CustomeMapper(); } - public static String MainConnectionString + public String MainConnectionString { get { - return GetConnectionString(Path.Combine(_enviromentProvider.AppDataPath, "nzbdrone.sdf")); + return GetConnectionString(_pathProvider.NzbDronoeDbFile); } } - public static String LogConnectionString + public String LogConnectionString { get { - return GetConnectionString(Path.Combine(_enviromentProvider.AppDataPath, "log.sdf")); + return GetConnectionString(_pathProvider.LogDbFile); } } + public static string GetConnectionString(string path) + { + //return String.Format("Data Source={0};Version=3;Cache Size=30000;Pooling=true;Default Timeout=2", path); + return String.Format("Data Source={0}", path); + } + + public IDatabase GetMainPetaPocoDb(Boolean profiled = true) + { + return GetPetaPocoDb(MainConnectionString, profiled); + } + + public IDatabase GetLogPetaPocoDb(Boolean profiled = true) + { + return GetPetaPocoDb(LogConnectionString, profiled); + } public static IDatabase GetPetaPocoDb(string connectionString, Boolean profiled = true) { diff --git a/NzbDrone.Core/Instrumentation/LogConfiguration.cs b/NzbDrone.Core/Instrumentation/LogConfiguration.cs index d94c6d520..8ce6037e7 100644 --- a/NzbDrone.Core/Instrumentation/LogConfiguration.cs +++ b/NzbDrone.Core/Instrumentation/LogConfiguration.cs @@ -7,17 +7,23 @@ using NzbDrone.Core.Providers; namespace NzbDrone.Core.Instrumentation { - public static class LogConfiguration + public class LogConfiguration { + private readonly PathProvider _pathProvider; - public static void Setup() + public LogConfiguration(PathProvider pathProvider) + { + _pathProvider = pathProvider; + } + + public void Setup() { if (Common.EnviromentProvider.IsProduction) { LogManager.ThrowExceptions = false; } - LogManager.Configuration = new XmlLoggingConfiguration(Path.Combine(new EnviromentProvider().WebRoot, "log.config"), false); + LogManager.Configuration = new XmlLoggingConfiguration(_pathProvider.LogConfigFile, false); Common.LogConfiguration.RegisterConsoleLogger(LogLevel.Info, "NzbDrone.Web.MvcApplication"); Common.LogConfiguration.RegisterConsoleLogger(LogLevel.Info, "NzbDrone.Core.CentralDispatch"); diff --git a/NzbDrone.Core/Providers/Core/ConfigFileProvider.cs b/NzbDrone.Core/Providers/Core/ConfigFileProvider.cs index 5047fec37..25ffac54e 100644 --- a/NzbDrone.Core/Providers/Core/ConfigFileProvider.cs +++ b/NzbDrone.Core/Providers/Core/ConfigFileProvider.cs @@ -9,12 +9,13 @@ namespace NzbDrone.Core.Providers.Core { public class ConfigFileProvider { - private string _configFile = Path.Combine(new EnviromentProvider().AppDataPath, "Config.xml"); + private readonly PathProvider _pathProvider; - public string ConfigFile + private readonly string _configFile; + public ConfigFileProvider(PathProvider pathProvider) { - get { return _configFile; } - set { _configFile = value; } + _pathProvider = pathProvider; + _configFile = _pathProvider.AppConfigFile; } public virtual int Port @@ -37,7 +38,7 @@ namespace NzbDrone.Core.Providers.Core public virtual string GetValue(string key, object defaultValue, string parent = null) { - var xDoc = XDocument.Load(ConfigFile); + var xDoc = XDocument.Load(_configFile); var config = xDoc.Descendants("Config").Single(); var parentContainer = config; @@ -50,7 +51,7 @@ namespace NzbDrone.Core.Providers.Core SetValue(key, defaultValue, parent); //Reload the configFile - xDoc = XDocument.Load(ConfigFile); + xDoc = XDocument.Load(_configFile); config = xDoc.Descendants("Config").Single(); } @@ -81,7 +82,7 @@ namespace NzbDrone.Core.Providers.Core public virtual void SetValue(string key, object value, string parent = null) { - var xDoc = XDocument.Load(ConfigFile); + var xDoc = XDocument.Load(_configFile); var config = xDoc.Descendants("Config").Single(); var parentContainer = config; @@ -105,18 +106,18 @@ namespace NzbDrone.Core.Providers.Core else parentContainer.Descendants(key).Single().Value = value.ToString(); - xDoc.Save(ConfigFile); + xDoc.Save(_configFile); } public virtual void CreateDefaultConfigFile() { - if (!File.Exists(ConfigFile)) + if (!File.Exists(_configFile)) { var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); xDoc.Add(new XElement("Config")); - xDoc.Save(ConfigFile); + xDoc.Save(_configFile); } } } diff --git a/NzbDrone.Core/Providers/Jobs/BannerDownloadJob.cs b/NzbDrone.Core/Providers/Jobs/BannerDownloadJob.cs index 15e0ad431..8f8da8622 100644 --- a/NzbDrone.Core/Providers/Jobs/BannerDownloadJob.cs +++ b/NzbDrone.Core/Providers/Jobs/BannerDownloadJob.cs @@ -18,18 +18,20 @@ namespace NzbDrone.Core.Providers.Jobs private readonly HttpProvider _httpProvider; private readonly DiskProvider _diskProvider; private readonly EnviromentProvider _enviromentProvider; + private readonly PathProvider _pathProvider; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - private string _bannerPath = ""; private const string _bannerUrlPrefix = "http://www.thetvdb.com/banners/"; [Inject] - public BannerDownloadJob(SeriesProvider seriesProvider, HttpProvider httpProvider, DiskProvider diskProvider, EnviromentProvider enviromentProvider) + public BannerDownloadJob(SeriesProvider seriesProvider, HttpProvider httpProvider, DiskProvider diskProvider, + EnviromentProvider enviromentProvider, PathProvider pathProvider) { _seriesProvider = seriesProvider; _httpProvider = httpProvider; _diskProvider = diskProvider; _enviromentProvider = enviromentProvider; + _pathProvider = pathProvider; } public BannerDownloadJob() @@ -51,8 +53,8 @@ namespace NzbDrone.Core.Providers.Jobs { Logger.Debug("Starting banner download job"); - _bannerPath = Path.Combine(_enviromentProvider.WebRoot, "Content", "Images", "Banners"); - _diskProvider.CreateDirectory(_bannerPath); + + _diskProvider.CreateDirectory(_pathProvider.BannerPath); if (targetId > 0) { @@ -76,7 +78,7 @@ namespace NzbDrone.Core.Providers.Jobs public virtual void DownloadBanner(ProgressNotification notification, Series series) { - var bannerFilename = String.Format("{0}{1}{2}.jpg", _bannerPath, Path.DirectorySeparatorChar, series.SeriesId); + var bannerFilename = Path.Combine(_pathProvider.BannerPath, series.SeriesId.ToString(), ".jpg"); notification.CurrentMessage = string.Format("Downloading banner for '{0}'", series.Title); diff --git a/NzbDrone.Core/Providers/TvDbProvider.cs b/NzbDrone.Core/Providers/TvDbProvider.cs index da8465070..7998e55ef 100644 --- a/NzbDrone.Core/Providers/TvDbProvider.cs +++ b/NzbDrone.Core/Providers/TvDbProvider.cs @@ -13,22 +13,20 @@ namespace NzbDrone.Core.Providers { public class TvDbProvider { - private readonly EnviromentProvider _enviromentProvider; private const string TVDB_APIKEY = "5D2D188E86E07F4F"; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly TvdbHandler _handler; [Inject] - public TvDbProvider(EnviromentProvider enviromentProvider) + public TvDbProvider(PathProvider pathProvider) { - _enviromentProvider = enviromentProvider; - _handler = new TvdbHandler(new XmlCacheProvider(_enviromentProvider.AppDataPath + @"\cache\tvdb"), TVDB_APIKEY); + _handler = new TvdbHandler(new XmlCacheProvider(pathProvider.CacheFolder), TVDB_APIKEY); } public TvDbProvider() { - + } public virtual IList SearchSeries(string title) diff --git a/NzbDrone.Core/Providers/UpdateProvider.cs b/NzbDrone.Core/Providers/UpdateProvider.cs index bc682c026..dab67ad13 100644 --- a/NzbDrone.Core/Providers/UpdateProvider.cs +++ b/NzbDrone.Core/Providers/UpdateProvider.cs @@ -18,19 +18,22 @@ namespace NzbDrone.Core.Providers private readonly HttpProvider _httpProvider; private readonly ConfigProvider _configProvider; private readonly EnviromentProvider _enviromentProvider; + private readonly PathProvider _pathProvider; private readonly DiskProvider _diskProvider; private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Regex ParseRegex = new Regex(@"(?:\>)(?NzbDrone.+?(?\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase); - public const string SandboxFolderName = "nzbdrone_update"; + [Inject] - public UpdateProvider(HttpProvider httpProvider, ConfigProvider configProvider, EnviromentProvider enviromentProvider, DiskProvider diskProvider) + public UpdateProvider(HttpProvider httpProvider, ConfigProvider configProvider, EnviromentProvider enviromentProvider, + PathProvider pathProvider, DiskProvider diskProvider) { _httpProvider = httpProvider; _configProvider = configProvider; _enviromentProvider = enviromentProvider; + _pathProvider = pathProvider; _diskProvider = diskProvider; } @@ -73,15 +76,14 @@ namespace NzbDrone.Core.Providers public virtual void PreformUpdate(UpdatePackage updatePackage) { - var tempSubFolder = Path.Combine(_enviromentProvider.TempPath, SandboxFolderName); - var packageDestination = Path.Combine(tempSubFolder, updatePackage.FileName); + var packageDestination = Path.Combine(_pathProvider.UpdateSandboxFolder, updatePackage.FileName); Logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination); _httpProvider.DownloadFile(updatePackage.Url, packageDestination); Logger.Info("Download completed for update package from [{0}]", updatePackage.FileName); Logger.Info("Extracting Update package"); - _diskProvider.ExtractArchive(packageDestination, tempSubFolder); + _diskProvider.ExtractArchive(packageDestination, _pathProvider.UpdateSandboxFolder); Logger.Info("Update package extracted successfully"); } diff --git a/NzbDrone.Test.Common/LoggingFixtures.cs b/NzbDrone.Test.Common/LoggingTest.cs similarity index 88% rename from NzbDrone.Test.Common/LoggingFixtures.cs rename to NzbDrone.Test.Common/LoggingTest.cs index f8db8d252..df68f0479 100644 --- a/NzbDrone.Test.Common/LoggingFixtures.cs +++ b/NzbDrone.Test.Common/LoggingTest.cs @@ -5,11 +5,9 @@ using NzbDrone.Common; namespace NzbDrone.Test.Common { - public abstract class LoggingFixtures + public abstract class LoggingTest { - - [SetUp] - public void SetUpBase() + protected static void InitLogging() { LogConfiguration.RegisterConsoleLogger(LogLevel.Trace); LogConfiguration.RegisterUdpLogger(); diff --git a/NzbDrone.Test.Common/NzbDrone.Test.Common.csproj b/NzbDrone.Test.Common/NzbDrone.Test.Common.csproj index a30c1aab0..fba895fe7 100644 --- a/NzbDrone.Test.Common/NzbDrone.Test.Common.csproj +++ b/NzbDrone.Test.Common/NzbDrone.Test.Common.csproj @@ -69,7 +69,7 @@ - + diff --git a/NzbDrone.Update.Test/Fixtures.cs b/NzbDrone.Update.Test/Fixtures.cs index b3a5640d5..4089933e0 100644 --- a/NzbDrone.Update.Test/Fixtures.cs +++ b/NzbDrone.Update.Test/Fixtures.cs @@ -3,6 +3,6 @@ using NUnit.Framework; using NzbDrone.Test.Common; [SetUpFixture] -public class Fixtures : LoggingFixtures +public class Fixtures : LoggingTest { } diff --git a/NzbDrone.Web/Global.asax.cs b/NzbDrone.Web/Global.asax.cs index fb0f24137..906410b98 100644 --- a/NzbDrone.Web/Global.asax.cs +++ b/NzbDrone.Web/Global.asax.cs @@ -43,24 +43,23 @@ namespace NzbDrone.Web //base.OnApplicationStarted(); AreaRegistration.RegisterAllAreas(); - var razor =ViewEngines.Engines.Where(e => e.GetType() == typeof (RazorViewEngine)).Single(); + var razor = ViewEngines.Engines.Where(e => e.GetType() == typeof(RazorViewEngine)).Single(); ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(razor); RegisterGlobalFilters(GlobalFilters.Filters); - + Logger.Info("Fully initialized and ready."); } protected override IKernel CreateKernel() { - LogConfiguration.Setup(); + var kernel = CentralDispatch.NinjectKernel; + kernel.Get().Setup(); Logger.Info("NZBDrone Starting up."); - CentralDispatch.DedicateToHost(); - var kernel = CentralDispatch.NinjectKernel; + CentralDispatch.DedicateToHost(); - // kernel.Bind().ToConstant(kernel.Get("LogDb")); kernel.Load(Assembly.GetExecutingAssembly()); return kernel; } diff --git a/config.xml b/config.xml new file mode 100644 index 000000000..5d35eea2e --- /dev/null +++ b/config.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file