~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/UserInterfaceTests/MonoDevelop.Components.AutoTest/StressTest.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// StressTestAttribute.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Reflection;
 
29
using System.Collections.Generic;
 
30
using System.Linq;
 
31
 
 
32
namespace MonoDevelop.Components.AutoTest
 
33
{
 
34
        public class StressTest
 
35
        {
 
36
                List<TestCase> cachedTests;
 
37
                
 
38
                public StressTest ()
 
39
                {
 
40
                }
 
41
                
 
42
                public AutoTestClientSession Session {
 
43
                        get { return TestService.Session; }
 
44
                }
 
45
                
 
46
                protected virtual void Setup ()
 
47
                {
 
48
                }
 
49
                
 
50
                protected virtual void TearDown ()
 
51
                {
 
52
                }
 
53
        
 
54
                public void Run (TestPlan testPlan)
 
55
                {
 
56
                        List<TestCase> tests = GetTests ();
 
57
                        
 
58
                        Setup ();
 
59
                        for (int n=0; n<testPlan.Repeat; n++) {
 
60
                                Console.WriteLine ("Test suite iteration: " + n);
 
61
                                foreach (TestCase t in tests) {
 
62
                                        int ti = testPlan.GetIterationsForTest (t.Method.Name);
 
63
                                        for (int i=0; i<ti; i++) {
 
64
                                                Console.WriteLine ("Running test {0}: {1}", t.Method.Name, i);
 
65
                                                t.Method.Invoke (this, null);
 
66
                                                if (i < ti-1 && t.UndoMethod != null) {
 
67
                                                        t.UndoMethod.Invoke (this, null);
 
68
                                                }
 
69
                                        }
 
70
                                }
 
71
                        }
 
72
                        TearDown ();
 
73
                }
 
74
                
 
75
                public bool HasTest (string name)
 
76
                {
 
77
                        return GetTests ().Any (t => t.Method.Name == name);
 
78
                }
 
79
                
 
80
                List<TestCase> GetTests ()
 
81
                {
 
82
                        if (cachedTests != null)
 
83
                                return cachedTests;
 
84
                        
 
85
                        List<TestCase> tests = new List<TestCase> ();
 
86
                        
 
87
                        foreach (MethodInfo met in GetType().GetMethods ())
 
88
                        {
 
89
                                foreach (TestStepAttribute sat in met.GetCustomAttributes (typeof(TestStepAttribute), true)) {
 
90
                                        TestCase tc = new TestCase () {
 
91
                                                Method = met,
 
92
                                                RunAfter = sat.RunAfter,
 
93
                                                Stage = sat.Stage
 
94
                                        };
 
95
                                        AddSorted (tests, tc);
 
96
                                }
 
97
                                foreach (UndoTestStepAttribute sat in met.GetCustomAttributes (typeof(UndoTestStepAttribute), true)) {
 
98
                                        AddUndo (tests, sat.Undoes, met);
 
99
                                }
 
100
                        }
 
101
                        cachedTests = tests;
 
102
                        return tests;
 
103
                }
 
104
                
 
105
                void AddSorted (List<TestCase> list, TestCase t)
 
106
                {
 
107
                        for (int n=0; n<list.Count; n++) {
 
108
                                if (list[n].Method.Name == t.RunAfter) {
 
109
                                        list.Insert (n+1, t);
 
110
                                        return;
 
111
                                }
 
112
                                if (list[n].RunAfter == t.Method.Name) {
 
113
                                        list.Insert (n, t);
 
114
                                        return;
 
115
                                }
 
116
                        }
 
117
                        list.Add (t);
 
118
                }
 
119
                
 
120
                void AddUndo (List<TestCase> list, string methodName, MethodInfo undoer)
 
121
                {
 
122
                        foreach (TestCase t in list) {
 
123
                                if (t.Method.Name == methodName)
 
124
                                        t.UndoMethod = undoer;
 
125
                        }
 
126
                }
 
127
        }
 
128
        
 
129
        class TestCase
 
130
        {
 
131
                public MethodInfo Method { get; set; }
 
132
                public MethodInfo UndoMethod { get; set; }
 
133
                public string RunAfter { get; set; }
 
134
                public string Stage { get; set; }
 
135
        }
 
136
        
 
137
        public class TestPlan
 
138
        {
 
139
                Dictionary<string,int> itersByTest = new Dictionary<string, int> ();
 
140
                
 
141
                public int Repeat { get; set; }
 
142
                
 
143
                public int Iterations { get; set; }
 
144
                
 
145
                public int GetIterationsForTest (string name)
 
146
                {
 
147
                        int res;
 
148
                        if (itersByTest.TryGetValue (name, out res))
 
149
                                return res;
 
150
                        else
 
151
                                return Iterations > 0 ? Iterations : 1;
 
152
                }
 
153
                
 
154
                public void SetIterationsForTest (string name, int iterations)
 
155
                {
 
156
                        itersByTest [name] = iterations;
 
157
                }
 
158
        }
 
159
}
 
160