Skip to content

Commit

Permalink
Merge branch 'xsltcustomisation'
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed Jun 17, 2010
2 parents 833613a + d40dcfa commit 4e893ed
Show file tree
Hide file tree
Showing 32 changed files with 11,135 additions and 70 deletions.
43 changes: 19 additions & 24 deletions Reporting/NUnitExecutionReport/NUnitExecutionReportGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using TechTalk.SpecFlow.Generator.Configuration;
Expand All @@ -15,24 +13,15 @@ public class NUnitExecutionReportGenerator
{
private ReportElements.NUnitExecutionReport report;
private readonly SpecFlowProject specFlowProject;
private readonly string xmlTestResultPath;
private readonly string labeledTestOutputPath;
private readonly NUnitExecutionReportParameters reportParameters;

public NUnitExecutionReportGenerator(string projectFile, string xmlTestResultPath, string labeledTestOutputPath)
{
this.xmlTestResultPath = xmlTestResultPath;
this.specFlowProject = MsBuildProjectReader.LoadSpecFlowProjectFromMsBuild(projectFile);
this.labeledTestOutputPath = labeledTestOutputPath;
}

public NUnitExecutionReportGenerator(SpecFlowProject specFlowProject, string xmlTestResultPath, string labeledTestOutputPath)
{
this.xmlTestResultPath = xmlTestResultPath;
this.specFlowProject = specFlowProject;
this.labeledTestOutputPath = labeledTestOutputPath;
public NUnitExecutionReportGenerator(NUnitExecutionReportParameters reportParameters)
{
specFlowProject = MsBuildProjectReader.LoadSpecFlowProjectFromMsBuild(reportParameters.ProjectFile);
this.reportParameters = reportParameters;
}

public void GenerateReport()
private void GenerateReport()
{
report = new ReportElements.NUnitExecutionReport();
report.ProjectName = specFlowProject.ProjectName;
Expand All @@ -41,9 +30,9 @@ public void GenerateReport()
XmlDocument xmlTestResult = LoadXmlTestResult();
report.NUnitXmlTestResult = xmlTestResult.DocumentElement;

if (File.Exists(labeledTestOutputPath))
if (File.Exists(reportParameters.LabelledTestOutput))
{
using(var reader = new StreamReader(labeledTestOutputPath))
using(var reader = new StreamReader(reportParameters.LabelledTestOutput))
{
string currentTest = "unknown";
List<string> testLines = new List<string>();
Expand Down Expand Up @@ -84,25 +73,31 @@ private XmlDocument LoadXmlTestResult()
XmlDocument xmlTestResult = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("", ReportElements.NUnitExecutionReport.XmlNUnitNamespace);
using(XmlReader reader = XmlReader.Create(xmlTestResultPath, new XmlReaderSettings(), new XmlParserContext(null, nsmgr, null, XmlSpace.None)))
using(XmlReader reader = XmlReader.Create(reportParameters.XmlTestResult, new XmlReaderSettings(), new XmlParserContext(null, nsmgr, null, XmlSpace.None)))
{
xmlTestResult.Load(reader);
}
return xmlTestResult;
}

public void TransformReport(string outputFilePath)
private void TransformReport()
{
XmlSerializer serializer = new XmlSerializer(typeof(ReportElements.NUnitExecutionReport), ReportElements.NUnitExecutionReport.XmlNamespace);

if (XsltHelper.IsXmlOutput(outputFilePath))
if (XsltHelper.IsXmlOutput(reportParameters.OutputFile))
{
XsltHelper.TransformXml(serializer, report, outputFilePath);
XsltHelper.TransformXml(serializer, report, reportParameters.OutputFile);
}
else
{
XsltHelper.TransformHtml(serializer, report, GetType(), outputFilePath, specFlowProject.GeneratorConfiguration);
XsltHelper.TransformHtml(serializer, report, GetType(), reportParameters.OutputFile, specFlowProject.GeneratorConfiguration, reportParameters.XsltFile);
}
}

public void GenerateAndTransformReport()
{
GenerateReport();
TransformReport();
}
}
}
17 changes: 17 additions & 0 deletions Reporting/NUnitExecutionReport/NUnitExecutionReportParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.IO;

namespace TechTalk.SpecFlow.Reporting.NUnitExecutionReport
{
public class NUnitExecutionReportParameters : ReportParameters
{
public string LabelledTestOutput { get; private set; }
public string XmlTestResult { get; private set; }

public NUnitExecutionReportParameters(string projectFile, string xmlTestResult, string labelledTestOutput, string outputFile, string xsltFile)
: base(projectFile, outputFile, xsltFile)
{
this.XmlTestResult = Path.GetFullPath(xmlTestResult);
this.LabelledTestOutput = Path.GetFullPath(labelledTestOutput);
}
}
}
18 changes: 18 additions & 0 deletions Reporting/ReportParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.IO;

namespace TechTalk.SpecFlow.Reporting
{
public abstract class ReportParameters
{
public string XsltFile { get; private set; }
public string OutputFile { get; private set; }
public string ProjectFile { get; private set; }

protected ReportParameters(string projectFile, string outputFile, string xsltFile)
{
this.ProjectFile = projectFile;
this.OutputFile = Path.GetFullPath(outputFile);
this.XsltFile = Path.GetFullPath(xsltFile);
}
}
}
38 changes: 17 additions & 21 deletions Reporting/StepDefinitionReport/StepDefinitionReportGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Xsl;
using TechTalk.SpecFlow.Generator.Configuration;
using TechTalk.SpecFlow.Parser.SyntaxElements;
using TechTalk.SpecFlow.Reporting.StepDefinitionReport.ReportElements;
Expand All @@ -17,42 +15,33 @@ namespace TechTalk.SpecFlow.Reporting.StepDefinitionReport
{
public class StepDefinitionReportGenerator
{
public StepDefinitionReportParameters ReportParameters { get; set; }
private readonly SpecFlowProject specFlowProject;
private readonly List<BindingInfo> bindings;
private readonly List<Feature> parsedFeatures;
private readonly bool showBindingsWithoutInsance;

private ReportElements.StepDefinitionReport report;
private Dictionary<BindingInfo, StepDefinition> stepDefByBinding;
private Dictionary<StepDefinition, BindingInfo> bindingByStepDef;
private readonly List<StepDefinition> stepDefsWithNoBinding = new List<StepDefinition>();

public StepDefinitionReportGenerator(string projectFile, string binFolder, bool showBindingsWithoutInsance)
public StepDefinitionReportGenerator(StepDefinitionReportParameters reportParameters)
{
specFlowProject = MsBuildProjectReader.LoadSpecFlowProjectFromMsBuild(projectFile);
ReportParameters = reportParameters;

specFlowProject = MsBuildProjectReader.LoadSpecFlowProjectFromMsBuild(reportParameters.ProjectFile);
parsedFeatures = ParserHelper.GetParsedFeatures(specFlowProject);

var basePath = Path.Combine(specFlowProject.ProjectFolder, binFolder);
var basePath = Path.Combine(specFlowProject.ProjectFolder, reportParameters.BinFolder);
bindings = BindingCollector.CollectBindings(specFlowProject, basePath);

this.showBindingsWithoutInsance = showBindingsWithoutInsance;
}

public StepDefinitionReportGenerator(SpecFlowProject specFlowProject, List<BindingInfo> bindings, List<Feature> parsedFeatures, bool showBindingsWithoutInsance)
{
this.specFlowProject = specFlowProject;
this.showBindingsWithoutInsance = showBindingsWithoutInsance;
this.bindings = bindings;
this.parsedFeatures = parsedFeatures;
}

public ReportElements.StepDefinitionReport GenerateReport()
{
report = new ReportElements.StepDefinitionReport();
report.ProjectName = specFlowProject.ProjectName;
report.GeneratedAt = DateTime.Now.ToString("g", CultureInfo.InvariantCulture);
report.ShowBindingsWithoutInsance = showBindingsWithoutInsance;
report.ShowBindingsWithoutInsance = ReportParameters.ShowBindingsWithoutInsance;

stepDefByBinding = new Dictionary<BindingInfo, StepDefinition>();
bindingByStepDef = new Dictionary<StepDefinition, BindingInfo>();
Expand Down Expand Up @@ -215,17 +204,17 @@ private string GetSampleText(BindingInfo bindingInfo)
return sampleText;
}

public void TransformReport(string outputFilePath)
public void TransformReport()
{
XmlSerializer serializer = new XmlSerializer(typeof(ReportElements.StepDefinitionReport), ReportElements.StepDefinitionReport.XmlNamespace);

if (XsltHelper.IsXmlOutput(outputFilePath))
if (XsltHelper.IsXmlOutput(ReportParameters.OutputFile))
{
XsltHelper.TransformXml(serializer, report, outputFilePath);
XsltHelper.TransformXml(serializer, report, ReportParameters.OutputFile);
}
else
{
XsltHelper.TransformHtml(serializer, report, GetType(), outputFilePath, specFlowProject.GeneratorConfiguration);
XsltHelper.TransformHtml(serializer, report, GetType(), ReportParameters.OutputFile, specFlowProject.GeneratorConfiguration, ReportParameters.XsltFile);
}
}

Expand Down Expand Up @@ -355,5 +344,12 @@ private ScenarioStep CloneTo(ScenarioStep step, string currentBlock)
newStep.TableArg = Clone(step.TableArg);
return newStep;
}


public void GenerateAndTransformReport()
{
GenerateReport();
TransformReport();
}
}
}
20 changes: 20 additions & 0 deletions Reporting/StepDefinitionReport/StepDefinitionReportParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TechTalk.SpecFlow.Reporting.StepDefinitionReport
{
public class StepDefinitionReportParameters : ReportParameters
{
public string BinFolder { get; private set; }
public bool ShowBindingsWithoutInsance { get; private set; }

public StepDefinitionReportParameters(string projectFile, string outputFile, string xsltFile, string binFolder, bool showBindingsWithoutInsance)
: base(projectFile, outputFile, xsltFile)
{
BinFolder = binFolder;
ShowBindingsWithoutInsance = showBindingsWithoutInsance;
}
}
}
3 changes: 3 additions & 0 deletions Reporting/TechTalk.SpecFlow.Reporting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="NUnitExecutionReport\NUnitExecutionReportGenerator.cs" />
<Compile Include="NUnitExecutionReport\ReportElements\NUnitExecutionReport.cs" />
<Compile Include="NUnitExecutionReport\ReportElements\ScenarioOutput.cs" />
<Compile Include="ReportParameters.cs" />
<Compile Include="ParserHelper.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand All @@ -72,6 +73,8 @@
<Compile Include="StepDefinitionReport\ReportElements\StepDefinitionReport.cs" />
<Compile Include="ResourceXmlReader.cs" />
<Compile Include="StepDefinitionReport\StepDefinitionReportGenerator.cs" />
<Compile Include="NUnitExecutionReport\NUnitExecutionReportParameters.cs" />
<Compile Include="StepDefinitionReport\StepDefinitionReportParameters.cs" />
<Compile Include="XmlResourceResolver.cs" />
<Compile Include="XsltHelper.cs" />
</ItemGroup>
Expand Down
12 changes: 4 additions & 8 deletions Reporting/XmlResourceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@

namespace TechTalk.SpecFlow.Reporting
{
public class XmlResourceResolver : XmlResolver
public class XmlResourceResolver : XmlUrlResolver
{
private ICredentials credentials;

public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
if (absoluteUri == null || !"resource".Equals(absoluteUri.Scheme, StringComparison.InvariantCultureIgnoreCase))
return base.GetEntity(absoluteUri, role, ofObjectToReturn);

string resourceName = absoluteUri.AbsolutePath.TrimStart(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Type.Delimiter);
string assemblyName = absoluteUri.Host;

Assembly assembly = Assembly.Load(assemblyName);
return new ResourceXmlReader(assembly, resourceName);
}

public override ICredentials Credentials
{
set { credentials = value; }
}
}
}
19 changes: 12 additions & 7 deletions Reporting/XsltHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
Expand All @@ -27,19 +26,19 @@ public static void TransformXml(XmlSerializer serializer, object report, string
}
}

