-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXUnit2NUnitConverter.cs
102 lines (94 loc) · 3.78 KB
/
XUnit2NUnitConverter.cs
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
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks.Dataflow;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using Service.Contracts;
using Service.Data;
namespace Service.Flow
{
public class XUnit2NUnitConverter : IConversionScheduler
{
private readonly ITargetBlock<Tuple<Guid, XUnitData>> targetBlock;
public XUnit2NUnitConverter(IConversionRepository repository)
{
// cache the xslt to transform from xUnit to nUnit
var transform = new XslCompiledTransform();
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(typeof(Startup), "Resources.NUnitXml.xslt"))
{
using (var xmlReader = new XmlTextReader(stream))
{
transform.Load(xmlReader);
}
}
// save the request in the database
var saveRequest = new TransformBlock<Tuple<Guid, XUnitData>, Tuple<Guid, XUnitData>>(input =>
{
try
{
repository.AddRequest(input.Item1, input.Item2);
return input;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return input;
}
});
// convert the input string to an XPathDocument
var readXml = new TransformBlock<Tuple<Guid, XUnitData>, Tuple<Guid, XPathDocument>>(input =>
{
try
{
using (var reader = new StringReader(input.Item2.XUnitResult))
{
var doc = new XPathDocument(reader);
return new Tuple<Guid, XPathDocument>(input.Item1, doc);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return new Tuple<Guid, XPathDocument>(input.Item1, null);
}
});
// perform the actual conversion
var convertXml = new TransformBlock<Tuple<Guid, XPathDocument>, Tuple<Guid, string>>(input =>
{
try
{
using (var stringWriter = new StringWriter())
{
using (var writer = new XmlTextWriter(stringWriter))
{
transform.Transform(input.Item2, null, writer);
}
return new Tuple<Guid, string>(input.Item1, stringWriter.GetStringBuilder().ToString());
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return new Tuple<Guid, string>(input.Item1, null);
}
});
// convert to output
var createOutput = new TransformBlock<Tuple<Guid, string>, Tuple<Guid, NUnitData>>(input => new Tuple<Guid, NUnitData>(input.Item1, new NUnitData {NUnitResult = input.Item2}));
var saveResult = new ActionBlock<Tuple<Guid, NUnitData>>(input => repository.AddResult(input.Item1, input.Item2));
saveRequest.LinkTo(readXml);
readXml.LinkTo(convertXml);
convertXml.LinkTo(createOutput);
createOutput.LinkTo(saveResult);
targetBlock = saveRequest;
}
public Guid ScheduleConversion(XUnitData xUnitData)
{
var id = Guid.NewGuid();
targetBlock.Post(new Tuple<Guid, XUnitData>(id, xUnitData));
return id;
}
}
}