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

com.github.nylle.javafixture.specimen.InterfaceSpecimen Maven / Gradle / Ivy

Go to download

JavaFixture is the attempt to bring Mark Seemann's AutoFixture for .NET to the Java world. Its purpose is to generate full object graphs for use in test suites with a fluent API for customising the test objects during generation.

There is a newer version: 2.11.0
Show newest version
package com.github.nylle.javafixture.specimen;

import com.github.nylle.javafixture.Context;
import com.github.nylle.javafixture.ReflectionHelper;
import com.github.nylle.javafixture.ISpecimen;
import com.github.nylle.javafixture.SpecimenFactory;
import com.github.nylle.javafixture.SpecimenType;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

public class InterfaceSpecimen implements ISpecimen {

    private final Class type;
    private final Context context;
    private final SpecimenFactory specimenFactory;
    private final SpecimenType specimenType;

    public InterfaceSpecimen(final Class type, final Context context, final SpecimenFactory specimenFactory) {

        if(type == null) {
            throw new IllegalArgumentException("type: null");
        }

        if (context == null) {
            throw new IllegalArgumentException("context: null");
        }

        if (specimenFactory == null) {
            throw new IllegalArgumentException("specimenFactory: null");
        }

        if(!type.isInterface() || ReflectionHelper.isMap(type) || ReflectionHelper.isCollection(type)) {
            throw new IllegalArgumentException("type: " + type.getName());
        }

        this.type = type;
        this.context = context;
        this.specimenFactory = specimenFactory;
        this.specimenType = SpecimenType.forObject(type);
    }

    @Override
    public T create() {
        if(context.isCached(specimenType)){
            return (T) context.cached(specimenType);
        }

        return (T) context.cached(specimenType, Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, new GenericInvocationHandler()));
    }

    class GenericInvocationHandler implements InvocationHandler {

        private Map methodResults = new HashMap<>();

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) {
            if(method.getReturnType() != void.class) {
                methodResults.computeIfAbsent(method.toString(), x -> specimenFactory.build(method.getReturnType()).create());
                return methodResults.get(method.toString());
            }

            return null;
        }
    }


}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy