Skip to content

Commit

Permalink
Add first simple statistics page
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schäfer committed Sep 25, 2014
1 parent 22f98c0 commit 0f278cf
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void writeToc( PrintWriter writer ) {
writer.println( "<div id='toc'>" );
writer.println( "<i class='icon-cancel' onclick='hideToc()'></i>" );
writeSearchInput();
writer.println( "<h3><a href='index.html'>Summary</a></h3>" );
writePackages();
writeTagLinks();
writer.println( "</div> <!-- toc -->" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
public class ReportModelHtmlWriter extends ReportModelVisitor {
protected final PrintWriter writer;
protected final HtmlWriterUtils utils;
private ReportStatistics statistics;

public ReportModelHtmlWriter( PrintWriter writer ) {
this.writer = writer;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void write( ReportModel model, HtmlTocWriter htmlTocWriter ) {

private void writeStatistics( ReportModel model ) {
if( !model.getScenarios().isEmpty() ) {
ReportStatistics statistics = new StatisticsCalculator().getStatistics( model );
statistics = new StatisticsCalculator().getStatistics( model );
writer.print( "<div class='statistics'>" );
writer.print( statistics.numScenarios + " scenarios, "
+ statistics.numCases + " cases, "
Expand All @@ -88,6 +89,10 @@ private void writeStatistics( ReportModel model ) {
}
}

ReportStatistics getStatistics() {
return statistics;
}

public static String toString( final ScenarioModel model ) {
return toString( new Function<PrintWriter, Void>() {
@Override
Expand Down Expand Up @@ -185,11 +190,12 @@ public void visit( ScenarioModel scenarioModel ) {
scenarioModel.accept( scenarioHtmlWriter );
}

public static void writeModelToFile( ReportModel model, HtmlTocWriter tocWriter, File file ) {
static ReportModelHtmlWriter writeModelToFile( ReportModel model, HtmlTocWriter tocWriter, File file ) {
PrintWriter printWriter = CommonReportHelper.getPrintWriter( file );
try {
ReportModelHtmlWriter htmlWriter = new ReportModelHtmlWriter( printWriter );
htmlWriter.write( model, tocWriter );
return htmlWriter;
} finally {
ResourceUtil.close( printWriter );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

Expand All @@ -18,10 +17,10 @@
import com.google.common.io.Files;
import com.tngtech.jgiven.impl.util.ResourceUtil;
import com.tngtech.jgiven.report.html.PackageTocBuilder.PackageToc;
import com.tngtech.jgiven.report.impl.CommonReportHelper;
import com.tngtech.jgiven.report.json.JsonModelTraverser;
import com.tngtech.jgiven.report.json.ReportModelFileHandler;
import com.tngtech.jgiven.report.model.ReportModel;
import com.tngtech.jgiven.report.model.ReportStatistics;
import com.tngtech.jgiven.report.model.ScenarioModel;
import com.tngtech.jgiven.report.model.StatisticsCalculator;
import com.tngtech.jgiven.report.model.Tag;
Expand Down Expand Up @@ -96,38 +95,18 @@ public void handleReportModel( ReportModel model, File file ) {
public void writeEnd() {
PackageToc packageToc = new PackageTocBuilder( models ).getRootPackageToc();
HtmlTocWriter tocWriter = new HtmlTocWriter( tagMap, packageToc );
ReportStatistics totalStatistics = new ReportStatistics();

for( ModelFile modelFile : models ) {
ReportModelHtmlWriter.writeModelToFile( modelFile.model, tocWriter, modelFile.file );
ReportModelHtmlWriter modelWriter = ReportModelHtmlWriter.writeModelToFile( modelFile.model, tocWriter, modelFile.file );
totalStatistics = totalStatistics.add( modelWriter.getStatistics() );
}

writeTagFiles( tocWriter );

writeIndexFile( tocWriter );

}

private void writeIndexFile( HtmlTocWriter tocWriter ) {
File file = new File( toDir, "index.html" );
PrintWriter printWriter = CommonReportHelper.getPrintWriter( file );
try {
ReportModelHtmlWriter htmlWriter = new ReportModelHtmlWriter( printWriter );
htmlWriter.writeHtmlHeader( "Scenarios" );

ReportModel reportModel = new ReportModel();
reportModel.setClassName( ".Scenarios" );

tocWriter.writeToc( printWriter );
htmlWriter.visit( reportModel );

for( Tag tag : tocWriter.getSortedTags() ) {
printWriter.println( ScenarioHtmlWriter.tagToHtml( tag ) );
}
StatisticsPageHtmlWriter statisticsPageHtmlWriter = new StatisticsPageHtmlWriter( tocWriter, totalStatistics );
statisticsPageHtmlWriter.write( toDir );

htmlWriter.visitEnd( reportModel );
htmlWriter.writeHtmlFooter();
} finally {
ResourceUtil.close( printWriter );
}
}

private void writeTagFiles( HtmlTocWriter tocWriter ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.tngtech.jgiven.report.html;

import java.io.File;
import java.io.PrintWriter;

import com.tngtech.jgiven.impl.util.DurationFormatter;
import com.tngtech.jgiven.impl.util.ResourceUtil;
import com.tngtech.jgiven.report.impl.CommonReportHelper;
import com.tngtech.jgiven.report.model.ReportModel;
import com.tngtech.jgiven.report.model.ReportStatistics;

public class StatisticsPageHtmlWriter {

private final HtmlTocWriter tocWriter;
private final ReportStatistics statistics;
private PrintWriter printWriter;
private HtmlWriterUtils utils;

public StatisticsPageHtmlWriter( HtmlTocWriter tocWriter, ReportStatistics statistics ) {
this.tocWriter = tocWriter;
this.statistics = statistics;
}

public void write( File toDir ) {
writeIndexFile( toDir );
}

private void writeIndexFile( File toDir ) {
File file = new File( toDir, "index.html" );
printWriter = CommonReportHelper.getPrintWriter( file );
utils = new HtmlWriterUtils( printWriter );
try {
ReportModelHtmlWriter htmlWriter = new ReportModelHtmlWriter( printWriter );
htmlWriter.writeHtmlHeader( "Scenarios" );

ReportModel reportModel = new ReportModel();
reportModel.setClassName( ".Scenarios" );

tocWriter.writeToc( printWriter );
htmlWriter.visit( reportModel );

writeStatistics();

htmlWriter.visitEnd( reportModel );
htmlWriter.writeHtmlFooter();
} finally {
ResourceUtil.close( printWriter );
}
}

private void writeStatistics() {
printWriter.write( "Number of Classes: " + statistics.numClasses + "<br>" );
printWriter.write( "Number of Scenarios: " + statistics.numScenarios + "<br>" );
printWriter.write( "Number of Cases: " + statistics.numCases + "<br>" );
printWriter.write( "Number of Failed Cases: " + statistics.numFailedCases + "<br>" );
printWriter.write( "Number of Steps: " + statistics.numSteps + "<br>" );
printWriter.write( "Total Time: " + DurationFormatter.format( statistics.durationInNanos ) + "<br>" );
printWriter.write( "Average duration per Case: "
+ DurationFormatter.format( statistics.durationInNanos / statistics.numCases ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@ public class ReportStatistics {
public int numFailedCases;
public int numSteps;
public long durationInNanos;

public ReportStatistics add( ReportStatistics statistics ) {
ReportStatistics copy = new ReportStatistics();
copy.addModifying( this );
copy.addModifying( statistics );
return copy;
}

private void addModifying( ReportStatistics statistics ) {
this.numClasses += statistics.numClasses;
this.numScenarios += statistics.numScenarios;
this.numCases += statistics.numCases;
this.numFailedCases += statistics.numFailedCases;
this.numSteps += statistics.numSteps;
this.durationInNanos += statistics.durationInNanos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ h2, h2 a {
margin-top: 5px;
padding: 10px;
padding-top: 0px;
padding-bottom: 0px;
}

#toc ul li {
Expand All @@ -124,7 +125,7 @@ h2, h2 a {
line-height: 1.25;
}

#toc ul a {
#toc ul li a {
text-decoration: none;
color: #000066;
font-size: 14px;
Expand Down Expand Up @@ -172,11 +173,16 @@ h1 {
padding-left: 10px;
padding-top: 5px;
padding-bottom: 0px;
}

#toc h3, #toc h3 a {
font-size: 14px;
color: #444;
text-shadow: 1px 1px 1px #fff;
}



#toc > .icon-cancel {
position: absolute;
top: 2px;
Expand Down

0 comments on commit 0f278cf

Please sign in to comment.