Skip to content

Commit

Permalink
Fix embedding in HTML reports. Closes #412.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Nov 28, 2012
1 parent d0de066 commit 60bf05b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [Git master](https://github.com/cucumber/cucumber-jvm/compare/v1.1.1...master)

* [Core] Embedded data fails to display in HTML reports due to invalid string passed from HTMLFormatter ([#412](https://github.com/cucumber/cucumber-jvm/issues/412) Aslak Hellesøy)
* [Scala] Downgrade to scala 2.9.2 - we'll only use stable versions from now on. (Aslak Hellesøy)
* [Scala] Passing Scenario reference in Before and After hooks ([#431](https://github.com/cucumber/cucumber-jvm/pull/431) Anshul Bajpai)
* [Core] RunCukesTest prevents the execution of other tests ([#304](https://github.com/cucumber/cucumber-jvm/issues/304), [#430](https://github.com/cucumber/cucumber-jvm/pull/430) Mishail)
Expand Down
53 changes: 29 additions & 24 deletions core/src/main/java/cucumber/runtime/formatter/HTMLFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,37 @@ public void uri(String uri) {
.append(JS_FORMATTER_VAR).append(" = new CucumberHTML.DOMFormatter($('.cucumber-report'));");
firstFeature = false;
}
writeToJsReport("uri", "'" + uri + "'");
jsFunctionCall("uri", uri);
}

@Override
public void feature(Feature feature) {
writeToJsReport("feature", feature);
jsFunctionCall("feature", feature);
}

@Override
public void background(Background background) {
writeToJsReport("background", background);
jsFunctionCall("background", background);
}

@Override
public void scenario(Scenario scenario) {
writeToJsReport("scenario", scenario);
jsFunctionCall("scenario", scenario);
}

@Override
public void scenarioOutline(ScenarioOutline scenarioOutline) {
writeToJsReport("scenarioOutline", scenarioOutline);
jsFunctionCall("scenarioOutline", scenarioOutline);
}

@Override
public void examples(Examples examples) {
writeToJsReport("examples", examples);
jsFunctionCall("examples", examples);
}

@Override
public void step(Step step) {
writeToJsReport("step", step);
jsFunctionCall("step", step);
}

@Override
Expand All @@ -114,34 +114,24 @@ public void close() {
jsOut().close();
}

private void writeToJsReport(String functionName, String arg) {
String stringArg = gson.toJson(arg);
jsOut().append(JS_FORMATTER_VAR + ".").append(functionName).append("(").append(stringArg).append(");").println();
}

private void writeToJsReport(String functionName, Mappable arg) {
String stringArg = gson.toJson(arg.toMap());
jsOut().append(JS_FORMATTER_VAR + ".").append(functionName).append("(").append(stringArg).append(");").println();
}

@Override
public void result(Result result) {
writeToJsReport("result", result);
jsFunctionCall("result", result);
}

@Override
public void before(Match match, Result result) {
writeToJsReport("before", result);
jsFunctionCall("before", result);
}

@Override
public void after(Match match, Result result) {
writeToJsReport("after", result);
jsFunctionCall("after", result);
}

@Override
public void match(Match match) {
writeToJsReport("match", match);
jsFunctionCall("match", match);
}

@Override
Expand All @@ -151,19 +141,34 @@ public void embedding(String mimeType, byte[] data) {
if (extension != null) {
StringBuilder fileName = new StringBuilder("embedded").append(embeddedIndex++).append(".").append(extension);
writeBytesAndClose(data, reportFileOutputStream(fileName.toString()));
writeToJsReport("embedding", new StringBuilder("'").append(mimeType).append("','").append(fileName).append("'").toString());
jsFunctionCall("embedding", mimeType, fileName);
}
}

@Override
public void write(String text) {
writeToJsReport("write", text);
jsFunctionCall("write", text);
}

private void jsFunctionCall(String functionName, Object... args) {
NiceAppendable out = jsOut().append(JS_FORMATTER_VAR + ".").append(functionName).append("(");
boolean comma = false;
for (Object arg : args) {
if (comma) {
out.append(", ");
}
arg = arg instanceof Mappable ? ((Mappable) arg).toMap() : arg;
String stringArg = gson.toJson(arg);
out.append(stringArg);
comma = true;
}
out.append(");").println();
}

private void copyReportFiles() {
for (String textAsset : TEXT_ASSETS) {
InputStream textAssetStream = getClass().getResourceAsStream(textAsset);
if(textAssetStream == null) {
if (textAssetStream == null) {
throw new CucumberException("Couldn't find " + textAsset + ". Is cucumber-html on your classpath? Make sure you have the right version.");
}
String baseName = new File(textAsset).getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package cucumber.runtime.formatter;

import gherkin.formatter.model.Comment;
import gherkin.formatter.model.Scenario;
import gherkin.formatter.model.Tag;
import gherkin.util.FixJava;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
Expand All @@ -10,8 +14,10 @@
import org.mozilla.javascript.tools.shell.Global;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -48,9 +54,29 @@ public void writes_valid_report_js() throws IOException {
}
}

@Test
public void includes_uri() throws FileNotFoundException {
String reportJs = FixJava.readReader(new FileReader(new File(outputDir, "report.js")));
assertContains("formatter.uri(\"some\\\\windows\\\\path\\\\some.feature\");", reportJs);
}

@Test
public void included_embedding() throws FileNotFoundException {
String reportJs = FixJava.readReader(new FileReader(new File(outputDir, "report.js")));
assertContains("formatter.embedding(\"image/png\", \"embedded0.png\");", reportJs);
}

private void assertContains(String substring, String string) {
if (string.indexOf(substring) == -1) {
fail(String.format("[%s] not contained in [%s]", substring, string));
}
}

private void runFeaturesWithFormatter(File outputDir) throws IOException {
final HTMLFormatter f = new HTMLFormatter(outputDir);
f.uri("some\\windows\\path\\some.feature");
f.scenario(new Scenario(Collections.<Comment>emptyList(), Collections.<Tag>emptyList(), "Scenario", "some cukes", "", 10, "id"));
f.embedding("image/png", "fakedata".getBytes("US-ASCII"));
f.done();
f.close();
}
Expand Down

0 comments on commit 60bf05b

Please sign in to comment.