-
Notifications
You must be signed in to change notification settings - Fork 100
/
StaticHtmlReportGenerator.java
161 lines (134 loc) · 5.59 KB
/
StaticHtmlReportGenerator.java
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.tngtech.jgiven.report.html;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.tngtech.jgiven.impl.util.ResourceUtil;
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.ScenarioModel;
import com.tngtech.jgiven.report.model.StatisticsCalculator;
import com.tngtech.jgiven.report.model.Tag;
public class StaticHtmlReportGenerator implements ReportModelFileHandler {
private static final Logger log = LoggerFactory.getLogger( StaticHtmlReportGenerator.class );
static class ModelFile {
ReportModel model;
File file;
}
private final List<ModelFile> models = Lists.newArrayList();
private final Map<Tag, List<ScenarioModel>> tagMap = Maps.newHashMap();
private final StatisticsCalculator statisticsCalculator = new StatisticsCalculator();
private File toDir;
public void generate( File toDir, File sourceDir ) throws IOException {
this.toDir = toDir;
new JsonModelTraverser().traverseModels( sourceDir, this );
writeEnd();
copyFileToTargetDir( "style.css" );
copyFileToTargetDir( "default.css" );
copyFileToTargetDir( "print.css" );
copyFileToTargetDir( "report.js" );
}
protected void copyFileToTargetDir( String fileName ) throws IOException {
InputStream stream = null;
FileOutputStream fileOutputStream = null;
try {
stream = this.getClass().getResourceAsStream( "/com/tngtech/jgiven/report/html/" + fileName );
File file = new File( toDir, fileName );
fileOutputStream = new FileOutputStream( file );
ByteStreams.copy( stream, fileOutputStream );
} finally {
ResourceUtil.close( stream, fileOutputStream );
}
}
@Override
public void handleReportModel( ReportModel model, File file ) {
String targetFileName = Files.getNameWithoutExtension( file.getName() ) + ".html";
File targetFile = new File( toDir, targetFileName );
log.debug( "Writing to file " + targetFile );
try {
ModelFile modelFile = new ModelFile();
modelFile.model = model;
modelFile.file = targetFile;
models.add( modelFile );
for( ScenarioModel scenario : model.scenarios ) {
for( Tag tag : scenario.tags ) {
addToMap( tag, scenario );
}
}
} catch( Exception e ) {
log.error( "Error while trying to write to file " + file + ". " + e );
throw Throwables.propagate( e );
}
}
public void writeEnd() {
HtmlTocWriter tocWriter = new HtmlTocWriter( tagMap, models );
for( ModelFile modelFile : models ) {
HtmlWriter.writeModelToFile( modelFile.model, tocWriter, modelFile.file );
}
writeTagFiles( tocWriter );
writeIndexFile( tocWriter );
}
private void writeIndexFile( HtmlTocWriter tocWriter ) {
File file = new File( toDir, "index.html" );
PrintWriter printWriter = CommonReportHelper.getPrintWriter( file );
try {
HtmlWriter htmlWriter = new HtmlWriter( printWriter );
htmlWriter.writeHtmlHeader( "Scenarios" );
ReportModel reportModel = new ReportModel();
reportModel.className = ".Scenarios";
htmlWriter.writeHeader( reportModel );
tocWriter.writeToc( printWriter );
htmlWriter.visit( reportModel );
for( Tag tag : tocWriter.getSortedTags() ) {
printWriter.println( ScenarioHtmlWriter.tagToHtml( tag ) );
}
htmlWriter.visitEnd( reportModel );
htmlWriter.writeHtmlFooter();
} finally {
ResourceUtil.close( printWriter );
}
}
private void writeTagFiles( HtmlTocWriter tocWriter ) {
if( tagMap.isEmpty() )
return;
for( Tag tag : tagMap.keySet() ) {
writeTagFile( tag, tagMap.get( tag ), tocWriter );
}
}
private void writeTagFile( Tag tag, List<ScenarioModel> value, HtmlTocWriter tocWriter ) {
try {
ReportModel reportModel = new ReportModel();
reportModel.className = tag.getName();
if( tag.getValue() != null ) {
reportModel.className += "." + tag.getValueString();
}
reportModel.scenarios = value;
reportModel.description = tag.getDescription();
String fileName = HtmlTocWriter.tagToFilename( tag );
File targetFile = new File( toDir, fileName );
HtmlWriter.writeToFile( targetFile, reportModel, tocWriter );
} catch( Exception e ) {
log.error( "Error while trying to write HTML file for tag " + tag.getName() );
}
}
private void addToMap( Tag tag, ScenarioModel scenario ) {
List<ScenarioModel> list = tagMap.get( tag );
if( list == null ) {
list = Lists.newArrayList();
tagMap.put( tag, list );
}
list.add( scenario );
}
}