-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an annotation that will retry a test until it succeeds. Affects: #1
- Loading branch information
Showing
6 changed files
with
283 additions
and
5 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
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
43 changes: 43 additions & 0 deletions
43
...io7m.percentpass.extension/src/main/java/com/io7m/percentpass/extension/UntilPassing.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,43 @@ | ||
/* | ||
* Copyright © 2020 Mark Raynsford <[email protected]> https://www.io7m.com | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR | ||
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
package com.io7m.percentpass.extension; | ||
|
||
import org.junit.jupiter.api.TestTemplate; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* An annotation that produces a templated test where the test will be run | ||
* until it passes, up to a configurable maximum number of attempts. | ||
*/ | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD}) | ||
@ExtendWith(PercentPassExtension.class) | ||
@TestTemplate | ||
public @interface UntilPassing | ||
{ | ||
/** | ||
* @return The maximum number of executions of each test | ||
*/ | ||
|
||
int maximumExecutionCount() default 100; | ||
} |
106 changes: 106 additions & 0 deletions
106
...ass.extension/src/main/java/com/io7m/percentpass/extension/internal/UntilPassContext.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,106 @@ | ||
/* | ||
* Copyright © 2020 Mark Raynsford <[email protected]> https://www.io7m.com | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR | ||
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
package com.io7m.percentpass.extension.internal; | ||
|
||
import com.io7m.percentpass.extension.UntilPassing; | ||
import org.junit.jupiter.api.extension.TestTemplateInvocationContext; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* The percent pass context. | ||
*/ | ||
|
||
public final class UntilPassContext implements TestTemplateInvocationContext | ||
{ | ||
private final UntilPassing configuration; | ||
private final PercentPassDisplayNameFormatter formatter; | ||
private int remaining; | ||
private boolean succeeded; | ||
|
||
/** | ||
* The percent pass context. | ||
* | ||
* @param inConfiguration The configuration | ||
* @param inFormatter The formatter | ||
*/ | ||
|
||
public UntilPassContext( | ||
final UntilPassing inConfiguration, | ||
final PercentPassDisplayNameFormatter inFormatter) | ||
{ | ||
this.configuration = | ||
Objects.requireNonNull(inConfiguration, "configuration"); | ||
this.formatter = | ||
Objects.requireNonNull(inFormatter, "formatter"); | ||
this.remaining = | ||
this.configuration.maximumExecutionCount(); | ||
} | ||
|
||
/** | ||
* @return The percent passing configuration | ||
*/ | ||
|
||
public UntilPassing configuration() | ||
{ | ||
return this.configuration; | ||
} | ||
|
||
@Override | ||
public String getDisplayName(final int invocationIndex) | ||
{ | ||
return this.formatter.format( | ||
invocationIndex, | ||
this.configuration().maximumExecutionCount()); | ||
} | ||
|
||
/** | ||
* Add a new invocation. | ||
*/ | ||
|
||
public void addFailure() | ||
{ | ||
this.remaining = Math.max(0, this.remaining - 1); | ||
} | ||
|
||
/** | ||
* Set the context as having succeeded. | ||
*/ | ||
|
||
public void setSuccess() | ||
{ | ||
this.succeeded = true; | ||
} | ||
|
||
/** | ||
* @return {@code true} if the context has succeeded | ||
*/ | ||
|
||
public boolean hasSucceeded() | ||
{ | ||
return this.succeeded; | ||
} | ||
|
||
/** | ||
* @return {@code true} if the context has attempts left | ||
*/ | ||
|
||
public boolean hasRemaining() | ||
{ | ||
return this.remaining > 0; | ||
} | ||
} |
Oops, something went wrong.