public void GenericCSharpReplaceMethodReadOnlyTest() { Assert.Throws <FileReadOnlyException>(() => { // Get Project object Project testProject = (Project)(extensibility.Solution.Projects.Item(3)); ProjectItem codeFile = testProject.ProjectItems.Item("Program.cs"); CSharpHardCodedString hcs = new CSharpHardCodedString(codeFile, 19, 32); IExtractResourceAction actionObject = new Common.GenericCSharpExtractResourceAction(); ResourceFileCollection resources = new ResourceFileCollection(testProject, new FilterMethod(actionObject.IsValidResourceFile)); ResourceFile resFile = resources["Resource1.resx"]; string fileName = codeFile.get_FileNames(1); FileAttributes oldAttributes = System.IO.File.GetAttributes(fileName); System.IO.File.SetAttributes(fileName, oldAttributes | FileAttributes.ReadOnly); try { ExtractToResourceActionSite refactorSite = new ExtractToResourceActionSite(hcs); refactorSite.ExtractStringToResource(resFile, "Test"); } finally { System.IO.File.SetAttributes(fileName, oldAttributes); } }); }
/// <summary> /// Invokes replace method on the provided objects /// </summary> /// <param name="codeFile">Code file to test</param> /// <param name="resFile">Resource file to use</param> /// <param name="hcs">Hardcoded string to use</param> /// <param name="expected">Expected test string</param> internal static void TestReplaceMethod(ProjectItem codeFile, string resourceFileName, BaseHardCodedString hcs, string expected, string resourceName) { string fileName = codeFile.get_FileNames(1); bool readOnly = false; try { readOnly = CommonMethods.ToggleReadOnly(fileName, false); ExtractToResourceActionSite refactorSite = new ExtractToResourceActionSite(hcs); ResourceFileCollection resources = new ResourceFileCollection(codeFile.ContainingProject, new FilterMethod(refactorSite.ActionObject.IsValidResourceFile)); ResourceFile resFile = resources[resourceFileName]; refactorSite.ExtractStringToResource(resFile, resourceName); TextDocument doc = ((EnvDTE.TextDocument)codeFile.Document.Object(null)); EditPoint ep = doc.StartPoint.CreateEditPoint(); string line = ep.GetLines(hcs.StartingLine + 1, hcs.StartingLine + 2); Assert.AreEqual(expected, line, "New line does not match the expected output"); } finally { if (readOnly) { CommonMethods.ToggleReadOnly(fileName, true); } } }