Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/RandomOrdererUtils.java

raw symbols SCIP
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.jupiter.api;
12
13 import java.util.Optional;
14 import java.util.function.Function;
15
16 import org.junit.platform.commons.logging.Logger;
17
18 /**
19 * Shared utility methods for ordering test classes and test methods randomly.
20 *
21 * @since 5.11
22 * @see ClassOrderer.Random
23 * @see MethodOrderer.Random
24 */
25 class RandomOrdererUtils {
26
27 static final String RANDOM_SEED_PROPERTY_NAME = "junit.jupiter.execution.order.random.seed";
28
29 static final long DEFAULT_SEED = System.nanoTime();
30
31 static Long getSeed(Function<String, Optional<String>> configurationParameterLookup, Logger logger) {
32 return getCustomSeed(configurationParameterLookup, logger).orElse(DEFAULT_SEED);
33 }
34
35 private static Optional<Long> getCustomSeed(Function<String, Optional<String>> configurationParameterLookup,
36 Logger logger) {
37 return configurationParameterLookup.apply(RANDOM_SEED_PROPERTY_NAME).map(configurationParameter -> {
38 try {
39 logger.config(() -> String.format("Using custom seed for configuration parameter [%s] with value [%s].",
40 RANDOM_SEED_PROPERTY_NAME, configurationParameter));
41 return Long.valueOf(configurationParameter);
42 }
43 catch (NumberFormatException ex) {
44 logger.warn(ex,
45 () -> String.format(
46 "Failed to convert configuration parameter [%s] with value [%s] to a long. "
47 + "Using default seed [%s] as fallback.",
48 RANDOM_SEED_PROPERTY_NAME, configurationParameter, DEFAULT_SEED));
49 return null;
50 }
51 });
52 }
53 }