update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / Tests / ComponentModelUnitTest / System / ComponentModel / Composition / ExportableAttributeTests.cs
blob51b23d42a5fa80c4b37cd3fe93801104acb24573
1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
4 using System;
5 using System.Collections.Generic;
6 using System.ComponentModel.Composition;
7 using System.ComponentModel.Composition.Factories;
8 using System.ComponentModel.Composition.Hosting;
9 using System.Linq;
10 using System.UnitTesting;
11 using Microsoft.VisualStudio.TestTools.UnitTesting;
12 using System.Reflection;
13 using System.ComponentModel.Composition.Primitives;
15 namespace System.ComponentModel.Composition
17 [TestClass]
18 public class MetadataAttributeTests
20 [TestMethod]
21 [TestProperty("Type", "Integration")]
22 public void UntypedStructureTest()
24 var container = ContainerFactory.Create();
25 CompositionBatch batch = new CompositionBatch();
26 batch.AddPart(AttributedModelServices.CreatePart(new BasicTestComponent()));
27 container.Compose(batch);
28 var export = container.GetExport<BasicTestComponent, IDictionary<string, object>>();
31 Assert.IsNotNull(export.Metadata, "It should have metadata");
32 Assert.AreEqual("One", export.Metadata["String1"], "Property attribute should copy straight across");
33 Assert.AreEqual("Two", export.Metadata["String2"], "Property attribute should copy straight across");
34 var e = export.Metadata["Numbers"] as IList<int>;
35 Assert.IsNotNull(e, "Should get a collection of numbers");
36 Assert.IsTrue(e.Contains(1), "Should have 1 in the list");
37 Assert.IsTrue(e.Contains(2), "Should have 2 in the list");
38 Assert.IsTrue(e.Contains(3), "Should have 3 in the list");
39 Assert.AreEqual(3, e.Count, "Should be three numbers total");
42 #if !SILVERLIGHT
43 // Silverlight doesn't support strongly typed metadata
44 [TestMethod]
45 [TestProperty("Type", "Integration")]
46 public void StronglyTypedStructureTest()
48 var container = ContainerFactory.Create();
49 CompositionBatch batch = new CompositionBatch();
50 batch.AddPart(AttributedModelServices.CreatePart(new BasicTestComponent()));
51 container.Compose(batch);
53 var export = container.GetExport<BasicTestComponent, IStronglyTypedStructure>();
55 Assert.IsNotNull(export.Metadata, "It should have metadata");
56 Assert.AreEqual("One", export.Metadata.String1, "Property should copy straight across");
57 Assert.AreEqual("Two", export.Metadata.String2, "Property should copy straight across");
58 Assert.IsTrue(export.Metadata.Numbers.Contains(1), "Should have 1 in the list");
59 Assert.IsTrue(export.Metadata.Numbers.Contains(2), "Should have 2 in the list");
60 Assert.IsTrue(export.Metadata.Numbers.Contains(3), "Should have 3 in the list");
61 Assert.AreEqual(3, export.Metadata.Numbers.Length, "Should be three numbers total");
63 #endif //!SILVERLIGHT
65 [Export]
66 // Should cause a conflict with the multiple nature of Name.Bar because
67 // it isn't marked with IsMultiple=true
68 [ExportMetadata("Bar", "Blah")]
69 [Name("MEF")]
70 [Name("MEF2")]
71 [PartNotDiscoverable]
72 public class BasicTestComponentWithInvalidMetadata
76 [TestMethod]
77 [TestProperty("Type", "Integration")]
78 public void InvalidMetadataAttributeTest()
80 ComposablePart part = AttributedModelServices.CreatePart(new BasicTestComponentWithInvalidMetadata());
81 ExportDefinition export = part.ExportDefinitions.First();
83 var ex = ExceptionAssert.Throws<InvalidOperationException>(RetryMode.DoNotRetry, () =>
85 var metadata = export.Metadata;
86 });
88 Assert.IsTrue(ex.Message.Contains("Bar"));
91 [AttributeUsage(AttributeTargets.All)]
92 [MetadataAttribute]
93 public class MetadataWithInvalidCustomAttributeType : Attribute
95 public PersonClass Person { get { return new PersonClass(); } }
97 public class PersonClass
99 public string First { get { return "George"; } }
100 public string Last { get { return "Washington"; } }
104 [Export]
105 [MetadataWithInvalidCustomAttributeType]
106 [PartNotDiscoverable]
107 public class ClassWithInvalidCustomAttributeType
112 [TestMethod]
113 public void InvalidAttributType_CustomType_ShouldThrow()
115 ComposablePart part = AttributedModelServices.CreatePart(new ClassWithInvalidCustomAttributeType());
116 ExportDefinition export = part.ExportDefinitions.First();
118 // Should throw InvalidOperationException during discovery because
119 // the person class is an invalid metadata type
120 ExceptionAssert.Throws<InvalidOperationException>(RetryMode.DoNotRetry, () =>
122 var metadata = export.Metadata;
126 [AttributeUsage(AttributeTargets.All)]
127 [MetadataAttribute]
128 public class MetadataWithInvalidVersionPropertyAttributeType : Attribute
130 public MetadataWithInvalidVersionPropertyAttributeType()
132 this.Version = new Version(1, 1);
134 public Version Version { get; set; }
137 [Export]
138 [MetadataWithInvalidVersionPropertyAttributeType]
139 [PartNotDiscoverable]
140 public class ClassWithInvalidVersionPropertyAttributeType
145 [TestMethod]
146 public void InvalidAttributType_VersionPropertyType_ShouldThrow()
148 ComposablePart part = AttributedModelServices.CreatePart(new ClassWithInvalidVersionPropertyAttributeType());
149 ExportDefinition export = part.ExportDefinitions.First();
151 // Should throw InvalidOperationException during discovery because
152 // the person class is an invalid metadata type
153 ExceptionAssert.Throws<InvalidOperationException>(RetryMode.DoNotRetry, () =>
155 var metadata = export.Metadata;
159 [MetadataAttribute]
160 public class BaseMetadataAttribute : Attribute
162 public string BaseKey { get { return "BaseValue"; } }
165 public class DerivedMetadataAttribute : BaseMetadataAttribute
167 public string DerivedKey { get { return "DerivedValue"; } }
170 [Export]
171 [DerivedMetadata]
172 public class ExportWithDerivedMetadataAttribute { }
174 [TestMethod]
175 public void DerivedMetadataAttributeAttribute_ShouldSupplyMetadata()
177 ComposablePart part = AttributedModelServices.CreatePart(new ExportWithDerivedMetadataAttribute());
178 ExportDefinition export = part.ExportDefinitions.Single();
180 Assert.AreEqual("BaseValue", export.Metadata["BaseKey"]);
181 Assert.AreEqual("DerivedValue", export.Metadata["DerivedKey"]);
185 [AttributeUsage(AttributeTargets.All)]
186 [MetadataAttribute]
187 public class BasicMetadataAttribute : Attribute
189 public string String1 { get { return "One"; } }
191 public string String2 { get { return "Two"; } }
193 public int[] Numbers { get { return new int[] { 1, 2, 3 }; } }
195 public CreationPolicy Policy { get { return CreationPolicy.NonShared; } }
197 public Type Type { get { return typeof(BasicMetadataAttribute); } }
200 public interface IStronglyTypedStructure
202 string String1 { get; }
203 string String2 { get; }
204 int[] Numbers { get; }
205 CreationPolicy Policy { get; }
206 Type Type { get; }
209 [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
210 [MetadataAttribute]
211 public class Name : Attribute
213 public Name(string name) { Bar = name; }
215 public string Bar { set; get; }
218 [PartNotDiscoverable]
219 [Export]
220 [BasicMetadata]
221 public class BasicTestComponent