-
Notifications
You must be signed in to change notification settings - Fork 589
Expand file tree
/
Copy pathPlugInManagerTest.cs
More file actions
351 lines (309 loc) · 13.7 KB
/
Copy pathPlugInManagerTest.cs
File metadata and controls
351 lines (309 loc) · 13.7 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// <copyright file="PlugInManagerTest.cs" company="MUnique">
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>
namespace MUnique.OpenMU.PlugIns.Tests;
using System.ComponentModel.Design;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using MUnique.OpenMU.PlugIns;
using MUnique.OpenMU.Tests;
/// <summary>
/// Tests for the <see cref="PlugInManager"/>.
/// </summary>
[TestFixture]
public class PlugInManagerTest
{
/// <summary>
/// Tests that every <see cref="PlugInAttribute"/>-decorated type has a unique GUID.
/// </summary>
[Test]
public void AllPluginTypesHaveUniqueGuids()
{
var duplicates = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.DefinedTypes)
.Where(t => t.GetCustomAttribute<PlugInAttribute>() != null)
.GroupBy(t => t.GUID)
.Where(g => g.Count() > 1)
.ToList();
Assert.That(duplicates, Is.Empty,
$"Duplicate plugin GUIDs: {string.Join("; ", duplicates.Select(g =>
$"{g.Key} used by {string.Join(", ", g.Select(t => t.FullName))}"))}");
}
/// <summary>
/// Tests if registering a plugin type creates a proxy for it.
/// </summary>
[Test]
public void RegisteringPlugInCreatesProxy()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
manager.RegisterPlugIn<IExamplePlugIn, ExamplePlugIn>();
var point = manager.GetPlugInPoint<IExamplePlugIn>();
Assert.That(point, Is.InstanceOf<IExamplePlugIn>());
Assert.That(point, Is.InstanceOf<IPlugInContainer<IExamplePlugIn>>());
}
/// <summary>
/// Tests if registered plugins are active by default.
/// </summary>
[Test]
public async ValueTask RegisteredPlugInsActiveByDefaultAsync()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
var plugIn = new ExamplePlugIn();
manager.RegisterPlugInAtPlugInPoint<IExamplePlugIn>(plugIn);
var player = await PlayerTestHelper.CreatePlayerAsync().ConfigureAwait(false);
var command = "test";
var args = new MyEventArgs();
var point = manager.GetPlugInPoint<IExamplePlugIn>();
point!.DoStuff(player, command, args);
Assert.That(plugIn.WasExecuted, Is.True);
}
/// <summary>
/// Tests if plugins can be deactivated and are not executed if they are.
/// </summary>
[Test]
public async ValueTask DeactivatingPlugInsAsync()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
var plugIn = new ExamplePlugIn();
manager.RegisterPlugInAtPlugInPoint<IExamplePlugIn>(plugIn);
manager.DeactivatePlugIn<ExamplePlugIn>();
var player = await PlayerTestHelper.CreatePlayerAsync().ConfigureAwait(false);
var command = "test";
var args = new MyEventArgs();
var point = manager.GetPlugInPoint<IExamplePlugIn>();
point!.DoStuff(player, command, args);
Assert.That(plugIn.WasExecuted, Is.False);
}
/// <summary>
/// Tests if deactivating a deactivated plugin doesn't cause issues.
/// </summary>
[Test]
public async ValueTask DeactivatingDeactivatedPlugInAsync()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
var plugIn = new ExamplePlugIn();
manager.RegisterPlugInAtPlugInPoint<IExamplePlugIn>(plugIn);
manager.DeactivatePlugIn<ExamplePlugIn>();
manager.DeactivatePlugIn<ExamplePlugIn>();
var player = await PlayerTestHelper.CreatePlayerAsync().ConfigureAwait(false);
var command = "test";
var args = new MyEventArgs();
var point = manager.GetPlugInPoint<IExamplePlugIn>();
point!.DoStuff(player, command, args);
Assert.That(plugIn.WasExecuted, Is.False);
}
/// <summary>
/// Tests if activating an activated plugin doesn't cause issues.
/// </summary>
[Test]
public async ValueTask ActivatingActivatedPlugInAsync()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
var plugIn = new ExamplePlugIn();
manager.RegisterPlugInAtPlugInPoint<IExamplePlugIn>(plugIn);
manager.ActivatePlugIn<ExamplePlugIn>();
manager.ActivatePlugIn<ExamplePlugIn>();
var player = await PlayerTestHelper.CreatePlayerAsync().ConfigureAwait(false);
var command = "test";
var args = new MyEventArgs();
var point = manager.GetPlugInPoint<IExamplePlugIn>();
point!.DoStuff(player, command, args);
Assert.That(plugIn.WasExecuted, Is.True);
}
/// <summary>
/// Tests if deactivating a plugin doesn't affect another plugin.
/// </summary>
[Test]
public async ValueTask DeactivatingOnePlugInDoesntAffectOthersAsync()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
var plugIn = new ExamplePlugIn();
manager.RegisterPlugInAtPlugInPoint<IExamplePlugIn>(plugIn);
manager.RegisterPlugIn<IExamplePlugIn, ExamplePlugIn.NestedPlugIn>();
manager.DeactivatePlugIn<ExamplePlugIn.NestedPlugIn>();
manager.ActivatePlugIn<ExamplePlugIn.NestedPlugIn>();
manager.DeactivatePlugIn<ExamplePlugIn.NestedPlugIn>();
var player = await PlayerTestHelper.CreatePlayerAsync().ConfigureAwait(false);
var command = "test";
var args = new MyEventArgs();
var point = manager.GetPlugInPoint<IExamplePlugIn>();
point!.DoStuff(player, command, args);
Assert.That(plugIn.WasExecuted, Is.True);
}
/// <summary>
/// Tests if the plugins are in the correct active-state if they got created with a <see cref="PlugInConfiguration"/>.
/// </summary>
/// <param name="active">If set to <c>true</c>, the <see cref="PlugInConfiguration"/> is configured to be active.</param>
[TestCase(true)]
[TestCase(false)]
public async ValueTask CreatedAndActiveByConfigurationAsync(bool active)
{
var configuration = new PlugInConfiguration
{
TypeId = typeof(ExamplePlugIn).GUID,
IsActive = active,
};
var manager = new PlugInManager(new List<PlugInConfiguration> { configuration }, new NullLoggerFactory(), this.CreateServiceProvider(), null);
var player = await PlayerTestHelper.CreatePlayerAsync().ConfigureAwait(false);
var command = "test";
var args = new MyEventArgs();
var point = manager.GetPlugInPoint<IExamplePlugIn>();
point!.DoStuff(player, command, args);
Assert.That(args.WasExecuted, Is.EqualTo(active));
}
/// <summary>
/// Tests if a custom plugin in a non-existing assembly is not created and throws no errors.
/// </summary>
[Test]
public void CustomPlugInByExternalAssemblyNotFoundDoesntThrowError()
{
var configuration = new PlugInConfiguration
{
TypeId = new Guid("D88B1ACA-42B7-4A89-B3E0-3C97AA4C8578"),
IsActive = true,
ExternalAssemblyName = "DoesNotExist.dll",
};
_ = new PlugInManager(new List<PlugInConfiguration> { configuration }, new NullLoggerFactory(), this.CreateServiceProvider(), null);
}
/// <summary>
/// Tests if an unknown plugin in the configuration doesn't cause exceptions.
/// </summary>
[Test]
public void UnknownPlugInByConfigurationDoesntThrowError()
{
var configuration = new PlugInConfiguration
{
TypeId = new Guid("A9BDA3E2-4EB6-45C3-B234-37C1819C0CB6"),
IsActive = true,
};
_ = new PlugInManager(new List<PlugInConfiguration> { configuration }, new NullLoggerFactory(), this.CreateServiceProvider(), null);
}
/// <summary>
/// Tests if activating an unknown plugin doesn't cause exceptions.
/// </summary>
[Test]
public void ActivatingUnknownPlugInDoesNotThrowError()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
manager.ActivatePlugIn(new Guid("4C38A813-F9BF-428A-8EA1-A6C90A87E583"));
}
/// <summary>
/// Tests if deactivating an unknown plugin doesn't cause exceptions.
/// </summary>
[Test]
public void DeactivatingUnknownPlugInDoesNotThrowError()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
manager.ActivatePlugIn(new Guid("4C38A813-F9BF-428A-8EA1-A6C90A87E583"));
}
/// <summary>
/// Tests if plugins can be activated and are executed if they are.
/// </summary>
[Test]
public async ValueTask ActivatingPlugInsAsync()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
var plugIn = new ExamplePlugIn();
manager.RegisterPlugInAtPlugInPoint<IExamplePlugIn>(plugIn);
manager.DeactivatePlugIn<ExamplePlugIn>();
manager.ActivatePlugIn<ExamplePlugIn>();
var player = await PlayerTestHelper.CreatePlayerAsync().ConfigureAwait(false);
var command = "test";
var args = new MyEventArgs();
var point = manager.GetPlugInPoint<IExamplePlugIn>();
point!.DoStuff(player, command, args);
Assert.That(plugIn.WasExecuted, Is.True);
}
/// <summary>
/// Tests the automatic discovery of plugins of the loaded assemblies.
/// </summary>
[Test]
public void AutoDiscovery()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
manager.DiscoverAndRegisterPlugIns();
var examplePlugInPoint = manager.GetPlugInPoint<IExamplePlugIn>();
Assert.That(examplePlugInPoint, Is.InstanceOf<IExamplePlugIn>());
}
/// <summary>
/// Tests if registering a plug in without a unique identifier throws an error.
/// </summary>
[Test]
public void RegisteringPlugInWithoutGuidThrowsError()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
Assert.Throws<ArgumentException>(() => manager.RegisterPlugIn<IExamplePlugIn, ExamplePlugIn.NestedWithoutGuid>());
}
/// <summary>
/// Tests if the strategy provider is created for registered strategy plug in.
/// </summary>
[Test]
public void StrategyProviderCreatedForRegisteredStrategyPlugIn()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
manager.RegisterPlugIn<IExampleStrategyPlugIn, ExampleStrategyPlugIn>();
var strategyProvider = manager.GetStrategyProvider<string, IExampleStrategyPlugIn>();
Assert.That(strategyProvider, Is.Not.Null);
}
/// <summary>
/// Tests if the strategy provider is not created when there is no registered strategy plug in yet.
/// </summary>
[Test]
public void StrategyProviderNotCreatedWithoutRegisteredStrategyPlugIn()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
var strategyProvider = manager.GetStrategyProvider<string, IExampleStrategyPlugIn>();
Assert.That(strategyProvider, Is.Null);
}
/// <summary>
/// Tests if the registered strategy plug in is available.
/// </summary>
[Test]
public void RegisteredStrategyPlugInAvailable()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
manager.RegisterPlugIn<IExampleStrategyPlugIn, ExampleStrategyPlugIn>();
var strategy = manager.GetStrategy<IExampleStrategyPlugIn>(ExampleStrategyPlugIn.CommandKey);
Assert.That(strategy, Is.Not.Null);
Assert.That(strategy, Is.TypeOf<ExampleStrategyPlugIn>());
}
/// <summary>
/// Tests if the registered, but deactivated strategy plug in is not available.
/// </summary>
[Test]
public void DeactivatedStrategyPlugInNotAvailable()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
manager.RegisterPlugIn<IExampleStrategyPlugIn, ExampleStrategyPlugIn>();
manager.DeactivatePlugIn<ExampleStrategyPlugIn>();
var strategy = manager.GetStrategy<IExampleStrategyPlugIn>(ExampleStrategyPlugIn.CommandKey);
Assert.That(strategy, Is.Null);
}
/// <summary>
/// Tests if registering an already registered strategy plug in does not throw an error.
/// </summary>
[Test]
public void RegisteringRegisteredStrategyPlugInDoesntThrowError()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
manager.RegisterPlugIn<IExampleStrategyPlugIn, ExampleStrategyPlugIn>();
manager.RegisterPlugIn<IExampleStrategyPlugIn, ExampleStrategyPlugIn>();
}
/// <summary>
/// Tests if no plug in point is created and returned for strategy plug ins.
/// </summary>
[Test]
public void NoPlugInPointForStrategyPlugIn()
{
var manager = new PlugInManager(null, new NullLoggerFactory(), this.CreateServiceProvider(), null);
manager.RegisterPlugIn<IExampleStrategyPlugIn, ExampleStrategyPlugIn>();
Assert.That(manager.GetPlugInPoint<IExampleStrategyPlugIn>(), Is.Null);
}
private IServiceProvider CreateServiceProvider()
{
var provider = new ServiceContainer();
provider.AddService(typeof(ILoggerFactory), new NullLoggerFactory());
return provider;
}
}