Debian Codegraph @

junit-platform-engine/src/main/java/org/junit/platform/engine/ConfigurationParameters.java

raw
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
9 */
10
11 package org.junit.platform.engine;
12
13 import static org.apiguardian.api.API.Status.DEPRECATED;
14 import static org.apiguardian.api.API.Status.STABLE;
15
16 import java.util.Optional;
17 import java.util.Set;
18 import java.util.function.Function;
19
20 import org.apiguardian.api.API;
21 import org.junit.platform.commons.JUnitException;
22 import org.junit.platform.commons.util.Preconditions;
23
24 /**
25 * Configuration parameters that {@link TestEngine TestEngines} may use to
26 * influence test discovery and execution.
27 *
28 * <p>For example, the JUnit Jupiter engine uses a configuration parameter to
29 * enable IDEs and build tools to deactivate conditional test execution.
30 *
31 * <p>As of JUnit Platform 1.8, configuration parameters are also made available to
32 * implementations of the {@link org.junit.platform.launcher.TestExecutionListener}
33 * API via the {@link org.junit.platform.launcher.TestPlan}.
34 *
35 * @since 1.0
36 * @see TestEngine
37 * @see EngineDiscoveryRequest
38 * @see ExecutionRequest
39 */
40 @API(status = STABLE, since = "1.0")
41 public interface ConfigurationParameters {
42
43 /**
44 * Name of the JUnit Platform configuration file: {@value}.
45 *
46 * <p>If a properties file with this name is present in the root of the
47 * classpath, it will be used as a source for <em>configuration
48 * parameters</em>. If multiple files are present, only the first one
49 * detected in the classpath will be used.
50 *
51 * @see java.util.Properties
52 */
53 String CONFIG_FILE_NAME = "junit-platform.properties";
54
55 /**
56 * Get the configuration parameter stored under the specified {@code key}.
57 *
58 * <p>If no such key is present in this {@code ConfigurationParameters},
59 * an attempt will be made to look up the value as a JVM system property.
60 * If no such system property exists, an attempt will be made to look up
61 * the value in the {@linkplain #CONFIG_FILE_NAME JUnit Platform properties
62 * file}.
63 *
64 * @param key the key to look up; never {@code null} or blank
65 * @return an {@code Optional} containing the value; never {@code null}
66 * but potentially empty
67 *
68 * @see #getBoolean(String)
69 * @see System#getProperty(String)
70 * @see #CONFIG_FILE_NAME
71 */
72 Optional<String> get(String key);
73
74 /**
75 * Get the boolean configuration parameter stored under the specified
76 * {@code key}.
77 *
78 * <p>If no such key is present in this {@code ConfigurationParameters},
79 * an attempt will be made to look up the value as a JVM system property.
80 * If no such system property exists, an attempt will be made to look up
81 * the value in the {@linkplain #CONFIG_FILE_NAME JUnit Platform properties
82 * file}.
83 *
84 * @param key the key to look up; never {@code null} or blank
85 * @return an {@code Optional} containing the value; never {@code null}
86 * but potentially empty
87 *
88 * @see #get(String)
89 * @see Boolean#parseBoolean(String)
90 * @see System#getProperty(String)
91 * @see #CONFIG_FILE_NAME
92 */
93 Optional<Boolean> getBoolean(String key);
94
95 /**
96 * Get and transform the configuration parameter stored under the specified
97 * {@code key} using the specified {@code transformer}.
98 *
99 * <p>If no such key is present in this {@code ConfigurationParameters},
100 * an attempt will be made to look up the value as a JVM system property.
101 * If no such system property exists, an attempt will be made to look up
102 * the value in the {@linkplain #CONFIG_FILE_NAME JUnit Platform properties
103 * file}.
104 *
105 * <p>In case the transformer throws an exception, it will be wrapped in a
106 * {@link JUnitException} with a helpful message.
107 *
108 * @param key the key to look up; never {@code null} or blank
109 * @param transformer the transformer to apply in case a value is found;
110 * never {@code null}
111 * @return an {@code Optional} containing the value; never {@code null}
112 * but potentially empty
113 *
114 * @since 1.3
115 * @see #getBoolean(String)
116 * @see System#getProperty(String)
117 * @see #CONFIG_FILE_NAME
118 */
119 @API(status = STABLE, since = "1.3")
120 default <T> Optional<T> get(String key, Function<String, T> transformer) {
121 Preconditions.notNull(transformer, "transformer must not be null");
122 return get(key).map(input -> {
123 try {
124 return transformer.apply(input);
125 }
126 catch (Exception ex) {
127 String message = String.format(
128 "Failed to transform configuration parameter with key '%s' and initial value '%s'", key, input);
129 throw new JUnitException(message, ex);
130 }
131 });
132 }
133
134 /**
135 * Get the number of configuration parameters stored directly in this
136 * {@code ConfigurationParameters}.
137 * @deprecated as of JUnit Platform 1.9 in favor of {@link #keySet()}
138 */
139 @Deprecated
140 @API(status = DEPRECATED, since = "1.9")
141 int size();
142
143 /**
144 * Get the keys of all configuration parameters stored in this
145 * {@code ConfigurationParameters}.
146 *
147 * @return the set of keys contained in this {@code ConfigurationParameters}
148 */
149 @API(status = STABLE, since = "1.9")
150 Set<String> keySet();
151
152 }