public void ReadContentFilesTest()
        {
            DirectoryInfo di = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "NReco_LocalFileManagerTest"));
            DirectoryInfo subDi = Directory.CreateDirectory(Path.Combine(di.FullName, "subfolder_NReco_LocalFileManagerTest"));
            string testFilePath = Path.Combine(di.FullName, "file2.txt");
            string testSubFolderFilePath = Path.Combine(subDi.FullName, "file3.txt");
            string testContentText = "This is a text write by using class LocalFileManager!";
            string testSubFolderFileContent = "This is a content secondery file!";
            int contentLength = testContentText.Length + testSubFolderFileContent.Length;

            using (FileStream fs = File.Create(testFilePath))
            {
                AddText(fs, testContentText);
                fs.Flush();
                fs.Close();
            }
            using (FileStream fs = File.Create(testSubFolderFilePath))
            {
                AddText(fs, testSubFolderFileContent);
                fs.Flush();
                fs.Close();
            }

            try {
                LocalFileManager localFileManager = new LocalFileManager();

                localFileManager.RootPath = di.FullName;
                Assert.AreEqual(contentLength, localFileManager.Read("**.txt").Length);
            } finally {
                File.Delete(testSubFolderFilePath);
                Directory.Delete(subDi.FullName);
                File.Delete(testFilePath);
                Directory.Delete(di.FullName);
            }
        }
        public void InsertTextFileTest()
        {
            CreateTestDirectoryAndFiles();

            string insertNode = "test1";
            string test_text = "<text-insert file=\"test3.result.xml\" start=\"&lt;test&gt;\"><" + insertNode + "/></text-insert>";

            string xmlContent = "<test><t2>bbb</t2></test>";

            ModifyTextFileRule modifyTextFileRule = new ModifyTextFileRule();
            IFileManager fileManager = new LocalFileManager(TestDiretoryPath);

            using (FileStream fs = File.Create(TestFilePath))
            {
                AddText(fs, test_text);
                fs.Flush();
                fs.Close();
            }
            using (FileStream fs = File.Create(TestXmlFilePath))
            {
                AddText(fs, xmlContent);
                fs.Flush();
                fs.Close();
            }

            XPathDocument ruleXPathDoc = new XPathDocument(new StringReader(test_text) );
            XPathNavigator ruleFileNav = ruleXPathDoc.CreateNavigator().SelectSingleNode("/*");
            Console.WriteLine(ruleFileNav.LocalName);

            bool isMatched = modifyTextFileRule.IsMatch(ruleFileNav);
            Assert.AreEqual(true, isMatched);

            FileRuleContext fileRuleContext = new FileRuleContext(TestFilePath, fileManager, ruleFileNav);

            modifyTextFileRule.Execute(fileRuleContext);
            Assert.AreEqual(true, File.ReadAllText(TestXmlFilePath).Contains(insertNode));

            Assert.AreEqual(true, IsNodeInXmlFile(TestXmlFilePath, insertNode));
            DeleteTestDirectoryAndFiles();
        }
        public void ReadContentFileTest()
        {
            DirectoryInfo di = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "NReco_LocalFileManagerTest"));
            string testDir = di.FullName;
            string testFilePath = Path.Combine(testDir, "file1.txt");
            string test_text = "This is a NUnit test for LocalFileManager class!";
            using (FileStream fs = File.Create(testFilePath))
            {
                AddText(fs, test_text);
                fs.Flush();
                fs.Close();
            }
            try {
                LocalFileManager localFileManager = new LocalFileManager();

                localFileManager.RootPath = di.FullName;

                Assert.AreEqual(test_text, localFileManager.Read(testFilePath));
                Assert.AreNotEqual("This is a wrong text! ", localFileManager.Read(testFilePath));
            } finally {
                File.Delete(testFilePath);
                Directory.Delete(testDir);
            }
        }
        public object Create(object parent, object configContext, XmlNode section)
        {
            try {
                string componentsXml;
                if (section.Name=="components") {
                    componentsXml = section.OuterXml;
                } else {
                    StringBuilder tmpsb = new StringBuilder();
                    tmpsb.Append("<components>");
                    tmpsb.Append(section.InnerXml);
                    tmpsb.Append("</components>");
                    componentsXml = tmpsb.ToString();
                }

                string rootDir = GetAppBasePath();
                if (section.Attributes["includebasepath"] != null) {
                    var explicitBase = section.Attributes["includebasepath"].Value;
                    rootDir = Path.IsPathRooted(explicitBase) ? explicitBase : Path.Combine(rootDir, explicitBase);
                }

                var xmlRdr = XmlReader.Create(new StringReader(componentsXml), null,
                        new XmlParserContext(null, null, null, XmlSpace.Default) { BaseURI = rootDir });
                Mvp.Xml.XInclude.XIncludingReader xmlContentRdr = new Mvp.Xml.XInclude.XIncludingReader(xmlRdr);
                LocalFileManager fileManager = new LocalFileManager(rootDir);
                xmlContentRdr.XmlResolver = new FileManagerXmlResolver(fileManager,"./");
                XPathDocument xmlXPathDoc = new XPathDocument(xmlContentRdr);

                IModifyXmlDocumentHandler preprocessor = GetPreprocessor(section.Name, fileManager);

                XmlComponentsConfig config = new XmlComponentsConfig(
                    xmlXPathDoc.CreateNavigator().OuterXml, preprocessor);
                return config;
            } catch (Exception ex) {
                throw new ConfigurationException(ex.Message, ex);
            }
        }
        public void WriteContentToFileTest()
        {
            DirectoryInfo di = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "NReco_LocalFileManagerTest"));
            string testDir = di.FullName;
            string testFilePath = Path.Combine(testDir, "file2.txt");
            string testContentText = "This is a text write by using class LocalFileManager!";

            LocalFileManager localFileManager = new LocalFileManager();
            localFileManager.Write(testFilePath, testContentText);

            try {
                string readText = File.ReadAllText(testFilePath);

                Assert.AreEqual(testContentText, readText);
                Assert.AreEqual(testContentText, localFileManager.Read(testFilePath));
            } finally {
                File.Delete(testFilePath);
                Directory.Delete(testDir);
            }
        }
        public void RemoveItemFromFileTest()
        {
            CreateTestDirectoryAndFiles();
            string test_text = "<text-remove file=\"test3.result.xml\" regex=\"bbb\"></text-remove>";

            string xmlContent = "<test><t2>bbb</t2></test>";

            ModifyTextFileRule modifyTextFileRule = new ModifyTextFileRule();
            IFileManager fileManager = new LocalFileManager(TestDiretoryPath);

            using (FileStream fs = File.Create(TestFilePath))
            {
                AddText(fs, test_text);
                fs.Flush();
                fs.Close();
            }
            using (FileStream fs = File.Create(TestXmlFilePath))
            {
                AddText(fs, xmlContent);
                fs.Flush();
                fs.Close();
            }
            XPathDocument ruleXPathDoc = new XPathDocument(new StringReader(test_text));
            XPathNavigator ruleFileNav = ruleXPathDoc.CreateNavigator().SelectSingleNode("/*");

            FileRuleContext fileRuleContext = new FileRuleContext(TestFilePath, fileManager,ruleFileNav);

            modifyTextFileRule.Execute(fileRuleContext);

            Assert.AreEqual(false, File.ReadAllText(TestXmlFilePath).Contains("bbb"));
        }