Skip to content

Commit

Permalink
JUnit5: Add @BlankString
Browse files Browse the repository at this point in the history
Passes either `null` or empty string or string with spaces.
  • Loading branch information
ctapobep committed Jun 12, 2017
1 parent 5cc3c55 commit b307237
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 39 deletions.
2 changes: 1 addition & 1 deletion datagen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qala-datagen-parent</artifactId>
<groupId>io.qala.datagen</groupId>
<version>2.0.0</version>
<version>2.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qala-datagen-parent</artifactId>
<groupId>io.qala.datagen</groupId>
<version>2.0.0</version>
<version>2.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ void zeroIsNotPassed(long value, String name) {
void zeroIsNotPassed(double value, String name) {
assertFalse(value > -1 && value < 1, "Failed case: " + name);
}

@English(max=1)
@BlankString
void canAlsoPassNullsOrBlankStrings(String value, String name) {
assertTrue(value == null || value.trim().length() < 2, name);
}
}
2 changes: 1 addition & 1 deletion java8types/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qala-datagen-parent</artifactId>
<groupId>io.qala.datagen</groupId>
<version>2.0.0</version>
<version>2.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
8 changes: 7 additions & 1 deletion junit5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ void zeroIsNotPassed(long value, String name) {
void zeroIsNotPassed(double value, String name) {
assertFalse(value > -1 && value < 1, "Failed case: " + name);
}

@English(max=1)
@BlankString
void canAlsoPassNullsOrBlankStrings(String value, String name) {
assertTrue(value == null || value.trim().length() < 2, name);
}
```

In the end results look like this:
Expand Down Expand Up @@ -125,7 +131,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>2.0.0</version>
<version>2.1.0</version>
<scope>test</scope>
</dependency>

Expand Down
2 changes: 1 addition & 1 deletion junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>qala-datagen-parent</artifactId>
<groupId>io.qala.datagen</groupId>
<version>2.0.0</version>
<version>2.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
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.alphanumeric;

class AlphanumericArgumentProvider implements ArgumentsProvider, AnnotationConsumer<Alphanumeric> {
class AlphanumericArgumentProvider extends RandomizedArgumentProvider<Alphanumeric> {
private Alphanumeric annotation;

@Override
public void accept(Alphanumeric annotation) {
this.annotation = annotation;
}

@Override
public Stream<? extends Arguments> provideArguments(ContainerExtensionContext context) throws Exception {
DatagenUtils.setCurrentSeedIfNotSetYet(context);
if (DatagenUtils.passCaseNameToTestMethod(context))
return Stream.of(annotation)
.map(AlphanumericArgumentProvider::generateParams)
.map(Arguments::of);
return Stream.of(annotation)
.map(AlphanumericArgumentProvider::generateParam)
.map(Arguments::of);
@Override Stream<Object[]> getValueWithDescription() {
return Stream.of(annotation).map(AlphanumericArgumentProvider::generateParams);
}
@Override Stream<Object> getValue() {
return Stream.of(annotation).map(AlphanumericArgumentProvider::generateParam);
}

static String generateParam(Alphanumeric annotation) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
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 AlphanumericsArgumentProvider implements ArgumentsProvider, AnnotationConsumer<Alphanumerics> {
class AlphanumericsArgumentProvider extends RandomizedArgumentProvider<Alphanumerics> {
private Alphanumerics annotation;

@Override
public void accept(Alphanumerics annotation) {
this.annotation = annotation;
}

@Override
public Stream<? extends Arguments> provideArguments(ContainerExtensionContext context) throws Exception {
DatagenUtils.setCurrentSeedIfNotSetYet(context);
if (DatagenUtils.passCaseNameToTestMethod(context))
return Arrays.stream(annotation.value())
.map(AlphanumericArgumentProvider::generateParams)
.map(Arguments::of);
return Arrays.stream(annotation.value())
.map(AlphanumericArgumentProvider::generateParam)
.map(Arguments::of);
@Override Stream<Object[]> getValueWithDescription() {
return Arrays.stream(annotation.value()).map(AlphanumericArgumentProvider::generateParams);
}
@Override Stream<Object> getValue() {
return Arrays.stream(annotation.value()).map(AlphanumericArgumentProvider::generateParam);
}

}
20 changes: 20 additions & 0 deletions junit5/src/main/java/io/qala/datagen/junit5/BlankString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.qala.datagen.junit5;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

import java.lang.annotation.*;

/** Passes either {@code null} or empty string or string with spaces to JUnit5 tests. */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})

@ArgumentsSource(BlankStringProvider.class)
@ParameterizedTest
public @interface BlankString {
/**
* Is passed to the test case if 2nd parameter is available in the test method. If left blank it's going to be
* filled with the description of what's passed - null or empty string or string with spaces.
*/
String name() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.qala.datagen.junit5;

import java.util.stream.Stream;

import static io.qala.datagen.RandomShortApi.nullOrBlank;

public class BlankStringProvider extends RandomizedArgumentProvider<BlankString> {
private BlankString annotation;

@Override
public void accept(BlankString annotation) {
this.annotation = annotation;
}

@Override Stream<Object[]> getValueWithDescription() {
return Stream.of(annotation)
.map(BlankStringProvider::generateParams);
}

@Override Stream<Object> getValue() {
return Stream.of(generateParam());
}

static String generateParam() {
return nullOrBlank();
}

static Object[] generateParams(BlankString annotation) {
String randomValue = generateParam();
String name = annotation.name();
if(name.isEmpty()) {
if(randomValue == null) name = "null";
else if(randomValue.isEmpty()) name = "empty string";
else name = "string with spaces only";
}
return new Object[]{randomValue, name};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,16 @@ void nameIsGeneric_byDefault(String value, String name) {
}
}

@BlankString void passesNull_empty_or_space_string(String string) {
assertTrue(string == null || string.trim().isEmpty());
}
@BlankString void blankString_nameDefaultToWhateverIsPassedInValue(String string, String caseName) {
if(string == null) assertEquals("null", caseName);
else if(string.isEmpty()) assertEquals("empty string", caseName);
else if(string.trim().isEmpty()) assertEquals("string with spaces only", caseName);
else fail("Something weird was passed but it wasn't a blank: " + string);
}

// Such tests are impossible to set seed for as for now, JUnit5 doesn't have callbacks that are run before
// Argument Providers are invoked.
@Seed(1234)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>io.qala.datagen</groupId>
<artifactId>qala-datagen-parent</artifactId>
<packaging>pom</packaging>
<version>2.0.0</version>
<version>2.1.0</version>

<licenses>
<license>
Expand Down

0 comments on commit b307237

Please sign in to comment.