Skip to content

Commit

Permalink
JUnit5: Add @RandomLong
Browse files Browse the repository at this point in the history
  • Loading branch information
ctapobep committed Jun 10, 2017
1 parent d67680e commit 24f112b
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.qala.datagen.examples.junit5;

import io.qala.datagen.junit5.Alphanumeric;
import io.qala.datagen.junit5.English;
import io.qala.datagen.junit5.Numeric;
import io.qala.datagen.junit5.Unicode;
import io.qala.datagen.junit5.*;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class JUnit5ExampleTest {
Expand All @@ -17,4 +15,14 @@ class JUnit5ExampleTest {
void canGenerateMultipleAlphanumerics(String value, String name) {
assertTrue(value.length() >= 1 && value.length() <= 31, "Failed case: " + name);
}
@RandomInt(min = 1, name = "greater than zero")
@RandomInt(max = -1, name = "less than zero")
void zeroInt_isNotPassed(int param, String name) {
assertNotEquals(0, param, "Failed case: " + name);
}
@RandomLong(min = 1, name = "greater than zero")
@RandomLong(max = -1, name = "less than zero")
void zeroLong_isNotPassed(long value, String name) {
assertNotEquals(0, value, "Failed case: " + name);
}
}
14 changes: 10 additions & 4 deletions junit5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ You can use Datagen + JUnit5 integration to facilitate randomization in paramete
}
```

This will run the test 6 times with different parameters according to the annotations. This test will run 2 times:
This will run the test 6 times with different parameters according to the annotations. These tests will run 2 times
each:

```
@RandomInt(min = 1, name = "greater than zero")
@RandomInt(max = -1, name = "less than zero")
void zeroIsNotPassed(int param) {
assertNotEquals(0, param);
void zeroInt_isNotPassed(int param, String name) {
assertNotEquals(0, param, "Failed case: " + name);
}
@RandomLong(min = 1, name = "greater than zero")
@RandomLong(max = -1, name = "less than zero")
void zeroLong_isNotPassed(long value, String name) {
assertNotEquals(0, value, "Failed case: " + name);
}
```

Expand All @@ -39,7 +45,7 @@ API may change in the future, you can give it a try. In order for this to work y
<dependency>
<groupId>io.qala.datagen</groupId>
<artifactId>qala-datagen-junit5</artifactId>
<version>1.12.0</version>
<version>1.13.0</version>
<scope>test</scope>
</dependency>
Expand Down
31 changes: 31 additions & 0 deletions junit5/src/main/java/io/qala/datagen/junit5/RandomLong.java
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 "";
}
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 junit5/src/main/java/io/qala/datagen/junit5/RandomLongs.java
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();
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,52 @@ void nameIsGeneric_byDefault(int value, String name) {
}
}

@Nested class RandomLongGenerator {
@RandomLong(min = 1, max = 10)
void generatesIntInBoundaries(long param) {
assertTrue(param <= 10);
assertTrue(param >= 1);
}

@RandomLong @RandomLong @SuppressWarnings("ConstantConditions")
void generatesAnyNumberByDefault(long param) {
assertTrue(param >= Long.MIN_VALUE);
assertTrue(param <= Long.MAX_VALUE);
assertChangedFromLastTime(param);
}

@RandomLong(name = "zero")
void ignoresName_if2ndParamIsNotPresent(long param) {
}

@RandomLong(name = "blah")
void passesName_asSecondParam(long param, String name) {
assertEquals("blah", name);
}
}
@Nested class RandomLongsGenerator {
@RandomLong(min = 1, name = "greater than zero")
@RandomLong(max = -1, name = "less than zero")
void canGenerateMultipleNumbers(long param) {
assertNotEquals(0, param);
assertChangedFromLastTime(param);
}

@RandomLong(name = "zero") @RandomLong
void ignoresName_if2ndParamIsNotPresent(long param) {
}

@RandomLong(name = "blah") @RandomLong(name = "blah")
void passesName_asSecondParam(long param, String name) {
assertEquals("blah", name);
}

@RandomLong(min = 2, max = 10) @RandomLong(min = 2, max = 10)
void nameIsGeneric_byDefault(long value, String name) {
assertEquals("long from 2 to 10", name);
}
}

@Nested class AlphanumericGenerator {
@Alphanumeric void generatesStringWithCorrectSymbols(String value) {
assertEquals(value.getBytes().length, value.length());
Expand Down Expand Up @@ -85,8 +131,7 @@ void canGenerateMultipleAlphanumerics(String value) {
assertChangedFromLastTime(value);
}

@Alphanumeric(length = 1, name = "min boundary")
@Alphanumeric(min = 2, max = 29, name = "middle value")
@Alphanumeric(min = 20, max = 29, name = "middle value")
@Alphanumeric(length = 30, name = "max boundary")
void ignoresCaseName_ifItIsNotPassed(String value) {
assertTrue(value.length() >= 1 && value.length() <= 31);
Expand Down

0 comments on commit 24f112b

Please sign in to comment.