Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/maven/ch.qos.logback-logback-cl…
Browse files Browse the repository at this point in the history
…assic-1.2.13
  • Loading branch information
paveljandejsek authored Dec 22, 2023
2 parents e87d292 + bafab7e commit b7f3f05
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 9 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI
on: [push, pull_request]

jobs:
build:
name: Build on Java ${{ matrix.java }}
runs-on: ubuntu-latest
strategy:
matrix:
java: [ 8 ]
steps:
- uses: actions/checkout@v4
- name: Set up Java ${{ matrix.java }}
uses: actions/setup-java@v4
with:
distribution: 'zulu'
java-version: ${{ matrix.java }}
cache: 'maven'
- name: Build with Java ${{ matrix.java }}
run: mvn clean verify -B
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ public class BasicStory extends JUnit5Story {
}
```

#### Ordering of test classes
The ordering of classes is not guaranteed by default by the engine.
If you need to influence order you can supply your own comparator class name in the parameter `jbehave.execution.order.comparator`
either in system properties or in the JUnit Platform configuration file named `junit-platform.properties`.
This comparator needs to implement the `Comparator<TestDescriptor>` interface and have a no-args constructor available.

Example configuration:
```java
package com.application.comparator;

public class DisplayNameComparator implements Comparator<TestDescriptor> {
@Override
public int compare(TestDescriptor o1, TestDescriptor o2) {
return o1.getDisplayName().compareTo(o2.getDisplayName());
}
}
```
junit-platform.properties:
```properties
jbehave.execution.order.comparator=com.application.comparator.DisplayNameComparator
```

### JUnit 4
To use JUnit4 runner please add a dependency for `junit` or `junit-vintage-engine` to your project explicitly.
Very simple java class with runner implementation:
Expand Down Expand Up @@ -81,3 +103,4 @@ In the IDE reporting is shown:
| 4.8.0 | 4.8 |
| 4.8.3 | 4.8.3 |
| 5.0.0 | 5.0 |
| 5.0.1 | 5.0 |
25 changes: 21 additions & 4 deletions src/main/java/org/jbehavesupport/engine/JBehaveTestEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.jbehavesupport.engine;

import lombok.SneakyThrows;
import org.jbehavesupport.engine.descriptor.JBehaveTestDescriptor;
import org.jbehavesupport.engine.discovery.JBehaveDiscoverer;
import org.jbehavesupport.engine.executor.JBehaveExecutor;
Expand All @@ -28,13 +29,17 @@
import org.junit.platform.engine.TestEngine;
import org.junit.platform.engine.UniqueId;

import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;

import static org.junit.platform.engine.TestExecutionResult.successful;

public final class JBehaveTestEngine implements TestEngine {

@Override
public static final String COMPARATOR_PROPERTY = "jbehave.execution.order.comparator";

@Override
public String getId() {
return "jbehave";
}
Expand All @@ -56,17 +61,29 @@ public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId

@Override
public void execute(ExecutionRequest request) {
Optional<Comparator<TestDescriptor>> sortingComparator = request.getConfigurationParameters()
.get(COMPARATOR_PROPERTY, JBehaveTestEngine::getComparatorInstance);

EngineExecutionListener engineExecutionListener = request.getEngineExecutionListener();
TestDescriptor engineDescriptor = request.getRootTestDescriptor();
engineExecutionListener.executionStarted(engineDescriptor);
JBehaveExecutor jBehaveExecutor = new JBehaveExecutor(request);
engineDescriptor.getChildren()
Stream<? extends JBehaveTestDescriptor> testDescriptorStream = engineDescriptor.getChildren()
.stream()
.map(JBehaveTestDescriptor.class::cast)
.filter(JBehaveTestDescriptor::isRunnable)
.forEach(jBehaveExecutor::execute);
.filter(JBehaveTestDescriptor::isRunnable);

if (sortingComparator.isPresent()) {
testDescriptorStream = testDescriptorStream.sorted(sortingComparator.get());
}
testDescriptorStream.forEach(jBehaveExecutor::execute);

engineExecutionListener.executionFinished(engineDescriptor, successful());
}

@SneakyThrows(ReflectiveOperationException.class)
private static Comparator<TestDescriptor> getComparatorInstance(String className) {
return (Comparator<TestDescriptor>) Class.forName(className).newInstance();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import org.junit.platform.engine.support.descriptor.EngineDescriptor;
import org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolver;

import java.util.function.Predicate;

import static java.lang.reflect.Modifier.isAbstract;

public class JBehaveDiscoverer {

public EngineDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
Expand All @@ -37,9 +41,21 @@ public EngineDescriptor discover(EngineDiscoveryRequest discoveryRequest, Unique

private EngineDiscoveryRequestResolver<TestDescriptor> getResolver(EngineDiscoveryRequest discoveryRequest, UniqueId engineId) {
return EngineDiscoveryRequestResolver.builder()
.addClassContainerSelectorResolver(JUnit5Stories.class::isAssignableFrom)
.addClassContainerSelectorResolver(getJBehaveClassSelector())
.addSelectorResolver(new JBehaveSelectorResolver(discoveryRequest, engineId))
.build();
}

private static Predicate<Class<?>> getJBehaveClassSelector() {
return isCorrectClass().and(isNotAbstract());
}

private static Predicate<Class<?>> isNotAbstract() {
return clazz -> !isAbstract(clazz.getModifiers());
}

private static Predicate<Class<?>> isCorrectClass() {
return JUnit5Stories.class::isAssignableFrom;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.failures.PendingStepFound;
import org.jbehave.core.failures.UUIDExceptionWrapper;
import org.jbehave.core.model.Lifecycle;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Step;
import org.jbehave.core.model.Story;
Expand Down Expand Up @@ -57,6 +58,8 @@ public class StepLoggingReporter extends AbstractLoggingReporter {
private Deque<TestDescriptor> currentStepDescriptor = new ArrayDeque<>();

private boolean isInBeforeStories = false;
private boolean isInBeforeScenario = false;
private boolean isInAfterScenario = false;
private boolean isInAfterStories = false;
private boolean isInMainScenario = false;

Expand Down Expand Up @@ -170,6 +173,32 @@ public void beforeScenario(Scenario scenario) {
}
}

@Override
public void beforeScenarioSteps(StepCollector.Stage stage, Lifecycle.ExecutionType cycle){
// as in jbehave-core v5.0:
// Always trigger StoryReporter.beforeStep(Step) hook and report all outcomes (previously only failures were reported, successful outcome was silent) for methods annotated with @BeforeStories, @AfterStories, @BeforeStory, @AfterStory, @BeforeScenario, @AfterScenario
// @BeforeScenario steps are executed between cycle SYSTEM and stage BEFORE and next stage, so we won't report steps in this combination
if (cycle == Lifecycle.ExecutionType.SYSTEM && stage == StepCollector.Stage.BEFORE) {
isInBeforeScenario = true;
} else {
isInBeforeScenario = false;
}
super.beforeScenarioSteps(stage, cycle);
}

@Override
public void afterScenarioSteps(StepCollector.Stage stage, Lifecycle.ExecutionType cycle){
// as in jbehave-core v5.0:
// Always trigger StoryReporter.beforeStep(Step) hook and report all outcomes (previously only failures were reported, successful outcome was silent) for methods annotated with @BeforeStories, @AfterStories, @BeforeStory, @AfterStory, @BeforeScenario, @AfterScenario
// @AfterScenario steps are executed between cycle USER and stage AFTER and next stage, so we won't report steps in this combination
if (cycle == Lifecycle.ExecutionType.USER && stage == StepCollector.Stage.AFTER) {
isInAfterScenario = true;
} else if (cycle == Lifecycle.ExecutionType.SYSTEM && stage == StepCollector.Stage.AFTER) {
isInAfterScenario = false;
}
super.beforeScenarioSteps(stage, cycle);
}

private List<TestDescriptor> getAllExamples(Set<? extends TestDescriptor> children) {
List<TestDescriptor> result = new ArrayList<>();
for (TestDescriptor child : children) {
Expand Down Expand Up @@ -201,7 +230,7 @@ private List<TestDescriptor> getAllChildren(Set<? extends TestDescriptor> childr
@Override
public void afterScenario(Timing timing) {
super.afterScenario(timing);
if (shouldReportStep()) {
if (notAGivenStory() && (!isInBeforeStories || !isInAfterStories)) {
engineExecutionListener.executionFinished(currentScenarioDescriptor, TestExecutionResult.successful());
// main scenario starts before given stories are run,
// so we need to handle the case of afterScenario of given story
Expand Down Expand Up @@ -290,9 +319,12 @@ public void ignorable(String step) {
private boolean shouldReportStep() {
// not a given story
// not in before stories or after stories
// not in before scenario or after scenario
// and is in scenario of the main story (e.g. not some custom before story hook on method or something like that)
return notAGivenStory()
&& (!isInBeforeStories || !isInAfterStories)
&& !isInBeforeScenario
&& !isInAfterScenario
&& isInMainScenario;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.failures.UUIDExceptionWrapper;
import org.jbehave.core.model.Lifecycle;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Step;
import org.jbehave.core.model.Story;
Expand Down Expand Up @@ -53,6 +54,8 @@ public class JUnitStepReporter extends AbstractJUnitReporter {
private Deque<Description> currentStepDescription = new ArrayDeque<>();

private boolean isInBeforeStories = false;
private boolean isInBeforeScenario = false;
private boolean isInAfterScenario = false;
private boolean isInAfterStories = false;
private boolean isInMainScenario = false;

Expand Down Expand Up @@ -165,6 +168,32 @@ public void beforeScenario(Scenario scenario) {
}
}

@Override
public void beforeScenarioSteps(StepCollector.Stage stage, Lifecycle.ExecutionType cycle){
// as in jbehave-core v5.0:
// Always trigger StoryReporter.beforeStep(Step) hook and report all outcomes (previously only failures were reported, successful outcome was silent) for methods annotated with @BeforeStories, @AfterStories, @BeforeStory, @AfterStory, @BeforeScenario, @AfterScenario
// @BeforeScenario steps are executed between cycle SYSTEM and stage BEFORE and next stage, so we won't report steps in this combination
if (cycle == Lifecycle.ExecutionType.SYSTEM && stage == StepCollector.Stage.BEFORE) {
isInBeforeScenario = true;
} else {
isInBeforeScenario = false;
}
super.beforeScenarioSteps(stage, cycle);
}

@Override
public void afterScenarioSteps(StepCollector.Stage stage, Lifecycle.ExecutionType cycle){
// as in jbehave-core v5.0:
// Always trigger StoryReporter.beforeStep(Step) hook and report all outcomes (previously only failures were reported, successful outcome was silent) for methods annotated with @BeforeStories, @AfterStories, @BeforeStory, @AfterStory, @BeforeScenario, @AfterScenario
// @AfterScenario steps are executed between cycle USER and stage AFTER and next stage, so we won't report steps in this combination
if (cycle == Lifecycle.ExecutionType.USER && stage == StepCollector.Stage.AFTER) {
isInAfterScenario = true;
} else if (cycle == Lifecycle.ExecutionType.SYSTEM && stage == StepCollector.Stage.AFTER) {
isInAfterScenario = false;
}
super.beforeScenarioSteps(stage, cycle);
}

private List<Description> getAllExamples(ArrayList<Description> children) {
List<Description> result = new ArrayList<>();
for (Description child : children) {
Expand Down Expand Up @@ -196,7 +225,7 @@ private List<Description> getAllChildren(ArrayList<Description> children, List<D
@Override
public void afterScenario(Timing timing) {
super.afterScenario(timing);
if (shouldReportStep()) {
if (notAGivenStory() && (!isInBeforeStories || !isInAfterStories)) {
notifier.fireTestFinished(currentScenarioDescription);
// main scenario starts before given stories are run,
// so we need to handle the case of afterScenario of given story
Expand Down Expand Up @@ -285,9 +314,12 @@ public void ignorable(String step) {
private boolean shouldReportStep() {
// not a given story
// not in before stories or after stories
// not in before scenario or after scenario
// and is in scenario of the main story (e.g. not some custom before story hook on method or something like that)
return notAGivenStory()
&& (!isInBeforeStories || !isInAfterStories)
&& !isInBeforeScenario
&& !isInAfterScenario
&& isInMainScenario;
}

Expand Down
91 changes: 91 additions & 0 deletions src/test/groovy/org/jbehavesupport/engine/OrderingTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jbehavesupport.engine

import org.jbehavesupport.engine.story.AndStepStories
import org.jbehavesupport.engine.story.BasicStory
import org.junit.platform.engine.TestDescriptor
import org.junit.platform.testkit.engine.EngineTestKit
import spock.lang.Specification
import spock.lang.Unroll
import spock.util.environment.RestoreSystemProperties

import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass
import static org.junit.platform.testkit.engine.EventConditions.container
import static org.junit.platform.testkit.engine.EventConditions.engine
import static org.junit.platform.testkit.engine.EventConditions.event
import static org.junit.platform.testkit.engine.EventConditions.finishedSuccessfully
import static org.junit.platform.testkit.engine.EventConditions.started
import static org.junit.platform.testkit.engine.EventConditions.test

class OrderingTest extends Specification {

@Unroll
@RestoreSystemProperties
def "Test ordering with #comparatorClassName"() {
given:
System.setProperty("jbehave.report.level", "STORY")
System.setProperty(JBehaveTestEngine.COMPARATOR_PROPERTY, comparatorClassName)
EngineTestKit.Builder builder = EngineTestKit.engine("jbehave")
.enableImplicitConfigurationParameters(true)
.selectors(selectClass(BasicStory), selectClass(AndStepStories))

when:
def executionResults = builder.execute()

then:
executionResults.allEvents()
.assertEventsMatchExactly(
event(engine(), started()),
event(container(firstClass), started()),
event(test(firstStory), started()),
event(test(firstStory), finishedSuccessfully()),
event(container(firstClass), finishedSuccessfully()),
event(container(secondClass), started()),
event(test(secondStory), started()),
event(test(secondStory), finishedSuccessfully()),
event(container(secondClass), finishedSuccessfully()),
event(engine(), finishedSuccessfully())
)

where:
comparatorClassName || firstStory || firstClass || secondStory || secondClass
ReverseComparator.class.getName() || "basic_story" || BasicStory || "AndStep" || AndStepStories
DisplayNameComparator.class.getName() || "AndStep" || AndStepStories || "basic_story" || BasicStory

}

}

class ReverseComparator implements Comparator<TestDescriptor> {

private DisplayNameComparator displayNameComparator = new DisplayNameComparator()

@Override
int compare(TestDescriptor o1, TestDescriptor o2) {
displayNameComparator.reversed().compare(o1, o2)
}
}

class DisplayNameComparator implements Comparator<TestDescriptor> {
@Override
int compare(TestDescriptor o1, TestDescriptor o2) {
o1.displayName <=> o2.displayName
}
}
Loading

0 comments on commit b7f3f05

Please sign in to comment.