protected override List <ScanResult> ScanSources(ScanSource textSource)
        {
            var result = new List <ScanResult>();

            List <string> rowsList = _textUtil.SplitTextToRows(textSource.Text);

            foreach (var itemScanSource in _textsForScan)
            {
                List <string>            itemRowsList  = _textUtil.SplitTextToRows(itemScanSource.Text);
                string                   processedText = "";
                Dictionary <string, int> dupliateTexts = TextRowsCompare(rowsList, itemRowsList, out processedText);
                itemScanSource.Text = processedText;

                foreach (var duplItem in dupliateTexts)
                {
                    if (result.Any(w => w.DuplicateText == duplItem.Key))
                    {
                        ScanResult scanResultItem = result.First(w => w.DuplicateText == duplItem.Key);
                        var        duplFileInfo   = new FileWithDuplicates()
                        {
                            Name = itemScanSource.Name,
                            Path = itemScanSource.Path,
                            DupliateItemCount = duplItem.Value
                        };

                        scanResultItem.DuplicateFilesInfos.Add(duplFileInfo);
                    }
                    else
                    {
                        var selfSourceInfo = new FileWithDuplicates()
                        {
                            Name = textSource.Name,
                            Path = textSource.Path,
                            DupliateItemCount = 1
                        };

                        var duplSourceInfo = new FileWithDuplicates()
                        {
                            Name = itemScanSource.Name,
                            Path = itemScanSource.Path,
                            DupliateItemCount = duplItem.Value
                        };

                        var scanResultItem = new ScanResult()
                        {
                            DuplicateText       = duplItem.Key,
                            DuplicateFilesInfos = new List <FileWithDuplicates>()
                            {
                                selfSourceInfo, duplSourceInfo
                            }
                        };

                        result.Add(scanResultItem);
                    }
                }
            }

            return(result);
        }
Example #2
0
        public void TestSample_04_RowByRowScanner()
        {
            //Arrange
            string[] text1 =
            {
                "Row1",
                "Row2",
                "Row3",
                "Row4",
                "Row5",
                "Row6",
                "Row7",

                "Row1",
                "Row2",
                "Row3",
                "Row4.1"
            };

            string[] text2 =
            {
                "Row1",
                "Row2",
                "Row3",
                "Row4.1",

                "Row1",
                "Row2",
                "Row3",
                "Row4",
                "Row5",
                "Row6",

                "Row1",
                "Row2",
                "Row3",

                "Row1",
                "Row2",
                "Row3",
                "Row4.1",
                "Row5",

                "Row1",
                "Row2",
                "Row3"
            };


            var sourceQueue = new Queue <ScanSource>();

            sourceQueue.Enqueue(new ScanSource()
            {
                Name = "Text1", Text = string.Join(envNewLine, text1)
            });
            sourceQueue.Enqueue(new ScanSource()
            {
                Name = "Text2", Text = string.Join(envNewLine, text2)
            });

            ///

            string[] duplicateText_01 =
            {
                "Row1",
                "Row2",
                "Row3",
                "Row4",
                "Row5",
                "Row6"
            };

            var text1Info_01 = new FileWithDuplicates()
            {
                Name = "Text1",
                DupliateItemCount = 1,
                Path = ""
            };

            var text2Info_01 = new FileWithDuplicates()
            {
                Name = "Text2",
                DupliateItemCount = 1,
                Path = ""
            };

            var duplicateFilesInfos_01 = new List <FileWithDuplicates>()
            {
                text1Info_01,
                text2Info_01
            };

            ///

            string[] duplicateText_02 =
            {
                "Row1",
                "Row2",
                "Row3",
                "Row4.1"
            };

            var text1Info_02 = new FileWithDuplicates()
            {
                Name = "Text1",
                DupliateItemCount = 1,
                Path = ""
            };

            var text2Info_02 = new FileWithDuplicates()
            {
                Name = "Text2",
                DupliateItemCount = 2,
                Path = ""
            };

            var duplicateFilesInfos_02 = new List <FileWithDuplicates>()
            {
                text1Info_02,
                text2Info_02
            };

            ///

            string[] duplicateText_03 =
            {
                "Row1",
                "Row2",
                "Row3"
            };

            var text1Info_03 = new FileWithDuplicates()
            {
                Name = "Text1",
                DupliateItemCount = 2,
                Path = ""
            };

            var text2Info_03 = new FileWithDuplicates()
            {
                Name = "Text2",
                DupliateItemCount = 2,
                Path = ""
            };

            var duplicateFilesInfos_03 = new List <FileWithDuplicates>()
            {
                text1Info_03,
                text2Info_03
            };

            ///

            var expectedResult = new List <ScanResult>()
            {
                new ScanResult()
                {
                    DuplicateText       = string.Join(envNewLine, duplicateText_01),
                    DuplicateFilesInfos = duplicateFilesInfos_01
                },
                new ScanResult()
                {
                    DuplicateText       = string.Join(envNewLine, duplicateText_02),
                    DuplicateFilesInfos = duplicateFilesInfos_02
                },
                new ScanResult()
                {
                    DuplicateText       = string.Join(envNewLine, duplicateText_03),
                    DuplicateFilesInfos = duplicateFilesInfos_03
                }
            };

            //Act
            var scanerObj  = new RowByRowScanner(sourceQueue);
            var resultReal = scanerObj.SearchDuplicates();

            string expJson  = JsonConvert.SerializeObject(expectedResult);
            string realJson = JsonConvert.SerializeObject(resultReal);

            //Assert
            Assert.Equal(expJson, realJson);
        }