Пример #1
0
        public void TestThatIsManipulatingFieldThrowsDeliveryEngineSystemExceptionIfTableIfNotFound()
        {
            var fixture = new Fixture();

            fixture.Customize <ITable>(e => e.FromFactory(() =>
            {
                var tableMock = MockRepository.GenerateMock <ITable>();
                tableMock.Expect(m => m.NameTarget)
                .Return(fixture.CreateAnonymous <string>())
                .Repeat.Any();
                return(tableMock);
            }));
            fixture.Customize <IDataSource>(e => e.FromFactory(() =>
            {
                var dataSourceMock = MockRepository.GenerateMock <IDataSource>();
                dataSourceMock.Expect(m => m.Tables)
                .Return(new ReadOnlyObservableCollection <ITable>(new ObservableCollection <ITable>(fixture.CreateMany <ITable>(5).ToList())))
                .Repeat.Any();
                return(dataSourceMock);
            }));

            var metadataRepositoryMock = MockRepository.GenerateMock <IMetadataRepository>();

            metadataRepositoryMock.Expect(m => m.DataSourceGet())
            .Return(fixture.CreateAnonymous <IDataSource>())
            .Repeat.Any();
            fixture.Customize <IMetadataRepository>(e => e.FromFactory(() => metadataRepositoryMock));

            var containerMock = MockRepository.GenerateMock <IContainer>();

            containerMock.Expect(m => m.Resolve <IMetadataRepository>())
            .Return(metadataRepositoryMock)
            .Repeat.Any();
            fixture.Customize <IContainer>(e => e.FromFactory(() => containerMock));

            var workerMock = MockRepository.GenerateMock <IMissingForeignKeyWorker>();

            workerMock.Expect(m => m.IsManipulatingField(Arg <string> .Is.NotNull, Arg <ITable> .Is.NotNull))
            .Return(false)
            .Repeat.Any();
            fixture.Customize <IMissingForeignKeyWorker>(e => e.FromFactory(() => workerMock));

            var missingForeignKeyHandler = new MissingForeignKeyHandler(fixture.CreateAnonymous <string>(), fixture.CreateAnonymous <IMissingForeignKeyWorker>(), fixture.CreateAnonymous <IContainer>());

            Assert.That(missingForeignKeyHandler, Is.Not.Null);

            // ReSharper disable PossibleNullReferenceException
            missingForeignKeyHandler.GetType().GetField("_currentDataSource", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static).SetValue(missingForeignKeyHandler, null);
            // ReSharper restore PossibleNullReferenceException
            Assert.Throws <DeliveryEngineSystemException>(() => missingForeignKeyHandler.IsManipulatingField(fixture.CreateAnonymous <string>()));

            containerMock.AssertWasCalled(m => m.Resolve <IMetadataRepository>());
            metadataRepositoryMock.AssertWasCalled(m => m.DataSourceGet());
            workerMock.AssertWasNotCalled(m => m.IsManipulatingField(Arg <string> .Is.Anything, Arg <ITable> .Is.Anything));
        }
