More code to support service, isn't working yet. (Console still works fine)

pull/6/head
kay.one 13 years ago
parent be651660a4
commit 495d7b8595

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentAssertions;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Providers;

@ -72,6 +72,7 @@
<Compile Include="AutoMoq\AutoMoqerTest.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingBuilderStrategy.cs" />
<Compile Include="AutoMoq\Unity\AutoMockingContainerExtension.cs" />
<Compile Include="ProgramTest.cs" />
<Compile Include="MonitoringProviderTest.cs" />
<Compile Include="ConfigProviderTest.cs" />
<Compile Include="IISProviderTest.cs" />

@ -0,0 +1,42 @@
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Providers;
namespace NzbDrone.App.Test
{
[TestFixture]
public class ProgramTest
{
[TestCase(null, ApplicationMode.Console)]
[TestCase("", ApplicationMode.Console)]
[TestCase("1", ApplicationMode.Help)]
[TestCase("ii", ApplicationMode.Help)]
[TestCase("uu", ApplicationMode.Help)]
[TestCase("i", ApplicationMode.InstallService)]
[TestCase("I", ApplicationMode.InstallService)]
[TestCase("/I", ApplicationMode.InstallService)]
[TestCase("/i", ApplicationMode.InstallService)]
[TestCase("-I", ApplicationMode.InstallService)]
[TestCase("-i", ApplicationMode.InstallService)]
[TestCase("u", ApplicationMode.UninstallService)]
[TestCase("U", ApplicationMode.UninstallService)]
[TestCase("/U", ApplicationMode.UninstallService)]
[TestCase("/u", ApplicationMode.UninstallService)]
[TestCase("-U", ApplicationMode.UninstallService)]
[TestCase("-u", ApplicationMode.UninstallService)]
public void GetApplicationMode_single_arg(string arg, ApplicationMode mode)
{
Console.GetApplicationMode(new[] { arg }).Should().Be(mode);
}
[TestCase("", "", ApplicationMode.Console)]
[TestCase("", null, ApplicationMode.Console)]
[TestCase("i", "n", ApplicationMode.Help)]
public void GetApplicationMode_two_args(string a, string b, ApplicationMode mode)
{
Console.GetApplicationMode(new[] { a, b }).Should().Be(mode);
}
}
}

@ -18,7 +18,7 @@ namespace NzbDrone.Core.Test
public class BacklogSearchJobTest
{
[Test]
public void no_missing_epsiodes()
public void no_missing_epsiodes_should_not_trigger_any_search()
{
//Setup
var notification = new ProgressNotification("Backlog Search Job Test");
@ -42,7 +42,7 @@ namespace NzbDrone.Core.Test
}
[Test]
public void individual_missing_episode_only()
public void individual_missing_episode()
{
//Setup
var notification = new ProgressNotification("Backlog Search Job Test");

@ -1,187 +0,0 @@
// ReSharper disable RedundantUsingDirective
using System;
using AutoMoq;
using Moq;
using NUnit.Framework;
namespace NzbDrone.Core.Test
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class AutoMoqerTest
{
[Test]
public void GetMock_on_interface_returns_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var mock = mocker.GetMock<IDependency>();
//Assert
Assert.IsNotNull(mock);
}
[Test]
public void GetMock_on_concrete_returns_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var mock = mocker.GetMock<ConcreteClass>();
//Assert
Assert.IsNotNull(mock);
}
[Test]
public void Resolve_doesnt_return_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<ConcreteClass>().Do();
//Assert
Assert.AreEqual("hello", result);
}
[Test]
public void Resolve_with_dependency_doesnt_return_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<VirtualDependency>().VirtualMethod();
//Assert
Assert.AreEqual("hello", result);
}
[Test]
public void Resolve_with_mocked_dependency_uses_mock()
{
//Arrange
var mocker = new AutoMoqer();
mocker.GetMock<VirtualDependency>()
.Setup(m => m.VirtualMethod())
.Returns("mocked");
//Act
var result = mocker.Resolve<ClassWithVirtualDependencies>().CallVirtualChild();
//Assert
Assert.AreEqual("mocked", result);
}
[Test]
public void Resolve_with_unbound_concerete_dependency_uses_mock()
{
//Arrange
var mocker = new AutoMoqer();
//Act
var result = mocker.Resolve<ClassWithVirtualDependencies>().CallVirtualChild();
var mockedResult = new Mock<VirtualDependency>().Object.VirtualMethod();
//Assert
Assert.AreEqual(mockedResult, result);
}
[Test]
public void Resolve_with_constant_concerete_dependency_uses_constant()
{
//Arrange
var mocker = new AutoMoqer();
var constant = new VirtualDependency { PropValue = Guid.NewGuid().ToString() };
mocker.SetConstant(constant);
//Act
var result = mocker.Resolve<ClassWithVirtualDependencies>().GetVirtualProperty();
//Assert
Assert.AreEqual(constant.PropValue, result);
}
}
public class ConcreteClass
{
public string Do()
{
return "hello";
}
}
public class Dependency : IDependency
{
}
public interface IDependency
{
}
public class ClassWithDependencies
{
public ClassWithDependencies(IDependency dependency)
{
Dependency = dependency;
}
public IDependency Dependency { get; set; }
}
public class ClassWithVirtualDependencies
{
private readonly VirtualDependency _virtualDependency;
public ClassWithVirtualDependencies(IDependency dependency, VirtualDependency virtualDependency)
{
_virtualDependency = virtualDependency;
Dependency = dependency;
}
public IDependency Dependency { get; set; }
public string CallVirtualChild()
{
return _virtualDependency.VirtualMethod();
}
public string GetVirtualProperty()
{
return _virtualDependency.PropValue;
}
}
public class VirtualDependency
{
private readonly IDependency _dependency;
public VirtualDependency()
{
}
public VirtualDependency(IDependency dependency)
{
_dependency = dependency;
}
public string PropValue { get; set; }
public virtual string VirtualMethod()
{
return "hello";
}
}
}

