Skip to content

Commit 5b02cb1

Browse files
committed
Use builder pattern for the naming strategy
- Makes the whole naming strategy configurable - Make suite name configurable - Make test class name configurable
1 parent 555db83 commit 5b02cb1

File tree

4 files changed

+79
-24
lines changed

4 files changed

+79
-24
lines changed

java/src/main/java/io/cucumber/junitxmlformatter/MessagesToJunitXmlWriter.java

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.OutputStreamWriter;
1010
import java.nio.charset.StandardCharsets;
1111

12+
import static io.cucumber.query.NamingStrategy.ExampleName.NUMBER_AND_PICKLE_IF_PARAMETERIZED;
1213
import static io.cucumber.query.NamingStrategy.FeatureName.EXCLUDE;
1314
import static io.cucumber.query.NamingStrategy.Strategy.LONG;
1415
import static java.util.Objects.requireNonNull;
@@ -22,32 +23,75 @@
2223
*/
2324
public final class MessagesToJunitXmlWriter implements AutoCloseable {
2425

26+
private static final String DEFAULT_TEST_SUITE_NAME = "Cucumber";
2527
private final OutputStreamWriter out;
2628
private final XmlReportData data;
2729
private boolean streamClosed = false;
2830

2931
public MessagesToJunitXmlWriter(OutputStream out) {
30-
this(NamingStrategy.ExampleName.NUMBER_AND_PICKLE_IF_PARAMETERIZED, out);
32+
this("Cucumber", null, createNamingStrategy(NUMBER_AND_PICKLE_IF_PARAMETERIZED), out);
3133
}
3234

35+
@Deprecated
3336
public MessagesToJunitXmlWriter(NamingStrategy.ExampleName exampleNameStrategy, OutputStream out) {
34-
this(createNamingStrategy(requireNonNull(exampleNameStrategy), LONG, EXCLUDE), out);
37+
this("Cucumber", null, createNamingStrategy(requireNonNull(exampleNameStrategy)), out);
3538
}
3639

37-
public MessagesToJunitXmlWriter(NamingStrategy.ExampleName exampleNameStrategy, NamingStrategy.Strategy strategy, OutputStream out) {
38-
this(createNamingStrategy(requireNonNull(exampleNameStrategy), requireNonNull(strategy), EXCLUDE), out);
40+
public static Builder builder() {
41+
return new Builder();
3942
}
4043

41-
public MessagesToJunitXmlWriter(NamingStrategy.ExampleName exampleNameStrategy, NamingStrategy.Strategy strategy, NamingStrategy.FeatureName featureNameStrategy, OutputStream out) {
42-
this(createNamingStrategy(requireNonNull(exampleNameStrategy), requireNonNull(strategy), requireNonNull(featureNameStrategy)), out);
44+
public static class Builder {
45+
46+
private String testSuiteName = DEFAULT_TEST_SUITE_NAME;
47+
private String testClassName;
48+
private NamingStrategy testNamingStrategy = NamingStrategy.strategy(LONG)
49+
.featureName(EXCLUDE)
50+
.exampleName(NUMBER_AND_PICKLE_IF_PARAMETERIZED)
51+
.build();
52+
53+
private Builder() {
54+
55+
}
56+
57+
/**
58+
* Sets the value for the {@code <testsuite name="..." .../>} attribute. Defaults to {@value DEFAULT_TEST_SUITE_NAME}.
59+
*/
60+
public Builder testSuiteName(String testSuiteName) {
61+
this.testSuiteName = requireNonNull(testSuiteName);
62+
return this;
63+
}
64+
65+
/**
66+
* Sets the value for the {@code <testcase classname="..." .../>} attribute. Defaults to the name of the
67+
* feature file.
68+
*/
69+
public Builder testClassName(String testClassName) {
70+
this.testClassName = testClassName;
71+
return this;
72+
}
73+
74+
/**
75+
* Set the naming strategy used for the {@code <testcase name="...".../> attribute}. Defaults to the
76+
* {@link NamingStrategy.Strategy#LONG} strategy with {@link NamingStrategy.FeatureName#EXCLUDE} and
77+
* {@link NamingStrategy.ExampleName#NUMBER_AND_PICKLE_IF_PARAMETERIZED}.
78+
*/
79+
public Builder namingStrategy(NamingStrategy namingStrategy) {
80+
this.testNamingStrategy = requireNonNull(namingStrategy);
81+
return this;
82+
}
83+
84+
public MessagesToJunitXmlWriter build(OutputStream out) {
85+
return new MessagesToJunitXmlWriter(testSuiteName, testClassName, testNamingStrategy, requireNonNull(out));
86+
}
4387
}
4488

45-
private static NamingStrategy createNamingStrategy(NamingStrategy.ExampleName exampleName, NamingStrategy.Strategy strategy, NamingStrategy.FeatureName featureName) {
46-
return NamingStrategy.strategy(strategy).featureName(featureName).exampleName(exampleName).build();
89+
private static NamingStrategy createNamingStrategy(NamingStrategy.ExampleName exampleName) {
90+
return NamingStrategy.strategy(NamingStrategy.Strategy.LONG).featureName(NamingStrategy.FeatureName.EXCLUDE).exampleName(exampleName).build();
4791
}
4892

49-
private MessagesToJunitXmlWriter(NamingStrategy namingStrategy, OutputStream out) {
50-
this.data = new XmlReportData(namingStrategy);
93+
private MessagesToJunitXmlWriter(String testSuiteName, String testClassName, NamingStrategy testNamingStrategy, OutputStream out) {
94+
this.data = new XmlReportData(testSuiteName, testClassName, testNamingStrategy);
5195
this.out = new OutputStreamWriter(
5296
requireNonNull(out),
5397
StandardCharsets.UTF_8

java/src/main/java/io/cucumber/junitxmlformatter/XmlReportData.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,25 @@
2828
import static io.cucumber.messages.types.TestStepResultStatus.PASSED;
2929
import static io.cucumber.query.Repository.RepositoryFeature.INCLUDE_GHERKIN_DOCUMENTS;
3030
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
31+
import static java.util.Objects.requireNonNull;
3132
import static java.util.concurrent.TimeUnit.SECONDS;
3233
import static java.util.stream.Collectors.toList;
3334

3435
class XmlReportData {
36+
private static final long MILLIS_PER_SECOND = SECONDS.toMillis(1L);
3537

3638
private final Repository repository = Repository.builder()
3739
.feature(INCLUDE_GHERKIN_DOCUMENTS, true)
3840
.build();
3941
private final Query query = new Query(repository);
40-
private final NamingStrategy namingStrategy;
41-
42-
private static final long MILLIS_PER_SECOND = SECONDS.toMillis(1L);
43-
44-
XmlReportData(NamingStrategy namingStrategy) {
45-
this.namingStrategy = namingStrategy;
42+
private final String testSuiteName;
43+
private final String testClassName;
44+
private final NamingStrategy testNamingStrategy;
45+
46+
XmlReportData(String testSuiteName, String testClassName, NamingStrategy testNamingStrategy) {
47+
this.testSuiteName = requireNonNull(testSuiteName);
48+
this.testClassName = testClassName;
49+
this.testNamingStrategy = requireNonNull(testNamingStrategy);
4650
}
4751

4852
void collect(Envelope envelope) {
@@ -74,20 +78,27 @@ private Pickle getPickle(TestCaseStarted testCaseStarted) {
7478
.orElseThrow(() -> new IllegalStateException("No pickle for " + testCaseStarted.getId()));
7579
}
7680

77-
String getPickleName(TestCaseStarted testCaseStarted) {
81+
String getTestName(TestCaseStarted testCaseStarted) {
7882
Pickle pickle = getPickle(testCaseStarted);
7983
return query.findLineageBy(pickle)
80-
.map(lineage -> namingStrategy.reduce(lineage, pickle))
84+
.map(lineage -> testNamingStrategy.reduce(lineage, pickle))
8185
.orElseGet(pickle::getName);
8286
}
8387

84-
String getFeatureName(TestCaseStarted testCaseStarted) {
88+
String getTestClassName(TestCaseStarted testCaseStarted) {
89+
if (testClassName != null) {
90+
return testClassName;
91+
}
8592
return query.findLineageBy(testCaseStarted)
8693
.flatMap(Lineage::feature)
8794
.map(Feature::getName)
8895
.orElseGet(() -> this.getPickle(testCaseStarted).getUri());
8996
}
9097

98+
String getTestSuiteName() {
99+
return testSuiteName;
100+
}
101+
91102
List<Entry<String, String>> getStepsAndResult(TestCaseStarted testCaseStarted) {
92103
return query.findTestStepFinishedAndTestStepBy(testCaseStarted)
93104
.stream()
@@ -138,7 +149,7 @@ TestStepResult getTestCaseStatus(TestCaseStarted testCaseStarted) {
138149
.orElse(SCENARIO_WITH_NO_STEPS);
139150
}
140151

141-
public Optional<String> getTestRunStartedAt() {
152+
Optional<String> getTestRunStartedAt() {
142153
return query.findTestRunStarted()
143154
.map(TestRunStarted::getTimestamp)
144155
.map(Convertor::toInstant)

java/src/main/java/io/cucumber/junitxmlformatter/XmlReportWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private void writeTestsuite(EscapingXmlStreamWriter writer) throws XMLStreamExce
4747
}
4848

4949
private void writeSuiteAttributes(EscapingXmlStreamWriter writer) throws XMLStreamException {
50-
writer.writeAttribute("name", "Cucumber");
50+
writer.writeAttribute("name", data.getTestSuiteName());
5151
writer.writeAttribute("time", String.valueOf(data.getSuiteDurationInSeconds()));
5252

5353
Map<TestStepResultStatus, Long> counts = data.getTestCaseStatusCounts();
@@ -85,8 +85,8 @@ private void writeTestcase(EscapingXmlStreamWriter writer, TestCaseStarted testC
8585
}
8686

8787
private void writeTestCaseAttributes(EscapingXmlStreamWriter writer, TestCaseStarted testCaseStarted) throws XMLStreamException {
88-
writer.writeAttribute("classname", data.getFeatureName(testCaseStarted));
89-
writer.writeAttribute("name", data.getPickleName(testCaseStarted));
88+
writer.writeAttribute("classname", data.getTestClassName(testCaseStarted));
89+
writer.writeAttribute("name", data.getTestName(testCaseStarted));
9090
writer.writeAttribute("time", String.valueOf(data.getDurationInSeconds(testCaseStarted)));
9191
}
9292

java/src/test/java/io/cucumber/junitxmlformatter/MessagesToJunitXmlWriterAcceptanceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void updateExpectedFiles(TestCase testCase) throws IOException {
9494
private static <T extends OutputStream> T writeJunitXmlReport(TestCase testCase, T out) throws IOException {
9595
try (InputStream in = Files.newInputStream(testCase.source)) {
9696
try (NdjsonToMessageIterable envelopes = new NdjsonToMessageIterable(in, deserializer)) {
97-
try (MessagesToJunitXmlWriter writer = new MessagesToJunitXmlWriter(out)) {
97+
try (MessagesToJunitXmlWriter writer = MessagesToJunitXmlWriter.builder().build(out)) {
9898
for (Envelope envelope : envelopes) {
9999
writer.write(envelope);
100100
}

0 commit comments

Comments
 (0)