Пример #2
0
        public void TestThatFinalizeDataManipulationReturnsWithoutFinalizingDataManipulationIfTableSourceNameDoesNotMatchTableNameInTheConstructor()
        {
            var fixture = new Fixture();

            fixture.Customize <Type>(e => e.FromFactory(() => typeof(int)));
            fixture.Customize <IContainer>(e => e.FromFactory(() => MockRepository.GenerateMock <IContainer>()));
            fixture.Customize <IMap>(e => e.FromFactory(() => MockRepository.GenerateMock <IMap>()));
            fixture.Customize <IField>(e => e.FromFactory(() =>
            {
                var fieldMock = MockRepository.GenerateMock <IField>();
                fieldMock.Expect(m => m.NameSource)
                .Return(fixture.CreateAnonymous <string>())
                .Repeat.Any();
                return(fieldMock);
            }));

            var workerMock = MockRepository.GenerateMock <IMissingForeignKeyWorker>();

            fixture.Customize <IMissingForeignKeyWorker>(e => e.FromFactory(() => workerMock));

            var tableMock = MockRepository.GenerateMock <ITable>();

            tableMock.Expect(m => m.NameSource)
            .Return(fixture.CreateAnonymous <string>())
            .Repeat.Any();
            tableMock.Expect(m => m.Fields)
            .Return(new ReadOnlyObservableCollection <IField>(new ObservableCollection <IField>(fixture.CreateMany <IField>(5).ToList())))
            .Repeat.Any();
            fixture.Customize <ITable>(e => e.FromFactory(() => tableMock));

            var dataCollectionMock = new Collection <IEnumerable <IDataObjectBase> >();

            while (dataCollectionMock.Count < 250)
            {
                var dataObjects = new Collection <IDataObjectBase>();
                for (var i = 0; i < tableMock.Fields.Count; i++)
                {
                    dataObjects.Add(MockRepository.GenerateMock <IFieldData <int, int> >());
                }
                dataCollectionMock.Add(dataObjects);
            }

            var missingForeignKeyHandler = new MissingForeignKeyHandler(fixture.CreateAnonymous <string>(), fixture.CreateAnonymous <IMissingForeignKeyWorker>(), fixture.CreateAnonymous <IContainer>());

            Assert.That(missingForeignKeyHandler, Is.Not.Null);

            var manipulatedData = missingForeignKeyHandler.FinalizeDataManipulation(fixture.CreateAnonymous <ITable>(), dataCollectionMock);

            Assert.That(manipulatedData, Is.Not.Null);
            Assert.That(manipulatedData, Is.Not.Empty);
            Assert.That(manipulatedData.Count(), Is.EqualTo(dataCollectionMock.Count));

            workerMock.AssertWasNotCalled(m => m.FinalizeDataManipulation(Arg <ITable> .Is.Anything, Arg <IDataRepository> .Is.Anything, Arg <IEnumerable <IEnumerable <IDataObjectBase> > > .Is.Anything));
        }
Пример #3
0
        public void TestThatConstructorInitializeMissingForeignKeyHandler()
        {
            var fixture = new Fixture();

            fixture.Customize <IMissingForeignKeyWorker>(e => e.FromFactory(() => MockRepository.GenerateMock <IMissingForeignKeyWorker>()));

            var metadataRepositoryMock = MockRepository.GenerateMock <IMetadataRepository>();

            fixture.Customize <IMetadataRepository>(e => e.FromFactory(() => metadataRepositoryMock));

            var dataRepositoryMock = MockRepository.GenerateMock <IDataRepository>();

            fixture.Customize <IDataRepository>(e => e.FromFactory(() => dataRepositoryMock));

            var containerMock = MockRepository.GenerateMock <IContainer>();

            containerMock.Expect(m => m.Resolve <IMetadataRepository>())
            .Return(metadataRepositoryMock)
            .Repeat.Any();
            containerMock.Expect(m => m.Resolve <IDataRepository>())
            .Return(fixture.CreateAnonymous <IDataRepository>())
            .Repeat.Any();
            fixture.Customize <IContainer>(e => e.FromFactory(() => containerMock));

            var tableName = fixture.CreateAnonymous <string>();
            var worker    = fixture.CreateAnonymous <IMissingForeignKeyWorker>();
            var missingForeignKeyHandler = new MissingForeignKeyHandler(tableName, worker, fixture.CreateAnonymous <IContainer>());

            Assert.That(missingForeignKeyHandler, Is.Not.Null);
            Assert.That(missingForeignKeyHandler.TableName, Is.Not.Null);
            Assert.That(missingForeignKeyHandler.TableName, Is.Not.Empty);
            Assert.That(missingForeignKeyHandler.TableName, Is.EqualTo(tableName));
            Assert.That(missingForeignKeyHandler.DataRepository, Is.Not.Null);
            Assert.That(missingForeignKeyHandler.DataRepository, Is.EqualTo(dataRepositoryMock));
            Assert.That(missingForeignKeyHandler.MetadataRepository, Is.Not.Null);
            Assert.That(missingForeignKeyHandler.MetadataRepository, Is.EqualTo(metadataRepositoryMock));
            Assert.That(missingForeignKeyHandler.Worker, Is.Not.Null);
            Assert.That(missingForeignKeyHandler.Worker, Is.EqualTo(worker));

            containerMock.AssertWasCalled(m => m.Resolve <IDataRepository>());
        }
