~ubuntu-branches/ubuntu/vivid/monodevelop/vivid-proposed

« back to all changes in this revision

Viewing changes to src/addins/NUnit/Services/XmlResultsStore.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2014-10-09 14:09:23 UTC
  • mfrom: (10.3.5)
  • Revision ID: package-import@ubuntu.com-20141009140923-s0d22u5f9kg8jvds
Tags: 5.5.0.227-1
* [b2c8331] Imported Upstream version 5.5.0.227 (Closes: #754316)
* [d210995] Delete obsolete patches
* [1b59ae1] Clear patch fizz, via quilt refresh
* [3dd147d] Fix error in configure.in which applies for tarball builds but 
  not git builds when running autoreconf
* [21c2a57] Remove Metacity references for good
* [3331661] Ensure NUnit 2.6.3 is installed
* [fd85c88] Build-depend on NuGet
* [a1ae116] Add WebKit to build dependencies, for Xwt moduleref resolution
* [9b4cf12] Since the GDB addin is integrated now, declare it in 
  debian/control
* [6231562] Correct NUnit links
* [3d2b693] Fix NuGet addin, by copying libs locally
* [74bf9a8] Don't symlink unused Mocks NUnit assembly
* [ade52b2] Ensure IKVM.Reflection is built with default (4.5) profile

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// XmlResultsStore.cs
3
 
//
4
 
// Author:
5
 
//   Lluis Sanchez Gual
6
 
//
7
 
// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining
10
 
// a copy of this software and associated documentation files (the
11
 
// "Software"), to deal in the Software without restriction, including
12
 
// without limitation the rights to use, copy, modify, merge, publish,
13
 
// distribute, sublicense, and/or sell copies of the Software, and to
14
 
// permit persons to whom the Software is furnished to do so, subject to
15
 
// the following conditions:
16
 
// 
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
// 
20
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
 
//
28
 
 
29
 
using System;
30
 
using System.IO;
31
 
using System.Linq;
32
 
using System.Collections;
33
 
using System.Xml.Serialization;
34
 
using System.Globalization;
35
 
using MonoDevelop.Core;
36
 
 
37
 
namespace MonoDevelop.NUnit
38
 
{
39
 
        public class XmlResultsStore: IResultsStore
40
 
        {
41
 
                Hashtable fileCache = new Hashtable ();
42
 
                string basePath;
43
 
                string storeId;
44
 
                Hashtable cachedRootList = new Hashtable ();
45
 
                
46
 
                static XmlSerializer serializer = new XmlSerializer (typeof(TestRecord));
47
 
                
48
 
                public XmlResultsStore (string directory, string storeId)
49
 
                {
50
 
                        basePath = directory;
51
 
                        this.storeId = storeId;
52
 
                }
53
 
                
54
 
                public void RegisterResult (string configuration, UnitTest test, UnitTestResult result)
55
 
                {
56
 
                        string aname = test.StoreRelativeName;
57
 
                        
58
 
                        TestRecord root = GetRootRecord (configuration, result.TestDate);
59
 
                        if (root == null) {
60
 
                                root = new TestRecord ();
61
 
                                fileCache [GetRootFileName (configuration, result.TestDate)] = root;
62
 
                        }
63
 
                        root.Modified = true;
64
 
                        TestRecord record = root;
65
 
                        
66
 
                        if (aname.Length > 0) {
67
 
                                string[] path = test.StoreRelativeName.Split ('.');
68
 
                                foreach (string p in path) {
69
 
                                        TestRecord ctr = record.Tests != null ? record.Tests [p] : null;
70
 
                                        if (ctr == null) {
71
 
                                                ctr = new TestRecord ();
72
 
                                                ctr.Name = p;
73
 
                                                if (record.Tests == null)
74
 
                                                        record.Tests = new TestRecordCollection ();
75
 
                                                record.Tests.Add (ctr);
76
 
                                        }
77
 
                                        record = ctr;
78
 
                                }
79
 
                        }
80
 
                        
81
 
                        if (record.Results == null)
82
 
                                record.Results = new UnitTestResultCollection ();
83
 
                        record.Results.Add (result);
84
 
                }
85
 
                
86
 
                public UnitTestResult GetNextResult (string configuration, UnitTest test, DateTime date)
87
 
                {
88
 
                        DateTime currentDate = date;
89
 
                        TestRecord root = GetRootRecord (configuration, currentDate);
90
 
                        if (root == null)
91
 
                                root = GetNextRootRecord (configuration, ref currentDate);
92
 
                        
93
 
                        while (root != null) {
94
 
                                TestRecord tr = FindRecord (root, test.StoreRelativeName);
95
 
                                if (tr != null && tr.Results != null) {
96
 
                                        foreach (UnitTestResult res in tr.Results) {
97
 
                                                if (res.TestDate > date)
98
 
                                                        return res;
99
 
                                        }
100
 
                                }
101
 
                                root = GetNextRootRecord (configuration, ref currentDate);
102
 
                        }
103
 
                        return null;
104
 
                }
105
 
                
106
 
                public UnitTestResult GetPreviousResult (string configuration, UnitTest test, DateTime date)
107
 
                {
108
 
                        DateTime currentDate = date;
109
 
                        TestRecord root = GetRootRecord (configuration, currentDate);
110
 
                        if (root == null)
111
 
                                root = GetPreviousRootRecord (configuration, ref currentDate);
112
 
                        
113
 
                        while (root != null) {
114
 
                                TestRecord tr = FindRecord (root, test.StoreRelativeName);
115
 
                                if (tr != null && tr.Results != null) {
116
 
                                        for (int n = tr.Results.Count - 1; n >= 0; n--) {
117
 
                                                UnitTestResult res = (UnitTestResult) tr.Results [n];
118
 
                                                if (res.TestDate < date)
119
 
                                                        return res;
120
 
                                        }
121
 
                                }
122
 
                                root = GetPreviousRootRecord (configuration, ref currentDate);
123
 
                        }
124
 
                        return null;
125
 
                }
126
 
                
127
 
                public UnitTestResult GetLastResult (string configuration, UnitTest test, DateTime date)
128
 
                {
129
 
                        return GetPreviousResult (configuration, test, date.AddTicks (1));
130
 
                }
131
 
                
132
 
                public UnitTestResult[] GetResults (string configuration, UnitTest test, DateTime startDate, DateTime endDate)
133
 
                {
134
 
                        ArrayList list = new ArrayList ();
135
 
                        DateTime firstDay = new DateTime (startDate.Year, startDate.Month, startDate.Day);
136
 
                        
137
 
                        DateTime[] dates = GetStoreDates (configuration);
138
 
                        
139
 
                        foreach (DateTime date in dates) {
140
 
                                if (date < firstDay)
141
 
                                        continue;
142
 
                                if (date > endDate)
143
 
                                        break;
144
 
                                
145
 
                                TestRecord root = GetRootRecord (configuration, date);
146
 
                                if (root == null) continue;
147
 
 
148
 
                                TestRecord tr = FindRecord (root, test.StoreRelativeName);
149
 
                                if (tr != null && tr.Results != null) {
150
 
                                        foreach (UnitTestResult res in tr.Results) {
151
 
                                                if (res.TestDate >= startDate && res.TestDate <= endDate)
152
 
                                                        list.Add (res);
153
 
                                        }
154
 
                                }
155
 
                        }
156
 
                        
157
 
                        return (UnitTestResult[]) list.ToArray (typeof(UnitTestResult));
158
 
                }
159
 
                
160
 
                public UnitTestResult[] GetResultsToDate (string configuration, UnitTest test, DateTime endDate, int count)
161
 
                {
162
 
                        ArrayList list = new ArrayList ();
163
 
                        DateTime[] dates = GetStoreDates (configuration);
164
 
                        
165
 
                        for (int n = dates.Length - 1; n >= 0 && list.Count < count; n--) {
166
 
                                if (dates [n] > endDate)
167
 
                                        continue;
168
 
                                        
169
 
                                TestRecord root = GetRootRecord (configuration, dates [n]);
170
 
                                if (root == null) continue;
171
 
 
172
 
                                TestRecord tr = FindRecord (root, test.StoreRelativeName);
173
 
                                if (tr != null && tr.Results != null) {
174
 
                                        for (int m = tr.Results.Count - 1; m >= 0 && list.Count < count; m--) {
175
 
                                                UnitTestResult res = (UnitTestResult) tr.Results [m];
176
 
                                                if (res.TestDate <= endDate)
177
 
                                                        list.Add (res);
178
 
                                        }
179
 
                                }
180
 
                        }
181
 
                        
182
 
                        UnitTestResult[] array = (UnitTestResult[]) list.ToArray (typeof(UnitTestResult));
183
 
                        Array.Reverse (array);
184
 
                        return array;
185
 
                }
186
 
                
187
 
                public void Save ()
188
 
                {
189
 
                        if (!Directory.Exists (basePath))
190
 
                                Directory.CreateDirectory (basePath);
191
 
 
192
 
                        foreach (DictionaryEntry entry in fileCache) {
193
 
                                TestRecord record = (TestRecord) entry.Value;
194
 
                                if (!record.Modified)
195
 
                                        continue;
196
 
 
197
 
                                string file = Path.Combine (basePath, (string)entry.Key);
198
 
                                StreamWriter writer = new StreamWriter (file);
199
 
                                try {
200
 
                                        serializer.Serialize (writer, record);
201
 
                                } finally {
202
 
                                        writer.Close ();
203
 
                                }
204
 
                                record.Modified = false;
205
 
                        }
206
 
                        cachedRootList.Clear ();
207
 
                }
208
 
                
209
 
                TestRecord FindRecord (TestRecord root, string aname)
210
 
                {
211
 
                        if (aname.Length == 0)
212
 
                                return root;
213
 
                        else {
214
 
                                string[] path = aname.Split ('.');
215
 
                                TestRecord tr = root;
216
 
                                foreach (string p in path) {
217
 
                                        if (tr.Tests == null)
218
 
                                                return null;
219
 
                                        tr = tr.Tests [p];
220
 
                                        if (tr == null)
221
 
                                                return null;
222
 
                                }
223
 
                                return tr;
224
 
                        }
225
 
                }
226
 
                
227
 
                TestRecord GetRootRecord (string configuration, DateTime date)
228
 
                {
229
 
                        string file = GetRootFileName (configuration, date);
230
 
                        TestRecord res = (TestRecord) fileCache [file];
231
 
                        if (res != null)
232
 
                                return res;
233
 
                        string filePath;
234
 
                        try {
235
 
                                filePath = Path.Combine (basePath, file);
236
 
                        } catch (Exception) {
237
 
                                return null;
238
 
                        }
239
 
                        if (!File.Exists (filePath))
240
 
                                return null;
241
 
 
242
 
                        StreamReader s = new StreamReader (filePath);
243
 
                        try {
244
 
                                res = (TestRecord) serializer.Deserialize (s);
245
 
                        } catch (Exception ex) {
246
 
                                LoggingService.LogError (ex.ToString ());
247
 
                                return null;
248
 
                        } finally {
249
 
                                s.Close ();
250
 
                        }
251
 
                        fileCache [file] = res;
252
 
                        return res;
253
 
                }
254
 
                
255
 
                TestRecord GetNextRootRecord (string configuration, ref DateTime date)
256
 
                {
257
 
                        DateTime[] dates = GetStoreDates (configuration);
258
 
                        foreach (DateTime d in dates) {
259
 
                                if (d > date) {
260
 
                                        date = d;
261
 
                                        return GetRootRecord (configuration, d);
262
 
                                }
263
 
                        }
264
 
                        return null;
265
 
                }
266
 
                
267
 
                TestRecord GetPreviousRootRecord (string configuration, ref DateTime date)
268
 
                {
269
 
                        date = new DateTime (date.Year, date.Month, date.Day);
270
 
                        DateTime[] dates = GetStoreDates (configuration);
271
 
                        for (int n = dates.Length - 1; n >= 0; n--) {
272
 
                                if (dates [n] < date) {
273
 
                                        date = dates [n];
274
 
                                        return GetRootRecord (configuration, dates [n]);
275
 
                                }
276
 
                        }
277
 
                        return null;
278
 
                }
279
 
                
280
 
                string GetRootFileName (string configuration, DateTime date)
281
 
                {
282
 
                        // Filter out all invalid path characters in the file name (see: Bug 3023 - Running NUnit tests throws ArgumentException: Illegal Characters in path)
283
 
                        var filteredConfiguration = new string (configuration.Where (c => !Path.GetInvalidPathChars ().Any (i => c == i)).ToArray ());
284
 
                        return storeId + "-" + filteredConfiguration + "-" + date.ToString ("yyyy-MM-dd", CultureInfo.InvariantCulture) + ".xml";
285
 
                }
286
 
                
287
 
                DateTime ParseFileNameDate (string configuration, string fileName)
288
 
                {
289
 
                        fileName = Path.GetFileNameWithoutExtension (fileName);
290
 
                        fileName = fileName.Substring (storeId.Length + configuration.Length + 2);
291
 
                        return DateTime.ParseExact (fileName, "yyyy-MM-dd", CultureInfo.InvariantCulture);
292
 
                }
293
 
                
294
 
                DateTime[] GetStoreDates (string configuration)
295
 
                {
296
 
                        if (!Directory.Exists (basePath))
297
 
                                return new DateTime [0];
298
 
                        
299
 
                        DateTime[] res = (DateTime[]) cachedRootList [configuration];
300
 
                        if (res != null)
301
 
                                return res;
302
 
 
303
 
                        ArrayList dates = new ArrayList ();
304
 
                        foreach (string file in Directory.GetFiles (basePath, storeId + "-" + configuration + "-*")) {
305
 
                                try {
306
 
                                        DateTime t = ParseFileNameDate (configuration, Path.GetFileName (file));
307
 
                                        dates.Add (t);
308
 
                                } catch { }
309
 
                        }
310
 
                        res = (DateTime[]) dates.ToArray (typeof(DateTime));
311
 
                        cachedRootList [configuration] = res;
312
 
                        return res;
313
 
                }
314
 
        }
315
 
        
316
 
        public class TestRecord
317
 
        {
318
 
                string name;
319
 
                UnitTestResultCollection results;
320
 
                TestRecordCollection tests;
321
 
                internal bool Modified;
322
 
                
323
 
                [XmlAttribute]
324
 
                public string Name {
325
 
                        get { return name; }
326
 
                        set { name = value; }
327
 
                }
328
 
                
329
 
                public UnitTestResultCollection Results {
330
 
                        get { return results; }
331
 
                        set { results = value; }
332
 
                }
333
 
                
334
 
                public TestRecordCollection Tests {
335
 
                        get { return tests; }
336
 
                        set { tests = value; }
337
 
                }
338
 
        }
339
 
        
340
 
        public class TestRecordCollection: CollectionBase
341
 
        {
342
 
                public new TestRecord this [int n] {
343
 
                        get { return (TestRecord) ((IList)this) [n]; }
344
 
                }
345
 
                
346
 
                public new TestRecord this [string name] {
347
 
                        get {
348
 
                                for (int n=0; n<List.Count; n++)
349
 
                                        if (((TestRecord)List [n]).Name == name)
350
 
                                                return (TestRecord) List [n];
351
 
                                return null;
352
 
                        }
353
 
                }
354
 
                
355
 
                public void Add (TestRecord test)
356
 
                {
357
 
                        ((IList)this).Add (test);
358
 
                }
359
 
        }
360
 
        
361
 
        public class UnitTestResultCollection: CollectionBase
362
 
        {
363
 
                public new UnitTestResult this [int n] {
364
 
                        get { return (UnitTestResult) ((IList)this) [n]; }
365
 
                }
366
 
                
367
 
                public void Add (UnitTestResult test)
368
 
                {
369
 
                        ((IList)this).Add (test);
370
 
                }
371
 
        }       
372
 
}
373