-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce run ordering as a generic feature
Extending the initial implementation for run order randomisation, the former is not merely a special case of run ordering. We now have - DefaultSpecOrderer (basically a no-op) - RandomSpecOrderer, - AlphabeticalSpecOrderer, - AnnotatationBasedSpecOrderer with @order(int). Relates to #1443.
- Loading branch information
Showing
12 changed files
with
600 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...in/java/org/spockframework/runtime/extension/builtin/orderer/AlphabeticalSpecOrderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.spockframework.runtime.extension.builtin.orderer; | ||
|
||
import org.spockframework.runtime.model.SpecInfo; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class AlphabeticalSpecOrderer extends SpecOrderer { | ||
private final boolean descending; | ||
|
||
public AlphabeticalSpecOrderer(boolean orderSpecs, boolean orderFeatures, boolean descending) { | ||
super(orderSpecs, orderFeatures); | ||
this.descending = descending; | ||
} | ||
|
||
public AlphabeticalSpecOrderer(boolean orderSpecs, boolean orderFeatures) { | ||
this(orderSpecs, orderFeatures, false); | ||
} | ||
|
||
@Override | ||
protected void orderSpecs(Collection<SpecInfo> specs) { | ||
AtomicInteger i = new AtomicInteger(); | ||
specs.stream() | ||
.sorted((o1, o2) -> descending | ||
? o2.getDisplayName().compareTo(o1.getDisplayName()) | ||
: o1.getDisplayName().compareTo(o2.getDisplayName()) | ||
) | ||
.forEach(specInfo -> specInfo.setExecutionOrder(i.getAndIncrement())); | ||
} | ||
|
||
@Override | ||
protected void orderFeatures(Collection<SpecInfo> specs) { | ||
for (SpecInfo spec : specs) { | ||
AtomicInteger i = new AtomicInteger(); | ||
spec.getAllFeatures().stream() | ||
.sorted((o1, o2) -> descending | ||
? o2.getDisplayName().compareTo(o1.getDisplayName()) | ||
: o1.getDisplayName().compareTo(o2.getDisplayName()) | ||
) | ||
.forEach(featureInfo -> featureInfo.setExecutionOrder(i.getAndIncrement())); | ||
} | ||
} | ||
|
||
public boolean isDescending() { | ||
return descending; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...va/org/spockframework/runtime/extension/builtin/orderer/AnnotatationBasedSpecOrderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.spockframework.runtime.extension.builtin.orderer; | ||
|
||
import org.spockframework.runtime.model.FeatureInfo; | ||
import org.spockframework.runtime.model.SpecInfo; | ||
import spock.lang.Order; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class AnnotatationBasedSpecOrderer extends SpecOrderer { | ||
public AnnotatationBasedSpecOrderer() { | ||
super(true, true); | ||
} | ||
|
||
@Override | ||
protected void orderSpecs(Collection<SpecInfo> specs) { | ||
for (SpecInfo spec : specs) { | ||
Order orderAnnotation = spec.getAnnotation(Order.class); | ||
spec.setExecutionOrder(orderAnnotation == null ? 0 : orderAnnotation.value()); | ||
} | ||
} | ||
|
||
@Override | ||
protected void orderFeatures(Collection<SpecInfo> specs) { | ||
for (SpecInfo spec : specs) { | ||
for (FeatureInfo feature : spec.getAllFeatures()) { | ||
Order orderAnnotation = feature.getFeatureMethod().getAnnotation(Order.class); | ||
feature.setExecutionOrder(orderAnnotation == null ? 0 : orderAnnotation.value()); | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...rc/main/java/org/spockframework/runtime/extension/builtin/orderer/DefaultSpecOrderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.spockframework.runtime.extension.builtin.orderer; | ||
|
||
import org.spockframework.runtime.model.SpecInfo; | ||
|
||
import java.util.Collection; | ||
|
||
public class DefaultSpecOrderer extends SpecOrderer { | ||
public DefaultSpecOrderer() { | ||
super(false, false); | ||
} | ||
|
||
@Override | ||
protected void orderSpecs(Collection<SpecInfo> specs) { } | ||
|
||
@Override | ||
protected void orderFeatures(Collection<SpecInfo> specs) { } | ||
} |
39 changes: 39 additions & 0 deletions
39
...src/main/java/org/spockframework/runtime/extension/builtin/orderer/RandomSpecOrderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.spockframework.runtime.extension.builtin.orderer; | ||
|
||
import org.spockframework.runtime.model.FeatureInfo; | ||
import org.spockframework.runtime.model.SpecInfo; | ||
|
||
import java.util.Collection; | ||
import java.util.Random; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class RandomSpecOrderer extends SpecOrderer { | ||
private final Random random; | ||
|
||
public RandomSpecOrderer(boolean orderSpecs, boolean orderFeatures, long seed) { | ||
super(orderSpecs, orderFeatures); | ||
random = new Random(seed); | ||
} | ||
|
||
public RandomSpecOrderer(boolean orderSpecs, boolean orderFeatures) { | ||
this(orderSpecs, orderFeatures, System.currentTimeMillis()); | ||
} | ||
|
||
public RandomSpecOrderer() { | ||
this(true, true); | ||
} | ||
|
||
@Override | ||
protected void orderSpecs(Collection<SpecInfo> specs) { | ||
for (SpecInfo spec : specs) | ||
spec.setExecutionOrder(random.nextInt()); | ||
} | ||
|
||
@Override | ||
protected void orderFeatures(Collection<SpecInfo> specs) { | ||
for (SpecInfo spec : specs) { | ||
for (FeatureInfo feature : spec.getAllFeatures()) | ||
feature.setExecutionOrder(random.nextInt()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* https://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 spock.lang; | ||
|
||
import org.spockframework.runtime.extension.builtin.OrderExtension; | ||
import org.spockframework.runtime.extension.builtin.orderer.AnnotatationBasedSpecOrderer; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Assigns an execution order to a specification or feature. | ||
* <p> | ||
* Annotations of this type are picked up by the {@link OrderExtension}, if and only if the | ||
* {@link AnnotatationBasedSpecOrderer} is activated in the Spock configuration file: <pre> | ||
* runner { | ||
* orderer new AnnotatationBasedSpecOrderer() | ||
* }</pre> | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
public @interface Order { | ||
int value(); | ||
} |
Oops, something went wrong.