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
|
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Factories;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace System.ComponentModel.Composition
{
[TestClass]
public class CatalogFilteringTests
{
[TestMethod]
public void FilteredCatalog_ScopeA()
{
var cat = GetCatalog();
var contA = new CompositionContainer(ScopeCatalog(cat, "A"));
Assert.IsTrue(contA.IsPresent<ScopeAComponent1>());
Assert.IsTrue(contA.IsPresent<ScopeAComponent2>());
Assert.IsFalse(contA.IsPresent<ScopeBComponent>());
Assert.IsFalse(contA.IsPresent<ScopeCComponent>());
}
[TestMethod]
public void FilteredCatalog_ScopeB()
{
var cat = GetCatalog();
var contA = new CompositionContainer(ScopeCatalog(cat, "A"));
var contB = new CompositionContainer(ScopeCatalog(cat, "B"), contA);
Assert.IsTrue(contB.IsPresent<ScopeAComponent1>());
Assert.IsTrue(contB.IsPresent<ScopeAComponent2>());
Assert.IsTrue(contB.IsPresent<ScopeBComponent>());
Assert.IsFalse(contB.IsPresent<ScopeCComponent>());
}
[TestMethod]
public void FilteredCatalog_ScopeC()
{
var cat = GetCatalog();
var contA = new CompositionContainer(ScopeCatalog(cat, "A"));
var contB = new CompositionContainer(ScopeCatalog(cat, "B"), contA);
var contC = new CompositionContainer(ScopeCatalog(cat, "C"), contB);
Assert.IsTrue(contC.IsPresent<ScopeAComponent1>());
Assert.IsTrue(contC.IsPresent<ScopeAComponent2>());
Assert.IsTrue(contC.IsPresent<ScopeBComponent>());
Assert.IsTrue(contC.IsPresent<ScopeCComponent>());
}
[TestMethod]
[Ignore]
[WorkItem(812029)]
public void FilteredCatalog_EventsFired()
{
var aggCatalog = CatalogFactory.CreateAggregateCatalog();
var cat1 = CatalogFactory.CreateAttributed(typeof(ScopeAComponent1), typeof(ScopeBComponent));
var filteredCatalog = CatalogFactory.CreateFiltered(aggCatalog,
partDef => partDef.Metadata.ContainsKey("Scope") &&
partDef.Metadata["Scope"].ToString() == "A");
var container = ContainerFactory.Create(filteredCatalog);
Assert.IsFalse(container.IsPresent<ScopeAComponent1>(), "sa before add");
Assert.IsFalse(container.IsPresent<ScopeBComponent>(), "sb before add");
aggCatalog.Catalogs.Add(cat1);
Assert.IsTrue(container.IsPresent<ScopeAComponent1>(), "sa after add");
Assert.IsFalse(container.IsPresent<ScopeBComponent>(), "sb after add");
aggCatalog.Catalogs.Remove(cat1);
Assert.IsFalse(container.IsPresent<ScopeAComponent1>(), "sa after remove");
Assert.IsFalse(container.IsPresent<ScopeBComponent>(), "sb after remove");
}
private ComposablePartCatalog GetCatalog()
{
return CatalogFactory.CreateAttributed(
typeof(ScopeAComponent1),
typeof(ScopeAComponent2),
typeof(ScopeBComponent),
typeof(ScopeCComponent));
}
private ComposablePartCatalog ScopeCatalog(ComposablePartCatalog catalog, string scope)
{
return CatalogFactory.CreateFiltered(catalog,
partDef => partDef.Metadata.ContainsKey("Scope") &&
partDef.Metadata["Scope"].ToString() == scope);
}
[Export]
[PartMetadata("Scope", "A")]
public class ScopeAComponent1
{
}
[Export]
[PartMetadata("Scope", "A")]
public class ScopeAComponent2
{
[Import]
public ScopeAComponent1 ScopeA { get; set; }
}
[Export]
[PartMetadata("Scope", "B")]
public class ScopeBComponent
{
[Import]
public ScopeAComponent1 ScopeA { get; set; }
}
[Export]
[PartMetadata("Scope", "C")]
public class ScopeCComponent
{
[Import]
public ScopeBComponent ScopeB { get; set; }
}
}
}
|