Пример #1
0
        public void AutoService_IActionValidatorFactory_GetValidators_1_Tests1()
        {
            var actionValidatorFactory = DiContainer.Resolve <IActionValidatorFactory>();
            var settings = DiContainer.Resolve <ISettings>();

            var actionValidatorValuesProvider = DiContainer.Resolve <IActionValidatorValuesProvider>();
            var projectGuids = DiContainer.Resolve <IProjectGuids>();

            // Method call 1
            IReadOnlyList <IActionValidator> actionValidators = actionValidatorFactory.GetValidators(ActionTypes.ViewFilesList, new Guid("8663708F-C707-47E1-AEDC-2CD9291AD4CB"));

            Assert.AreEqual(6, actionValidators.Count);

            // IActionValidator 1

            Assert.AreSame(typeof(ActionValidator3), actionValidators[0].GetType());
            Assert.AreEqual(7, ((ActionValidator3)actionValidators[0]).IntParam);

            // IActionValidator 2
            Assert.AreSame(DiContainer.Resolve <ActionValidatorWithDependencyOnActionValidatorFactory>(), actionValidators[1]);
            Assert.AreSame(actionValidatorFactory, ((ActionValidatorWithDependencyOnActionValidatorFactory)actionValidators[1]).ActionValidatorFactory);

            // IActionValidator 3

            ActionValidator1 actionValidator1_3 = actionValidators[2] as ActionValidator1;

            Assert.IsNotNull(actionValidator1_3);
            Assert.AreSame(DiContainer.Resolve <Services.IInterface1>(), actionValidator1_3.Property1);
            Assert.AreSame(DiContainer.Resolve <Services.IInterface2>(), actionValidator1_3.Property2);

            // IActionValidator 4
            Assert.AreSame(DiContainer.Resolve(Helpers.GetType("TestPluginAssembly1.Implementations.Plugin1ActionValidator")),
                           actionValidators[3]);

            // IActionValidator 5
            Assert.AreSame(StaticAndConstMembers.ActionValidator1, actionValidators[4]);
            Assert.IsTrue(StaticAndConstMembers.ActionValidator1.GetType().IsNestedPrivate);

            // IActionValidator 6

            Assert.AreSame(actionValidatorValuesProvider.DefaultActionValidator, actionValidators[5]);
            Assert.IsTrue(actionValidatorValuesProvider.DefaultActionValidator.GetType().IsNestedPrivate);

            // Lets explicitly check that plugin 3 value is not included. This is already tested by the checks above, but lets make this explicit
            // for clarity.

            Assert.IsFalse(actionValidators.Any(x => x.GetType().FullName.Contains("TestPluginAssembly3.Implementations.Plugin3ActionValidator")));

            // Method call 2
            actionValidators = actionValidatorFactory.GetValidators(ActionTypes.ViewFileContents,
                                                                    settings.GetSettingValueOrThrow <Guid>("Project1Guid"));

            Assert.AreEqual(2, actionValidators.Count);

            // IActionValidator 1
            Assert.AreSame(DiContainer.Resolve <ActionValidator1>(), actionValidators[0]);

            // IActionValidator 2
            Assert.AreSame(actionValidatorValuesProvider.GetViewOnlyActionvalidator(), actionValidators[1]);

            Assert.IsTrue(actionValidatorValuesProvider.GetViewOnlyActionvalidator().GetType().IsNestedPrivate);

            // Method call 3
            actionValidators = actionValidatorFactory.GetValidators(StaticAndConstMembers.DefaultActionType,
                                                                    projectGuids.Project1);

            Assert.AreEqual(0, actionValidators.Count);

            // Method call 4

            actionValidators = actionValidatorFactory.GetValidators(ActionTypes.ViewFileContents,
                                                                    StaticAndConstMembers.GetDefaultProjectGuid());

            Assert.AreEqual(2, actionValidators.Count);
            Assert.AreSame(DiContainer.Resolve <ActionValidator2>(), actionValidators[0]);
            Assert.AreSame(DiContainer.Resolve <ActionValidator1>(), actionValidators[1]);

            // Method call 5
            actionValidators = actionValidatorFactory.GetValidators(ActionTypes.ViewFilesList,
                                                                    actionValidatorFactory.PublicProjectId);

            Assert.AreEqual(1, actionValidators.Count);
            Assert.AreSame(actionValidatorFactory.DefaultActionValidator, actionValidators[0]);

            // Method call 6
            // This is the default case (else case).
            actionValidators = actionValidatorFactory.GetValidators(ActionTypes.ViewFilesList, Guid.Empty);
            Assert.AreEqual(3, actionValidators.Count);

            Assert.AreSame(actionValidatorFactory.DefaultActionValidator, actionValidators[0]);

            Assert.AreSame(DiContainer.Resolve <ActionValidator3>(), actionValidators[1]);
            // The value of property ActionValidator3.IntParam is set from int constructor parameter.
            // The value of int parameter injected into constructor is int.MinValue,
            // since we added a module IoC.Configuration.Tests.PrimitiveTypeDefaultBindingsModule in modules section,
            // with defaultInt32 parameter=int.MinValue.
            Assert.AreEqual(int.MinValue, ((ActionValidator3)actionValidators[1]).IntParam);

            Assert.AreEqual("DynamicallyLoadedAssembly2.ActionValidator4", actionValidators[2].GetType().FullName);
        }
