PageRenderTime 112ms CodeModel.GetById 42ms RepoModel.GetById 7ms app.codeStats 0ms

/test/EFTools/InProcTests/RefactorRenameTests.cs

https://bitbucket.org/vanickdigital/totus.entityframework
C# | 210 lines | 179 code | 28 blank | 3 comment | 4 complexity | abf60836fb82d1cb0e82e568e5376b07 MD5 | raw file
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. namespace EFDesigner.InProcTests
  3. {
  4. using System;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using EFDesignerTestInfrastructure;
  8. using EFDesignerTestInfrastructure.EFDesigner;
  9. using EFDesignerTestInfrastructure.VS;
  10. using Microsoft.Data.Entity.Design.Model;
  11. using Microsoft.Data.Entity.Design.Model.Commands;
  12. using Microsoft.Data.Entity.Design.Refactoring;
  13. using Microsoft.Data.Entity.Design.VisualStudio;
  14. using Microsoft.Data.Entity.Design.VisualStudio.Package;
  15. using Microsoft.VisualStudio.TestTools.UnitTesting;
  16. using Microsoft.VisualStudio.TestTools.VsIdeTesting;
  17. [TestClass]
  18. public class RefactorRenameTests
  19. {
  20. public TestContext TestContext { get; set; }
  21. private readonly IEdmPackage _package;
  22. public RefactorRenameTests()
  23. {
  24. PackageManager.LoadEDMPackage(VsIdeTestHostContext.ServiceProvider);
  25. _package = PackageManager.Package;
  26. }
  27. private const string PubSimpleProgramText = @"using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using System.Text;
  31. namespace {0}
  32. {{
  33. class Program
  34. {{
  35. static void Main(string[] args)
  36. {{
  37. using (var c = new PUBSEntities())
  38. {{
  39. author author = new author() {{ au_id = ""foo"" }};
  40. c.AddToauthors(author);
  41. foreach (var i in c.authors)
  42. {{}}
  43. }}
  44. }}
  45. }}
  46. }}";
  47. private const string RefactorRenameEntityResult = @"using System;
  48. using System.Collections.Generic;
  49. using System.Linq;
  50. using System.Text;
  51. namespace RefactorRenameEntity
  52. {
  53. class Program
  54. {
  55. static void Main(string[] args)
  56. {
  57. using (var c = new PUBSEntities())
  58. {
  59. renamedAuthor author = new renamedAuthor() { au_id = ""foo"" };
  60. c.AddTorenamedAuthor(author);
  61. foreach (var i in c.renamedAuthor)
  62. {}
  63. }
  64. }
  65. }
  66. }";
  67. private const string RefactorRenamePropertyResult = @"using System;
  68. using System.Collections.Generic;
  69. using System.Linq;
  70. using System.Text;
  71. namespace RefactorRenameProperty
  72. {
  73. class Program
  74. {
  75. static void Main(string[] args)
  76. {
  77. using (var c = new PUBSEntities())
  78. {
  79. author author = new author() { renamedId = ""foo"" };
  80. c.AddToauthors(author);
  81. foreach (var i in c.authors)
  82. {}
  83. }
  84. }
  85. }
  86. }";
  87. // [TestMethod, HostType("VS IDE")] http://entityframework.codeplex.com/workitem/992
  88. public void RefactorRenameEntity()
  89. {
  90. RefactorRenameTest(
  91. "RefactorRenameEntity", (artifact, cpc, programDocData) =>
  92. {
  93. var authorType = ModelHelper.FindEntityType(artifact.ConceptualModel, "author");
  94. Assert.IsNotNull(authorType, "Could not find author type in the model");
  95. RefactorEFObject.RefactorRenameElement(authorType, "renamedAuthor", false);
  96. var textLines = VSHelpers.GetVsTextLinesFromDocData(programDocData);
  97. Assert.IsNotNull(textLines, "Could not get VsTextLines for program DocData");
  98. Assert.AreEqual(
  99. RefactorRenameEntityResult, VSHelpers.GetTextFromVsTextLines(textLines), "Refactor results are incorrect");
  100. var renamedAuthorType = ModelHelper.FindEntityType(artifact.ConceptualModel, "renamedAuthor");
  101. Assert.IsNotNull(renamedAuthorType, "Could not find renamedAuthor type in the model");
  102. });
  103. }
  104. // [TestMethod, HostType("VS IDE")] http://entityframework.codeplex.com/workitem/992
  105. public void RefactorRenameProperty()
  106. {
  107. RefactorRenameTest(
  108. "RefactorRenameProperty", (artifact, cpc, programDocData) =>
  109. {
  110. var authorType = ModelHelper.FindEntityType(artifact.ConceptualModel, "author");
  111. Assert.IsNotNull(authorType, "Could not find author type in the model");
  112. var idProperty = ModelHelper.FindProperty(authorType, "au_id");
  113. Assert.IsNotNull(idProperty, "Could not find au_id property in the model");
  114. RefactorEFObject.RefactorRenameElement(idProperty, "renamedId", false);
  115. var textLines = VSHelpers.GetVsTextLinesFromDocData(programDocData);
  116. Assert.IsNotNull(textLines, "Could not get VsTextLines for program DocData");
  117. Assert.AreEqual(
  118. RefactorRenamePropertyResult, VSHelpers.GetTextFromVsTextLines(textLines), "Refactor results are incorrect");
  119. authorType = ModelHelper.FindEntityType(artifact.ConceptualModel, "author");
  120. Assert.IsNotNull(authorType, "Could not find author type in the model");
  121. var renamedIdProperty = ModelHelper.FindProperty(authorType, "renamedId");
  122. Assert.IsNotNull(renamedIdProperty, "Could not find renamedId property in the model");
  123. });
  124. }
  125. #region Helper Method
  126. private void RefactorRenameTest(string projectName, Action<EntityDesignArtifact, CommandProcessorContext, object> test)
  127. {
  128. var modelEdmxFilePath = Path.Combine(TestContext.DeploymentDirectory, @"TestData\Model\v3\PubSimple.edmx");
  129. var dte = VsIdeTestHostContext.Dte;
  130. var serviceProvider = VsIdeTestHostContext.ServiceProvider;
  131. UITestRunner.Execute(TestContext.TestName,
  132. () =>
  133. {
  134. EntityDesignArtifact entityDesignArtifact = null;
  135. try
  136. {
  137. var project = dte.CreateProject(
  138. TestContext.TestRunDirectory,
  139. projectName,
  140. DteExtensions.ProjectKind.Executable,
  141. DteExtensions.ProjectLanguage.CSharp);
  142. var projectItem = dte.AddExistingItem(new FileInfo(modelEdmxFilePath).FullName, project);
  143. dte.OpenFile(projectItem.FileNames[0]);
  144. entityDesignArtifact =
  145. (EntityDesignArtifact)new EFArtifactHelper(EFArtifactHelper.GetEntityDesignModelManager(serviceProvider))
  146. .GetNewOrExistingArtifact(TestUtils.FileName2Uri(projectItem.FileNames[0]));
  147. var editingContext =
  148. _package.DocumentFrameMgr.EditingContextManager.GetNewOrExistingContext(entityDesignArtifact.Uri);
  149. var cpc = new CommandProcessorContext(
  150. editingContext, "DiagramTest" + projectName, "DiagramTestTxn" + projectName, entityDesignArtifact);
  151. var programDocData = VSHelpers.GetDocData(
  152. serviceProvider, Path.Combine(Path.GetDirectoryName(project.FullName), "Program.cs"));
  153. Debug.Assert(programDocData != null, "Could not get DocData for program file");
  154. var textLines = VSHelpers.GetVsTextLinesFromDocData(programDocData);
  155. Debug.Assert(textLines != null, "Could not get VsTextLines for program DocData");
  156. VsUtils.SetTextForVsTextLines(textLines, string.Format(PubSimpleProgramText, projectName));
  157. test(entityDesignArtifact, cpc, programDocData);
  158. }
  159. catch (Exception ex)
  160. {
  161. TestContext.WriteLine(ex.ToString());
  162. throw;
  163. }
  164. finally
  165. {
  166. if (entityDesignArtifact != null)
  167. {
  168. entityDesignArtifact.Dispose();
  169. }
  170. dte.CloseSolution(false);
  171. }
  172. });
  173. }
  174. #endregion
  175. }
  176. }