Skip to content

Commit

Permalink
Fixed some bugs. Moved from FileWriter to File in UnitFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
trnl committed Feb 28, 2012
1 parent a9c73ff commit 6bf58e6
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 55 deletions.
11 changes: 3 additions & 8 deletions core/src/main/java/cucumber/formatter/FormatterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import gherkin.formatter.PrettyFormatter;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -38,14 +37,10 @@ private Formatter createFormatterFromClassName(String className, Object out) {
Class ctorArgClass = Appendable.class;
if (out instanceof File) {
File file = (File) out;
if (file.isDirectory()) {
out = file;
ctorArgClass = File.class;
} else {
out = new FileWriter(file);
ctorArgClass = FileWriter.class;
}
out = file;
ctorArgClass = File.class;
}

Class<Formatter> formatterClass = getFormatterClass(className);
// TODO: Remove these if statements. We should fix PrettyFormatter and ProgressFormatter to only take a single Appendable arg.
// Whether or not to use Monochrome is tricky. Maybe always enforce another 2nd argument for that
Expand Down
21 changes: 4 additions & 17 deletions core/src/main/java/cucumber/formatter/HTMLFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,9 @@
import gherkin.formatter.Mappable;
import gherkin.formatter.NiceAppendable;
import gherkin.formatter.Reporter;
import gherkin.formatter.model.Background;
import gherkin.formatter.model.Examples;
import gherkin.formatter.model.Feature;
import gherkin.formatter.model.Match;
import gherkin.formatter.model.Result;
import gherkin.formatter.model.Scenario;
import gherkin.formatter.model.ScenarioOutline;
import gherkin.formatter.model.Step;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import gherkin.formatter.model.*;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -181,6 +167,7 @@ private NiceAppendable jsOut() {
}

private OutputStream reportFileOutputStream(String fileName) {
htmlReportDir.mkdirs();
File file = new File(htmlReportDir, fileName);
try {
return new FileOutputStream(file);
Expand Down
29 changes: 15 additions & 14 deletions core/src/main/java/cucumber/formatter/UnitFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileWriter;
import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -28,13 +28,13 @@
* Time: 4:02 PM
*/
public class UnitFormatter implements Formatter, Reporter {
private FileWriter out;
private File out;
private Document doc;
private Element rootElement;
private TestCase testCase;


public UnitFormatter(FileWriter out) {
public UnitFormatter(File out) {
this.out = out;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Expand Down Expand Up @@ -67,14 +67,6 @@ public void scenario(Scenario scenario) {
increaseAttributeValue(rootElement, "tests");
}

@Override
public void scenarioOutline(ScenarioOutline scenarioOutline) {
}

@Override
public void examples(Examples examples) {
}

@Override
public void step(Step step) {
if (testCase != null) testCase.steps.add(step);
Expand All @@ -85,7 +77,7 @@ public void step(Step step) {
public void done() {
try {
//set up a transformer
rootElement.setAttribute("failed",String.valueOf(rootElement.getElementsByTagName("failure").getLength()));
rootElement.setAttribute("failed", String.valueOf(rootElement.getElementsByTagName("failure").getLength()));
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
Expand Down Expand Up @@ -114,7 +106,15 @@ private void increaseAttributeValue(Element element, String attribute) {
value = Integer.parseInt(element.getAttribute(attribute));
}
element.setAttribute(attribute, String.valueOf(++value));
}

@Override
public void scenarioOutline(ScenarioOutline scenarioOutline) {
}

@Override
public void examples(Examples examples) {
TestCase.examples = examples.getRows().size()-1;
}

@Override
Expand Down Expand Up @@ -156,13 +156,14 @@ private TestCase() {

Scenario scenario;
static Feature feature;
static int examples = 0;
List<Step> steps = new ArrayList<Step>();
List<Result> results = new ArrayList<Result>();

private Element writeTo(Document doc) {
Element tc = doc.createElement("testcase");
tc.setAttribute("classname", "Feature:" + feature.getName());
tc.setAttribute("name", "Scenario:" + scenario.getName());
tc.setAttribute("classname", feature.getName());
tc.setAttribute("name", examples > 0 ? scenario.getName() + "_" + examples-- : scenario.getName());
long time = 0;
for (Result r : results) {
time += r.getDuration() != null ? r.getDuration() : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public void shouldInstantiateHtmlFormatter() {
assertThat(formatterFactory.createFormatter("html", new File(System.getProperty("user.dir"))), is(HTMLFormatter.class));
}

@Test
public void shouldInstantiateUnitFormatter() {
assertThat(formatterFactory.createFormatter("unit", new File(System.getProperty("user.dir")+"report.xml")), is(UnitFormatter.class));
}

@Test
public void shouldInstantiateCustomFormatterFromClassNameWithAppender() {
StringWriter writer = new StringWriter();
Expand Down
14 changes: 7 additions & 7 deletions core/src/test/java/cucumber/formatter/UnitFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ public void featureWithBackgroundTest() throws Exception {
compareXML("cucumber/formatter/UnitFormatterTest_2.report.xml", "report.xml");
}

// @Test
// public void featureWithOutlineTest() throws Exception {
// runFeaturesWithFormatter(asList("cucumber/formatter/UnitFormatterTest_3.feature"));
// compareXML("cucumber/formatter/UnitFormatterTest_3.report.xml", "report.xml");
// }
@Test
public void featureWithOutlineTest() throws Exception {
runFeaturesWithFormatter(asList("cucumber/formatter/UnitFormatterTest_3.feature"));
compareXML("cucumber/formatter/UnitFormatterTest_3.report.xml", "report.xml");
}

private void runFeaturesWithFormatter(final List<String> featurePaths) throws IOException {
File report = new File("report.xml");
report.deleteOnExit();
final UnitFormatter f = new UnitFormatter(new FileWriter(report));
// report.deleteOnExit();
final UnitFormatter f = new UnitFormatter(report);
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader);
final List<String> gluePaths = emptyList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testsuite tests="2" failed="0">
<testcase classname="Feature:Feature_1" name="Scenario:Scenario_1" time="0">
<testcase classname="Feature_1" name="Scenario_1" time="0">
<skipped><![CDATA[Given step_1................................................................undefined
When step_2.................................................................undefined
Then step_3.................................................................undefined
]]></skipped>
</testcase>
<testcase classname="Feature:Feature_1" name="Scenario:Scenario_2" time="0">
<testcase classname="Feature_1" name="Scenario_2" time="0">
<skipped><![CDATA[Given step_1................................................................undefined
When step_2.................................................................undefined
Then step_3.................................................................undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testsuite tests="2" failed="0">
<testcase classname="Feature:Feature_2" name="Scenario:Scenario_1" time="0">
<testcase classname="Feature_2" name="Scenario_1" time="0">
<skipped><![CDATA[Given bg_1.................................................................undefined
When bg_2..................................................................undefined
Then bg_3..................................................................undefined
Expand All @@ -9,7 +9,7 @@ When step_2.................................................................unde
Then step_3.................................................................undefined
]]></skipped>
</testcase>
<testcase classname="Feature:Feature_2" name="Scenario:Scenario_2" time="0">
<testcase classname="Feature_2" name="Scenario_2" time="0">
<skipped><![CDATA[Given bg_1.................................................................undefined
When bg_2..................................................................undefined
Then bg_3..................................................................undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ Feature: Feature_3
| 12 | 5 | 7 |
| 20 | 5 | 15 |

Scenario: Scenario_2
Given a
Then b
When c

Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
<testsuite errors="1" failures="0" hostname="hazelnut.osuosl.org" name="net.cars.engine.BougieTest" tests="2"
time="0.017" timestamp="2007-11-02T23:13:50">
<testcase classname="Feature:Feature_3" name="Scenario:Scenario_1" time="1"/>
<testcase classname="Feature:Feature_3" name="Scenario:Scenario_2" time="1"/>
</testsuite>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testsuite failed="0" tests="4">
<testcase classname="Feature_3" name="Scenario_1" time="0">
<skipped><![CDATA[Given bg_1.................................................................undefined
When bg_2..................................................................undefined
Then bg_3..................................................................undefined
Given step_1................................................................undefined
When step_2.................................................................undefined
Then step_3.................................................................undefined
]]></skipped>
</testcase>
<testcase classname="Feature_3" name="ScenarioOutline_1_2" time="0">
<skipped><![CDATA[Given bg_1.................................................................undefined
When bg_2..................................................................undefined
Then bg_3..................................................................undefined
Given so_1 12................................................................undefined
When so_2 7 cucumbers............................................................undefined
Then 5 so_3.................................................................undefined
]]></skipped>
</testcase>
<testcase classname="Feature_3" name="ScenarioOutline_1_1" time="0">
<skipped><![CDATA[Given bg_1.................................................................undefined
When bg_2..................................................................undefined
Then bg_3..................................................................undefined
Given so_1 20................................................................undefined
When so_2 15 cucumbers...........................................................undefined
Then 5 so_3.................................................................undefined
]]></skipped>
</testcase>
<testcase classname="Feature_3" name="Scenario_2" time="0">
<skipped><![CDATA[Given bg_1.................................................................undefined
When bg_2..................................................................undefined
Then bg_3..................................................................undefined
Given a...................................................................undefined
Then b...................................................................undefined
When c...................................................................undefined
]]></skipped>
</testcase>
</testsuite>

0 comments on commit 6bf58e6

Please sign in to comment.