| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
} |