public static void TransformHtml(XmlSerializer serializer, object report, Type reportType, string outputFilePath, GeneratorConfiguration generatorConfiguration)
public static void TransformHtml(XmlSerializer serializer, object report, Type reportType, string outputFilePath, GeneratorConfiguration generatorConfiguration, string xsltFile)
{
var xmlOutputWriter = new StringWriter();
serializer.Serialize(xmlOutputWriter, report);

XslCompiledTransform xslt = new XslCompiledTransform();

var xsltSettings = new XsltSettings(true, false);
var resourceResolver = new XmlResourceResolver();
XmlResolver resourceResolver;

var reportName = reportType.Name.Replace("Generator", "");
using (var xsltReader = new ResourceXmlReader(reportType, reportName + ".xslt"))
using (var xsltReader = GetTemplateReader(reportType, reportName, xsltFile))
{
resourceResolver = new XmlResourceResolver();
xslt.Load(xsltReader, xsltSettings, resourceResolver);
}

Expand All @@ -54,10 +53,16 @@ public static void TransformHtml(XmlSerializer serializer, object report, Type r
}
}

static public void Transform(this XslCompiledTransform xslt, XmlReader input, XsltArgumentList arguments, Stream results, XmlResolver documentResolver)
private static XmlReader GetTemplateReader(Type reportType, string reportName, string xsltFile)
{
//xslt.command.Execute(input, new XmlUrlResolver(), arguments, results);
if (string.IsNullOrEmpty(xsltFile))
return new ResourceXmlReader(reportType, reportName + ".xslt");

return new XmlTextReader(xsltFile);
}

