Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Add tests for framework initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Friesen committed May 21, 2014
1 parent 465f4c0 commit e7b10b1
Show file tree
Hide file tree
Showing 16 changed files with 313 additions and 19 deletions.
@@ -0,0 +1,14 @@
namespace Rock.Core.IntegrationTests.AssemblyInitialization
{
// NOTE: This module initializer exists only to ensure that the module initialization tests
// run successfully, even if the integration tests start first. NUnit seems to create a single
// app domain for all of its tests, so order is important. This is also the reason why this
// project references the unit test project.
internal static class ModuleInitializer
{
internal static void Run()
{
Rock.AssemblyInitialization.ModuleInitializer.Run();
}
}
}
12 changes: 12 additions & 0 deletions Rock.Core.IntegrationTests/Rock.Core.IntegrationTests.csproj
Expand Up @@ -45,10 +45,19 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInitialization\ModuleInitializer.cs" />
<Compile Include="Net\EndpointDetectorTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Rock.Core.ModuleInitializer.UnitTestSupport\Rock.Core.ModuleInitializer.UnitTestSupport.csproj">
<Project>{34d6b6ad-5725-469c-af77-eab7dbe61666}</Project>
<Name>Rock.Core.ModuleInitializer.UnitTestSupport</Name>
</ProjectReference>
<ProjectReference Include="..\Rock.Core.UnitTests\Rock.Core.UnitTests.csproj">
<Project>{6c71e326-e9b5-407e-889d-443311b0b22b}</Project>
<Name>Rock.Core.UnitTests</Name>
</ProjectReference>
<ProjectReference Include="..\Rock.Core\Rock.Core.csproj">
<Project>{0feb58cd-b15c-4cdc-83d6-7621c1db98c7}</Project>
<Name>Rock.Core</Name>
Expand All @@ -58,6 +67,9 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="InjectModuleInitializer" AfterTargets="AfterBuild">
<Exec Command="..\packages\InjectModuleInitializer.1.3.1\tools\InjectModuleInitializer.exe &quot;$(TargetPath)&quot;" />
</Target>
<!-- 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">
Expand Down
1 change: 1 addition & 0 deletions Rock.Core.IntegrationTests/packages.config
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="InjectModuleInitializer" version="1.3.1" targetFramework="net45" developmentDependency="true" />
<package id="NUnit" version="2.6.3" targetFramework="net45" />
<package id="XSerializer" version="0.1.11" targetFramework="net45" />
</packages>
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using Rock.AssemblyInitialization;

namespace Rock.Core.ModuleInitializer.UnitTestSupport.AssemblyInitialization
{
public static class FrameworkInitializerTestHelper
{
private static readonly List<Type> _calledFrameworkInitializers = new List<Type>();

/// <summary>
/// A method to be called when an inheritor of <see cref="IFrameworkInitializer"/> has its
/// <see cref="IFrameworkInitializer.Initialize"/> method called.
/// </summary>
public static void RegisterInitializeCall(Type frameworkInitializerType)
{
_calledFrameworkInitializers.Add(frameworkInitializerType);
}

/// <summary>
/// Gets a collection of types that are in the order that they were added via the
/// <see cref="RegisterInitializeCall"/> method.
/// </summary>
public static IEnumerable<Type> CalledFrameworkInitializerTypes
{
get { return _calledFrameworkInitializers; }
}
}
}
@@ -0,0 +1,10 @@
namespace Rock.Core.ModuleInitializer.UnitTestSupport.AssemblyInitialization
{
internal static class ModuleInitializer
{
internal static void Run()
{
Rock.AssemblyInitialization.ModuleInitializer.Run();
}
}
}
@@ -0,0 +1,21 @@
using Rock.AssemblyInitialization;

namespace Rock.Core.ModuleInitializer.UnitTestSupport.AssemblyInitialization
{
// ReSharper disable once InconsistentNaming
/// <summary>
/// This inheritor of IFrameworkInitializer counts the number of times any instance has its Initialize() method
/// invoked. It also registers the instance call with FrameworkInitializerTestHelper when its Initializer()
/// method is called.
/// </summary>
public class Rock_Core_ModuleInitializer_UnitTestSupport_FrameworkInitializer : IFrameworkInitializer
{
public void Initialize()
{
InitializeInvocationCount++;
FrameworkInitializerTestHelper.RegisterInitializeCall(GetType());
}

public static int InitializeInvocationCount { get; private set; }
}
}
@@ -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("Rock.Core.ModuleInitializer.UnitTestSupport")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Rock.Core.ModuleInitializer.UnitTestSupport")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[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("5e3dbeb1-6c6c-4549-8aa8-0f10844763e9")]

