|
1 | 1 | package io.cucumber.junit.platform.engine; |
2 | 2 |
|
| 3 | +import io.cucumber.core.gherkin.Pickle; |
| 4 | +import io.cucumber.core.resource.ClasspathSupport; |
| 5 | +import org.junit.platform.engine.ConfigurationParameters; |
3 | 6 | import org.junit.platform.engine.TestSource; |
| 7 | +import org.junit.platform.engine.TestTag; |
4 | 8 | import org.junit.platform.engine.UniqueId; |
| 9 | +import org.junit.platform.engine.support.config.PrefixedConfigurationParameters; |
5 | 10 | import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor; |
| 11 | +import org.junit.platform.engine.support.descriptor.ClasspathResourceSource; |
| 12 | +import org.junit.platform.engine.support.hierarchical.ExclusiveResource; |
| 13 | +import org.junit.platform.engine.support.hierarchical.ExclusiveResource.LockMode; |
| 14 | +import org.junit.platform.engine.support.hierarchical.Node; |
6 | 15 |
|
7 | | -class NodeDescriptor extends AbstractTestDescriptor { |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.LinkedHashSet; |
| 19 | +import java.util.Locale; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.stream.Stream; |
8 | 23 |
|
9 | | - NodeDescriptor(UniqueId uniqueId, String name, TestSource source) { |
| 24 | +import static io.cucumber.junit.platform.engine.Constants.EXECUTION_EXCLUSIVE_RESOURCES_PREFIX; |
| 25 | +import static io.cucumber.junit.platform.engine.Constants.EXECUTION_MODE_FEATURE_PROPERTY_NAME; |
| 26 | +import static io.cucumber.junit.platform.engine.Constants.READ_SUFFIX; |
| 27 | +import static io.cucumber.junit.platform.engine.Constants.READ_WRITE_SUFFIX; |
| 28 | +import static java.util.stream.Collectors.collectingAndThen; |
| 29 | +import static java.util.stream.Collectors.toCollection; |
| 30 | + |
| 31 | +abstract class NodeDescriptor extends AbstractTestDescriptor implements Node<CucumberEngineExecutionContext> { |
| 32 | + |
| 33 | + private final ExecutionMode executionMode; |
| 34 | + |
| 35 | + NodeDescriptor(ConfigurationParameters parameters, UniqueId uniqueId, String name, TestSource source) { |
10 | 36 | super(uniqueId, name, source); |
| 37 | + this.executionMode = parameters |
| 38 | + .get(EXECUTION_MODE_FEATURE_PROPERTY_NAME, |
| 39 | + value -> ExecutionMode.valueOf(value.toUpperCase(Locale.US))) |
| 40 | + .orElse(ExecutionMode.CONCURRENT); |
11 | 41 | } |
12 | 42 |
|
13 | 43 | @Override |
14 | | - public Type getType() { |
15 | | - return Type.CONTAINER; |
| 44 | + public ExecutionMode getExecutionMode() { |
| 45 | + return executionMode; |
| 46 | + } |
| 47 | + |
| 48 | + static final class ExamplesDescriptor extends NodeDescriptor { |
| 49 | + |
| 50 | + ExamplesDescriptor(ConfigurationParameters parameters, UniqueId uniqueId, String name, TestSource source) { |
| 51 | + super(parameters, uniqueId, name, source); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Type getType() { |
| 56 | + return Type.CONTAINER; |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + static final class RuleDescriptor extends NodeDescriptor { |
| 62 | + |
| 63 | + RuleDescriptor(ConfigurationParameters parameters, UniqueId uniqueId, String name, TestSource source) { |
| 64 | + super(parameters, uniqueId, name, source); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Type getType() { |
| 69 | + return Type.CONTAINER; |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + static final class ScenarioOutlineDescriptor extends NodeDescriptor { |
| 75 | + |
| 76 | + ScenarioOutlineDescriptor( |
| 77 | + ConfigurationParameters parameters, UniqueId uniqueId, String name, |
| 78 | + TestSource source |
| 79 | + ) { |
| 80 | + super(parameters, uniqueId, name, source); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Type getType() { |
| 85 | + return Type.CONTAINER; |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + static final class PickleDescriptor extends NodeDescriptor { |
| 91 | + |
| 92 | + private final Pickle pickleEvent; |
| 93 | + private final Set<TestTag> tags; |
| 94 | + private final Set<ExclusiveResource> exclusiveResources = new LinkedHashSet<>(0); |
| 95 | + |
| 96 | + PickleDescriptor( |
| 97 | + ConfigurationParameters parameters, UniqueId uniqueId, String name, TestSource source, |
| 98 | + Pickle pickleEvent |
| 99 | + ) { |
| 100 | + super(parameters, uniqueId, name, source); |
| 101 | + this.pickleEvent = pickleEvent; |
| 102 | + this.tags = getTags(pickleEvent); |
| 103 | + this.tags.forEach(tag -> { |
| 104 | + ExclusiveResourceOptions exclusiveResourceOptions = new ExclusiveResourceOptions(parameters, tag); |
| 105 | + exclusiveResourceOptions.exclusiveReadWriteResource() |
| 106 | + .map(resource -> new ExclusiveResource(resource, LockMode.READ_WRITE)) |
| 107 | + .forEach(exclusiveResources::add); |
| 108 | + exclusiveResourceOptions.exclusiveReadResource() |
| 109 | + .map(resource -> new ExclusiveResource(resource, LockMode.READ)) |
| 110 | + .forEach(exclusiveResources::add); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + private Set<TestTag> getTags(Pickle pickleEvent) { |
| 115 | + return pickleEvent.getTags().stream() |
| 116 | + .map(tag -> tag.substring(1)) |
| 117 | + .filter(TestTag::isValid) |
| 118 | + .map(TestTag::create) |
| 119 | + // Retain input order |
| 120 | + .collect(collectingAndThen(toCollection(LinkedHashSet::new), Collections::unmodifiableSet)); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Type getType() { |
| 125 | + return Type.TEST; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public SkipResult shouldBeSkipped(CucumberEngineExecutionContext context) { |
| 130 | + return Stream.of(shouldBeSkippedByTagFilter(context), shouldBeSkippedByNameFilter(context)) |
| 131 | + .flatMap(skipResult -> skipResult.map(Stream::of).orElseGet(Stream::empty)) |
| 132 | + .filter(SkipResult::isSkipped) |
| 133 | + .findFirst() |
| 134 | + .orElseGet(SkipResult::doNotSkip); |
| 135 | + } |
| 136 | + |
| 137 | + private Optional<SkipResult> shouldBeSkippedByTagFilter(CucumberEngineExecutionContext context) { |
| 138 | + return context.getOptions().tagFilter().map(expression -> { |
| 139 | + if (expression.evaluate(pickleEvent.getTags())) { |
| 140 | + return SkipResult.doNotSkip(); |
| 141 | + } |
| 142 | + return SkipResult |
| 143 | + .skip( |
| 144 | + "'" + Constants.FILTER_TAGS_PROPERTY_NAME + "=" + expression |
| 145 | + + "' did not match this scenario"); |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + private Optional<SkipResult> shouldBeSkippedByNameFilter(CucumberEngineExecutionContext context) { |
| 150 | + return context.getOptions().nameFilter().map(pattern -> { |
| 151 | + if (pattern.matcher(pickleEvent.getName()).matches()) { |
| 152 | + return SkipResult.doNotSkip(); |
| 153 | + } |
| 154 | + return SkipResult |
| 155 | + .skip("'" + Constants.FILTER_NAME_PROPERTY_NAME + "=" + pattern |
| 156 | + + "' did not match this scenario"); |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public CucumberEngineExecutionContext execute( |
| 162 | + CucumberEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor |
| 163 | + ) { |
| 164 | + context.runTestCase(pickleEvent); |
| 165 | + return context; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public Set<ExclusiveResource> getExclusiveResources() { |
| 170 | + return exclusiveResources; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Returns the set of {@linkplain TestTag tags} for a pickle. |
| 175 | + * <p> |
| 176 | + * Note that Cucumber will remove the {code @} symbol from all Gherkin |
| 177 | + * tags. So a scenario tagged with {@code @Smoke} becomes a test tagged |
| 178 | + * with {@code Smoke}. |
| 179 | + * |
| 180 | + * @return the set of tags |
| 181 | + */ |
| 182 | + @Override |
| 183 | + public Set<TestTag> getTags() { |
| 184 | + return tags; |
| 185 | + } |
| 186 | + |
| 187 | + Optional<String> getPackage() { |
| 188 | + return getSource() |
| 189 | + .filter(ClasspathResourceSource.class::isInstance) |
| 190 | + .map(ClasspathResourceSource.class::cast) |
| 191 | + .map(ClasspathResourceSource::getClasspathResourceName) |
| 192 | + .map(ClasspathSupport::packageNameOfResource); |
| 193 | + } |
| 194 | + |
| 195 | + private static final class ExclusiveResourceOptions { |
| 196 | + |
| 197 | + private final ConfigurationParameters parameters; |
| 198 | + |
| 199 | + ExclusiveResourceOptions(ConfigurationParameters parameters, TestTag tag) { |
| 200 | + this.parameters = new PrefixedConfigurationParameters( |
| 201 | + parameters, |
| 202 | + EXECUTION_EXCLUSIVE_RESOURCES_PREFIX + tag.getName()); |
| 203 | + } |
| 204 | + |
| 205 | + public Stream<String> exclusiveReadWriteResource() { |
| 206 | + return parameters.get(READ_WRITE_SUFFIX, s -> Arrays.stream(s.split(",")) |
| 207 | + .map(String::trim)) |
| 208 | + .orElse(Stream.empty()); |
| 209 | + } |
| 210 | + |
| 211 | + public Stream<String> exclusiveReadResource() { |
| 212 | + return parameters.get(READ_SUFFIX, s -> Arrays.stream(s.split(",")) |
| 213 | + .map(String::trim)) |
| 214 | + .orElse(Stream.empty()); |
| 215 | + } |
| 216 | + |
| 217 | + } |
| 218 | + |
16 | 219 | } |
17 | 220 |
|
18 | 221 | } |
0 commit comments