001/**
002 * The MIT License (MIT)
003 *
004 * Copyright (c) 2017 tools4j.org (Marco Terzer)
005 *
006 * Permission is hereby granted, free of charge, to any person obtaining a copy
007 * of this software and associated documentation files (the "Software"), to deal
008 * in the Software without restriction, including without limitation the rights
009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010 * copies of the Software, and to permit persons to whom the Software is
011 * furnished to do so, subject to the following conditions:
012 *
013 * The above copyright notice and this permission notice shall be included in all
014 * copies or substantial portions of the Software.
015 *
016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022 * SOFTWARE.
023 */
024package org.tools4j.spockito;
025
026import org.junit.runners.model.FrameworkMethod;
027
028import java.lang.reflect.Method;
029import java.util.Objects;
030
031/**
032 * Extension of framwork method representing a test method with a data row.
033 */
034public class UnrolledTestMethod extends FrameworkMethod {
035
036    private final TableRow tableRow;
037    private final ValueConverter valueConverter;
038
039    public UnrolledTestMethod(final Method method, final TableRow tableRow, final ValueConverter valueConverter) {
040        super(method);
041        this.tableRow = Objects.requireNonNull(tableRow);
042        this.valueConverter = Objects.requireNonNull(valueConverter);
043    }
044
045    @Override
046    public Object invokeExplosively(final Object target, final Object... params) throws Throwable {
047        return super.invokeExplosively(target, getTestArgs());
048    }
049
050    protected TableRow getTableRow() {
051        return tableRow;
052    }
053
054    protected Object[] getTestArgs() {
055        return tableRow.convertValues(getMethod(), valueConverter);
056    }
057
058    @Override
059    public String getName() {
060        return super.getName();
061    }
062
063    @Override
064    public int hashCode() {
065        int code = super.hashCode();
066        code = 31 * code + tableRow.getTable().hashCode();
067        code = 31 * code + tableRow.getRowIndex();
068        return code;
069    }
070
071    @Override
072    public boolean equals(final Object obj) {
073        if (obj == this) {
074            return true;
075        }
076        if (obj == null || obj.getClass() != getClass() || !super.equals(obj)) {
077            return false;
078        }
079        final UnrolledTestMethod other = (UnrolledTestMethod)obj;
080        return this.tableRow.getTable().equals(other.tableRow.getTable()) &&
081                this.tableRow.getRowIndex() == other.tableRow.getRowIndex();
082    }
083
084    @Override
085    public String toString() {
086        return super.toString() + "[" + tableRow.getRowIndex() + "]";
087    }
088}