@ -122,7 +122,6 @@
<Compile Include="IndexerTests.cs" />
<Compile Include="InventoryProvider_QualityNeededTest.cs" />
<Compile Include="Framework\AutoMoq\AutoMoqer.cs" />
<Compile Include="Framework\AutoMoq\AutoMoqerTest.cs" />
<Compile Include="Framework\AutoMoq\Unity\AutoMockingBuilderStrategy.cs">
<SubType>Code</SubType>
</Compile>

@ -2,6 +2,7 @@
using System.Net;
using System.Threading;
using NLog;
using Ninject;
using NzbDrone.Providers;
namespace NzbDrone
@ -11,22 +12,20 @@ namespace NzbDrone
private static readonly Logger Logger = LogManager.GetLogger("Host.App");
private readonly ConfigProvider _configProvider;
private readonly ConsoleProvider _consoleProvider;
private readonly DebuggerProvider _debuggerProvider;
private readonly EnviromentProvider _enviromentProvider;
private readonly IISProvider _iisProvider;
private readonly ProcessProvider _processProvider;
private readonly WebClient _webClient;
[Inject]
public Application(ConfigProvider configProvider, WebClient webClient, IISProvider iisProvider,
ConsoleProvider consoleProvider,
DebuggerProvider debuggerProvider, EnviromentProvider enviromentProvider,
ProcessProvider processProvider)
{
_configProvider = configProvider;
_webClient = webClient;
_iisProvider = iisProvider;
_consoleProvider = consoleProvider;
_debuggerProvider = debuggerProvider;
_enviromentProvider = enviromentProvider;
_processProvider = processProvider;
@ -37,7 +36,11 @@ namespace NzbDrone
Thread.CurrentThread.Name = "Host";
}
public void Start()
public Application()
{
}
public virtual void Start()
{
_iisProvider.StopServer();
_iisProvider.StartServer();
@ -55,11 +58,9 @@ namespace NzbDrone
{
Logger.ErrorException("Failed to open URL in default browser.", e);
}
_consoleProvider.WaitForClose();
return;
}
else
{
try
{
_webClient.DownloadString(_iisProvider.AppUrl);
@ -69,9 +70,13 @@ namespace NzbDrone
Logger.ErrorException("Failed to load home page.", e);
}
}
}
public void Stop()
public virtual void Stop()
{
Logger.Info("Attempting to stop application.");
_iisProvider.StopServer();
Logger.Info("Application has finished stop routine.");
}
}
}

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone
{
public enum ApplicationMode
{
Console,
Help,
InstallService,
UninstallService
}
}

@ -0,0 +1,62 @@
using System;
using System.Linq;
using System.Reflection;
using NLog;
using Ninject;
using NzbDrone.Providers;
namespace NzbDrone
{
public static class Console
{
private static readonly StandardKernel Kernel = new StandardKernel();
private static readonly Logger Logger = LogManager.GetLogger("Host.Main");
private static void Main(string[] args)
{
try
{
System.Console.WriteLine("Starting NzbDrone Console. Version " + Assembly.GetExecutingAssembly().GetName().Version);
Kernel.Bind<ConfigProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ConsoleProvider>().ToSelf().InSingletonScope();
Kernel.Bind<DebuggerProvider>().ToSelf().InSingletonScope();
Kernel.Bind<EnviromentProvider>().ToSelf().InSingletonScope();
Kernel.Bind<IISProvider>().ToSelf().InSingletonScope();
Kernel.Bind<MonitoringProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ProcessProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ServiceProvider>().ToSelf().InSingletonScope();
Kernel.Bind<WebClientProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ApplicationMode>().ToConstant(GetApplicationMode(args));
Kernel.Get<Router>().Route();
}
catch (Exception e)
{
System.Console.WriteLine(e.ToString());
Logger.Fatal(e.ToString());
}
System.Console.WriteLine("Press enter to exit.");
System.Console.ReadLine();
}
public static ApplicationMode GetApplicationMode(string[] args)
{
if (args == null) return ApplicationMode.Console;
var cleanArgs = args.Where(c => c != null && !String.IsNullOrWhiteSpace(c)).ToList();
if (cleanArgs.Count == 0) return ApplicationMode.Console;
if (cleanArgs.Count != 1) return ApplicationMode.Help;
var arg = cleanArgs.First().Trim('/', '\\', '-').ToLower();
if (arg == "i") return ApplicationMode.InstallService;
if (arg == "u") return ApplicationMode.UninstallService;
return ApplicationMode.Help;
}
}
}