private static void Transform(this XslCompiledTransform xslt, XmlReader input, XsltArgumentList arguments, Stream results, XmlResolver documentResolver)
{
var command = xslt.GetType().GetField("command", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(xslt);

var executeMethod = command.GetType().GetMethod("Execute", new Type[] { typeof(XmlReader), typeof(XmlResolver), typeof(XsltArgumentList), typeof(Stream) });
Expand Down
26 changes: 26 additions & 0 deletions TechTalk.SpecFlow.sln
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Resources", "Resources", "{
Installer\Resources\welcome_dialog.bmp = Installer\Resources\welcome_dialog.bmp
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReportingTests", "Tests\ReportingTests\ReportingTests.csproj", "{1965463E-6972-4618-8E59-D3259AE7A125}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReportingTest.SampleProject", "Tests\ReportingTest.SampleProject\ReportingTest.SampleProject.csproj", "{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -185,6 +189,26 @@ Global
{89167EB9-F458-48DA-9D8F-F639A74F5871}.Release|Mixed Platforms.Build.0 = Release|x86
{89167EB9-F458-48DA-9D8F-F639A74F5871}.Release|x86.ActiveCfg = Release|x86
{89167EB9-F458-48DA-9D8F-F639A74F5871}.Release|x86.Build.0 = Release|x86
{1965463E-6972-4618-8E59-D3259AE7A125}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1965463E-6972-4618-8E59-D3259AE7A125}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1965463E-6972-4618-8E59-D3259AE7A125}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{1965463E-6972-4618-8E59-D3259AE7A125}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{1965463E-6972-4618-8E59-D3259AE7A125}.Debug|x86.ActiveCfg = Debug|Any CPU
{1965463E-6972-4618-8E59-D3259AE7A125}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1965463E-6972-4618-8E59-D3259AE7A125}.Release|Any CPU.Build.0 = Release|Any CPU
{1965463E-6972-4618-8E59-D3259AE7A125}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{1965463E-6972-4618-8E59-D3259AE7A125}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{1965463E-6972-4618-8E59-D3259AE7A125}.Release|x86.ActiveCfg = Release|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Debug|x86.ActiveCfg = Debug|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Release|Any CPU.Build.0 = Release|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -198,5 +222,7 @@ Global
{3FE793A8-E662-4026-B4EC-891324073235} = {A10B5CD6-38EC-4D7E-9D1C-2EBA8017E437}
{D3F6B835-B228-4DCF-B533-B6ED469A33B3} = {A10B5CD6-38EC-4D7E-9D1C-2EBA8017E437}
{3836A6FC-4ECC-413A-AC8F-83A0A773EC9E} = {A10B5CD6-38EC-4D7E-9D1C-2EBA8017E437}
{1965463E-6972-4618-8E59-D3259AE7A125} = {A10B5CD6-38EC-4D7E-9D1C-2EBA8017E437}
{E5C299D5-E7CC-4477-9A0B-4797B74BC88B} = {A10B5CD6-38EC-4D7E-9D1C-2EBA8017E437}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions Tests/ReportingTest.SampleProject/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow"/>
</configSections>

<specFlow>
<unitTestProvider name="NUnit"/>
</specFlow>
</configuration>
Loading

0 comments on commit 4e893ed

Please sign in to comment.