-
Notifications
You must be signed in to change notification settings - Fork 46
/
EndpointCustomizationConfigurationExtensions.cs
51 lines (42 loc) · 2.06 KB
/
EndpointCustomizationConfigurationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
namespace ServiceControl.AcceptanceTesting.EndpointTemplates
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NServiceBus.AcceptanceTesting.Support;
using NServiceBus.Hosting.Helpers;
public static class EndpointCustomizationConfigurationExtensions
{
public static IEnumerable<Type> GetTypesScopedByTestClass<TBootstrapper>(this EndpointCustomizationConfiguration endpointConfiguration)
{
var assemblies = new AssemblyScanner().GetScannableAssemblies();
var assembliesToScan = assemblies.Assemblies
//exclude acceptance tests by default
.Where(a => a != Assembly.GetExecutingAssembly() && a != typeof(TBootstrapper).Assembly)
.Where(a => a.FullName.StartsWith("ServiceControl") == false)
.ToList();
var types = assembliesToScan
.SelectMany(a => a.GetTypes());
types = types.Union(GetNestedTypeRecursive(endpointConfiguration.BuilderType.DeclaringType, endpointConfiguration.BuilderType));
types = types.Union(endpointConfiguration.TypesToInclude);
return types.Where(t => !endpointConfiguration.TypesToExclude.Contains(t)).ToList();
}
static IEnumerable<Type> GetNestedTypeRecursive(Type rootType, Type builderType)
{
if (rootType == null)
{
throw new InvalidOperationException("Make sure you nest the endpoint infrastructure inside the TestFixture as nested classes");
}
yield return rootType;
if (typeof(IEndpointConfigurationFactory).IsAssignableFrom(rootType) && rootType != builderType)
{
yield break;
}
foreach (var nestedType in rootType.GetNestedTypes(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).SelectMany(t => GetNestedTypeRecursive(t, builderType)))
{
yield return nestedType;
}
}
}
}