PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Atlassian.Jira.Test.Integration.Setup/SetupProgram.cs

https://bitbucket.org/headspring/atlassian.net-sdk
C# | 137 lines | 108 code | 24 blank | 5 comment | 5 complexity | 59006e62476962773eb55d090232571f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Web.Testing.Light;
  6. using System.IO;
  7. using System.Diagnostics;
  8. using System.Threading;
  9. namespace Atlassian.Jira.Test.Integration.Setup
  10. {
  11. class SetupProgram
  12. {
  13. static void Main(string[] args)
  14. {
  15. var currentDir = Path.GetDirectoryName(typeof(SetupProgram).Assembly.Location);
  16. Environment.CurrentDirectory = currentDir;
  17. if (args.Length > 0)
  18. {
  19. switch (args[0].ToLowerInvariant())
  20. {
  21. case "start":
  22. StartJira(currentDir);
  23. break;
  24. case "setup":
  25. SetupTestData(currentDir);
  26. break;
  27. default:
  28. throw new ArgumentException(String.Format("Unknwon command '{0}'", args[0]));
  29. }
  30. }
  31. else
  32. {
  33. StartJira(currentDir);
  34. SetupTestData(currentDir);
  35. }
  36. }
  37. private static bool IsJiraReady(int seconds)
  38. {
  39. Console.WriteLine("-------------------------------------------------------");
  40. Console.WriteLine(String.Format("Checking if JIRA is up (wait time: {0} seconds).", seconds));
  41. Console.WriteLine("-------------------------------------------------------");
  42. HtmlPage page = new HtmlPage(new Uri("http://localhost:2990/jira/"));
  43. try
  44. {
  45. page.Navigate("login.jsp");
  46. return page.Elements.Find("h2", 0).CachedInnerText.Trim().StartsWith("Welcome", StringComparison.OrdinalIgnoreCase);
  47. }
  48. catch
  49. {
  50. }
  51. return false;
  52. }
  53. private static void SetupTestData(string currentDir)
  54. {
  55. int seconds = 0;
  56. while (!IsJiraReady(seconds))
  57. {
  58. Thread.Sleep(1000);
  59. seconds++;
  60. }
  61. Console.WriteLine("-------------------------------------------------------");
  62. Console.WriteLine("Restoring test data.");
  63. Console.WriteLine("-------------------------------------------------------");
  64. HtmlPage page = new HtmlPage(new Uri("http://localhost:2990/jira/"));
  65. // login
  66. Login(page);
  67. // Restore TestData
  68. RestoreBackup(page, currentDir);
  69. // login
  70. Login(page);
  71. // enable RPC
  72. page.Navigate("secure/admin/EditApplicationProperties!default.jspa");
  73. page.Elements.Find("allowRpcOn", MatchMethod.Literal).Click();
  74. page.Elements.Find("edit_property").Click();
  75. Console.WriteLine("-------------------------------------------------------");
  76. Console.WriteLine("JIRA Setup Complete. You can now run the integration tests.");
  77. Console.WriteLine("-------------------------------------------------------");
  78. }
  79. private static void RestoreBackup(HtmlPage page, string currentDir)
  80. {
  81. page.Navigate("secure/admin/XmlRestore!default.jspa");
  82. File.Copy(
  83. Path.Combine(currentDir, "TestData.zip"),
  84. Path.Combine(currentDir, @"amps-standalone\target\jira\home\import\TestData.zip"),
  85. true);
  86. page.Elements.Find("filename", MatchMethod.Literal).SetText("TestData.zip");
  87. page.Elements.Find("restore_submit").Click();
  88. // TODO: proper wait until import is complete
  89. Thread.Sleep(15000);
  90. }
  91. private static void Login(HtmlPage page)
  92. {
  93. var timeout = DateTime.Now.AddSeconds(10);
  94. do
  95. {
  96. page.Navigate("login.jsp");
  97. }
  98. while (!page.Elements.Exists("login-form-username") && DateTime.Now < timeout);
  99. page.Elements.Find("login-form-username").SetText("admin");
  100. page.Elements.Find("login-form-password").SetText("admin");
  101. page.Elements.Find("login").Click();
  102. }
  103. private static void StartJira(string currentDir)
  104. {
  105. Console.WriteLine("-------------------------------------------------------");
  106. Console.WriteLine("Starting JIRA.");
  107. Console.WriteLine("-------------------------------------------------------");
  108. var process = new Process();
  109. process.StartInfo.FileName = Path.Combine(currentDir, "StartJira.bat");
  110. process.Start();
  111. }
  112. }
  113. }