Skip to content

Commit

Permalink
Added tests for Windsor configuration and controller naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
dlidstrom committed Jun 15, 2011
1 parent 77bedd8 commit c892fe7
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 26 deletions.
15 changes: 9 additions & 6 deletions .gitignore
@@ -1,6 +1,9 @@

*.suo
*.user
*.Cache
SnittListan/bin
SnittListan/obj

*.suo
*.user
*.Cache
SnittListan/bin
SnittListan/obj

SnittListan.Test/bin
SnittListan.Test/obj
106 changes: 106 additions & 0 deletions SnittListan.Test/ControllersInstallerTest.cs
@@ -0,0 +1,106 @@
using System;
using System.Linq;
using System.Web.Mvc;
using Castle.Core.Internal;
using Castle.MicroKernel;
using Castle.Windsor;
using SnittListan.Controllers;
using SnittListan.Installers;
using Xunit;
using Castle.Core;

namespace SnittListan.Test
{
public class ControllersInstallerTest
{
private readonly IWindsorContainer container;

public ControllersInstallerTest()
{
container = new WindsorContainer()
.Install(new ControllerInstaller());
}

[Fact]
public void AllControllersImplementIController()
{
var allHandlers = GetAllHandlers(container);
var controllerHandlers = GetHandlersFor(typeof(IController), container);
Assert.NotEmpty(allHandlers);
Assert.Equal(allHandlers, controllerHandlers);
}

[Fact]
public void AllControllersAreRegistered()
{
// Is<TType> is a helper extension method from Windsor
// which behaves like 'is' keyword in C# but at a Type, not instance level

var allControllers = GetPublicClassesFromApplicationAssembly(c => c.Is<IController>());
var registeredControllers = GetImplementationTypesFor(typeof(IController), container);
Assert.Equal(allControllers, registeredControllers);
}

[Fact]
public void AllAndOnlyControllersHaveControllerSuffix()
{
var allControllers = GetPublicClassesFromApplicationAssembly(c => c.Name.EndsWith("Controller"));
var registeredControllers = GetImplementationTypesFor(typeof(IController), container);
Assert.Equal(allControllers, registeredControllers);
}

[Fact]
public void AllAndOnlyControllersLiveInControllersNamespace()
{
var allControllers = GetPublicClassesFromApplicationAssembly(c => c.Namespace.Contains("Controllers"));
var registeredControllers = GetImplementationTypesFor(typeof(IController), container);
Assert.Equal(allControllers, registeredControllers);
}

[Fact]
public void AllControllersAreTransient()
{
var nonTransientControllers = GetHandlersFor(typeof(IController), container)
.Where(c => c.ComponentModel.LifestyleType != LifestyleType.Transient)
.ToArray();
Assert.Empty(nonTransientControllers);
}

[Fact]
public void AllControllersExposeThemselvesAsService()
{
var controllersWithWrongName = GetHandlersFor(typeof(IController), container)
.Where(c => c.Service != c.ComponentModel.Implementation)
.ToArray();
Assert.Empty(controllersWithWrongName);
}

private static IHandler[] GetAllHandlers(IWindsorContainer container)
{
return GetHandlersFor(typeof(object), container);
}

private static IHandler[] GetHandlersFor(Type type, IWindsorContainer container)
{
return container.Kernel.GetAssignableHandlers(type);
}

private static Type[] GetImplementationTypesFor(Type type, IWindsorContainer container)
{
return GetHandlersFor(type, container)
.Select(h => h.ComponentModel.Implementation)
.OrderBy(t => t.Name)
.ToArray();
}

private static Type[] GetPublicClassesFromApplicationAssembly(Predicate<Type> where)
{
return typeof(HomeController).Assembly.GetExportedTypes()
.Where(t => t.IsClass)
.Where(t => t.IsAbstract == false)
.Where(where.Invoke)
.OrderBy(t => t.Name)
.ToArray();
}
}
}
36 changes: 36 additions & 0 deletions SnittListan.Test/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SnittListan.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("SnittListan.Test")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3a0431ce-e7b6-4406-bbc3-e85a10127df9")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
79 changes: 79 additions & 0 deletions SnittListan.Test/SnittListan.Test.csproj
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B3C8CEC4-7F39-4223-A1AB-45EAD2DBE2B9}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SnittListan.Test</RootNamespace>
<AssemblyName>SnittListan.Test</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core">
<HintPath>..\packages\Castle.Core.2.5.2\lib\NET35\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Castle.Windsor">
<HintPath>..\packages\Castle.Windsor.2.5.3\lib\NET40\Castle.Windsor.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="xunit">
<HintPath>..\packages\xunit.1.8.0.1545\lib\xunit.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ControllersInstallerTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SnittListan\SnittListan.csproj">
<Project>{5E81B34E-A864-4F3C-8B13-9843FB029EA7}</Project>
<Name>SnittListan</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>$(SolutionDir)packages\xunit.1.8.0.1545\lib\xunit.console.clr4.exe $(TargetPath)</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
6 changes: 6 additions & 0 deletions SnittListan.Test/packages.config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xunit" version="1.8.0.1545" />
<package id="Castle.Core" version="2.5.2" />
<package id="Castle.Windsor" version="2.5.3" />
</packages>
46 changes: 26 additions & 20 deletions SnittListan.sln
@@ -1,20 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Web Developer Express 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SnittListan", "SnittListan\SnittListan.csproj", "{5E81B34E-A864-4F3C-8B13-9843FB029EA7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5E81B34E-A864-4F3C-8B13-9843FB029EA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E81B34E-A864-4F3C-8B13-9843FB029EA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E81B34E-A864-4F3C-8B13-9843FB029EA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E81B34E-A864-4F3C-8B13-9843FB029EA7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Web Developer Express 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SnittListan", "SnittListan\SnittListan.csproj", "{5E81B34E-A864-4F3C-8B13-9843FB029EA7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SnittListan.Test", "SnittListan.Test\SnittListan.Test.csproj", "{B3C8CEC4-7F39-4223-A1AB-45EAD2DBE2B9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5E81B34E-A864-4F3C-8B13-9843FB029EA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E81B34E-A864-4F3C-8B13-9843FB029EA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E81B34E-A864-4F3C-8B13-9843FB029EA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E81B34E-A864-4F3C-8B13-9843FB029EA7}.Release|Any CPU.Build.0 = Release|Any CPU
{B3C8CEC4-7F39-4223-A1AB-45EAD2DBE2B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3C8CEC4-7F39-4223-A1AB-45EAD2DBE2B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3C8CEC4-7F39-4223-A1AB-45EAD2DBE2B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3C8CEC4-7F39-4223-A1AB-45EAD2DBE2B9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

0 comments on commit c892fe7

Please sign in to comment.