|
| 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.params; |
| 12 | + |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.extension.ExtensionConfigurationException; |
| 18 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 19 | +import org.junit.jupiter.api.extension.InvocationInterceptor; |
| 20 | +import org.junit.jupiter.api.extension.ReflectiveInvocationContext; |
| 21 | +import org.junit.jupiter.params.provider.Arguments; |
| 22 | +import org.junit.platform.commons.logging.Logger; |
| 23 | +import org.junit.platform.commons.logging.LoggerFactory; |
| 24 | +import org.junit.platform.commons.util.Preconditions; |
| 25 | + |
| 26 | +class ArgumentCountValidator implements InvocationInterceptor { |
| 27 | + private static final Logger logger = LoggerFactory.getLogger(ArgumentCountValidator.class); |
| 28 | + |
| 29 | + static final String ARGUMENT_COUNT_VALIDATION_KEY = "junit.jupiter.params.argumentCountValidation"; |
| 30 | + private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create( |
| 31 | + ArgumentCountValidator.class); |
| 32 | + |
| 33 | + private final ParameterizedTestMethodContext methodContext; |
| 34 | + private final Arguments arguments; |
| 35 | + |
| 36 | + ArgumentCountValidator(ParameterizedTestMethodContext methodContext, Arguments arguments) { |
| 37 | + this.methodContext = methodContext; |
| 38 | + this.arguments = arguments; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void interceptTestTemplateMethod(InvocationInterceptor.Invocation<Void> invocation, |
| 43 | + ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable { |
| 44 | + validateArgumentCount(extensionContext, arguments); |
| 45 | + invocation.proceed(); |
| 46 | + } |
| 47 | + |
| 48 | + private ExtensionContext.Store getStore(ExtensionContext context) { |
| 49 | + return context.getRoot().getStore(NAMESPACE); |
| 50 | + } |
| 51 | + |
| 52 | + private void validateArgumentCount(ExtensionContext extensionContext, Arguments arguments) { |
| 53 | + ArgumentCountValidationMode argumentCountValidationMode = getArgumentCountValidationMode(extensionContext); |
| 54 | + switch (argumentCountValidationMode) { |
| 55 | + case DEFAULT: |
| 56 | + case NONE: |
| 57 | + return; |
| 58 | + case STRICT: |
| 59 | + int testParamCount = extensionContext.getRequiredTestMethod().getParameterCount(); |
| 60 | + int argumentsCount = arguments.get().length; |
| 61 | + Preconditions.condition(testParamCount == argumentsCount, () -> String.format( |
| 62 | + "Configuration error: the @ParameterizedTest has %s argument(s) but there were %s argument(s) provided.%nNote: the provided arguments are %s", |
| 63 | + testParamCount, argumentsCount, Arrays.toString(arguments.get()))); |
| 64 | + break; |
| 65 | + default: |
| 66 | + throw new ExtensionConfigurationException( |
| 67 | + "Unsupported argument count validation mode: " + argumentCountValidationMode); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private ArgumentCountValidationMode getArgumentCountValidationMode(ExtensionContext extensionContext) { |
| 72 | + ParameterizedTest parameterizedTest = methodContext.annotation; |
| 73 | + if (parameterizedTest.argumentCountValidation() != ArgumentCountValidationMode.DEFAULT) { |
| 74 | + return parameterizedTest.argumentCountValidation(); |
| 75 | + } |
| 76 | + else { |
| 77 | + return getArgumentCountValidationModeConfiguration(extensionContext); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private ArgumentCountValidationMode getArgumentCountValidationModeConfiguration(ExtensionContext extensionContext) { |
| 82 | + String key = ARGUMENT_COUNT_VALIDATION_KEY; |
| 83 | + ArgumentCountValidationMode fallback = ArgumentCountValidationMode.NONE; |
| 84 | + ExtensionContext.Store store = getStore(extensionContext); |
| 85 | + return store.getOrComputeIfAbsent(key, __ -> { |
| 86 | + Optional<String> optionalConfigValue = extensionContext.getConfigurationParameter(key); |
| 87 | + if (optionalConfigValue.isPresent()) { |
| 88 | + String configValue = optionalConfigValue.get(); |
| 89 | + Optional<ArgumentCountValidationMode> enumValue = Arrays.stream( |
| 90 | + ArgumentCountValidationMode.values()).filter( |
| 91 | + mode -> mode.name().equalsIgnoreCase(configValue)).findFirst(); |
| 92 | + if (enumValue.isPresent()) { |
| 93 | + logger.config(() -> String.format( |
| 94 | + "Using ArgumentCountValidationMode '%s' set via the '%s' configuration parameter.", |
| 95 | + enumValue.get().name(), key)); |
| 96 | + return enumValue.get(); |
| 97 | + } |
| 98 | + else { |
| 99 | + logger.warn(() -> String.format( |
| 100 | + "Invalid ArgumentCountValidationMode '%s' set via the '%s' configuration parameter. " |
| 101 | + + "Falling back to the %s default value.", |
| 102 | + configValue, key, fallback.name())); |
| 103 | + return fallback; |
| 104 | + } |
| 105 | + } |
| 106 | + else { |
| 107 | + return fallback; |
| 108 | + } |
| 109 | + }, ArgumentCountValidationMode.class); |
| 110 | + } |
| 111 | +} |
0 commit comments