// ----------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. // ----------------------------------------------------------------------- using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.ComponentModel.Composition.Primitives; using System.Linq; using System.Linq.Expressions; using System.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.ComponentModel.Composition.ReflectionModel; using System.Reflection; namespace System.ComponentModel.Composition { [TestClass] public class ConstraintServicesTests { [TestMethod] public void TypeIdentityConstraint_ValidMatchingExportDef_ShouldMatch() { var contractName = "MyContract"; var typeIdentity = AttributedModelServices.GetTypeIdentity(typeof(ConstraintServicesTests)); var metadata = new Dictionary(); metadata.Add(CompositionConstants.ExportTypeIdentityMetadataName, typeIdentity); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, typeIdentity, null, CreationPolicy.Any); var predicate = constraint.Compile(); Assert.IsTrue(predicate(exportDefinition)); } [TestMethod] public void TypeIdentityConstraint_ValidNonMatchingExportDef_ShouldNotMatch() { var contractName = "MyContract"; var typeIdentity = AttributedModelServices.GetTypeIdentity(typeof(ConstraintServicesTests)); var metadata = new Dictionary(); metadata.Add(CompositionConstants.ExportTypeIdentityMetadataName, typeIdentity + "Another Identity"); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, typeIdentity, null, CreationPolicy.Any); var predicate = constraint.Compile(); Assert.IsFalse(predicate(exportDefinition)); } [TestMethod] public void TypeIdentityConstraint_InvalidExportDef_ShouldNotMatch() { var contractName = "MyContract"; var typeIdentity = AttributedModelServices.GetTypeIdentity(typeof(ConstraintServicesTests)); var metadata = new Dictionary(); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, typeIdentity, null, CreationPolicy.Any); var predicate = constraint.Compile(); Assert.IsFalse(predicate(exportDefinition)); } [TestMethod] public void CreationPolicyConstraint_ValidMatchingCreationPolicy_ShouldMatch() { var contractName = "MyContract"; var metadata = new Dictionary(); metadata.Add(CompositionConstants.PartCreationPolicyMetadataName, CreationPolicy.Shared); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, null, null, CreationPolicy.Shared); var predicate = constraint.Compile(); Assert.IsTrue(predicate(exportDefinition)); } [TestMethod] public void CreationPolicyConstraint_ValidNonMatchingCreationPolicy_ShouldNotMatch() { var contractName = "MyContract"; var metadata = new Dictionary(); metadata.Add(CompositionConstants.PartCreationPolicyMetadataName, CreationPolicy.NonShared); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, null, null, CreationPolicy.Shared); var predicate = constraint.Compile(); Assert.IsFalse(predicate(exportDefinition)); } [TestMethod] public void CreationPolicyConstraint_InvalidCreationPolicy_ShouldNotMatch() { var contractName = "MyContract"; var metadata = new Dictionary(); metadata.Add(CompositionConstants.PartCreationPolicyMetadataName, "Shared"); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, null, null, CreationPolicy.Shared); var predicate = constraint.Compile(); Assert.IsFalse(predicate(exportDefinition)); } [TestMethod] public void CreationPolicyConstraint_NoCreationPolicy_ShouldNotMatch() { var contractName = "MyContract"; var metadata = new Dictionary(); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, null, null, CreationPolicy.Shared); var predicate = constraint.Compile(); Assert.IsTrue(predicate(exportDefinition)); } [TestMethod] public void PartCreatorConstraint_ShouldMatchPartCreatorExportDefinition() { var partCreatorImportDef = ReflectionModelServices.CreateImportDefinition( new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ConstraintServicesTests) }), "Foo", "Foo", new KeyValuePair[] { new KeyValuePair("MDKey", typeof(string)) }, ImportCardinality.ZeroOrMore, false, CreationPolicy.Any, true, // IsPartCreator null); var metadata = new Dictionary(); metadata["MDKey"] = "MDValue"; metadata[CompositionConstants.ExportTypeIdentityMetadataName] = "Foo"; var productExportDefinition = new ExportDefinition("Foo", metadata); metadata = new Dictionary(metadata); metadata[CompositionConstants.ExportTypeIdentityMetadataName] = CompositionConstants.PartCreatorTypeIdentity; metadata[CompositionConstants.ProductDefinitionMetadataName] = productExportDefinition; var exportDefinition = new ExportDefinition(CompositionConstants.PartCreatorContractName, metadata); var predicate = partCreatorImportDef.Constraint.Compile(); Assert.IsTrue(partCreatorImportDef.IsConstraintSatisfiedBy(exportDefinition)); Assert.IsTrue(predicate(exportDefinition)); } [TestMethod] public void TryParseConstraint_ConstraintFromCreateConstraintAsConstraintArgument1_CanParse() { var expectations = Expectations.GetContractNamesWithEmpty(); foreach (var e in expectations) { var constraint = ConstraintServices.CreateConstraint((string)e, null, null, CreationPolicy.Any); AssertCanParse(constraint, e, new Dictionary()); } } [TestMethod] public void TryParseConstraint_ConstraintFromCreateConstraintAsConstraintArgument2_CanParse() { var expectations = Expectations.GetRequiredMetadata(); foreach (var e in expectations) { var constraint = ConstraintServices.CreateConstraint((IEnumerable>)e); AssertCanParse(constraint, (string)null, e); } } [TestMethod] public void TryParseConstraint_ConstraintFromCreateConstraintAsConstraintArgument3_CanParse() { var contractNames = Expectations.GetContractNames(); var metadataValues = Expectations.GetRequiredMetadata(); foreach (var contractName in contractNames) { foreach (var metadataValue in metadataValues) { var constraint = ConstraintServices.CreateConstraint(contractName, null, metadataValue, CreationPolicy.Any); AssertCanParse(constraint, contractName, metadataValue); } } } [TestMethod] public void TryParseConstraint_ContractNameOperatorEqualsAsConstraintArgument_CanParse() { var expectations = new ExpectationCollection>, string>(); expectations.Add(item => item.ContractName == "", ""); expectations.Add(item => item.ContractName == " ", " "); expectations.Add(item => item.ContractName == " ", " "); expectations.Add(item => item.ContractName == "ContractName", "ContractName"); expectations.Add(item => item.ContractName == "contractName", "contractName"); expectations.Add(item => item.ContractName == "{ContractName}", "{ContractName}"); expectations.Add(item => item.ContractName == "{ContractName}Name", "{ContractName}Name"); expectations.Add(item => item.ContractName == "System.Windows.Forms.Control", "System.Windows.Forms.Control"); expectations.Add(item => item.ContractName == "{System.Windows.Forms}Control", "{System.Windows.Forms}Control"); foreach (var e in expectations) { AssertCanParse(e.Input, e.Output, new Dictionary()); } } [TestMethod] public void TryParseConstraint_MetadataContainsKeyAsConstraintArgument_CanParse() { var expectations = new ExpectationCollection>, Dictionary>(); expectations.Add( item => typeof(string).IsInstanceOfType(item.Metadata[""]), new Dictionary{ {"", typeof(string) }}); expectations.Add( item => typeof(string).IsInstanceOfType(item.Metadata["value"]), new Dictionary{ {"value", typeof(string) } }); expectations.Add( item => typeof(string).IsInstanceOfType(item.Metadata["Value"]), new Dictionary{ {"Value", typeof(string) } }); expectations.Add( item => typeof(string).IsInstanceOfType(item.Metadata["Value"]) && typeof(int).IsInstanceOfType(item.Metadata["value"]), new Dictionary{ {"Value", typeof(string)}, {"value", typeof(int) }}); expectations.Add( item => typeof(string).IsInstanceOfType(item.Metadata["Value"]) && typeof(int).IsInstanceOfType(item.Metadata["value"]) && typeof(object).IsInstanceOfType(item.Metadata["Metadata"]), new Dictionary{ {"Value", typeof(string)}, {"value", typeof(int)}, {"Metadata", typeof(object) } }); foreach (var e in expectations) { AssertCanParse(e.Input, (string)null, e.Output); } } [TestMethod] public void TryParseConstraint_ContractNameOperatorEqualsAndMetadataContainsKeyAsConstraintArgumen_CanParse() { var expectations = new ExpectationCollection>, KeyValuePair[]>(); expectations.Add( item => item.ContractName == "ContractName" && typeof(string).IsInstanceOfType(item.Metadata[""]), new KeyValuePair[] { new KeyValuePair("", typeof(string)) }); expectations.Add( item => item.ContractName == "ContractName" && typeof(string).IsInstanceOfType(item.Metadata["value"]), new KeyValuePair[] { new KeyValuePair("value", typeof(string)) }); expectations.Add( item => item.ContractName == "ContractName" && typeof(string).IsInstanceOfType(item.Metadata["Value"]), new KeyValuePair[] { new KeyValuePair("Value", typeof(string)) }); expectations.Add( item => item.ContractName == "ContractName" && typeof(string).IsInstanceOfType(item.Metadata["Value"]) && typeof(int).IsInstanceOfType(item.Metadata["value"]), new KeyValuePair[] { new KeyValuePair("Value", typeof(string)), new KeyValuePair("value", typeof(int)) }); expectations.Add( item => item.ContractName == "ContractName" && typeof(string).IsInstanceOfType(item.Metadata["Value"]) && typeof(int).IsInstanceOfType(item.Metadata["value"]) && typeof(object).IsInstanceOfType(item.Metadata["Metadata"]), new KeyValuePair[] { new KeyValuePair("Value", typeof(string)), new KeyValuePair("value", typeof(int)), new KeyValuePair("Metadata", typeof(object)) }); foreach (var e in expectations) { AssertCanParse(e.Input, "ContractName", e.Output); } } [TestMethod] public void TryParseConstraint_ContractNameReverseOperatorEqualsAsConstraintArgument_CanParse() { var expectations = new ExpectationCollection>, string>(); expectations.Add(item => "" == item.ContractName, ""); expectations.Add(item => " " == item.ContractName, " "); expectations.Add(item => " " == item.ContractName, " "); expectations.Add(item => "ContractName" == item.ContractName, "ContractName"); expectations.Add(item => "contractName" == item.ContractName, "contractName"); expectations.Add(item => "{ContractName}" == item.ContractName, "{ContractName}"); expectations.Add(item => "{ContractName}Name" == item.ContractName, "{ContractName}Name"); expectations.Add(item => "System.Windows.Forms.Control" == item.ContractName, "System.Windows.Forms.Control"); expectations.Add(item => "{System.Windows.Forms}Control" == item.ContractName, "{System.Windows.Forms}Control"); foreach (var e in expectations) { AssertCanParse(e.Input, e.Output, new Dictionary()); } } private static void AssertCanParse(Expression> constraint, string contractName, IEnumerable> requiredMetadata) { Assert.IsNotNull(constraint); string contractNameResult = null; IEnumerable> requiredMetadataResult = null; bool success = ContraintParser.TryParseConstraint(constraint, out contractNameResult, out requiredMetadataResult); Assert.IsTrue(success); Assert.AreEqual(contractName, contractNameResult); EnumerableAssert.AreEqual(requiredMetadata, requiredMetadataResult); } private static void AssertCanNotParse(Expression> constraint) { Assert.IsNotNull(constraint); string contractNameResult; IEnumerable> requiredMetadataResult; var success = ContraintParser.TryParseConstraint(constraint, out contractNameResult, out requiredMetadataResult); Assert.IsFalse(success); Assert.IsNull(contractNameResult); Assert.IsNull(requiredMetadataResult); } } }