Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExportableAttributeTests.cs « Composition « ComponentModel « System « ComponentModelUnitTest « Tests « System.ComponentModel.Composition « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 51b23d42a5fa80c4b37cd3fe93801104acb24573 (plain)
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
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Factories;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System.ComponentModel.Composition.Primitives;

namespace System.ComponentModel.Composition
{
    [TestClass]
    public class MetadataAttributeTests
    {
        [TestMethod]
        [TestProperty("Type", "Integration")]
        public void UntypedStructureTest()
        {
            var container = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();
            batch.AddPart(AttributedModelServices.CreatePart(new BasicTestComponent()));
            container.Compose(batch);
            var export = container.GetExport<BasicTestComponent, IDictionary<string, object>>();
            
            
            Assert.IsNotNull(export.Metadata, "It should have metadata");
            Assert.AreEqual("One", export.Metadata["String1"], "Property attribute should copy straight across");
            Assert.AreEqual("Two", export.Metadata["String2"], "Property attribute should copy straight across");
            var e = export.Metadata["Numbers"] as IList<int>;
            Assert.IsNotNull(e, "Should get a collection of numbers");
            Assert.IsTrue(e.Contains(1), "Should have 1 in the list");
            Assert.IsTrue(e.Contains(2), "Should have 2 in the list");
            Assert.IsTrue(e.Contains(3), "Should have 3 in the list");
            Assert.AreEqual(3, e.Count, "Should be three numbers total");
        }

#if !SILVERLIGHT
		// Silverlight doesn't support strongly typed metadata
        [TestMethod]
        [TestProperty("Type", "Integration")]
        public void StronglyTypedStructureTest()
        {
            var container = ContainerFactory.Create();
            CompositionBatch batch = new CompositionBatch();
            batch.AddPart(AttributedModelServices.CreatePart(new BasicTestComponent()));
            container.Compose(batch);

            var export = container.GetExport<BasicTestComponent, IStronglyTypedStructure>();

            Assert.IsNotNull(export.Metadata, "It should have metadata");
            Assert.AreEqual("One", export.Metadata.String1, "Property should copy straight across");
            Assert.AreEqual("Two", export.Metadata.String2, "Property should copy straight across");
            Assert.IsTrue(export.Metadata.Numbers.Contains(1), "Should have 1 in the list");
            Assert.IsTrue(export.Metadata.Numbers.Contains(2), "Should have 2 in the list");
            Assert.IsTrue(export.Metadata.Numbers.Contains(3), "Should have 3 in the list");
            Assert.AreEqual(3, export.Metadata.Numbers.Length, "Should be three numbers total");
        }
#endif //!SILVERLIGHT

        [Export]
        // Should cause a conflict with the multiple nature of Name.Bar because 
        // it isn't marked with IsMultiple=true
        [ExportMetadata("Bar", "Blah")]
        [Name("MEF")]
        [Name("MEF2")]
        [PartNotDiscoverable]
        public class BasicTestComponentWithInvalidMetadata
        {
        }

        [TestMethod]
        [TestProperty("Type", "Integration")]
        public void InvalidMetadataAttributeTest()
        {
            ComposablePart part = AttributedModelServices.CreatePart(new BasicTestComponentWithInvalidMetadata());
            ExportDefinition export = part.ExportDefinitions.First();

            var ex = ExceptionAssert.Throws<InvalidOperationException>(RetryMode.DoNotRetry, () => 
            {
                var metadata = export.Metadata;
            });
            
            Assert.IsTrue(ex.Message.Contains("Bar"));
        }

        [AttributeUsage(AttributeTargets.All)]
        [MetadataAttribute]
        public class MetadataWithInvalidCustomAttributeType : Attribute
        {
            public PersonClass Person { get { return new PersonClass(); } }

            public class PersonClass
            {
                public string First { get { return "George"; } }
                public string Last { get { return "Washington"; } }
            }
        }

        [Export]
        [MetadataWithInvalidCustomAttributeType]
        [PartNotDiscoverable]
        public class ClassWithInvalidCustomAttributeType
        {

        }

        [TestMethod]
        public void InvalidAttributType_CustomType_ShouldThrow()
        {
            ComposablePart part = AttributedModelServices.CreatePart(new ClassWithInvalidCustomAttributeType());
            ExportDefinition export = part.ExportDefinitions.First();
            
            // Should throw InvalidOperationException during discovery because
            // the person class is an invalid metadata type
            ExceptionAssert.Throws<InvalidOperationException>(RetryMode.DoNotRetry, () =>
            {
                var metadata = export.Metadata;
            });
        }

        [AttributeUsage(AttributeTargets.All)]
        [MetadataAttribute]
        public class MetadataWithInvalidVersionPropertyAttributeType : Attribute
        {
            public MetadataWithInvalidVersionPropertyAttributeType()
            {
                this.Version = new Version(1, 1);
            }
            public Version Version { get; set; }
        }

        [Export]
        [MetadataWithInvalidVersionPropertyAttributeType]
        [PartNotDiscoverable]
        public class ClassWithInvalidVersionPropertyAttributeType
        {

        }

        [TestMethod]
        public void InvalidAttributType_VersionPropertyType_ShouldThrow()
        {
            ComposablePart part = AttributedModelServices.CreatePart(new ClassWithInvalidVersionPropertyAttributeType());
            ExportDefinition export = part.ExportDefinitions.First();
            
            // Should throw InvalidOperationException during discovery because
            // the person class is an invalid metadata type
            ExceptionAssert.Throws<InvalidOperationException>(RetryMode.DoNotRetry, () =>
            {
                var metadata = export.Metadata;
            });
        }

        [MetadataAttribute]
        public class BaseMetadataAttribute : Attribute
        {
            public string BaseKey { get { return "BaseValue"; } }
        }

        public class DerivedMetadataAttribute : BaseMetadataAttribute
        {
            public string DerivedKey { get { return "DerivedValue"; } }
        }

        [Export]
        [DerivedMetadata]
        public class ExportWithDerivedMetadataAttribute { }

        [TestMethod]
        public void DerivedMetadataAttributeAttribute_ShouldSupplyMetadata()
        {
            ComposablePart part = AttributedModelServices.CreatePart(new ExportWithDerivedMetadataAttribute());
            ExportDefinition export = part.ExportDefinitions.Single();

            Assert.AreEqual("BaseValue", export.Metadata["BaseKey"]);
            Assert.AreEqual("DerivedValue", export.Metadata["DerivedKey"]);
        }
    }

    [AttributeUsage(AttributeTargets.All)]
    [MetadataAttribute]
    public class BasicMetadataAttribute : Attribute
    {
        public string String1 { get { return "One"; } }

        public string String2 { get { return "Two"; } }

        public int[] Numbers { get { return new int[] { 1, 2, 3 }; } }

        public CreationPolicy Policy { get { return CreationPolicy.NonShared; } }

        public Type Type { get { return typeof(BasicMetadataAttribute); } }
    }

    public interface IStronglyTypedStructure
    {
        string String1 { get; }
        string String2 { get; }
        int[] Numbers { get; }
        CreationPolicy Policy { get; }
        Type Type { get; }
    }

    [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
    [MetadataAttribute]
    public class Name : Attribute
    {
        public Name(string name) { Bar = name; }

        public string Bar { set; get; }
    }

    [PartNotDiscoverable]
    [Export]
    [BasicMetadata]
    public class BasicTestComponent
    {
    }
}