// 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")]
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{34D6B6AD-5725-469C-AF77-EAB7DBE61666}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Rock.Core.ModuleInitializer.UnitTestSupport</RootNamespace>
<AssemblyName>Rock.Core.ModuleInitializer.UnitTestSupport</AssemblyName>
<TargetFrameworkVersion>v4.5</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="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInitialization\FrameworkInitializerTestHelper.cs" />
<Compile Include="AssemblyInitialization\ModuleInitializer.cs" />
<Compile Include="AssemblyInitialization\Rock_Core_ModuleInitializer_UnitTestSupport_FrameworkInitializer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Rock.Core\Rock.Core.csproj">
<Project>{0feb58cd-b15c-4cdc-83d6-7621c1db98c7}</Project>
<Name>Rock.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="InjectModuleInitializer" AfterTargets="AfterBuild">
<Exec Command="..\packages\InjectModuleInitializer.1.3.1\tools\InjectModuleInitializer.exe &quot;$(TargetPath)&quot;" />
</Target>
<!-- 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>
4 changes: 4 additions & 0 deletions Rock.Core.ModuleInitializer.UnitTestSupport/packages.config
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="InjectModuleInitializer" version="1.3.1" targetFramework="net45" developmentDependency="true" />
</packages>
10 changes: 10 additions & 0 deletions Rock.Core.UnitTests/AssemblyInitialization/ModuleInitializer.cs
@@ -0,0 +1,10 @@
namespace Rock.Core.UnitTests.AssemblyInitialization
{
public static class ModuleInitializer
{
public static void Run()
{
global::Rock.AssemblyInitialization.ModuleInitializer.Run();
}
}
}
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Rock.AssemblyInitialization;
using Rock.Core.ModuleInitializer.UnitTestSupport.AssemblyInitialization;

namespace Rock.Core.UnitTests.AssemblyInitialization
{
public class ModuleInitializerTests
{
[Test]
public void EachImplementorOfIFrameworkInitializerHasItsInitializeMethodInvokedOnce()
{
Assert.That(Rock_Core_UnitTests_FrameworkInitializer.InitializeInvocationCount, Is.EqualTo(1));
Assert.That(Rock_Core_ModuleInitializer_UnitTestSupport_FrameworkInitializer.InitializeInvocationCount, Is.EqualTo(1));
}

/// <summary>
/// Verify that instances of the inheritors of <see cref="IFrameworkInitializer"/> are executed
/// in the correct order. The order should be such that an instance whose type's assembly
/// is referenced by the assembly of another instance's type in the list should be before those
/// other types.
/// </summary>
[Test]
public void ImplementorsOfFrameworkInitializerAreExecutedInAssemblyReferenceOrder()
{
Assert.That(FrameworkInitializerTestHelper.CalledFrameworkInitializerTypes, Is.EqualTo(TypesInAssemblyReferenceOrder));
}

/// <summary>
/// Get a collection of types, in "assembly reference" order. Since the assembly of
/// <see cref="Rock_Core_ModuleInitializer_UnitTestSupport_FrameworkInitializer"/> is referenced by
/// the assembly of <see cref="Rock_Core_UnitTests_FrameworkInitializer"/>,
/// <see cref="Rock_Core_ModuleInitializer_UnitTestSupport_FrameworkInitializer"/> is first, and
/// <see cref="Rock_Core_UnitTests_FrameworkInitializer"/> is second.
/// </summary>
private IEnumerable<Type> TypesInAssemblyReferenceOrder
{
get
{
yield return typeof(Rock_Core_ModuleInitializer_UnitTestSupport_FrameworkInitializer);
yield return typeof(Rock_Core_UnitTests_FrameworkInitializer);
}
}
}
}
@@ -0,0 +1,22 @@
using Rock.AssemblyInitialization;
using Rock.Core.ModuleInitializer.UnitTestSupport.AssemblyInitialization;

