All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.jupiter.engine.config.EnumConfigurationParameterConverter Maven / Gradle / Ivy

There is a newer version: 5.11.3
Show newest version
/*
 * Copyright 2015-2022 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
 *
 * https://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.engine.config;

import static org.apiguardian.api.API.Status.INTERNAL;

import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;

import org.apiguardian.api.API;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.commons.util.Preconditions;
import org.junit.platform.engine.ConfigurationParameters;

/**
 * @since 5.4
 */
@API(status = INTERNAL, since = "5.8")
public class EnumConfigurationParameterConverter> {

	private static final Logger logger = LoggerFactory.getLogger(EnumConfigurationParameterConverter.class);

	private final Class enumType;
	private final String enumDisplayName;

	public EnumConfigurationParameterConverter(Class enumType, String enumDisplayName) {
		this.enumType = enumType;
		this.enumDisplayName = enumDisplayName;
	}

	E get(ConfigurationParameters configParams, String key, E defaultValue) {
		Preconditions.notNull(configParams, "ConfigurationParameters must not be null");

		return get(key, configParams::get, defaultValue);
	}

	public E get(String key, Function> lookup, E defaultValue) {

		Optional value = lookup.apply(key);

		if (value.isPresent()) {
			String constantName = null;
			try {
				constantName = value.get().trim().toUpperCase(Locale.ROOT);
				E result = Enum.valueOf(enumType, constantName);
				logger.config(() -> String.format("Using %s '%s' set via the '%s' configuration parameter.",
					enumDisplayName, result, key));
				return result;
			}
			catch (Exception ex) {
				// local copy necessary for use in lambda expression
				String constant = constantName;
				logger.warn(() -> String.format(
					"Invalid %s '%s' set via the '%s' configuration parameter. "
							+ "Falling back to the %s default value.",
					enumDisplayName, constant, key, defaultValue.name()));
			}
		}

		return defaultValue;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy