001/*
002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.cuioss.test.generator.junit.parameterized;
017
018import de.cuioss.test.generator.TypedGenerator;
019import org.junit.jupiter.api.extension.ExtensionContext;
020import org.junit.jupiter.params.provider.Arguments;
021import org.junit.jupiter.params.support.AnnotationConsumer;
022
023import java.util.stream.Stream;
024
025/**
026 * Implementation of {@link org.junit.jupiter.params.provider.ArgumentsProvider} that provides arguments from a
027 * {@link TypedGenerator} for parameterized tests annotated with
028 * {@link TypeGeneratorSource}.
029 * 
030 * <p>
031 * This provider instantiates the specified {@link TypedGenerator} class and
032 * generates the requested number of values to be used as test arguments.
033 * </p>
034 * 
035 * <p>
036 * Seed management is integrated with the existing
037 * {@link de.cuioss.test.generator.junit.GeneratorControllerExtension} to ensure
038 * consistent and reproducible test data generation.
039 * </p>
040 * 
041 * @author Oliver Wolff
042 * @since 2.0
043 * @see TypeGeneratorSource
044 * @see TypedGenerator
045 */
046public class TypeGeneratorArgumentsProvider extends AbstractTypedGeneratorArgumentsProvider
047        implements AnnotationConsumer<TypeGeneratorSource> {
048
049    private Class<? extends TypedGenerator<?>> generatorClass;
050    private int count;
051    private long seed;
052
053    @Override
054    public void accept(TypeGeneratorSource annotation) {
055        generatorClass = annotation.value();
056        count = Math.max(1, annotation.count());
057        seed = annotation.seed();
058    }
059
060    @Override
061    protected Stream<? extends Arguments> provideArgumentsForGenerators(ExtensionContext context) {
062        // Create generator instance
063        var generator = createGeneratorInstance(generatorClass);
064
065        // Generate values
066        return generateArguments(generator).stream();
067    }
068
069    @Override
070    protected long getSeed() {
071        return seed;
072    }
073
074    @Override
075    protected int getCount() {
076        return count;
077    }
078}