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

com.tngtech.java.junit.dataprovider.internal.DefaultDataProviderMethodResolver Maven / Gradle / Ivy

Go to download

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

There is a newer version: 2.10
Show newest version
package com.tngtech.java.junit.dataprovider.internal;

import static com.tngtech.java.junit.dataprovider.common.Preconditions.checkNotNull;
import static java.util.Collections.singletonList;

import java.util.ArrayList;
import java.util.List;

import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.TestClass;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderMethodResolver;
import com.tngtech.java.junit.dataprovider.UseDataProvider;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
 * Default implementation to resolve the dataprovider method for a test method using {@link UseDataProvider} annotation.
 * 

* It is being tried to resolve the dataprovider method using various strategies. The locations of the dataprovider can optionally * specified using {@link UseDataProvider#location()}. If no specific location is specified, test class itself is used (= where * {@code @}{@link UseDataProvider} annotation is used). If multiple locations are specified, each is searched for an appropriate * dataprovider. *

* The name of the dataprovider method can be explicitly set via {@link UseDataProvider#value()} or derived by convention. The first * found dataprovider method will be used. Here are the applied strategies: *

    *
  • Explicitly configured name using {@link UseDataProvider#value()}. (no fallback if dataprovider could not be found)
  • *
  • @{@link DataProvider} annotated method which name equals the test method name
  • *
  • @{@link DataProvider} annotated method whereby prefix is replaced by one out of the following: * * * * * * * * * * * * * *
    prefixreplacement
    testdataProvider
    testdata
    *
  • *
  • @{@link DataProvider} annotated method whereby additional prefix "dataProvider" or "data" is given. Also the first letter of the * original test method name is uppercased, e.g. {@code shouldReturnTwoForOnePlusOne} corresponds to * {@code dataProviderShouldReturnTwoForOnePlusOne}.
  • *
*/ @SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS", justification = "for backwards compatibility and easier migration to core") public class DefaultDataProviderMethodResolver extends com.tngtech.junit.dataprovider.resolver.DefaultDataProviderMethodResolver implements DataProviderMethodResolver { /** * {@inheritDoc} *

* Look at the classes java doc for detailed description of the applied strategies. * * @return the resolved dataprovider method or an empty {@link List} if dataprovider could not be found (never null) * @see DefaultDataProviderMethodResolver */ @Override public List resolve(FrameworkMethod testMethod, UseDataProvider useDataProvider) { checkNotNull(testMethod, "testMethod must not be null"); checkNotNull(useDataProvider, "useDataProvider must not be null"); List dataProviderLocations = findDataProviderLocations(testMethod, useDataProvider.location()); return findDataProviderMethods(dataProviderLocations, testMethod.getName(), useDataProvider.value()); } protected List findDataProviderLocations(FrameworkMethod testMethod, Class[] useDataProviderLocation) { if (useDataProviderLocation.length == 0) { return singletonList(new TestClass(testMethod.getMethod().getDeclaringClass())); } List result = new ArrayList(); for (Class location : useDataProviderLocation) { result.add(new TestClass(location)); } return result; } protected List findDataProviderMethods(List locations, String testMethodName, String useDataProviderValue) { List result = new ArrayList(); for (TestClass location : locations) { FrameworkMethod method = findDataProviderMethod(location, testMethodName, useDataProviderValue); if (method != null) { result.add(method); } } return result; } protected FrameworkMethod findDataProviderMethod(TestClass location, String testMethodName, String useDataProviderValue) { List dataProviderMethods = location.getAnnotatedMethods(DataProvider.class); for (FrameworkMethod dataProviderMethod : dataProviderMethods) { if (UseDataProvider.DEFAULT_VALUE.equals(useDataProviderValue)) { if (isMatchingNameConvention(testMethodName, dataProviderMethod.getName())) { return dataProviderMethod; } } else if (dataProviderMethod.getName().equals(useDataProviderValue)) { return dataProviderMethod; } } return null; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy