Search in sources :

Example 11 with Arguments

use of org.junit.jupiter.params.provider.Arguments in project spring-framework by spring-projects.

the class ClientHttpConnectorTests method basic.

// Do not auto-close arguments since HttpComponentsClientHttpConnector implements
// AutoCloseable and is shared between parameterized test invocations.
@ParameterizedTest(autoCloseArguments = false)
@MethodSource("org.springframework.http.client.reactive.ClientHttpConnectorTests#methodsWithConnectors")
void basic(ClientHttpConnector connector, HttpMethod method) throws Exception {
    URI uri = this.server.url("/").uri();
    String responseBody = "bar\r\n";
    prepareResponse(response -> {
        response.setResponseCode(200);
        response.addHeader("Baz", "Qux");
        response.setBody(responseBody);
    });
    String requestBody = "foo\r\n";
    boolean requestHasBody = METHODS_WITH_BODY.contains(method);
    Mono<ClientHttpResponse> futureResponse = connector.connect(method, uri, request -> {
        assertThat(request.getMethod()).isEqualTo(method);
        assertThat(request.getURI()).isEqualTo(uri);
        request.getHeaders().add("Foo", "Bar");
        if (requestHasBody) {
            Mono<DataBuffer> body = Mono.fromCallable(() -> {
                byte[] bytes = requestBody.getBytes(StandardCharsets.UTF_8);
                return DefaultDataBufferFactory.sharedInstance.wrap(bytes);
            });
            return request.writeWith(body);
        } else {
            return request.setComplete();
        }
    });
    CountDownLatch latch = new CountDownLatch(1);
    StepVerifier.create(futureResponse).assertNext(response -> {
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(response.getHeaders().getFirst("Baz")).isEqualTo("Qux");
        DataBufferUtils.join(response.getBody()).map(buffer -> {
            String s = buffer.toString(StandardCharsets.UTF_8);
            DataBufferUtils.release(buffer);
            return s;
        }).subscribe(s -> assertThat(s).isEqualTo(responseBody), throwable -> {
            latch.countDown();
            fail(throwable.getMessage(), throwable);
        }, latch::countDown);
    }).verifyComplete();
    latch.await();
    expectRequest(request -> {
        assertThat(request.getMethod()).isEqualTo(method.name());
        assertThat(request.getHeader("Foo")).isEqualTo("Bar");
        if (requestHasBody) {
            assertThat(request.getBody().readUtf8()).isEqualTo(requestBody);
        }
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) URI(java.net.URI) MethodSource(org.junit.jupiter.params.provider.MethodSource) Buffer(okio.Buffer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) IOException(java.io.IOException) Target(java.lang.annotation.Target) Mono(reactor.core.publisher.Mono) ElementType(java.lang.annotation.ElementType) Arguments(org.junit.jupiter.params.provider.Arguments) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Assertions.fail(org.assertj.core.api.Assertions.fail) NonNull(org.springframework.lang.NonNull) MockResponse(okhttp3.mockwebserver.MockResponse) RetentionPolicy(java.lang.annotation.RetentionPolicy) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with Arguments

use of org.junit.jupiter.params.provider.Arguments in project dhis2-core by dhis2.

the class MetadataImportBasedOnSchemasTest method getSchemaEndpoints.

private Stream<Arguments> getSchemaEndpoints() {
    ApiResponse apiResponse = schemasActions.get();
    String jsonPathIdentifier = "schemas.findAll{it.relativeApiEndpoint && it.metadata && it.singular != 'externalFileResource'}";
    List<String> apiEndpoints = apiResponse.extractList(jsonPathIdentifier + ".plural");
    List<String> schemaEndpoints = apiResponse.extractList(jsonPathIdentifier + ".singular");
    List<Arguments> arguments = new ArrayList<>();
    for (int i = 0; i < apiEndpoints.size(); i++) {
        arguments.add(Arguments.of(apiEndpoints.get(i), schemaEndpoints.get(i)));
    }
    return arguments.stream();
}
Also used : Arguments(org.junit.jupiter.params.provider.Arguments) ArrayList(java.util.ArrayList) ApiResponse(org.hisp.dhis.dto.ApiResponse)

Example 13 with Arguments

use of org.junit.jupiter.params.provider.Arguments in project judge by zjnu-acm.

the class JudgeBridgeGroovyTest method data.

public static List<Arguments> data() throws Exception {
    Checker[] values = Checker.values();
    ArrayList<Arguments> list = new ArrayList<>(values.length);
    for (Checker checker : values) {
        Path path = program.resolve(checker.name());
        Files.list(path).filter(p -> p.getFileName().toString().endsWith(".groovy")).map(Object::toString).forEach(executable -> list.add(arguments(checker, executable)));
    }
    return list;
}
Also used : Path(java.nio.file.Path) Arguments(org.junit.jupiter.params.provider.Arguments) ArrayList(java.util.ArrayList)

Example 14 with Arguments

use of org.junit.jupiter.params.provider.Arguments in project junit5 by junit-team.

the class ParameterizedTestExtension method provideTestTemplateInvocationContexts.

@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext extensionContext) {
    Method templateMethod = extensionContext.getRequiredTestMethod();
    String displayName = extensionContext.getDisplayName();
    ParameterizedTestMethodContext methodContext = // 
    getStore(extensionContext).get(METHOD_CONTEXT_KEY, ParameterizedTestMethodContext.class);
    int argumentMaxLength = extensionContext.getConfigurationParameter(ARGUMENT_MAX_LENGTH_KEY, Integer::parseInt).orElse(512);
    ParameterizedTestNameFormatter formatter = createNameFormatter(extensionContext, templateMethod, methodContext, displayName, argumentMaxLength);
    AtomicLong invocationCount = new AtomicLong(0);
    // @formatter:off
    return findRepeatableAnnotations(templateMethod, ArgumentsSource.class).stream().map(ArgumentsSource::value).map(this::instantiateArgumentsProvider).map(provider -> AnnotationConsumerInitializer.initialize(templateMethod, provider)).flatMap(provider -> arguments(provider, extensionContext)).map(Arguments::get).map(arguments -> consumedArguments(arguments, methodContext)).map(arguments -> {
        invocationCount.incrementAndGet();
        return createInvocationContext(formatter, methodContext, arguments);
    }).onClose(() -> Preconditions.condition(invocationCount.get() > 0, "Configuration error: You must configure at least one set of arguments for this @ParameterizedTest"));
// @formatter:on
}
Also used : AnnotationConsumerInitializer(org.junit.jupiter.params.support.AnnotationConsumerInitializer) Preconditions(org.junit.platform.commons.util.Preconditions) Arrays(java.util.Arrays) AnnotationUtils.isAnnotated(org.junit.platform.commons.util.AnnotationUtils.isAnnotated) JUnitException(org.junit.platform.commons.JUnitException) AnnotationUtils.findAnnotation(org.junit.platform.commons.util.AnnotationUtils.findAnnotation) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Arguments(org.junit.jupiter.params.provider.Arguments) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) TestTemplateInvocationContext(org.junit.jupiter.api.extension.TestTemplateInvocationContext) ExceptionUtils(org.junit.platform.commons.util.ExceptionUtils) TestTemplateInvocationContextProvider(org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider) AnnotationUtils.findRepeatableAnnotations(org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations) ArgumentsProvider(org.junit.jupiter.params.provider.ArgumentsProvider) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource) Method(java.lang.reflect.Method) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) AtomicLong(java.util.concurrent.atomic.AtomicLong) Method(java.lang.reflect.Method) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Aggregations

