-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
188 additions
and
10 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
31 changes: 31 additions & 0 deletions
31
junit5/src/main/java/io/qala/datagen/junit5/RandomLong.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,31 @@ | ||
package io.qala.datagen.junit5; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Can pass a random long number to a JUnit5 test. | ||
* If multiple of these annotations are specified, the test will be run multiple times each time with a different value. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.PARAMETER}) | ||
@Repeatable(RandomLongs.class) | ||
|
||
@ArgumentsSource(RandomLongArgumentProvider.class) | ||
@ParameterizedTest | ||
public @interface RandomLong { | ||
/** Min value of the generated int. */ | ||
long min() default Long.MIN_VALUE; | ||
/** Max value of the generated int. */ | ||
long max() default Long.MAX_VALUE; | ||
/** | ||
* Name of the test case, useful when you have multiple annotations and you want to give title to each of the | ||
* generated string. Can be obtained in the test if there is a second param of type String. Ignored if the 2nd | ||
* param is not present in the test method. | ||
* | ||
* Defaults to "long from [min] to [max]". | ||
*/ | ||
String name() default ""; | ||
} |
40 changes: 40 additions & 0 deletions
40
junit5/src/main/java/io/qala/datagen/junit5/RandomLongArgumentProvider.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,40 @@ | ||
package io.qala.datagen.junit5; | ||
|
||
import org.junit.jupiter.api.extension.ContainerExtensionContext; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.support.AnnotationConsumer; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static io.qala.datagen.RandomShortApi.Long; | ||
|
||
class RandomLongArgumentProvider implements ArgumentsProvider, AnnotationConsumer<RandomLong> { | ||
private RandomLong annotation; | ||
|
||
@Override | ||
public void accept(RandomLong annotation) { | ||
this.annotation = annotation; | ||
} | ||
|
||
@Override | ||
public Stream<? extends Arguments> provideArguments(ContainerExtensionContext extensionContext) throws Exception { | ||
if (Utils.injectCaseName(extensionContext)) | ||
return Stream.of(annotation) | ||
.map(RandomLongArgumentProvider::generateParams) | ||
.map(Arguments::of); | ||
return Stream.of(annotation) | ||
.map(RandomLongArgumentProvider::generateParam) | ||
.map(Arguments::of); | ||
} | ||
|
||
static long generateParam(RandomLong annotation) { | ||
return Long(annotation.min(), annotation.max()); | ||
} | ||
|
||
static Object[] generateParams(RandomLong annotation) { | ||
String name = annotation.name(); | ||
if(name.isEmpty()) name = "long from " + annotation.min() + " to " + annotation.max(); | ||
return new Object[]{Long(annotation.min(), annotation.max()), name}; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
junit5/src/main/java/io/qala/datagen/junit5/RandomLongs.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,18 @@ | ||
package io.qala.datagen.junit5; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(RandomLongsArgumentProvider.class) | ||
public @interface RandomLongs { | ||
RandomLong[] value(); | ||
} |
30 changes: 30 additions & 0 deletions
30
junit5/src/main/java/io/qala/datagen/junit5/RandomLongsArgumentProvider.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,30 @@ | ||
package io.qala.datagen.junit5; | ||
|
||
import org.junit.jupiter.api.extension.ContainerExtensionContext; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.support.AnnotationConsumer; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Stream; | ||
|
||
class RandomLongsArgumentProvider implements ArgumentsProvider, AnnotationConsumer<RandomLongs> { | ||
private RandomLongs annotation; | ||
|
||
@Override | ||
public void accept(RandomLongs annotation) { | ||
this.annotation = annotation; | ||
} | ||
|
||
@Override | ||
public Stream<? extends Arguments> provideArguments(ContainerExtensionContext extensionContext) throws Exception { | ||
if (Utils.injectCaseName(extensionContext)) { | ||
return Arrays.stream(annotation.value()) | ||
.map(RandomLongArgumentProvider::generateParams) | ||
.map(Arguments::of); | ||
} | ||
return Arrays.stream(annotation.value()) | ||
.map(RandomLongArgumentProvider::generateParam) | ||
.map(Arguments::of); | ||
} | ||
} |
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