@ -86,6 +86,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Application.cs" />
<Compile Include="ApplicationMode.cs" />
<Compile Include="ProcessInfo.cs" />
<Compile Include="Providers\ConsoleProvider.cs" />
<Compile Include="Providers\DebuggerProvider.cs" />
@ -96,12 +97,13 @@
<Compile Include="ProcessAttacher.cs" />
<Compile Include="Providers\ConfigProvider.cs" />
<Compile Include="Providers\IISProvider.cs" />
<Compile Include="Program.cs" />
<Compile Include="Console.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\MonitoringProvider.cs" />
<Compile Include="Providers\ProcessProvider.cs" />
<Compile Include="Providers\ServiceProvider.cs" />
<Compile Include="Providers\WebClientProvider.cs" />
<Compile Include="Router.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />

@ -7,7 +7,5 @@ namespace NzbDrone
public int Id { get; set; }
public ProcessPriorityClass Priority { get; set; }
public string StartPath { get; set; }
public bool HasExited { get; set; }
}
}

@ -1,43 +0,0 @@
using System;
using System.Reflection;
using NLog;
using Ninject;
using NzbDrone.Providers;
namespace NzbDrone
{
public static class Program
{
public static readonly StandardKernel Kernel = new StandardKernel();
private static readonly Logger Logger = LogManager.GetLogger("Host.Main");
private static void Main()
{
try
{
Kernel.Bind<ConfigProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ConsoleProvider>().ToSelf().InSingletonScope();
Kernel.Bind<DebuggerProvider>().ToSelf().InSingletonScope();
Kernel.Bind<EnviromentProvider>().ToSelf().InSingletonScope();
Kernel.Bind<IISProvider>().ToSelf().InSingletonScope();
Kernel.Bind<MonitoringProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ProcessProvider>().ToSelf().InSingletonScope();
Kernel.Bind<ServiceProvider>().ToSelf().InSingletonScope();
Kernel.Bind<WebClientProvider>().ToSelf().InSingletonScope();
Console.WriteLine("Starting NzbDrone Console. Version " + Assembly.GetExecutingAssembly().GetName().Version);
Kernel.Get<MonitoringProvider>().Start();
Kernel.Get<Application>().Start();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Logger.Fatal(e.ToString());
}
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
}

@ -8,8 +8,13 @@ namespace NzbDrone.Providers
{
while (true)
{
Console.ReadLine();
System.Console.ReadLine();
}
}
public virtual void PrintHelp()
{
System.Console.WriteLine("Help");
}
}
}

@ -120,7 +120,7 @@ namespace NzbDrone.Providers
if (e.Data.Contains(" NzbDrone."))
{
Console.WriteLine(e.Data);
System.Console.WriteLine(e.Data);
return;
}

@ -103,7 +103,7 @@ namespace NzbDrone.Providers
private static void AppDomainException(object excepion)
{
Console.WriteLine("EPIC FAIL: {0}", excepion);
System.Console.WriteLine("EPIC FAIL: {0}", excepion);
Logger.Fatal("EPIC FAIL: {0}", excepion);
#if RELEASE

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NzbDrone.Providers;
namespace NzbDrone
{
class Router
{
private readonly Application _application;
private readonly ServiceProvider _serviceProvider;
private readonly ConsoleProvider _consoleProvider;
private readonly ApplicationMode _applicationMode;
public Router(Application application, ServiceProvider serviceProvider, ConsoleProvider consoleProvider, ApplicationMode applicationMode)
{
_application = application;
_serviceProvider = serviceProvider;
_consoleProvider = consoleProvider;
_applicationMode = applicationMode;
}
public void Route()
{
switch (_applicationMode)
{
case ApplicationMode.Console:
{
_application.Start();
_consoleProvider.WaitForClose();
break;
}
case ApplicationMode.InstallService:
{
_serviceProvider.Install();
break;
}
case ApplicationMode.UninstallService:
{
_serviceProvider.UnInstall();
break;
}
default:
{
_consoleProvider.PrintHelp();
break;
}
}
}
}
}
Loading…
Cancel
Save