From 78e074f3de37da122894faf6cca8e1c85689aa29 Mon Sep 17 00:00:00 2001 From: trnl Date: Mon, 27 Feb 2012 09:23:00 +0300 Subject: [PATCH 1/3] Adding Unit formatter --- .gitignore | 6 + core/pom.xml | 6 + .../cucumber/formatter/FormatterFactory.java | 1 + .../cucumber/formatter/UnitFormatter.java | 205 ++++++++++++++++++ .../cucumber/formatter/UnitFormatterTest.java | 67 ++++++ .../formatter/UnitFormatterTest_1.feature | 12 + .../formatter/UnitFormatterTest_1.report.xml | 15 ++ .../formatter/UnitFormatterTest_2.feature | 17 ++ .../formatter/UnitFormatterTest_2.report.xml | 21 ++ .../formatter/UnitFormatterTest_3.feature | 22 ++ .../formatter/UnitFormatterTest_3.report.xml | 5 + .../resources/cucumber/formatter/test.xsd | 91 ++++++++ .../test/resources/cucumber/test/a.feature | 1 + 13 files changed, 469 insertions(+) create mode 100644 core/src/main/java/cucumber/formatter/UnitFormatter.java create mode 100644 core/src/test/java/cucumber/formatter/UnitFormatterTest.java create mode 100644 core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.feature create mode 100644 core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml create mode 100644 core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.feature create mode 100644 core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.report.xml create mode 100644 core/src/test/resources/cucumber/formatter/UnitFormatterTest_3.feature create mode 100644 core/src/test/resources/cucumber/formatter/UnitFormatterTest_3.report.xml create mode 100644 core/src/test/resources/cucumber/formatter/test.xsd diff --git a/.gitignore b/.gitignore index 101881a002..f43342f6d9 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,9 @@ report.js chromedriver.log .scala_dependencies nexus.properties + +# OS generated files +.DS_Store* +ehthumbs.db +Icon? +Thumbs.db diff --git a/core/pom.xml b/core/pom.xml index 2ff31e88f6..cb2b23a86c 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -45,6 +45,12 @@ junit test + + xmlunit + xmlunit + 1.3 + test + org.mockito mockito-all diff --git a/core/src/main/java/cucumber/formatter/FormatterFactory.java b/core/src/main/java/cucumber/formatter/FormatterFactory.java index fdbaf1946a..e6b14a26ba 100644 --- a/core/src/main/java/cucumber/formatter/FormatterFactory.java +++ b/core/src/main/java/cucumber/formatter/FormatterFactory.java @@ -21,6 +21,7 @@ public class FormatterFactory { put("json", JSONFormatter.class.getName()); put("json-pretty", JSONPrettyFormatter.class.getName()); put("pretty", PrettyFormatter.class.getName()); + put("unit",UnitFormatter.class.getName()); }}; public FormatterFactory(ClassLoader classLoader) { diff --git a/core/src/main/java/cucumber/formatter/UnitFormatter.java b/core/src/main/java/cucumber/formatter/UnitFormatter.java new file mode 100644 index 0000000000..f6408afb3e --- /dev/null +++ b/core/src/main/java/cucumber/formatter/UnitFormatter.java @@ -0,0 +1,205 @@ +package cucumber.formatter; + +import cucumber.runtime.CucumberException; +import gherkin.formatter.Formatter; +import gherkin.formatter.Reporter; +import gherkin.formatter.model.*; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Uladzimir Mihura + * Date: 2/24/12 + * Time: 4:02 PM + */ +public class UnitFormatter implements Formatter, Reporter { + private File out; + private Document doc; + private Element rootElement; + private TestCase testCase; + + + public UnitFormatter(File out) { + this.out = out; + try { + doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + rootElement = doc.createElement("testsuite"); + doc.appendChild(rootElement); + } catch (ParserConfigurationException e) { + throw new CucumberException("Error while processing unit report: " + out.getAbsolutePath(), e); + } + } + + + @Override + public void feature(Feature feature) { + TestCase.feature = feature; + } + + @Override + public void background(Background background) { + testCase = new TestCase(); + } + + @Override + public void scenario(Scenario scenario) { + if (testCase != null) { + testCase.scenario = scenario; + } else { + testCase = new TestCase(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); + } + + + @Override + public void done() { + try { + //set up a transformer + TransformerFactory transfac = TransformerFactory.newInstance(); + Transformer trans = transfac.newTransformer(); + trans.setOutputProperty(OutputKeys.INDENT, "yes"); + StreamResult result = new StreamResult(new FileWriter(out)); + DOMSource source = new DOMSource(doc); + trans.transform(source, result); + } catch (IOException e) { + new CucumberException("Error creating file: " + out.getAbsolutePath(), e); + } catch (TransformerException e) { + new CucumberException("Error while transforming.", e); + } + } + + + @Override + public void result(Result result) { + testCase.results.add(result); + + if (testCase.scenario != null && testCase.results.size() == testCase.steps.size()) { + rootElement.appendChild(testCase.writeTo(doc)); + testCase = null; + } + } + + private void increaseAttributeValue(Element element, String attribute) { + int value = 0; + if (element.hasAttribute(attribute)) { + value = Integer.parseInt(element.getAttribute(attribute)); + } + element.setAttribute(attribute, String.valueOf(++value)); + + } + + @Override + public void match(Match match) { + } + + @Override + public void embedding(String mimeType, InputStream data) { + } + + @Override + public void write(String text) { + } + + @Override + public void uri(String uri) { + } + + @Override + public void close() { + + } + + @Override + public void eof() { + } + + @Override + public void syntaxError(String state, String event, List legalEvents, String uri, int line) { + } + + private static class TestCase { + private TestCase(Scenario scenario) { + this.scenario = scenario; + } + + private TestCase() { + } + + Scenario scenario; + static Feature feature; + List steps = new ArrayList(); + List results = new ArrayList(); + + private Element writeTo(Document doc) { + Element tc = doc.createElement("testcase"); + tc.setAttribute("classname", "Feature:" + feature.getName()); + tc.setAttribute("name", "Scenario:" + scenario.getName()); + long time = 0; + for (Result r : results) { + time += r.getDuration() != null ? r.getDuration() : 0; + } + tc.setAttribute("time", String.valueOf(time)); + + StringBuilder sb = new StringBuilder(); + Result skipped = null, failed = null; + for (int i = 0; i < steps.size(); i++) { + int length = sb.length(); + Step step = steps.get(i); + Result result = results.get(i); + if ("failed".equals(result.getStatus())) failed = result; + if ("undefined".equals(result.getStatus()) || "pending".equals(result.getStatus())) skipped = result; + sb.append(steps.get(i).getKeyword()); + sb.append(steps.get(i).getName()); + for (int j = 0; sb.length() - length + j < 60; j++) sb.append("."); + sb.append(result.getStatus()); + sb.append("\n"); + } + Element child; + if (failed != null) { + sb.append("StackTrace:\n"); + StringWriter sw = new StringWriter(); + failed.getError().printStackTrace(new PrintWriter(sw)); + sb.append(sw.toString()); + child = doc.createElement("failure"); + child.setAttribute("message", failed.getErrorMessage()); + child.appendChild(doc.createCDATASection(sb.toString())); + } else if (skipped != null) { + child = doc.createElement("skipped"); + child.appendChild(doc.createCDATASection(sb.toString())); + } else { + child = doc.createElement("system-out"); + child.appendChild(doc.createCDATASection(sb.toString())); + } + tc.appendChild(child); + return tc; + } + } + +} diff --git a/core/src/test/java/cucumber/formatter/UnitFormatterTest.java b/core/src/test/java/cucumber/formatter/UnitFormatterTest.java new file mode 100644 index 0000000000..136772fd9c --- /dev/null +++ b/core/src/test/java/cucumber/formatter/UnitFormatterTest.java @@ -0,0 +1,67 @@ +package cucumber.formatter; + +import cucumber.io.ClasspathResourceLoader; +import cucumber.runtime.Backend; +import cucumber.runtime.Runtime; +import org.custommonkey.xmlunit.Diff; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Test; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.List; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static junit.framework.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +/** + * @author Uladzimir Mihura + * Date: 2/25/12 + * Time: 8:39 PM + */ +public class UnitFormatterTest { + + @Test + public void featureSimpleTest() throws Exception { + runFeaturesWithFormatter(asList("cucumber/formatter/UnitFormatterTest_1.feature")); + compareXML("cucumber/formatter/UnitFormatterTest_1.report.xml", "report.xml"); + } + + @Test + public void featureWithBackgroundTest() throws Exception { + runFeaturesWithFormatter(asList("cucumber/formatter/UnitFormatterTest_2.feature")); + 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"); +// } + + private void runFeaturesWithFormatter(final List featurePaths) throws IOException { + File report = new File("report.xml"); +// report.deleteOnExit(); + final UnitFormatter f = new UnitFormatter(report); + final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader); + final List gluePaths = emptyList(); + final cucumber.runtime.Runtime runtime = new Runtime(resourceLoader, gluePaths, classLoader, asList(mock(Backend.class)), false); + runtime.run(featurePaths, emptyList(), f, f); + f.done(); + f.close(); + } + + private void compareXML(String expected, String received) throws IOException, ParserConfigurationException, SAXException { + XMLUnit.setIgnoreWhitespace(true); + Diff diff = new Diff(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(expected)), new FileReader(received)); + assertTrue("XML files are similar " + diff, diff.identical()); + } + +} diff --git a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.feature b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.feature new file mode 100644 index 0000000000..b219c163e8 --- /dev/null +++ b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.feature @@ -0,0 +1,12 @@ +Feature: Feature_1 + Some description + + Scenario: Scenario_1 + Given step_1 + When step_2 + Then step_3 + + Scenario: Scenario_2 + Given step_1 + When step_2 + Then step_3 diff --git a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml new file mode 100644 index 0000000000..2236842a32 --- /dev/null +++ b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml @@ -0,0 +1,15 @@ + + + + + + + + + diff --git a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.feature b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.feature new file mode 100644 index 0000000000..bc13535a50 --- /dev/null +++ b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.feature @@ -0,0 +1,17 @@ +Feature: Feature_2 + + Background: + Given bg_1 + When bg_2 + Then bg_3 + + Scenario: Scenario_1 + Given step_1 + When step_2 + Then step_3 + + Scenario: Scenario_2 + Given step_1 + When step_2 + Then step_3 + diff --git a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.report.xml b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.report.xml new file mode 100644 index 0000000000..4d282975b4 --- /dev/null +++ b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.report.xml @@ -0,0 +1,21 @@ + + + + + + + + + diff --git a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_3.feature b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_3.feature new file mode 100644 index 0000000000..8d4ebf8ff6 --- /dev/null +++ b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_3.feature @@ -0,0 +1,22 @@ +Feature: Feature_3 + + Background: + Given bg_1 + When bg_2 + Then bg_3 + + Scenario: Scenario_1 + Given step_1 + When step_2 + Then step_3 + + Scenario Outline: ScenarioOutline_1 + Given so_1 + When so_2 cucumbers + Then so_3 + + Examples: + | a | b | c | + | 12 | 5 | 7 | + | 20 | 5 | 15 | + diff --git a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_3.report.xml b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_3.report.xml new file mode 100644 index 0000000000..c72f29df5c --- /dev/null +++ b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_3.report.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/core/src/test/resources/cucumber/formatter/test.xsd b/core/src/test/resources/cucumber/formatter/test.xsd new file mode 100644 index 0000000000..a1d5ab04d2 --- /dev/null +++ b/core/src/test/resources/cucumber/formatter/test.xsd @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core/src/test/resources/cucumber/test/a.feature b/core/src/test/resources/cucumber/test/a.feature index 639d83a62f..e8467d5f5c 100644 --- a/core/src/test/resources/cucumber/test/a.feature +++ b/core/src/test/resources/cucumber/test/a.feature @@ -1,3 +1,4 @@ Feature: fa Scenario: sa Given ga + From a9c73fff39b4b1f02ec4dd27e2e70914a3371535 Mon Sep 17 00:00:00 2001 From: trnl Date: Mon, 27 Feb 2012 10:13:56 +0300 Subject: [PATCH 2/3] UnitFormatter bug fixing #1 --- .../cucumber/formatter/FormatterFactory.java | 3 ++- .../cucumber/formatter/UnitFormatter.java | 20 +++++++------- .../cucumber/formatter/UnitFormatterTest.java | 9 +++---- .../formatter/UnitFormatterTest_1.report.xml | 14 +++++----- .../formatter/UnitFormatterTest_2.report.xml | 26 +++++++++---------- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/core/src/main/java/cucumber/formatter/FormatterFactory.java b/core/src/main/java/cucumber/formatter/FormatterFactory.java index e6b14a26ba..8c3accd071 100644 --- a/core/src/main/java/cucumber/formatter/FormatterFactory.java +++ b/core/src/main/java/cucumber/formatter/FormatterFactory.java @@ -21,7 +21,7 @@ public class FormatterFactory { put("json", JSONFormatter.class.getName()); put("json-pretty", JSONPrettyFormatter.class.getName()); put("pretty", PrettyFormatter.class.getName()); - put("unit",UnitFormatter.class.getName()); + put("unit", UnitFormatter.class.getName()); }}; public FormatterFactory(ClassLoader classLoader) { @@ -43,6 +43,7 @@ private Formatter createFormatterFromClassName(String className, Object out) { ctorArgClass = File.class; } else { out = new FileWriter(file); + ctorArgClass = FileWriter.class; } } Class formatterClass = getFormatterClass(className); diff --git a/core/src/main/java/cucumber/formatter/UnitFormatter.java b/core/src/main/java/cucumber/formatter/UnitFormatter.java index f6408afb3e..9b299aad28 100644 --- a/core/src/main/java/cucumber/formatter/UnitFormatter.java +++ b/core/src/main/java/cucumber/formatter/UnitFormatter.java @@ -15,7 +15,10 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; -import java.io.*; +import java.io.FileWriter; +import java.io.InputStream; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.ArrayList; import java.util.List; @@ -25,20 +28,20 @@ * Time: 4:02 PM */ public class UnitFormatter implements Formatter, Reporter { - private File out; + private FileWriter out; private Document doc; private Element rootElement; private TestCase testCase; - public UnitFormatter(File out) { + public UnitFormatter(FileWriter out) { this.out = out; try { doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); rootElement = doc.createElement("testsuite"); doc.appendChild(rootElement); } catch (ParserConfigurationException e) { - throw new CucumberException("Error while processing unit report: " + out.getAbsolutePath(), e); + throw new CucumberException("Error while processing unit report", e); } } @@ -82,14 +85,13 @@ public void step(Step step) { public void done() { try { //set up a transformer + rootElement.setAttribute("failed",String.valueOf(rootElement.getElementsByTagName("failure").getLength())); TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.INDENT, "yes"); - StreamResult result = new StreamResult(new FileWriter(out)); + StreamResult result = new StreamResult(out); DOMSource source = new DOMSource(doc); trans.transform(source, result); - } catch (IOException e) { - new CucumberException("Error creating file: " + out.getAbsolutePath(), e); } catch (TransformerException e) { new CucumberException("Error while transforming.", e); } @@ -177,13 +179,13 @@ private Element writeTo(Document doc) { if ("undefined".equals(result.getStatus()) || "pending".equals(result.getStatus())) skipped = result; sb.append(steps.get(i).getKeyword()); sb.append(steps.get(i).getName()); - for (int j = 0; sb.length() - length + j < 60; j++) sb.append("."); + for (int j = 0; sb.length() - length + j < 140; j++) sb.append("."); sb.append(result.getStatus()); sb.append("\n"); } Element child; if (failed != null) { - sb.append("StackTrace:\n"); + sb.append("\nStackTrace:\n"); StringWriter sw = new StringWriter(); failed.getError().printStackTrace(new PrintWriter(sw)); sb.append(sw.toString()); diff --git a/core/src/test/java/cucumber/formatter/UnitFormatterTest.java b/core/src/test/java/cucumber/formatter/UnitFormatterTest.java index 136772fd9c..153c1f06aa 100644 --- a/core/src/test/java/cucumber/formatter/UnitFormatterTest.java +++ b/core/src/test/java/cucumber/formatter/UnitFormatterTest.java @@ -9,10 +9,7 @@ import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.util.List; import static java.util.Arrays.asList; @@ -47,8 +44,8 @@ public void featureWithBackgroundTest() throws Exception { private void runFeaturesWithFormatter(final List featurePaths) throws IOException { File report = new File("report.xml"); -// report.deleteOnExit(); - final UnitFormatter f = new UnitFormatter(report); + report.deleteOnExit(); + final UnitFormatter f = new UnitFormatter(new FileWriter(report)); final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader); final List gluePaths = emptyList(); diff --git a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml index 2236842a32..91a6820abe 100644 --- a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml +++ b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml @@ -1,15 +1,15 @@ - + - - diff --git a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.report.xml b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.report.xml index 4d282975b4..2be77c76e3 100644 --- a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.report.xml +++ b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_2.report.xml @@ -1,21 +1,21 @@ - + - - From 6bf58e6f8954fe826ebb5f82533122e3d5533f3a Mon Sep 17 00:00:00 2001 From: trnl Date: Tue, 28 Feb 2012 15:16:49 +0300 Subject: [PATCH 3/3] Fixed some bugs. Moved from FileWriter to File in UnitFormatter --- .../cucumber/formatter/FormatterFactory.java | 11 ++--- .../cucumber/formatter/HTMLFormatter.java | 21 ++------- .../cucumber/formatter/UnitFormatter.java | 29 ++++++------ .../formatter/FormatterFactoryTest.java | 5 +++ .../cucumber/formatter/UnitFormatterTest.java | 14 +++--- .../formatter/UnitFormatterTest_1.report.xml | 4 +- .../formatter/UnitFormatterTest_2.report.xml | 4 +- .../formatter/UnitFormatterTest_3.feature | 5 +++ .../formatter/UnitFormatterTest_3.report.xml | 44 ++++++++++++++++--- 9 files changed, 82 insertions(+), 55 deletions(-) diff --git a/core/src/main/java/cucumber/formatter/FormatterFactory.java b/core/src/main/java/cucumber/formatter/FormatterFactory.java index 8c3accd071..fe46ee1c77 100644 --- a/core/src/main/java/cucumber/formatter/FormatterFactory.java +++ b/core/src/main/java/cucumber/formatter/FormatterFactory.java @@ -7,7 +7,6 @@ import gherkin.formatter.PrettyFormatter; import java.io.File; -import java.io.FileWriter; import java.util.HashMap; import java.util.Map; @@ -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 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 diff --git a/core/src/main/java/cucumber/formatter/HTMLFormatter.java b/core/src/main/java/cucumber/formatter/HTMLFormatter.java index 44720aff29..586be3166d 100644 --- a/core/src/main/java/cucumber/formatter/HTMLFormatter.java +++ b/core/src/main/java/cucumber/formatter/HTMLFormatter.java @@ -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; @@ -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); diff --git a/core/src/main/java/cucumber/formatter/UnitFormatter.java b/core/src/main/java/cucumber/formatter/UnitFormatter.java index 9b299aad28..ccdb81158e 100644 --- a/core/src/main/java/cucumber/formatter/UnitFormatter.java +++ b/core/src/main/java/cucumber/formatter/UnitFormatter.java @@ -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; @@ -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(); @@ -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); @@ -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"); @@ -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 @@ -156,13 +156,14 @@ private TestCase() { Scenario scenario; static Feature feature; + static int examples = 0; List steps = new ArrayList(); List results = new ArrayList(); 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; diff --git a/core/src/test/java/cucumber/formatter/FormatterFactoryTest.java b/core/src/test/java/cucumber/formatter/FormatterFactoryTest.java index 838ca4bbb2..6fa3c7d5bb 100644 --- a/core/src/test/java/cucumber/formatter/FormatterFactoryTest.java +++ b/core/src/test/java/cucumber/formatter/FormatterFactoryTest.java @@ -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(); diff --git a/core/src/test/java/cucumber/formatter/UnitFormatterTest.java b/core/src/test/java/cucumber/formatter/UnitFormatterTest.java index 153c1f06aa..c1aed40a37 100644 --- a/core/src/test/java/cucumber/formatter/UnitFormatterTest.java +++ b/core/src/test/java/cucumber/formatter/UnitFormatterTest.java @@ -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 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 gluePaths = emptyList(); diff --git a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml index 91a6820abe..3c31ef6e66 100644 --- a/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml +++ b/core/src/test/resources/cucumber/formatter/UnitFormatterTest_1.report.xml @@ -1,12 +1,12 @@ - + - + - + - + - - - \ No newline at end of file + + + + + + + + + + + + + + +