namespace Rock.Core.UnitTests.AssemblyInitialization
{
// ReSharper disable once InconsistentNaming
/// <summary>
/// This inheritor of IFrameworkInitializer counts the number of times any instance has its Initialize() method
/// invoked. It also registers the instance call with FrameworkInitializerTestHelper when its Initializer()
/// method is called.
/// </summary>
public class Rock_Core_UnitTests_FrameworkInitializer : IFrameworkInitializer
{
public void Initialize()
{
InitializeInvocationCount++;
FrameworkInitializerTestHelper.RegisterInitializeCall(GetType());
}

public static int InitializeInvocationCount { get; private set; }
}
}
10 changes: 10 additions & 0 deletions Rock.Core.UnitTests/Rock.Core.UnitTests.csproj
Expand Up @@ -48,6 +48,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInitialization\ModuleInitializerTests.cs" />
<Compile Include="Collections\AsKeyedExtensionTests.cs" />
<Compile Include="Collections\ConcatExtensionsTests.cs" />
<Compile Include="Collections\FunctionalKeyedCollectionTests.cs" />
Expand All @@ -62,20 +63,29 @@
<Compile Include="KeyValueStores\Extensions\GetItemsExtensionTests.cs" />
<Compile Include="KeyValueStores\Extensions\KeyValueStoreExtensionsTestsBase.cs" />
<Compile Include="KeyValueStores\Extensions\TryGetExtensionsTests.cs" />
<Compile Include="AssemblyInitialization\ModuleInitializer.cs" />
<Compile Include="Net\EndpointSelectorTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rock.IO\TempStorageTests.cs" />
<Compile Include="AssemblyInitialization\Rock_Core_UnitTests_FrameworkInitializer.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Rock.Core.ModuleInitializer.UnitTestSupport\Rock.Core.ModuleInitializer.UnitTestSupport.csproj">
<Project>{34d6b6ad-5725-469c-af77-eab7dbe61666}</Project>
<Name>Rock.Core.ModuleInitializer.UnitTestSupport</Name>
</ProjectReference>
<ProjectReference Include="..\Rock.Core\Rock.Core.csproj">
<Project>{0feb58cd-b15c-4cdc-83d6-7621c1db98c7}</Project>
<Name>Rock.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="InjectModuleInitializer" AfterTargets="AfterBuild">
<Exec Command="..\packages\InjectModuleInitializer.1.3.1\tools\InjectModuleInitializer.exe &quot;$(TargetPath)&quot;" />
</Target>
<!-- 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">
Expand Down
1 change: 1 addition & 0 deletions Rock.Core.UnitTests/packages.config
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="InjectModuleInitializer" version="1.3.1" targetFramework="net45" developmentDependency="true" />
<package id="Moq" version="4.2.1402.2112" targetFramework="net45" />
<package id="NUnit" version="2.6.3" targetFramework="net45" />
<package id="XSerializer" version="0.1.11" targetFramework="net45" />
Expand Down
6 changes: 6 additions & 0 deletions Rock.Core.sln
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rock.Core.UnitTests", "Rock
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rock.Core.IntegrationTests", "Rock.Core.IntegrationTests\Rock.Core.IntegrationTests.csproj", "{B189B018-183A-47B8-A410-BCD9482D62FC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rock.Core.ModuleInitializer.UnitTestSupport", "Rock.Core.ModuleInitializer.UnitTestSupport\Rock.Core.ModuleInitializer.UnitTestSupport.csproj", "{34D6B6AD-5725-469C-AF77-EAB7DBE61666}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{B189B018-183A-47B8-A410-BCD9482D62FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B189B018-183A-47B8-A410-BCD9482D62FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B189B018-183A-47B8-A410-BCD9482D62FC}.Release|Any CPU.Build.0 = Release|Any CPU
{34D6B6AD-5725-469C-AF77-EAB7DBE61666}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34D6B6AD-5725-469C-AF77-EAB7DBE61666}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34D6B6AD-5725-469C-AF77-EAB7DBE61666}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34D6B6AD-5725-469C-AF77-EAB7DBE61666}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit e7b10b1

Please sign in to comment.