Skip to content

Commit 708a1f1

Browse files
authored
add new params api (via #648)
1 parent 78b6018 commit 708a1f1

File tree

9 files changed

+344
-22
lines changed

9 files changed

+344
-22
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Thank you so much for sending us a pull request!
33
44
Make sure you have a clear name for your pull request.
55
The name should start with a capital letter and no dot is required in the end of the sentence.
6-
To link the request with isses use the following notation: (fixes #123, fixes #321\)
6+
To link the request with issues use the following notation: (fixes #123, fixes #321\)
77
88
An example of good pull request names:
99
* Add Cucumber integration (fixes #123\)

allure-java-commons/src/main/java/io/qameta/allure/Allure.java

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,60 @@ public static void label(final String name, final String value) {
247247
/**
248248
* Adds parameter to current test or step (or fixture) if any. Takes no effect
249249
* if no test run at the moment.
250+
* <p>
251+
* Shortcut for {@link #parameter(String, Object, Boolean, Parameter.Mode)}.
250252
*
251253
* @param name the name of parameter.
252254
* @param value the value of parameter.
253255
*/
254256
public static <T> T parameter(final String name, final T value) {
255-
final Parameter parameter = createParameter(name, value);
257+
return parameter(name, value, null, null);
258+
}
259+
260+
/**
261+
* Adds parameter to current test or step (or fixture) if any. Takes no effect
262+
* if no test run at the moment.
263+
* <p>
264+
* Shortcut for {@link #parameter(String, Object, Boolean, Parameter.Mode)}.
265+
*
266+
* @param name the name of parameter.
267+
* @param value the value of parameter.
268+
* @param excluded true if parameter should be excluded from history key calculation, false otherwise.
269+
* @return the specified value.
270+
*/
271+
public static <T> T parameter(final String name, final T value, final Boolean excluded) {
272+
return parameter(name, value, excluded, null);
273+
}
274+
275+
/**
276+
* Adds parameter to current test or step (or fixture) if any. Takes no effect
277+
* if no test run at the moment.
278+
* <p>
279+
* Shortcut for {@link #parameter(String, Object, Boolean, Parameter.Mode)}.
280+
*
281+
* @param name the name of parameter.
282+
* @param value the value of parameter.
283+
* @param mode the parameter mode.
284+
* @return the specified value.
285+
*/
286+
public static <T> T parameter(final String name, final T value,
287+
final Parameter.Mode mode) {
288+
return parameter(name, value, null, mode);
289+
}
290+
291+
/**
292+
* Adds parameter to current test or step (or fixture) if any. Takes no effect
293+
* if no test run at the moment.
294+
*
295+
* @param name the name of parameter.
296+
* @param value the value of parameter.
297+
* @param excluded true if parameter should be excluded from history key calculation, false otherwise.
298+
* @param mode the parameter mode.
299+
* @return the specified value.
300+
*/
301+
public static <T> T parameter(final String name, final T value,
302+
final Boolean excluded, final Parameter.Mode mode) {
303+
final Parameter parameter = createParameter(name, value, excluded, mode);
256304
getLifecycle().updateTestCase(testResult -> testResult.getParameters().add(parameter));
257305
return value;
258306
}
@@ -485,12 +533,66 @@ public interface ThrowableContextRunnableVoid<T> {
485533
/**
486534
* Step context.
487535
*/
536+
@SuppressWarnings("MultipleStringLiterals")
488537
public interface StepContext {
489538

539+
/**
540+
* Sets step's name.
541+
*
542+
* @param name the deserted name of step.
543+
*/
490544
void name(String name);
491545

546+
/**
547+
* Adds parameter to a step.
548+
*
549+
* @param name the name of parameter.
550+
* @param value the value.
551+
* @param <T> the type of value.
552+
* @return the value.
553+
*/
492554
<T> T parameter(String name, T value);
493555

556+
/**
557+
* Adds parameter to a step.
558+
*
559+
* @param name the name of parameter.
560+
* @param value the value.
561+
* @param excluded true if parameter should be excluded from history key generation, false otherwise.
562+
* @param <T> the type of value.
563+
* @return the value.
564+
*/
565+
default <T> T parameter(String name, T value, Boolean excluded) {
566+
throw new UnsupportedOperationException("method is not implemented");
567+
}
568+
569+
/**
570+
* Adds parameter to a step.
571+
*
572+
* @param name the name of parameter.
573+
* @param value the value.
574+
* @param mode the parameter's mode.
575+
* @param <T> the type of value.
576+
* @return the value.
577+
*/
578+
default <T> T parameter(String name, T value, Parameter.Mode mode) {
579+
throw new UnsupportedOperationException("method is not implemented");
580+
}
581+
582+
/**
583+
* Adds parameter to a step.
584+
*
585+
* @param name the name of parameter.
586+
* @param value the value.
587+
* @param excluded true if parameter should be excluded from history key generation, false otherwise.
588+
* @param mode the parameter's mode.
589+
* @param <T> the type of value.
590+
* @return the value.
591+
*/
592+
default <T> T parameter(String name, T value, Boolean excluded, Parameter.Mode mode) {
593+
throw new UnsupportedOperationException("method is not implemented");
594+
}
595+
494596
}
495597

496598
/**
@@ -511,7 +613,22 @@ public void name(final String name) {
511613

512614
@Override
513615
public <T> T parameter(final String name, final T value) {
514-
final Parameter param = createParameter(name, value);
616+
return parameter(name, value, null, null);
617+
}
618+
619+
@Override
620+
public <T> T parameter(final String name, final T value, final Boolean excluded) {
621+
return parameter(name, value, excluded, null);
622+
}
623+
624+
@Override
625+
public <T> T parameter(final String name, final T value, final Parameter.Mode mode) {
626+
return parameter(name, value, null, mode);
627+
}
628+
629+
@Override
630+
public <T> T parameter(final String name, final T value, final Boolean excluded, final Parameter.Mode mode) {
631+
final Parameter param = createParameter(name, value, excluded, mode);
515632
getLifecycle().updateStep(uuid, stepResult -> stepResult.getParameters().add(param));
516633
return value;
517634
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2019 Qameta Software OÜ
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure;
17+
18+
import io.qameta.allure.model.Parameter;
19+
20+
import java.lang.annotation.Documented;
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Inherited;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* This annotation used to add parameters to results
29+
* from method parameters.
30+
*
31+
* @see Parameter
32+
* @see Parameter.Mode
33+
* @see Allure#parameter(String, Object)
34+
*/
35+
@Documented
36+
@Inherited
37+
@Retention(RetentionPolicy.RUNTIME)
38+
@Target({ElementType.PARAMETER})
39+
public @interface Param {
40+
41+
/**
42+
* Alias for {@link #name()}. Overrides value, specified in {@link #name()}.
43+
*/
44+
String value() default "";
45+
46+
/**
47+
* The name of parameter. Be careful, changing parameter's name
48+
* may affect method signature and, as result, may get different
49+
* test case generated.
50+
* <p>
51+
* If not specified, the parameter name from reflection will be used.
52+
*
53+
* @return the name of parameter.
54+
*/
55+
String name() default "";
56+
57+
/**
58+
* The parameter mode.
59+
*
60+
* @return the parameter mode.
61+
*/
62+
Parameter.Mode mode() default Parameter.Mode.DEFAULT;
63+
64+
/**
65+
* Set it to true to exclude the parameter from historyKey generation.
66+
*
67+
* @return true if parameter is excluded, false otherwise.
68+
*/
69+
boolean excluded() default false;
70+
71+
}

allure-java-commons/src/main/java/io/qameta/allure/util/AspectUtils.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.qameta.allure.util;
1717

18+
import io.qameta.allure.Param;
1819
import io.qameta.allure.model.Parameter;
1920
import org.aspectj.lang.JoinPoint;
2021
import org.aspectj.lang.reflect.MethodSignature;
@@ -25,6 +26,7 @@
2526
import java.util.Optional;
2627
import java.util.stream.Collectors;
2728
import java.util.stream.IntStream;
29+
import java.util.stream.Stream;
2830

2931
import static io.qameta.allure.util.NamingUtils.processNameTemplate;
3032
import static io.qameta.allure.util.ResultsUtils.createParameter;
@@ -38,19 +40,6 @@ private AspectUtils() {
3840
throw new IllegalStateException("Do not instance");
3941
}
4042

41-
/**
42-
* @deprecated use {@link AspectUtils#getName(String,JoinPoint)} instead.
43-
*/
44-
@Deprecated
45-
public static String getName(final String nameTemplate,
46-
final MethodSignature methodSignature,
47-
final Object... args) {
48-
return Optional.of(nameTemplate)
49-
.filter(v -> !v.isEmpty())
50-
.map(value -> processNameTemplate(value, getParametersMap(methodSignature, args)))
51-
.orElseGet(methodSignature::getName);
52-
}
53-
5443
public static String getName(final String nameTemplate, final JoinPoint joinPoint) {
5544
return Optional.of(nameTemplate)
5645
.filter(v -> !v.isEmpty())
@@ -81,9 +70,26 @@ public static Map<String, Object> getParametersMap(final JoinPoint joinPoint) {
8170
}
8271

8372
public static List<Parameter> getParameters(final MethodSignature signature, final Object... args) {
73+
final java.lang.reflect.Parameter[] params = signature.getMethod().getParameters();
8474
return IntStream
8575
.range(0, args.length)
86-
.mapToObj(index -> createParameter(signature.getParameterNames()[index], args[index]))
76+
.mapToObj(index -> {
77+
final Parameter parameter = createParameter(signature.getParameterNames()[index], args[index]);
78+
final java.lang.reflect.Parameter ref = params[index];
79+
Stream.of(ref.getAnnotationsByType(Param.class))
80+
.findFirst()
81+
.ifPresent(param -> {
82+
Stream.of(param.value(), param.name())
83+
.map(String::trim)
84+
.filter(name -> name.length() > 0)
85+
.findFirst()
86+
.ifPresent(parameter::setName);
87+
88+
parameter.setMode(param.mode());
89+
parameter.setExcluded(param.excluded());
90+
});
91+
return parameter;
92+
})
8793
.collect(Collectors.toList());
8894
}
8995

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2021 Qameta Software OÜ
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.qameta.allure.util;
17+
18+
import io.qameta.allure.Param;
19+
import io.qameta.allure.model.Parameter;
20+
21+
import java.lang.reflect.Method;
22+
import java.util.List;
23+
import java.util.Objects;
24+
import java.util.stream.Collectors;
25+
import java.util.stream.IntStream;
26+
import java.util.stream.Stream;
27+
28+
/**
29+
* @author charlie (Dmitry Baev).
30+
*/
31+
public final class ParameterUtils {
32+
33+
private ParameterUtils() {
34+
throw new IllegalStateException("do not instance");
35+
}
36+
37+
public static List<Parameter> createParameters(final Method method,
38+
final Object... args) {
39+
final java.lang.reflect.Parameter[] parameters = method.getParameters();
40+
return IntStream.range(0, parameters.length)
41+
.mapToObj(i -> {
42+
final java.lang.reflect.Parameter parameter = parameters[i];
43+
final Object value = args[i];
44+
final Param annotation = parameter.getAnnotation(Param.class);
45+
if (Objects.isNull(annotation)) {
46+
return ResultsUtils.createParameter(parameter.getName(), value);
47+
}
48+
final String name = Stream.of(annotation.value(), annotation.name(), parameter.getName())
49+
.map(String::trim)
50+
.filter(s -> s.length() > 0)
51+
.findFirst()
52+
.orElseGet(() -> "arg" + i);
53+
54+
return ResultsUtils.createParameter(
55+
name,
56+
value,
57+
annotation.excluded(),
58+
annotation.mode()
59+
);
60+
})
61+
.collect(Collectors.toList());
62+
}
63+
64+
}

allure-java-commons/src/main/java/io/qameta/allure/util/ResultsUtils.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@
6464
"ClassDataAbstractionCoupling",
6565
"PMD.ExcessiveImports",
6666
"PMD.TooManyMethods",
67-
"PMD.GodClass",
68-
"deprecation"
67+
"PMD.GodClass"
6968
})
7069
public final class ResultsUtils {
7170

@@ -109,7 +108,16 @@ private ResultsUtils() {
109108
}
110109

111110
public static Parameter createParameter(final String name, final Object value) {
112-
return new Parameter().setName(name).setValue(ObjectUtils.toString(value));
111+
return createParameter(name, value, null, null);
112+
}
113+
114+
public static Parameter createParameter(final String name, final Object value,
115+
final Boolean excluded, final Parameter.Mode mode) {
116+
return new Parameter()
117+
.setName(name)
118+
.setValue(ObjectUtils.toString(value))
119+
.setExcluded(excluded)
120+
.setMode(mode);
113121
}
114122

115123
public static Label createSuiteLabel(final String suite) {
@@ -353,7 +361,7 @@ private static String getRealHostName() {
353361
try {
354362
cachedHost = InetAddress.getLocalHost().getHostName();
355363
} catch (UnknownHostException e) {
356-
LOGGER.debug("Could not get host name {}", e);
364+
LOGGER.debug("Could not get host name", e);
357365
cachedHost = "default";
358366
}
359367
}

0 commit comments

Comments
 (0)