Permalink
Browse files

Add interface for WindowsServiceFacade and WindowsServiceLocator.

  • Loading branch information...
1 parent bf3a051 commit 093ef0947443980dc296448dce9b85f6a891f1b7 @lanwin committed May 10, 2012
@@ -43,6 +43,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Testing\FakeWindowsServiceFacade.cs" />
+ <Compile Include="Testing\FakeWindowsServiceLocator.cs" />
+ <Compile Include="Infrastructure\IWindowsServiceFacade.cs" />
+ <Compile Include="Infrastructure\IWindowsServiceLocator.cs" />
<Compile Include="Scratchpad.cs" />
<Compile Include="Core\NotNullAttribute.cs" />
<Compile Include="Infrastructure\WindowsServiceFacadeReal.cs" />
@@ -0,0 +1,13 @@
+using System;
+
+namespace CommandBasedComponents.Infrastructure
+{
+ public interface IWindowsServiceFacade
+ {
+ bool IsStarted { get; }
+ bool IsStopped { get; }
+ void StopAndWait(TimeSpan timeout);
+ void Kill();
+ void Start();
+ }
+}
@@ -0,0 +1,13 @@
+using CommandBasedComponents.Core;
+using CommandBasedComponents.Testing;
+
+namespace CommandBasedComponents.Infrastructure
+{
+ public interface IWindowsServiceLocator
+ {
+ [NotNull]
+ FakeWindowsServiceFacade Find(string name);
+
+ FakeWindowsServiceFacade TryFind(string name);
+ }
+}
@@ -4,7 +4,7 @@
namespace CommandBasedComponents.Infrastructure
{
- public class WindowsServiceFacadeReal
+ public class WindowsServiceFacadeReal : IWindowsServiceFacade
{
readonly ServiceController _controller;
@@ -1,12 +1,13 @@
using System;
using CommandBasedComponents.Core;
+using CommandBasedComponents.Testing;
namespace CommandBasedComponents.Infrastructure
{
- public class WindowsServiceLocatorReal
+ public class WindowsServiceLocatorReal : IWindowsServiceLocator
{
[NotNull]
- public WindowsServiceFacade Find(string name)
+ public FakeWindowsServiceFacade Find(string name)
{
var windowsService = TryFind(name);
if(windowsService == null)
@@ -16,7 +17,7 @@ public WindowsServiceFacade Find(string name)
return windowsService;
}
- public WindowsServiceFacade TryFind(string name)
+ public FakeWindowsServiceFacade TryFind(string name)
{
throw new NotImplementedException();/*
return ServiceController.GetServices()
View
@@ -4,6 +4,8 @@
using System.Diagnostics;
using System.Linq;
using CommandBasedComponents.Core;
+using CommandBasedComponents.Infrastructure;
+using CommandBasedComponents.Testing;
namespace CommandBasedComponents
{
@@ -49,9 +51,7 @@ public ServiceDescription(HammerPackage package)
ExecutablePath = package.ExecutablePath;
if(string.IsNullOrWhiteSpace(ServiceName))
- {
ServiceName = package.Id;
- }
}
public string ExecutablePath { get; set; }
@@ -73,58 +73,6 @@ public void Uninstall(ServiceDescription service)
}
}
- public class WindowsServiceLocator
- {
- [NotNull]
- public WindowsServiceFacade Find(string name)
- {
- var windowsService = TryFind(name);
- if(windowsService == null)
- {
- throw new InvalidOperationException("Service " + name + " is not installed");
- }
- return windowsService;
- }
-
- public WindowsServiceFacade TryFind(string name)
- {
- return new WindowsServiceFacade(name);
- }
- }
-
- public class WindowsServiceFacade
- {
- readonly string _name;
-
- public WindowsServiceFacade(string name)
- {
- _name = name;
- IsStopped = true;
- }
-
- public bool IsStarted { get; set; }
- public bool IsStopped { get; set; }
-
- public void StopAndWait(TimeSpan timeout)
- {
- IsStopped = true;
- IsStarted = false;
- Console.WriteLine("Service " + _name + " stopped");
- }
-
- public void Kill()
- {
- Console.WriteLine("Service " + _name + " killed");
- }
-
- public void Start()
- {
- IsStarted = true;
- IsStopped = false;
- Console.WriteLine("Service " + _name + " started");
- }
- }
-
public class NugetLocator
{
public HammerPackage FindLocalPackage(string id)
@@ -174,7 +122,7 @@ public static class ServiceKeys
public static readonly ContextKey<NugetInstaller> NugetInstaller = new ContextKey<NugetInstaller>();
public static readonly ContextKey<NugetLocator> NugetLocator = new ContextKey<NugetLocator>();
public static readonly ContextKey<WindowsServiceInstaller> WindowsServiceInstaller = new ContextKey<WindowsServiceInstaller>();
- public static readonly ContextKey<WindowsServiceLocator> WindowsServiceLocator = new ContextKey<WindowsServiceLocator>();
+ public static readonly ContextKey<IWindowsServiceLocator> WindowsServiceLocator = new ContextKey<IWindowsServiceLocator>();
}
// commands
@@ -188,9 +136,7 @@ public void Execute(IContext context)
var package = locator.FindLocalPackage(id);
if(package == null)
- {
return;
- }
context.Put(PackageKeys.LocalPackage, package, CleanupStrategy.Scope);
}
@@ -205,9 +151,7 @@ public void Execute(IContext context)
var package = locator.FindRemotePackage(id);
if(package == null)
- {
return;
- }
context.Put(PackageKeys.RemotePackage, package, CleanupStrategy.Scope);
}
@@ -257,16 +201,12 @@ public void Execute(IContext context)
var windowsService = locator.TryFind(service.ServiceName);
if(windowsService == null || windowsService.IsStopped)
- {
return;
- }
windowsService.StopAndWait(TimeSpan.FromSeconds(30));
if(windowsService.IsStopped)
- {
return;
- }
windowsService.Kill();
}
@@ -282,13 +222,9 @@ public void Execute(IContext context)
var windowsService = locator.TryFind(service.ServiceName);
if(windowsService == null)
- {
return;
- }
if(windowsService.IsStopped == false)
- {
throw new InvalidOperationException("Service " + service.ServiceName + " need to be stopped to remove it");
- }
installer.Uninstall(service);
}
@@ -317,9 +253,7 @@ public void Execute(IContext context)
var windowsService = locator.TryFind(service.ServiceName);
if(windowsService != null)
- {
return;
- }
installer.Install(service);
}
@@ -335,9 +269,7 @@ public void Execute(IContext context)
var windowsService = locator.Find(service.ServiceName);
if(windowsService.IsStarted)
- {
return;
- }
windowsService.Start();
}
@@ -395,9 +327,7 @@ public class InitializeRequiredPackageIds : ICommand
public void Execute(IContext context)
{
if(context.HasValue(PackageKeys.RequiredPackageIds))
- {
return;
- }
context.Put(PackageKeys.RequiredPackageIds, new string[0]);
}
}
@@ -412,13 +342,11 @@ public IterateRequiredPackageIds()
public override void Execute(IContext context)
{
foreach(var packageId in context.Get(PackageKeys.RequiredPackageIds))
- {
using(var scope = new Context(context))
{
scope.Put(PackageKeys.Id, packageId, CleanupStrategy.Scope);
base.Execute(scope);
}
- }
}
}
@@ -512,7 +440,7 @@ public void Run()
using(var context = Context.Empty)
{
- context.Put(ServiceKeys.WindowsServiceLocator, new WindowsServiceLocator());
+ context.Put(ServiceKeys.WindowsServiceLocator, new FakeWindowsServiceLocator());
context.Put(ServiceKeys.WindowsServiceInstaller, new WindowsServiceInstaller());
context.Put(ServiceKeys.NugetInstaller, new NugetInstaller());
context.Put(ServiceKeys.NugetLocator, new NugetLocator());
@@ -0,0 +1,38 @@
+using System;
+using CommandBasedComponents.Infrastructure;
+
+namespace CommandBasedComponents.Testing
+{
+ public class FakeWindowsServiceFacade : IWindowsServiceFacade
+ {
+ readonly string _name;
+
+ public FakeWindowsServiceFacade(string name)
+ {
+ _name = name;
+ IsStopped = true;
+ }
+
+ public bool IsStarted { get; set; }
+ public bool IsStopped { get; set; }
+
+ public void StopAndWait(TimeSpan timeout)
+ {
+ IsStopped = true;
+ IsStarted = false;
+ Console.WriteLine("Service " + _name + " stopped");
+ }
+
+ public void Kill()
+ {
+ Console.WriteLine("Service " + _name + " killed");
+ }
+
+ public void Start()
+ {
+ IsStarted = true;
+ IsStopped = false;
+ Console.WriteLine("Service " + _name + " started");
+ }
+ }
+}
@@ -0,0 +1,25 @@
+using System;
+using CommandBasedComponents.Core;
+using CommandBasedComponents.Infrastructure;
+
+namespace CommandBasedComponents.Testing
+{
+ public class FakeWindowsServiceLocator : IWindowsServiceLocator
+ {
+ [NotNull]
+ public FakeWindowsServiceFacade Find(string name)
+ {
+ var windowsService = TryFind(name);
+ if(windowsService == null)
+ {
+ throw new InvalidOperationException("Service " + name + " is not installed");
+ }
+ return windowsService;
+ }
+
+ public FakeWindowsServiceFacade TryFind(string name)
+ {
+ return new FakeWindowsServiceFacade(name);
+ }
+ }
+}

0 comments on commit 093ef09

Please sign in to comment.