-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApplicationBootstrapperTests.cs
148 lines (131 loc) · 5.5 KB
/
ApplicationBootstrapperTests.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Dapplo - building blocks for .NET applications
// Copyright (C) 2016-2021 Dapplo
//
// For more information see: http://dapplo.net/
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
//
// This file is part of Dapplo.Addons
//
// Dapplo.Addons is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Dapplo.Addons is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have a copy of the GNU Lesser General Public License
// along with Dapplo.Addons. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
using System;
using System.Threading.Tasks;
using Autofac;
using Dapplo.Addons.Bootstrapper;
using Dapplo.Addons.Bootstrapper.Resolving;
using Dapplo.Addons.Tests.Utils;
using Dapplo.Log;
using Dapplo.Log.XUnit;
using Xunit;
using Xunit.Abstractions;
namespace Dapplo.Addons.Tests
{
[Collection("IniConfig")]
public sealed class ApplicationBootstrapperTests
{
private const string ApplicationName = "Dapplo";
private readonly string[] _testAssemblyDirectories = {
FileLocations.StartupDirectory,
#if DEBUG
@"..\..\..\..\Dapplo.Addons.TestAddon\bin\Debug\net6.0-windows",
@"..\..\..\..\Dapplo.Addons.TestAddonWithCostura\bin\Debug\net6.0-windows"
#else
@"..\..\..\..\Dapplo.Addons.TestAddon\bin\Release\net6.0-windows",
@"..\..\..\..\Dapplo.Addons.TestAddonWithCostura\bin\Release\net6.0-windows"
#endif
};
public ApplicationBootstrapperTests(ITestOutputHelper testOutputHelper)
{
LogSettings.RegisterDefaultLogger<XUnitLogger>(LogLevels.Verbose, testOutputHelper);
}
[Fact]
public async Task Test_StartupException()
{
bool isDisposed = false;
AssemblyUtils.SetEntryAssembly(GetType().Assembly);
var applicationConfig = ApplicationConfigBuilder
.Create()
.WithApplicationName(ApplicationName)
.WithScanDirectories(
_testAssemblyDirectories
)
// Add all file starting with Dapplo and ending on .dll
.WithAssemblyPatterns("Dapplo*")
.BuildApplicationConfig();
using (var bootstrapper = new ApplicationBootstrapper(applicationConfig))
{
bootstrapper.Configure();
// Makes the startup break
bootstrapper.Builder.Register(context => true);
bootstrapper.RegisterForDisposal(SimpleDisposable.Create(() => isDisposed = true));
// Initialize, so we can export
Assert.True(await bootstrapper.InitializeAsync().ConfigureAwait(false), "Not initialized");
// Start the composition, and IStartupActions
await Assert.ThrowsAsync<NotSupportedException>(async () => await bootstrapper.StartupAsync().ConfigureAwait(false));
}
// Dispose automatically calls IShutdownActions
Assert.True(isDisposed);
}
[Fact]
public void TestConstructorAndCleanup()
{
var applicationConfig = ApplicationConfigBuilder
.Create()
.WithApplicationName(ApplicationName)
.BuildApplicationConfig();
var bootstrapper = new ApplicationBootstrapper(applicationConfig);
bootstrapper.Dispose();
}
[Fact]
public void TestConstructorWithMutexAndCleanup()
{
var applicationConfig = ApplicationConfigBuilder
.Create()
.WithApplicationName("Test")
.WithMutex(Guid.NewGuid().ToString())
.BuildApplicationConfig();
using (var bootstrapper = new ApplicationBootstrapper(applicationConfig))
{
Assert.False(bootstrapper.IsAlreadyRunning);
}
}
[Fact]
public void TestNewNullApplicationName()
{
Assert.Throws<ArgumentNullException>(() => new ApplicationBootstrapper(null));
}
[Fact]
public async Task TestStartupShutdown()
{
AssemblyUtils.SetEntryAssembly(GetType().Assembly);
var applicationConfig = ApplicationConfigBuilder
.Create()
.WithApplicationName(ApplicationName)
.WithScanDirectories(_testAssemblyDirectories)
// Add all assemblies starting with Dapplo
.WithAssemblyPatterns("Dapplo*")
.BuildApplicationConfig();
using (var bootstrapper = new ApplicationBootstrapper(applicationConfig))
{
bootstrapper.Configure();
#if DEBUG
bootstrapper.EnableActivationLogging = true;
#endif
// Start the composition, and IStartupActions
Assert.True(await bootstrapper.InitializeAsync().ConfigureAwait(false), "Couldn't run");
Assert.Contains(bootstrapper.LoadedAssemblies, addon => addon.GetName().Name.EndsWith("TestAddon"));
}
// Dispose automatically calls IShutdownActions
}
}
}