-
Notifications
You must be signed in to change notification settings - Fork 71
/
PeopleProviderSources.java
86 lines (65 loc) · 2.28 KB
/
PeopleProviderSources.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright 2016-2023 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.junitpioneer.jupiter.cartesian;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Parameter;
import java.util.Objects;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.support.AnnotationConsumer;
// implementation example, does not contain tests
public class PeopleProviderSources {
// tag::cartesian_people_provider_with_CartesianParameterArgumentsProvider[]
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@CartesianArgumentsSource(PeopleProvider.class)
@interface People {
String[] names();
int[] ages();
}
class PeopleProvider implements CartesianParameterArgumentsProvider<Person> {
@Override
public Stream<Person> provideArguments(ExtensionContext context, Parameter parameter) {
People source = Objects.requireNonNull(parameter.getAnnotation(People.class));
return IntStream
.range(0, source.names().length)
.mapToObj(i -> new Person(source.names()[i], source.ages()[i]));
}
}
// end::cartesian_people_provider_with_CartesianParameterArgumentsProvider[]
static class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// tag::cartesian_people_provider_with_AnnotationConsumer[]
class PeopleProviderWithAnnotationConsumer
implements CartesianParameterArgumentsProvider<Person>, AnnotationConsumer<People> {
private People source;
@Override
public Stream<Person> provideArguments(ExtensionContext context, Parameter parameter) {
return IntStream
.range(0, source.names().length)
.mapToObj(i -> new Person(source.names()[i], source.ages()[i]));
}
@Override
public void accept(People source) {
this.source = source;
}
}
// end::cartesian_people_provider_with_AnnotationConsumer[]
}