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

com.insightfullogic.lambdabehave.impl.specifications.TitledTable Maven / Gradle / Ivy

There is a newer version: 0.4
Show newest version
package com.insightfullogic.lambdabehave.impl.specifications;

import com.insightfullogic.lambdabehave.impl.Specifier;
import com.insightfullogic.lambdabehave.specifications.ColumnDataSpecification;
import org.mockito.cglib.proxy.Enhancer;
import org.mockito.cglib.proxy.MethodInterceptor;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TitledTable {
    private final List methods;
    private final Specifier specifier;
    private final Class clazz;

    private final List firsts = new ArrayList<>();
    private final List seconds = new ArrayList<>();

    public TitledTable(final List methods, final Specifier specifier, final Class clazz) {
        this.methods = methods;
        this.specifier = specifier;
        this.clazz = clazz;
    }

    public TitledTable toShow(final String description, final ColumnDataSpecification specification) {
        final Iterator fit = firsts.iterator();
        final Iterator sit = seconds.iterator();
        while (fit.hasNext()) {
            Object[] values = new Object[]{fit.next(), sit.next()};
            final T mock = (T) Enhancer.create(clazz, (MethodInterceptor) (o, method, objects, methodProxy) -> values[methods.indexOf(method)]);
            final String describe = String.format(description,
                    values[0], values[1],
                    methods.get(0).getName(), methods.get(1).getName());
            specifier.specifyBehaviour(describe, mock, specification);
        }
        return this;
    }

    public TitledTable withRow(final F first, final S second) {
        firsts.add(first);
        seconds.add(second);
        return this;
    }
}