///<summary> ///When overridden in a derived class, executes the task. ///</summary> /// ///<returns> ///true if the task successfully executed; otherwise, false. ///</returns> public override bool Execute() { modifiedFile = GetEffectivePathToTnsNamesFile(); if (String.IsNullOrEmpty(ModifiedFile)) { Log.LogError(Properties.Resources.TnsNamesFileNotFound); return(false); } originalFileText = fileSystem.ReadTextFromFile(modifiedFile); TnsParser parser = new TnsParser(originalFileText); TnsEntry targetEntry = parser.FindEntry(entryName); if (targetEntry == null) { // append entry definition to the beginning of the file modifiedFileText = String.Format("{0} = {1}{2}{3}", entryName, entryText, Environment.NewLine, originalFileText); Log.LogMessage(Properties.Resources.TnsnameAdded, entryName, modifiedFile); } else { if (!allowUpdates) { Log.LogError(Properties.Resources.TnsnameUpdateAborted, entryName, modifiedFile); return(false); } // replace the entry definition within the file string beforeEntry = originalFileText.Substring(0, targetEntry.StartPosition); string afterEntry = originalFileText.Substring(targetEntry.StartPosition + targetEntry.Length); modifiedFileText = String.Format("{0}{1} = {2}{3}", beforeEntry, entryName, entryText, afterEntry); Log.LogMessage(Properties.Resources.TnsnameUpdated, entryName, modifiedFile); } fileSystem.WriteTextToFile(modifiedFile, modifiedFileText); return(true); }
///<summary> ///When overridden in a derived class, executes the task. ///</summary> /// ///<returns> ///true if the task successfully executed; otherwise, false. ///</returns> public override bool Execute() { modifiedFile = GetEffectivePathToTnsNamesFile(); if (String.IsNullOrEmpty(ModifiedFile)) { Log.LogError(Properties.Resources.TnsNamesFileNotFound); return false; } originalFileText = fileSystem.ReadTextFromFile(modifiedFile); TnsParser parser = new TnsParser(originalFileText); TnsEntry targetEntry = parser.FindEntry(entryName); if (targetEntry == null) { // append entry definition to the beginning of the file modifiedFileText = String.Format("{0} = {1}{2}{3}", entryName, entryText, Environment.NewLine, originalFileText); Log.LogMessage(Properties.Resources.TnsnameAdded, entryName, modifiedFile); } else { if (!allowUpdates) { Log.LogError(Properties.Resources.TnsnameUpdateAborted, entryName, modifiedFile); return false; } // replace the entry definition within the file string beforeEntry = originalFileText.Substring(0, targetEntry.StartPosition); string afterEntry = originalFileText.Substring(targetEntry.StartPosition + targetEntry.Length); modifiedFileText = String.Format("{0}{1} = {2}{3}", beforeEntry, entryName, entryText, afterEntry); Log.LogMessage(Properties.Resources.TnsnameUpdated, entryName, modifiedFile); } fileSystem.WriteTextToFile(modifiedFile, modifiedFileText); return true; }
public void FindEntry_should_only_ecognize_entry_names_that_start_in_the_first_column_of_a_new_line() { string textThatIsInTheFileButNotAnEntryName = "CONNECT_DATA"; StringAssert.Contains(textThatIsInTheFileButNotAnEntryName, AddTnsNameTest.SAMPLE_FILE); TnsParser parser = new TnsParser(AddTnsNameTest.SAMPLE_FILE); TnsEntry foundEntry = parser.FindEntry(textThatIsInTheFileButNotAnEntryName); Assert.IsNull(foundEntry); }
public void FindEntry_should_return_position_of_the_requested_entry_when_it_does_exist() { string entryName = "SOUTHWIND.WORLD"; int startPosition = AddTnsNameTest.SAMPLE_FILE.IndexOf(entryName); Assert.IsTrue(startPosition >=0, "Entry should exist in sample file."); TnsParser parser = new TnsParser(AddTnsNameTest.SAMPLE_FILE); TnsEntry foundEntry = parser.FindEntry(entryName); Assert.AreEqual(startPosition, foundEntry.StartPosition); }
public void FindEntry_should_return_length_of_the_found_entry_definition() { string entryName = "SOUTHWIND.WORLD"; const int LENGTH_OF_SOUTHWIND_DEFINITION = 189; // this corresponds to the text of SAMPLE_FILE int startPosition = AddTnsNameTest.SAMPLE_FILE.IndexOf(entryName); Assert.IsTrue(startPosition >= 0, "Entry should exist in sample file."); TnsParser parser = new TnsParser(AddTnsNameTest.SAMPLE_FILE); TnsEntry foundEntry = parser.FindEntry(entryName); Assert.AreEqual(LENGTH_OF_SOUTHWIND_DEFINITION, foundEntry.Length); }
public void FindEntry_should_return_null_when_the_requested_entry_does_not_exist() { TnsParser parser = new TnsParser(AddTnsNameTest.SAMPLE_FILE); TnsEntry foundEntry = parser.FindEntry("eastwind.world"); Assert.IsNull(foundEntry); }