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

com.tngtech.java.junit.dataprovider.DataProviderFrameworkMethod Maven / Gradle / Ivy

Go to download

A TestNG like dataprovider runner for JUnit having a simplified syntax compared to all the existing JUnit4 features.

The newest version!
/*
 * Copyright 2019 TNG Technology Consulting GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tngtech.java.junit.dataprovider;

import static com.tngtech.java.junit.dataprovider.common.Preconditions.checkArgument;
import static com.tngtech.java.junit.dataprovider.common.Preconditions.checkNotNull;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

import org.junit.runners.model.FrameworkMethod;

import com.tngtech.java.junit.dataprovider.format.DataProviderPlaceholderFormatter;
import com.tngtech.junit.dataprovider.format.DataProviderTestNameFormatter;

/**
 * A special framework method that allows the usage of parameters for the test method.
 */
public class DataProviderFrameworkMethod extends FrameworkMethod {

    /**
     * Index of exploded test method such that each get a unique name.
     * 

* This field is package private (= visible) for testing. *

*/ final int idx; /** * Parameters to invoke the test method. *

* This field is package private (= visible) for testing. *

*/ final Object[] parameters; /** * Format of test method name. *

* This field is package private (= visible) for testing. *

*/ final String nameFormat; /** * Formatter for test method name. May be {@code null}. *

* This field is package private (= visible) for testing. *

*/ final Class nameFormatter; /** * Create a {@link FrameworkMethod} extended with special attributes for using this test with a dataprovider. * * @param method test method for which the {@link FrameworkMethod} is created * @param idx the index (row) of the used dataprovider * @param parameters used for invoking this test method * @param nameFormat defines the format of the test method name according to {@code @}{@link DataProvider#format()} */ public DataProviderFrameworkMethod(Method method, int idx, Object[] parameters, String nameFormat) { this(method, idx, parameters, nameFormat, null); } /** * Create a {@link FrameworkMethod} extended with special attributes for using this test with a dataprovider. * * @param method test method for which the {@link FrameworkMethod} is created * @param idx the index (row) of the used dataprovider * @param parameters used for invoking this test method * @param nameFormat defines the format of the test method name according to {@code @}{@link DataProvider#format()} * @param nameFormatter defines the test method name formatter */ public DataProviderFrameworkMethod(Method method, int idx, Object[] parameters, String nameFormat, Class nameFormatter) { super(method); checkNotNull(parameters, "parameter must not be null"); checkNotNull(nameFormat, "nameFormat must not be null"); checkArgument(parameters.length != 0, "parameter must not be empty"); this.idx = idx; this.parameters = Arrays.copyOf(parameters, parameters.length); this.nameFormat = nameFormat; this.nameFormatter = nameFormatter; } @Override public String getName() { if (nameFormatter == null || DataProviderPlaceholderFormatter.class.equals(nameFormatter)) { return new DataProviderPlaceholderFormatter(nameFormat, Placeholders.all()).format(getMethod(), idx, Arrays.asList(parameters)); } try { return nameFormatter.getDeclaredConstructor().newInstance().format(getMethod(), idx, Arrays.asList(parameters)); } catch (InstantiationException e) { throw new IllegalStateException(String .format("Could not instantiate name formatter using default constructor '%s'.", nameFormatter), e); } catch (IllegalAccessException e) { throw new IllegalStateException( String.format("Default constructor not accessable of name formatter '%s'.", nameFormatter), e); } catch (InvocationTargetException e) { throw new IllegalStateException(String.format("Default constructor of name formatter '%s' has thrown: %s", nameFormatter, e.getMessage()), e); } catch (NoSuchMethodException e) { throw new IllegalStateException( String.format("Default constructor not found for name formatter '%s'.", nameFormatter), e); } catch (Exception e) { throw new IllegalStateException(String.format( "Unexpected exception while finding and invoking default constructor of name formatter '%s': %s", nameFormatter, e.getMessage()), e); } } @Override public Object invokeExplosively(Object target, Object... params) throws Throwable { return super.invokeExplosively(target, parameters); } @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + idx; result = prime * result + ((nameFormat == null) ? 0 : nameFormat.hashCode()); result = prime * result + Arrays.hashCode(parameters); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!super.equals(obj)) { return false; } if (getClass() != obj.getClass()) { return false; } DataProviderFrameworkMethod other = (DataProviderFrameworkMethod) obj; if (idx != other.idx) { return false; } if (nameFormat == null) { if (other.nameFormat != null) { return false; } } else if (!nameFormat.equals(other.nameFormat)) { return false; } if (!Arrays.equals(parameters, other.parameters)) { return false; } return true; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy