PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/VisualStudio/ABB.SrcML.VisualStudio.SrcMLService.IntegrationTests/SrcMLServiceCSharpTests.cs

https://github.com/nkcsgexi/SrcML.NET
C# | 170 lines | 134 code | 25 blank | 11 comment | 6 complexity | 43b10cc14c38cc42fd4a17c7937c9eef MD5 | raw file
  1. using ABB.SrcML.Test.Utilities;
  2. using EnvDTE;
  3. using Microsoft.VisualStudio;
  4. using Microsoft.VisualStudio.Shell.Interop;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Microsoft.VSSDK.Tools.VsIdeTesting;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace ABB.SrcML.VisualStudio.SrcMLService.IntegrationTests {
  16. [TestClass]
  17. public class SrcMLServiceCSharpTests : IInvoker {
  18. private const string TestSolutionName = "TestCSharpSolution";
  19. private static Solution TestSolution;
  20. private static object TestLock;
  21. private static string TestSolutionPath = Path.Combine(TestSolutionName, TestSolutionName + ".sln");
  22. [ClassInitialize]
  23. public static void ClassSetup(TestContext testContext) {
  24. // Create a local copy of the solution
  25. FileUtils.CopyDirectory(Path.Combine(TestConstants.InputFolderPath, TestSolutionName), TestSolutionName);
  26. TestLock = new object();
  27. }
  28. [TestInitialize]
  29. public void TestSetup() {
  30. TestSolution = VsIdeTestHostContext.Dte.Solution;
  31. Assert.IsNotNull(TestSolution, "Could not get the solution");
  32. TestSolution.Open(Path.GetFullPath(TestSolutionPath));
  33. TestHelpers.TestScaffold.Service.StartMonitoring();
  34. }
  35. [TestCleanup]
  36. public void Cleanup() {
  37. TestHelpers.TestScaffold.Service.StopMonitoring();
  38. TestSolution.Close();
  39. //TestSolution = null;
  40. // TestScaffold.Cleanup();
  41. }
  42. [TestMethod]
  43. [HostType("VS IDE")]
  44. public void TestCsServiceStartup() {
  45. Assert.IsTrue(TestHelpers.WaitForServiceToFinish(TestHelpers.TestScaffold.Service, 5000));
  46. var archive = TestHelpers.TestScaffold.Service.GetSrcMLArchive();
  47. Assert.IsNotNull(archive, "Could not get the SrcML Archive");
  48. Assert.AreEqual(2, archive.FileUnits.Count(), "There should only be two files in the srcML archive");
  49. }
  50. [TestMethod]
  51. [HostType("VS IDE")]
  52. public void TestCsFileOperations() {
  53. // setup
  54. Project project = TestHelpers.GetProjects(TestSolution).FirstOrDefault();
  55. Assert.IsNotNull(project, "Couldn't get the project");
  56. var archive = TestHelpers.TestScaffold.Service.GetSrcMLArchive();
  57. Assert.IsNotNull(archive, "Could not get the SrcML Archive");
  58. AutoResetEvent resetEvent = new AutoResetEvent(false);
  59. string expectedFilePath = null;
  60. FileEventType expectedEventType = FileEventType.FileDeleted;
  61. EventHandler<FileEventRaisedArgs> action = (o, e) => {
  62. lock(TestLock) {
  63. if(e.FilePath.Equals(expectedFilePath, StringComparison.InvariantCultureIgnoreCase) && e.EventType == expectedEventType) {
  64. resetEvent.Set();
  65. }
  66. }
  67. };
  68. TestHelpers.TestScaffold.Service.SourceFileChanged += action;
  69. // add a file
  70. var fileTemplate = Path.Combine(TestConstants.TemplatesFolder, "NewCSharpClass1.cs");
  71. expectedFilePath = Path.Combine(Path.GetDirectoryName(project.FullName), "NewCSharpClass1.cs");
  72. expectedEventType = FileEventType.FileAdded;
  73. var item = project.ProjectItems.AddFromFileCopy(fileTemplate);
  74. project.Save();
  75. Assert.IsTrue(resetEvent.WaitOne(500));
  76. Assert.IsTrue(archive.ContainsFile(expectedFilePath));
  77. Assert.IsFalse(archive.IsOutdated(expectedFilePath));
  78. // rename a file
  79. string oldFilePath = expectedFilePath;
  80. expectedFilePath = Path.Combine(Path.GetDirectoryName(project.FullName), "NewCSharpClass2.cs");
  81. expectedEventType = FileEventType.FileAdded;
  82. item.Open();
  83. item.SaveAs(expectedFilePath);
  84. File.Delete(oldFilePath);
  85. project.Save();
  86. Assert.IsTrue(resetEvent.WaitOne(500));
  87. Assert.IsTrue(archive.ContainsFile(expectedFilePath), "The archive should contain {0}", expectedFilePath);
  88. Assert.IsFalse(archive.ContainsFile(oldFilePath), "the archive should not contain {0}", oldFilePath);
  89. Assert.IsFalse(archive.IsOutdated(expectedFilePath), String.Format("{0} is outdated", expectedFilePath));
  90. // delete the file
  91. expectedEventType = FileEventType.FileDeleted;
  92. item = TestSolution.FindProjectItem(expectedFilePath);
  93. item.Delete();
  94. project.Save();
  95. Assert.IsTrue(resetEvent.WaitOne(500));
  96. Assert.IsFalse(archive.IsOutdated(expectedFilePath));
  97. //Assert.AreEqual(2, archive.FileUnits.Count());
  98. TestHelpers.TestScaffold.Service.SourceFileChanged -= action;
  99. }
  100. [TestMethod]
  101. [HostType("VS IDE")]
  102. public void TestCsProjectOperations() {
  103. var archive = TestHelpers.TestScaffold.Service.GetSrcMLArchive();
  104. AutoResetEvent resetEvent = new AutoResetEvent(false);
  105. var testProjectName = "ClassLibrary1";
  106. var expectedProjectDirectory = Path.GetFullPath(Path.Combine(TestSolutionName, testProjectName));
  107. var expectedEventType = FileEventType.FileAdded;
  108. HashSet<string> expectedFiles = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase) {
  109. Path.Combine(expectedProjectDirectory, "Class1.cs"),
  110. Path.Combine(expectedProjectDirectory, "Properties", "AssemblyInfo.cs")
  111. };
  112. EventHandler<FileEventRaisedArgs> action = (o, e) => {
  113. lock(TestLock) {
  114. if(expectedFiles.Contains(Path.GetFullPath(e.FilePath)) && e.EventType == expectedEventType) {
  115. resetEvent.Set();
  116. }
  117. }
  118. };
  119. TestHelpers.TestScaffold.Service.SourceFileChanged += action;
  120. var projectTemplate = Path.GetFullPath(Path.Combine(TestConstants.TemplatesFolder, testProjectName, testProjectName, testProjectName + ".csproj"));
  121. // add a new project
  122. var addedProject = TestSolution.AddFromTemplate(projectTemplate, expectedProjectDirectory, testProjectName);
  123. addedProject.Save();
  124. Assert.IsTrue(resetEvent.WaitOne(500));
  125. Assert.IsTrue(resetEvent.WaitOne(500));
  126. foreach(var expectedFile in expectedFiles) {
  127. Assert.IsTrue(File.Exists(expectedFile));
  128. Assert.IsTrue(archive.ContainsFile(expectedFile));
  129. Assert.IsFalse(archive.IsOutdated(expectedFile));
  130. }
  131. // remove the project
  132. expectedEventType = FileEventType.FileDeleted;
  133. TestSolution.Remove(addedProject);
  134. Assert.IsTrue(resetEvent.WaitOne(500));
  135. // Assert.IsTrue(resetEvent.WaitOne(500));
  136. foreach(var expectedFile in expectedFiles) {
  137. Assert.IsFalse(archive.ContainsFile(expectedFile));
  138. }
  139. TestHelpers.TestScaffold.Service.SourceFileChanged -= action;
  140. }
  141. public void Invoke(MethodInvoker globalSystemWindowsFormsMethodInvoker) {
  142. UIThreadInvoker.Invoke(globalSystemWindowsFormsMethodInvoker);
  143. }
  144. }
  145. }