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