Пример #4
0
        public void TestThatFinalizeDataManipulationReturnsFinalizedDataFromWorkerWhichManipulatesMissingForeignKeyValues()
        {
            var fixture = new Fixture();

            fixture.Customize <Type>(e => e.FromFactory(() => typeof(int)));
            fixture.Customize <IMap>(e => e.FromFactory(() => MockRepository.GenerateMock <IMap>()));
            fixture.Customize <IField>(e => e.FromFactory(() =>
            {
                var fieldMock = MockRepository.GenerateMock <IField>();
                fieldMock.Expect(m => m.NameSource)
                .Return(fixture.CreateAnonymous <string>())
                .Repeat.Any();
                return(fieldMock);
            }));

            var dataRepositoryMock = MockRepository.GenerateMock <IDataRepository>();

            fixture.Customize <IDataRepository>(e => e.FromFactory(() => dataRepositoryMock));

            var containerMock = MockRepository.GenerateMock <IContainer>();

            containerMock.Expect(m => m.Resolve <IDataRepository>())
            .Return(fixture.CreateAnonymous <IDataRepository>())
            .Repeat.Any();
            fixture.Customize <IContainer>(e => e.FromFactory(() => containerMock));

            var workerMock = MockRepository.GenerateMock <IMissingForeignKeyWorker>();

            workerMock.Expect(m => m.FinalizeDataManipulation(Arg <ITable> .Is.NotNull, Arg <IDataRepository> .Is.NotNull, Arg <IEnumerable <IEnumerable <IDataObjectBase> > > .Is.NotNull))
            .Return(new Collection <IEnumerable <IDataObjectBase> >())
            .Repeat.Any();
            fixture.Customize <IMissingForeignKeyWorker>(e => e.FromFactory(() => workerMock));

            var tableMock = MockRepository.GenerateMock <ITable>();

            tableMock.Expect(m => m.NameSource)
            .Return(fixture.CreateAnonymous <string>())
            .Repeat.Any();
            tableMock.Expect(m => m.Fields)
            .Return(new ReadOnlyObservableCollection <IField>(new ObservableCollection <IField>(fixture.CreateMany <IField>(5).ToList())))
            .Repeat.Any();
            // ReSharper disable ImplicitlyCapturedClosure
            fixture.Customize <ITable>(e => e.FromFactory(() => tableMock));
            // ReSharper restore ImplicitlyCapturedClosure

            var dataCollectionMock = new Collection <IEnumerable <IDataObjectBase> >();

            while (dataCollectionMock.Count < 250)
            {
                var dataObjects = new Collection <IDataObjectBase>();
                for (var i = 0; i < tableMock.Fields.Count; i++)
                {
                    dataObjects.Add(MockRepository.GenerateMock <IFieldData <int, int> >());
                }
                dataCollectionMock.Add(dataObjects);
            }

            var missingForeignKeyHandler = new MissingForeignKeyHandler(tableMock.NameSource, fixture.CreateAnonymous <IMissingForeignKeyWorker>(), fixture.CreateAnonymous <IContainer>());

            Assert.That(missingForeignKeyHandler, Is.Not.Null);

            var manipulatedData = missingForeignKeyHandler.FinalizeDataManipulation(fixture.CreateAnonymous <ITable>(), dataCollectionMock);

            Assert.That(manipulatedData, Is.Not.Null);
            Assert.That(manipulatedData, Is.Empty);

            workerMock.AssertWasCalled(m => m.FinalizeDataManipulation(Arg <ITable> .Is.Equal(tableMock), Arg <IDataRepository> .Is.Equal(dataRepositoryMock), Arg <IEnumerable <IEnumerable <IDataObjectBase> > > .Is.Equal(dataCollectionMock)));
        }
        /// <summary>
        ///  Adding configuration to the container for Inversion of Control.
        /// </summary>
        /// <param name="container">Container for Inversion of Control.</param>
        public void AddConfiguration(IWindsorContainer container)
        {
            var dataManipulatorsFileName = ConfigurationManager.AppSettings["DataManipulatorsFileName"];

            if (dataManipulatorsFileName == null)
            {
                throw new DeliveryEngineSystemException(Resource.GetExceptionMessage(ExceptionMessage.ApplicationSettingMissing, "DataManipulatorsFileName"));
            }

            dataManipulatorsFileName = Environment.ExpandEnvironmentVariables(dataManipulatorsFileName);
            if (string.IsNullOrEmpty(dataManipulatorsFileName))
            {
                container.Register(Component.For <IDataManipulators>().ImplementedBy <DataManipulators>().LifeStyle.Transient);
                return;
            }
            if (!File.Exists(dataManipulatorsFileName))
            {
                throw new DeliveryEngineSystemException(Resource.GetExceptionMessage(ExceptionMessage.FileNotFound, dataManipulatorsFileName));
            }

            var assembly = typeof(DataManipulators).Assembly;

            using (var resourceStream = assembly.GetManifestResourceStream(string.Format("{0}.Schemas.DataManipulators.xsd", assembly.GetName().Name)))
            {
                if (resourceStream == null)
                {
                    throw new DeliveryEngineSystemException(Resource.GetExceptionMessage(ExceptionMessage.ResourceNotFound, string.Format("{0}.Schemas.DataManipulators.xsd", assembly.GetName().Name)));
                }
                var schema         = XmlSchema.Read(resourceStream, ValidationEventHandler);
                var readerSettings = new XmlReaderSettings
                {
                    IgnoreComments = true,
                    IgnoreProcessingInstructions = true,
                    IgnoreWhitespace             = true,
                    ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
                    ValidationType  = ValidationType.Schema
                };
                readerSettings.Schemas.Add(schema);
                readerSettings.ValidationEventHandler += ValidationEventHandler;
                var reader = XmlReader.Create(dataManipulatorsFileName, readerSettings);
                try
                {
                    var document = new XmlDocument();
                    document.Load(reader);
                    var namespaceManager = new XmlNamespaceManager(document.NameTable);
                    namespaceManager.AddNamespace("ns", schema.TargetNamespace);
                    var dataSetterNodeList = document.SelectNodes("/ns:DataManipulators/ns:DataSetter", namespaceManager);
                    if (dataSetterNodeList != null && dataSetterNodeList.Count > 0)
                    {
                        var dataSetterNo = 0;
                        foreach (XmlNode dataSetterNode in dataSetterNodeList)
                        {
                            var criteriaConfigurations = GenerateCriteriaConfigurations(dataSetterNode.SelectNodes("ns:EqualCriteria | ns:PoolCriteria | ns:IntervalCriteria", namespaceManager));
                            // ReSharper disable PossibleNullReferenceException
                            var dataSetter = new DataSetter(dataSetterNode.Attributes["table"].Value, dataSetterNode.SelectSingleNode("ns:Field", namespaceManager).Attributes["name"].Value, dataSetterNode.SelectSingleNode("ns:Field", namespaceManager).Attributes["value"].Value, criteriaConfigurations);
                            // ReSharper restore PossibleNullReferenceException
                            container.Register(Component.For <IDataSetter>().Named(string.Format("{0}[{1}]", typeof(IDataSetter).Name, dataSetterNo++)).Instance(dataSetter).LifeStyle.Transient);
                        }
                    }
                    var regularExpressionReplacerNodeList = document.SelectNodes("/ns:DataManipulators/ns:RegularExpressionReplacer", namespaceManager);
                    if (regularExpressionReplacerNodeList != null && regularExpressionReplacerNodeList.Count > 0)
                    {
                        var regularExpressionReplacerNo = 0;
                        foreach (XmlNode regularExpressionReplacerNode in regularExpressionReplacerNodeList)
                        {
                            var fieldNode = regularExpressionReplacerNode.SelectSingleNode("ns:Field", namespaceManager);
                            // ReSharper disable PossibleNullReferenceException
                            var regularExpression         = new Regex(fieldNode.Attributes["regularExpression"].Value, RegexOptions.Compiled);
                            var newFieldValue             = fieldNode.Attributes["value"].Value;
                            var regularExpressionReplacer = new RegularExpressionReplacer(regularExpressionReplacerNode.Attributes["table"].Value, regularExpression, (RegularExpressionApplyOn)Enum.Parse(typeof(RegularExpressionApplyOn), fieldNode.Attributes["applyOn"].Value), fieldNode.Attributes["name"].Value, Equals(newFieldValue, "{null}") ? null : newFieldValue);
                            // ReSharper restore PossibleNullReferenceException
                            container.Register(Component.For <IRegularExpressionReplacer>().Named(string.Format("{0}[{1}]", typeof(IRegularExpressionReplacer).Name, regularExpressionReplacerNo++)).Instance(regularExpressionReplacer).LifeStyle.Transient);
                        }
                    }
                    var rowDuplicatorNodeList = document.SelectNodes("/ns:DataManipulators/ns:RowDuplicator", namespaceManager);
                    if (rowDuplicatorNodeList != null && rowDuplicatorNodeList.Count > 0)
                    {
                        var rowDuplicatorNo = 0;
                        foreach (XmlNode rowDuplicatorNode in rowDuplicatorNodeList)
                        {
                            var criteriaConfigurations = GenerateCriteriaConfigurations(rowDuplicatorNode.SelectNodes("ns:EqualCriteria | ns:PoolCriteria | ns:IntervalCriteria", namespaceManager));
                            var fieldUpdates           = new Collection <Tuple <string, object> >();
                            var fieldNodeList          = rowDuplicatorNode.SelectNodes("ns:Field", namespaceManager);
                            if (fieldNodeList != null && fieldNodeList.Count > 0)
                            {
                                foreach (XmlNode fieldNode in fieldNodeList)
                                {
                                    // ReSharper disable PossibleNullReferenceException
                                    fieldUpdates.Add(new Tuple <string, object>(fieldNode.Attributes["name"].Value, fieldNode.Attributes["value"].Value));
                                    // ReSharper restore PossibleNullReferenceException
                                }
                            }
                            // ReSharper disable PossibleNullReferenceException
                            var rowDuplicator = new RowDuplicator(rowDuplicatorNode.Attributes["table"].Value, fieldUpdates, criteriaConfigurations);
                            // ReSharper restore PossibleNullReferenceException
                            container.Register(Component.For <IRowDuplicator>().Named(string.Format("{0}[{1}]", typeof(IRowDuplicator).Name, rowDuplicatorNo++)).Instance(rowDuplicator).LifeStyle.Transient);
                        }
                    }
                    var missingForeignKeyHandlerNodeList = document.SelectNodes("ns:DataManipulators/ns:MissingForeignKeyHandler", namespaceManager);
                    if (missingForeignKeyHandlerNodeList != null && missingForeignKeyHandlerNodeList.Count > 0)
                    {
                        var missingForeignKeyHandlerNo = 0;
                        foreach (XmlNode missingForeignKeyHandlerNode in missingForeignKeyHandlerNodeList)
                        {
                            var workerNode = missingForeignKeyHandlerNode.SelectSingleNode("ns:PrimaryKeyAdder | ns:ForeignKeyCleaner | ns:ForeignKeyDeleter", namespaceManager);
                            if (workerNode == null)
                            {
                                continue;
                            }
                            var foreignKeyFields        = new List <string>();
                            var foreignKeyFieldNodeList = workerNode.SelectNodes("ns:ForeignKey", namespaceManager);
                            if (foreignKeyFieldNodeList != null && foreignKeyFieldNodeList.Count > 0)
                            {
                                // ReSharper disable PossibleNullReferenceException
                                foreignKeyFields.AddRange(from XmlNode foreignKeyFieldNode in foreignKeyFieldNodeList select foreignKeyFieldNode.Attributes["field"].Value);
                                // ReSharper restore PossibleNullReferenceException
                            }
                            IMissingForeignKeyWorker worker = null;
                            switch (workerNode.LocalName)
                            {
                            case "PrimaryKeyAdder":
                                var setFieldValues        = new Dictionary <string, object>();
                                var setFieldValueNodeList = workerNode.SelectNodes("ns:SetValue", namespaceManager);
                                if (setFieldValueNodeList != null && setFieldValueNodeList.Count > 0)
                                {
                                    foreach (XmlNode setFieldValueNode in setFieldValueNodeList)
                                    {
                                        // ReSharper disable PossibleNullReferenceException
                                        setFieldValues.Add(setFieldValueNode.Attributes["field"].Value, setFieldValueNode.Attributes["value"].Value);
                                        // ReSharper restore PossibleNullReferenceException
                                    }
                                }
                                try
                                {
                                    // ReSharper disable PossibleNullReferenceException
                                    worker = new PrimaryKeyAdder(workerNode.Attributes["targetTable"].Value, foreignKeyFields, setFieldValues, container.Resolve <IMetadataRepository>(), container.Resolve <IInformationLogger>());
                                    // ReSharper restore PossibleNullReferenceException
                                }
                                catch (DeliveryEngineSystemException)
                                {
                                    continue;
                                }
                                break;

                            case "ForeignKeyCleaner":
                                try
                                {
                                    // ReSharper disable PossibleNullReferenceException
                                    worker = new ForeignKeyCleaner(workerNode.Attributes["targetTable"].Value, foreignKeyFields, container.Resolve <IMetadataRepository>(), container.Resolve <IInformationLogger>());
                                    // ReSharper restore PossibleNullReferenceException
                                }
                                catch (DeliveryEngineSystemException)
                                {
                                    continue;
                                }
                                break;

                            case "ForeignKeyDeleter":
                                try
                                {
                                    // ReSharper disable PossibleNullReferenceException
                                    worker = new ForeignKeyDeleter(workerNode.Attributes["targetTable"].Value, foreignKeyFields, container.Resolve <IMetadataRepository>(), container.Resolve <IInformationLogger>());
                                    // ReSharper restore PossibleNullReferenceException
                                }
                                catch (DeliveryEngineSystemException)
                                {
                                    continue;
                                }
                                break;
                            }
                            // ReSharper disable PossibleNullReferenceException
                            var missingForeignKeHandler = new MissingForeignKeyHandler(missingForeignKeyHandlerNode.Attributes["table"].Value, worker, container.Resolve <IContainer>());
                            // ReSharper restore PossibleNullReferenceException
                            container.Register(Component.For <IMissingForeignKeyHandler>().Named(string.Format("{0}[{1}]", typeof(IMissingForeignKeyHandler).Name, missingForeignKeyHandlerNo++)).Instance(missingForeignKeHandler).LifeStyle.Transient);
                        }
                    }
                }
                finally
                {
                    reader.Close();
                }
                resourceStream.Close();
            }

            container.Register(Component.For <IDataManipulators>().ImplementedBy <DataManipulators>().LifeStyle.Transient);
        }