Arguments (org.junit.jupiter.params.provider.Arguments)14 ArrayList (java.util.ArrayList)11 Arrays (java.util.Arrays)3 Stream (java.util.stream.Stream)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 Path (java.nio.file.Path)2 List (java.util.List)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 RandomValues (com.cadenzauk.core.RandomValues)1 IsUtilityClass.isUtilityClass (com.cadenzauk.core.testutil.IsUtilityClass.isUtilityClass)1 DateFunctions.addDays (com.cadenzauk.siesta.grammar.expression.DateFunctions.addDays)1 DateFunctions.day (com.cadenzauk.siesta.grammar.expression.DateFunctions.day)1 DateFunctions.dayDiff (com.cadenzauk.siesta.grammar.expression.DateFunctions.dayDiff)1 DateFunctions.hour (com.cadenzauk.siesta.grammar.expression.DateFunctions.hour)1 DateFunctions.hourDiff (com.cadenzauk.siesta.grammar.expression.DateFunctions.hourDiff)1 DateFunctions.minute (com.cadenzauk.siesta.grammar.expression.DateFunctions.minute)1 DateFunctions.minuteDiff (com.cadenzauk.siesta.grammar.expression.DateFunctions.minuteDiff)1 DateFunctions.month (com.cadenzauk.siesta.grammar.expression.DateFunctions.month)1