Skip to content

Commit

Permalink
Support configurable NamingStrategy.ExampleName
Browse files Browse the repository at this point in the history
For example:

```java
public final class MyJUnitXmlFormatter implements ConcurrentEventListener {

    private final MessagesToJunitXmlWriter writer;

    public JUnitFormatter(OutputStream out) {
        this.writer = new MessagesToJunitXmlWriter(NamingStrategy.ExampleName.PICKLE, out);
    }

    @OverRide
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(Envelope.class, this::write);
    }

    private void write(Envelope event) {
        try {
            writer.write(event);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }

        if (event.getTestRunFinished().isPresent()) {
            try {
                writer.close();
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }
    }

}
```

Note: Cucumber-JVM at present doesn't allow much configuration of plugins.
To take advantage of this new option, the class above must be copied. And
can be used in Cucumber-JVM with
`cucumber.plugin=com.example.MyJUnitXmlFormatter:target/report.xml`.

Fixes: #27
  • Loading branch information
mpkorstanje committed Apr 5, 2024
1 parent b389855 commit d77dd29
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Support configurable `NamingStrategy.ExampleName` ([#32](https://github.com/cucumber/cucumber-junit-xml-formatter/pull/32), M.P. Korstanje)

## [0.4.0] - 2024-04-05
### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package io.cucumber.junitxmlformatter;

import io.cucumber.messages.types.Envelope;
import io.cucumber.query.NamingStrategy;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

import static io.cucumber.query.NamingStrategy.FeatureName.EXCLUDE;
import static io.cucumber.query.NamingStrategy.Strategy.LONG;
import static java.util.Objects.requireNonNull;

/**
Expand All @@ -20,10 +23,23 @@
public class MessagesToJunitXmlWriter implements AutoCloseable {

private final OutputStreamWriter out;
private final XmlReportData data = new XmlReportData();
private final XmlReportData data;
private boolean streamClosed = false;

public MessagesToJunitXmlWriter(OutputStream out) {
this(NamingStrategy.ExampleName.NUMBER, out);
}

public MessagesToJunitXmlWriter(NamingStrategy.ExampleName exampleNameStrategy, OutputStream out) {
this(createNamingStrategy(requireNonNull(exampleNameStrategy)), out);
}

private static NamingStrategy createNamingStrategy(NamingStrategy.ExampleName exampleName) {
return NamingStrategy.strategy(LONG).featureName(EXCLUDE).exampleName(exampleName).build();
}

private MessagesToJunitXmlWriter(NamingStrategy namingStrategy, OutputStream out) {
this.data = new XmlReportData(namingStrategy);
this.out = new OutputStreamWriter(
requireNonNull(out),
StandardCharsets.UTF_8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.Optional;

import static io.cucumber.messages.types.TestStepResultStatus.PASSED;
import static io.cucumber.query.NamingStrategy.FeatureName.EXCLUDE;
import static io.cucumber.query.NamingStrategy.Strategy.LONG;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.counting;
Expand All @@ -33,13 +31,14 @@
class XmlReportData {

private final Query query = new Query();
private final NamingStrategy namingStrategy = NamingStrategy
.strategy(LONG)
.featureName(EXCLUDE)
.build();
private final NamingStrategy namingStrategy;

private static final long MILLIS_PER_SECOND = SECONDS.toMillis(1L);

public XmlReportData(NamingStrategy namingStrategy) {
this.namingStrategy = namingStrategy;
}

void collect(Envelope envelope) {
query.update(envelope);
}
Expand Down

0 comments on commit d77dd29

Please sign in to comment.