atlassian.net-sdk-v2.0 /Atlassian.Jira.Test.Integration.Setup/SetupProgram.cs

Language C# Lines 138
MD5 Hash 59006e62476962773eb55d090232571f Estimated Cost $2,611 (why?)
Repository https://bitbucket.org/yyo/atlassian.net-sdk-v2.0.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Testing.Light;
using System.IO;
using System.Diagnostics;
using System.Threading;


namespace Atlassian.Jira.Test.Integration.Setup
{
    class SetupProgram
    {
        static void Main(string[] args)
        {
            var currentDir = Path.GetDirectoryName(typeof(SetupProgram).Assembly.Location);
            Environment.CurrentDirectory = currentDir;

            if (args.Length > 0)
            {
                switch (args[0].ToLowerInvariant())
                {
                    case "start":
                        StartJira(currentDir);
                        break;
                    case "setup":
                        SetupTestData(currentDir);
                        break;
                    default:
                        throw new ArgumentException(String.Format("Unknwon command '{0}'", args[0]));
                }
            }
            else
            {
                StartJira(currentDir);
                SetupTestData(currentDir);                
            }
        }

        private static bool IsJiraReady(int seconds)
        {
            Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine(String.Format("Checking if JIRA is up (wait time: {0} seconds).", seconds));
            Console.WriteLine("-------------------------------------------------------");

            HtmlPage page = new HtmlPage(new Uri("http://localhost:2990/jira/"));

            try
            {
                page.Navigate("login.jsp");
                return page.Elements.Find("h2", 0).CachedInnerText.Trim().StartsWith("Welcome", StringComparison.OrdinalIgnoreCase);
            }
            catch
            {
            }

            return false;
        }

        private static void SetupTestData(string currentDir)
        {
            int seconds = 0;
            while (!IsJiraReady(seconds))
            {
                Thread.Sleep(1000);
                seconds++;
            }

            Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("Restoring test data.");
            Console.WriteLine("-------------------------------------------------------");


            HtmlPage page = new HtmlPage(new Uri("http://localhost:2990/jira/"));

            // login
            Login(page);

            // Restore TestData
            RestoreBackup(page, currentDir);

            // login
            Login(page);

            // enable RPC
            page.Navigate("secure/admin/EditApplicationProperties!default.jspa");
            page.Elements.Find("allowRpcOn", MatchMethod.Literal).Click();
            page.Elements.Find("edit_property").Click();

            Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("JIRA Setup Complete. You can now run the integration tests.");
            Console.WriteLine("-------------------------------------------------------");
        }

        private static void RestoreBackup(HtmlPage page, string currentDir)
        {
            page.Navigate("secure/admin/XmlRestore!default.jspa");
            File.Copy(
                Path.Combine(currentDir, "TestData.zip"),
                Path.Combine(currentDir, @"amps-standalone\target\jira\home\import\TestData.zip"),
                true);

            page.Elements.Find("filename", MatchMethod.Literal).SetText("TestData.zip");
            page.Elements.Find("restore_submit").Click();

            // TODO: proper wait  until import is complete
            Thread.Sleep(15000);
        }

        private static void Login(HtmlPage page)
        {
            var timeout = DateTime.Now.AddSeconds(10);
            do
            {
                page.Navigate("login.jsp");
            }
            while (!page.Elements.Exists("login-form-username") && DateTime.Now < timeout);

            page.Elements.Find("login-form-username").SetText("admin");
            page.Elements.Find("login-form-password").SetText("admin");
            page.Elements.Find("login").Click();
        }

        private static void StartJira(string currentDir)
        {
            Console.WriteLine("-------------------------------------------------------");
            Console.WriteLine("Starting JIRA.");
            Console.WriteLine("-------------------------------------------------------");

            var process = new Process();
            process.StartInfo.FileName = Path.Combine(currentDir, "StartJira.bat");
            process.Start();
        }

    }
}
Back to Top