Commit 245da66d authored by Stein Eldar Johnsen's avatar Stein Eldar Johnsen 💬

Add regex resolver for junit data proviers.

parent 4f9ef495
Pipeline #51933503 failed with stages
in 1 minute and 25 seconds
......@@ -104,6 +104,10 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.tngtech.java</groupId>
<artifactId>junit-dataprovider</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
......@@ -156,6 +160,11 @@
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.tngtech.java</groupId>
<artifactId>junit-dataprovider</artifactId>
<version>1.13.1</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
......
package net.morimekta.testing;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import com.tngtech.java.junit.dataprovider.internal.DefaultDataProviderMethodResolver;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.TestClass;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Use a regex to match data provider method names instead of the default
* name replacement strategy, or simple name matching. This allows for
* depending on multiple data providers for the same test.
*
* <pre>{@code
* {@literal@}UseDataProvider(value = "(getQueries|postQueries)",
* resolveStrategy = AGGREGATE_ALL_MATCHES,
* resolver = RegexDataProviderMethodResolver.class)
* public void testPostGraphql(String query, String responseJson) throws IOException {
* // ...
* }
* }</pre>
*/
public class RegexDataProviderMethodResolver extends DefaultDataProviderMethodResolver {
@Override
protected List<FrameworkMethod> findDataProviderMethods(List<TestClass> locations, String testMethodName, String useDataProviderValue) {
if (UseDataProvider.DEFAULT_VALUE.equals(useDataProviderValue)) {
return super.findDataProviderMethods(locations, testMethodName, useDataProviderValue);
}
Pattern pattern = Pattern.compile(useDataProviderValue);
List<FrameworkMethod> result = new ArrayList<>();
for (TestClass location : locations) {
result.addAll(findDataProviderMethodsByRegex(location, pattern));
}
return result;
}
private List<FrameworkMethod> findDataProviderMethodsByRegex(TestClass location, Pattern pattern) {
List<FrameworkMethod> dataProviderMethods = location.getAnnotatedMethods(DataProvider.class);
List<FrameworkMethod> result = new ArrayList<>();
for (FrameworkMethod method : dataProviderMethods) {
if (pattern.matcher(method.getName()).matches()) {
result.add(method);
}
}
return result;
}
}
package net.morimekta.testing;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.Locale;
import static java.util.Arrays.asList;
import static net.morimekta.testing.DataProviderUtil.buildDataDimensions;
......@@ -11,6 +16,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@RunWith(DataProviderRunner.class)
public class DataProviderUtilTest {
@Test
public void testBuildDataDimension() {
......@@ -51,4 +57,25 @@ public class DataProviderUtilTest {
assertThat(e.getMessage(), is("Empty dimension in layer 2"));
}
}
@DataProvider
public static Object[][] firstProvider() {
return new Object[][] {
{"a", "A"},
};
}
@DataProvider
public static Object[][] secondProvider() {
return new Object[][] {
{"b", "B"},
};
}
@Test
@UseDataProvider(resolver = RegexDataProviderMethodResolver.class,
resolveStrategy = UseDataProvider.ResolveStrategy.AGGREGATE_ALL_MATCHES,
value = "(first|second)Provider")
public void testResolver(String lower, String upper) {
assertThat(lower.toUpperCase(Locale.US), is(upper));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment