ConstraintServicesTests.cs :  » 2.6.4-mono-.net-core » System.ComponentModel » System » ComponentModel » Composition » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » 2.6.4 mono .net core » System.ComponentModel 
System.ComponentModel » System » ComponentModel » Composition » ConstraintServicesTests.cs
// -----------------------------------------------------------------------
// 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<string, object>();
            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<string, object>();
            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<string, object>();

            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<string, object>();
            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<string, object>();
            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<string, object>();
            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<string, object>();

            var exportDefinition = new ExportDefinition(contractName, metadata);

            var constraint = ConstraintServices.CreateConstraint(contractName, null, null, CreationPolicy.Shared);

            var predicate = constraint.Compile();

            Assert.IsTrue(predicate(exportDefinition));
        }

#if SILVERLIGHT

        [TestMethod]
        public void PartCreatorConstraint_ShouldMatchPartCreatorExportDefinition()
        {
            var partCreatorImportDef = ReflectionModelServices.CreateImportDefinition(
                new LazyMemberInfo(MemberTypes.Field, () => new MemberInfo[] { typeof(ConstraintServicesTests) }),
                "Foo",
                "Foo",
                new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("MDKey", typeof(string)) },
                ImportCardinality.ZeroOrMore,
                false,
                CreationPolicy.Any,
                true, // IsPartCreator
                null);

            var metadata = new Dictionary<string, object>();
            metadata["MDKey"] = "MDValue";
            metadata[CompositionConstants.ExportTypeIdentityMetadataName] = "Foo";

            var productExportDefinition = new ExportDefinition("Foo", metadata);

            metadata = new Dictionary<string, object>(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));
        }
#endif

        [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<string, Type>());
            }
        }

        [TestMethod]
        public void TryParseConstraint_ConstraintFromCreateConstraintAsConstraintArgument2_CanParse()
        {
            var expectations = Expectations.GetRequiredMetadata();

            foreach (var e in expectations)
            {
                var constraint = ConstraintServices.CreateConstraint((IEnumerable<KeyValuePair<string, Type>>)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<Expression<Func<ExportDefinition, bool>>, 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<string, Type>());
            }
        }

        [TestMethod]
        public void TryParseConstraint_MetadataContainsKeyAsConstraintArgument_CanParse()
        {
            var expectations = new ExpectationCollection<Expression<Func<ExportDefinition, bool>>, Dictionary<string, Type>>();
            expectations.Add(
                item => typeof(string).IsInstanceOfType(item.Metadata[""]), 
                new Dictionary<string, Type>{ {"", typeof(string) }});
            expectations.Add(
                item => typeof(string).IsInstanceOfType(item.Metadata["value"]), 
                new Dictionary<string, Type>{ {"value", typeof(string) } });
            expectations.Add(
                item => typeof(string).IsInstanceOfType(item.Metadata["Value"]), 
                new Dictionary<string, Type>{ {"Value", typeof(string) } });
            expectations.Add(
                item => typeof(string).IsInstanceOfType(item.Metadata["Value"]) && typeof(int).IsInstanceOfType(item.Metadata["value"]),
                new Dictionary<string, Type>{ {"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<string, Type>{ {"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<Expression<Func<ExportDefinition, bool>>, KeyValuePair<string, Type>[]>();
            expectations.Add(
                item => item.ContractName == "ContractName" && typeof(string).IsInstanceOfType(item.Metadata[""]),
                new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("", typeof(string)) });
            expectations.Add(
                item => item.ContractName == "ContractName" && typeof(string).IsInstanceOfType(item.Metadata["value"]),
                new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("value", typeof(string)) });
            expectations.Add(
                item => item.ContractName == "ContractName"  && typeof(string).IsInstanceOfType(item.Metadata["Value"]),
                new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Value", typeof(string)) });
            expectations.Add(
                item => item.ContractName == "ContractName"  && typeof(string).IsInstanceOfType(item.Metadata["Value"]) && typeof(int).IsInstanceOfType(item.Metadata["value"]),
                new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Value", typeof(string)), new KeyValuePair<string, Type>("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<string, Type>[] { new KeyValuePair<string, Type>("Value", typeof(string)), new KeyValuePair<string, Type>("value", typeof(int)), new KeyValuePair<string, Type>("Metadata", typeof(object)) });

            foreach (var e in expectations)
            {
                AssertCanParse(e.Input, "ContractName", e.Output);
            }
        }


        [TestMethod]
        public void TryParseConstraint_ContractNameReverseOperatorEqualsAsConstraintArgument_CanParse()
        {
            var expectations = new ExpectationCollection<Expression<Func<ExportDefinition, bool>>, 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<string, Type>());
            }
        }

        private static void AssertCanParse(Expression<Func<ExportDefinition, bool>> constraint, string contractName, IEnumerable<KeyValuePair<string, Type>> requiredMetadata)
        {
            Assert.IsNotNull(constraint);

            string contractNameResult = null;
            IEnumerable<KeyValuePair<string, Type>> 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<Func<ExportDefinition, bool>> constraint)
        {
            Assert.IsNotNull(constraint);

            string contractNameResult;
            IEnumerable<KeyValuePair<string, Type>> requiredMetadataResult;

            var success = ContraintParser.TryParseConstraint(constraint, out contractNameResult, out requiredMetadataResult);
            Assert.IsFalse(success);
            Assert.IsNull(contractNameResult);
            Assert.IsNull(requiredMetadataResult);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.