-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
ExportFactoryTests.cs
239 lines (202 loc) · 7.27 KB
/
ExportFactoryTests.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Composition.Hosting;
using System.Composition.Hosting.Core;
using System.Composition.Runtime;
using System.Composition.UnitTests.Util;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace System.Composition.UnitTests
{
public class ExportFactoryTests : ContainerTests
{
private static class Boundaries
{
public const string DataConsistency = "DataConsistency";
public const string UserIdentity = "UserIdentity";
}
[Shared, Export]
public class SharedUnbounded { }
[Shared(Boundaries.DataConsistency), Export]
public class SharedBoundedByDC { }
[Export]
public class DataConsistencyBoundaryProvider
{
[Import, SharingBoundary(Boundaries.DataConsistency)]
public ExportFactory<CompositionContext> SharingScopeFactory { get; set; }
}
[Export]
public class SharedPartConsumer
{
public SharedBoundedByDC Sc1, Sc2;
[ImportingConstructor]
public SharedPartConsumer(SharedBoundedByDC sc1, SharedBoundedByDC sc2)
{
Sc1 = sc1;
Sc2 = sc2;
}
}
public interface IA { }
[Export(typeof(IA))]
public class A : IA, IDisposable
{
public bool IsDisposed;
public void Dispose()
{
IsDisposed = true;
}
}
[Shared, Export]
public class GloballySharedWithDependency
{
public IA A;
[ImportingConstructor]
public GloballySharedWithDependency(IA a)
{
A = a;
}
}
[Export]
public class UseExportFactory
{
[Import]
public ExportFactory<IA> AFactory { get; set; }
}
[Export]
public class DisposesFactoryProduct : IDisposable
{
private readonly ExportFactory<IA> _factory;
[ImportingConstructor]
public DisposesFactoryProduct(ExportFactory<IA> factory)
{
_factory = factory;
}
public Export<IA> Product { get; set; }
public void CreateProduct()
{
Product = _factory.CreateExport();
}
public void Dispose()
{
Product.Dispose();
}
}
[Fact]
public void SharedPartsAreSharedBetweenAllScopes()
{
var cc = CreateContainer(typeof(SharedUnbounded), typeof(DataConsistencyBoundaryProvider));
var bp = cc.GetExport<DataConsistencyBoundaryProvider>().SharingScopeFactory;
var x = bp.CreateExport().Value.GetExport<SharedUnbounded>();
var y = bp.CreateExport().Value.GetExport<SharedUnbounded>();
Assert.Same(x, y);
}
[Fact]
public void TheSameSharedInstanceIsReusedWithinItsSharingBoundary()
{
var cc = CreateContainer(typeof(SharedBoundedByDC), typeof(SharedPartConsumer), typeof(DataConsistencyBoundaryProvider));
var sf = cc.GetExport<DataConsistencyBoundaryProvider>().SharingScopeFactory;
var s = sf.CreateExport();
var s2 = sf.CreateExport();
var x = s.Value.GetExport<SharedPartConsumer>();
var y = s2.Value.GetExport<SharedPartConsumer>();
Assert.Same(x.Sc1, x.Sc2);
Assert.NotSame(x.Sc1, y.Sc1);
}
[Fact]
public void NonSharedInstancesCreatedByAnExportFactoryAreControlledByTheirExportLifetimeContext()
{
var cc = CreateContainer(typeof(A), typeof(UseExportFactory));
var bef = cc.GetExport<UseExportFactory>();
var a = bef.AFactory.CreateExport();
Assert.IsAssignableFrom<A>(a.Value);
Assert.False(((A)a.Value).IsDisposed);
a.Dispose();
Assert.True(((A)a.Value).IsDisposed);
}
[Fact]
public void DependenciesOfSharedPartsAreResolvedInTheGlobalScope()
{
var cc = new ContainerConfiguration()
.WithParts(typeof(GloballySharedWithDependency), typeof(A), typeof(DataConsistencyBoundaryProvider))
.CreateContainer();
var s = cc.GetExport<DataConsistencyBoundaryProvider>().SharingScopeFactory.CreateExport();
var g = s.Value.GetExport<GloballySharedWithDependency>();
s.Dispose();
var a = (A)g.A;
Assert.False(a.IsDisposed);
cc.Dispose();
Assert.True(a.IsDisposed);
}
[Fact]
public void WhenABoundaryIsPresentBoundedPartsCannotBeCreatedOutsideIt()
{
var container = CreateContainer(typeof(DataConsistencyBoundaryProvider), typeof(SharedBoundedByDC));
var x = Assert.Throws<CompositionFailedException>(() => container.GetExport<SharedBoundedByDC>());
}
[Fact]
public void TheProductOfAnExportFactoryCanBeDisposedDuringDisposalOfTheParent()
{
var container = new ContainerConfiguration()
.WithPart<DisposesFactoryProduct>()
.WithPart<A>()
.CreateContainer();
var dfp = container.GetExport<DisposesFactoryProduct>();
dfp.CreateProduct();
var a = dfp.Product.Value as A;
container.Dispose();
Assert.True(a.IsDisposed);
}
[Export("Special", typeof(IA))]
public class A1 : IA { }
[Export("Special", typeof(IA))]
public class A2 : IA { }
[Export]
public class AConsumer
{
[ImportMany("Special")]
public ExportFactory<IA>[] AFactories { get; set; }
}
[Fact]
public void ExportFactoryCanBeComposedWithImportManyAndNames()
{
var cc = CreateContainer(typeof(AConsumer), typeof(A1), typeof(A2));
var cons = cc.GetExport<AConsumer>();
Assert.Equal(2, cons.AFactories.Length);
}
[Export]
public class Disposable : IDisposable
{
public bool IsDisposed { get; set; }
public void Dispose()
{
IsDisposed = true;
}
}
[Export]
public class HasDisposableDependency
{
[Import]
public Disposable Dependency { get; set; }
}
[Export]
public class HasFactory
{
[Import]
public ExportFactory<HasDisposableDependency> Factory { get; set; }
}
[Fact]
public void WhenReleasingAnExportFromAnExportFactoryItsNonSharedDependenciesAreDisposed()
{
var cc = CreateContainer(typeof(Disposable), typeof(HasDisposableDependency), typeof(HasFactory));
var hf = cc.GetExport<HasFactory>();
var hddx = hf.Factory.CreateExport();
var hdd = hddx.Value;
hddx.Dispose();
Assert.True(hdd.Dependency.IsDisposed);
}
}
}