Пример #2
0
        public void ReUseValueAttribute_Tests()
        {
            var actionValidatorFactory = DiContainer.Resolve <IActionValidatorFactory>();

            var settings = DiContainer.Resolve <ISettings>();

            var actionValidatorValuesProvider = DiContainer.Resolve <IActionValidatorValuesProvider>();
            var projectGuids = DiContainer.Resolve <IProjectGuids>();

            void validateReuseResult(IReadOnlyList <IActionValidator> actionValidators1,
                                     IReadOnlyList <IActionValidator> actionValidators2,
                                     bool isReuseAttributeUsed, int numOfExpectedItems)
            {
                Assert.AreEqual(numOfExpectedItems, actionValidators1.Count);
                Assert.AreEqual(numOfExpectedItems, actionValidators1.Count);

                if (isReuseAttributeUsed)
                {
                    Assert.AreSame(actionValidators1, actionValidators2);
                }
                else
                {
                    Assert.AreNotSame(actionValidators1, actionValidators2);
                }
            }

            // IActionValidatorFactory.GetValidators(ActionTypes, Guid) uses reuseValue=true
            var validators1 = actionValidatorFactory.GetValidators(ActionTypes.ViewFilesList, Guid.Parse("8663708F-C707-47E1-AEDC-2CD9291AD4CB"));
            var validators2 = actionValidatorFactory.GetValidators(ActionTypes.ViewFilesList, Guid.Parse("8663708F-C707-47E1-AEDC-2CD9291AD4CB"));

            validateReuseResult(validators1, validators2, true, 6);

            validators1 = actionValidatorFactory.GetValidators(ActionTypes.ViewFileContents, settings.GetSettingValueOrThrow <Guid>("Project1Guid"));
            validators2 = actionValidatorFactory.GetValidators(ActionTypes.ViewFileContents, settings.GetSettingValueOrThrow <Guid>("Project1Guid"));
            validateReuseResult(validators1, validators2, true, 2);

            validators1 = actionValidatorFactory.GetValidators(StaticAndConstMembers.DefaultActionType, projectGuids.Project1);
            validators2 = actionValidatorFactory.GetValidators(StaticAndConstMembers.DefaultActionType, projectGuids.Project1);
            validateReuseResult(validators1, validators2, true, 0);

            validators1 = actionValidatorFactory.GetValidators(ActionTypes.ViewFileContents, StaticAndConstMembers.GetDefaultProjectGuid());
            validators2 = actionValidatorFactory.GetValidators(ActionTypes.ViewFileContents, StaticAndConstMembers.GetDefaultProjectGuid());
            validateReuseResult(validators1, validators2, true, 2);

            validators1 = actionValidatorFactory.GetValidators(ActionTypes.ViewFilesList, actionValidatorFactory.PublicProjectId);
            validators2 = actionValidatorFactory.GetValidators(ActionTypes.ViewFilesList, actionValidatorFactory.PublicProjectId);
            validateReuseResult(validators1, validators2, true, 1);

            // The default case
            validators1 = actionValidatorFactory.GetValidators(ActionTypes.ViewFilesList, Guid.Empty);
            validators2 = actionValidatorFactory.GetValidators(ActionTypes.ViewFilesList, Guid.Empty);
            validateReuseResult(validators1, validators2, true, 3);

            // IActionValidatorFactory.GetValidators(int, string) uses reuseValue=false
            validators1 = actionValidatorFactory.GetValidators((int)ActionTypes.ViewFilesList, "8663708F-C707-47E1-AEDC-2CD9291AD4CB");
            validators2 = actionValidatorFactory.GetValidators((int)ActionTypes.ViewFilesList, "8663708F-C707-47E1-AEDC-2CD9291AD4CB");
            validateReuseResult(validators1, validators2, false, 2);

            validators1 = actionValidatorFactory.GetValidators((int)ActionTypes.ViewFilesList, Guid.Empty.ToString());
            validators2 = actionValidatorFactory.GetValidators((int)ActionTypes.ViewFilesList, Guid.Empty.ToString());
            validateReuseResult(validators1, validators2, false, 4);
        }