-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first hacky idea, not sure if this should be within core or a custom …
…extension, done by the user ... (#101)
- Loading branch information
Showing
8 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...a/com/tngtech/test/junit/dataprovider/custom/argformat/AnnotationArgumentPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.tngtech.test.junit.dataprovider.custom.argformat; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Parameter; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import com.tngtech.junit.dataprovider.placeholder.AbstractArgumentPlaceholder; | ||
import com.tngtech.junit.dataprovider.placeholder.NamedArgumentPlaceholder; | ||
import com.tngtech.junit.dataprovider.placeholder.ReplacementData; | ||
|
||
class AnnotationArgumentPlaceholder extends AbstractArgumentPlaceholder { | ||
|
||
private static final Logger logger = Logger.getLogger(NamedArgumentPlaceholder.class.getName()); | ||
|
||
public AnnotationArgumentPlaceholder() { | ||
super("%aa\\[(-?[0-9]+|-?[0-9]+\\.\\.-?[0-9]+)\\]"); | ||
} | ||
|
||
@Override | ||
protected String getReplacementFor(String placeholder, ReplacementData data) { | ||
String subscript = placeholder.substring(4, placeholder.length() - 1); | ||
|
||
int from = Integer.MAX_VALUE; | ||
int to = Integer.MIN_VALUE; | ||
if (subscript.contains("..")) { | ||
String[] split = subscript.split("\\.\\."); | ||
|
||
from = Integer.parseInt(split[0]); | ||
to = Integer.parseInt(split[1]); | ||
} else { | ||
from = Integer.parseInt(subscript); | ||
to = from; | ||
} | ||
|
||
data.getTestMethod().getAnnotatedParameterTypes(); | ||
|
||
List<Object> arguments = data.getArguments(); | ||
from = (from >= 0) ? from : arguments.size() + from; | ||
to = (to >= 0) ? to + 1 : arguments.size() + to + 1; | ||
return formatAll(getSubArrayOfMethodParameters(data.getTestMethod(), from, to), | ||
arguments.subList(from, to)); | ||
} | ||
|
||
/** | ||
* Formats the given parameters and arguments to a comma-separated list of {@code $parameterName=$argumentName}. | ||
* Arguments {@link String} representation are therefore treated specially. | ||
* | ||
* @param parameters used to for formatting | ||
* @param arguments to be formatted | ||
* @return the formatted {@link String} of the given {@link Parameter}{@code []} and {@link List}{@code <Object>} | ||
*/ | ||
protected String formatAll(Parameter[] parameters, List<Object> arguments) { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
for (int idx = 0; idx < arguments.size(); idx++) { | ||
Parameter parameter = parameters[idx]; | ||
Object argument = arguments.get(idx); | ||
|
||
Format annotation = parameter.getAnnotation(Format.class); | ||
if (annotation != null) { | ||
try { | ||
stringBuilder.append(annotation.value().newInstance().format(argument)); | ||
|
||
} catch (InstantiationException | IllegalAccessException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} else { | ||
stringBuilder.append(format(argument)); | ||
} | ||
|
||
if (idx < arguments.size() - 1) { | ||
stringBuilder.append(", "); | ||
} | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
|
||
private Parameter[] getSubArrayOfMethodParameters(Method testMethod, int fromIndex, int toIndex) { | ||
Parameter[] parameters = testMethod.getParameters(); | ||
if (parameters.length > 0 && !parameters[0].isNamePresent()) { | ||
logger.warning(String.format("Parameter names on method '%s' are not available" | ||
+ ". To store formal parameter names, compile the source file with the '-parameters' option" | ||
+ ". See also https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html", | ||
testMethod)); | ||
} | ||
return Arrays.copyOfRange(parameters, fromIndex, toIndex); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...test/junit/dataprovider/custom/argformat/AnnotationArgumentPlaceholderAcceptanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.tngtech.test.junit.dataprovider.custom.argformat; | ||
|
||
import org.junit.jupiter.api.TestTemplate; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import com.tngtech.junit.dataprovider.DataProvider; | ||
import com.tngtech.junit.dataprovider.UseDataProvider; | ||
|
||
@ExtendWith(AnnotationArgumentPlaceholderDataProviderExtension.class) | ||
class AnnotationArgumentPlaceholderAcceptanceTest { | ||
|
||
@DataProvider(format = "[%i: %aa[0..-1]]") | ||
static Object[][] dataProvider() { | ||
// @formatter:off | ||
return new Object[][] { | ||
{ new WrappedClass(AnnotationArgumentPlaceholderAcceptanceTest.class) }, | ||
}; | ||
// @formatter:on | ||
} | ||
|
||
@TestTemplate | ||
@UseDataProvider | ||
void test(@Format(UnwrapFormatter.class) WrappedClass clazz) { | ||
// TODO | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...nit/dataprovider/custom/argformat/AnnotationArgumentPlaceholderDataProviderExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.tngtech.test.junit.dataprovider.custom.argformat; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.platform.commons.support.ReflectionSupport; | ||
|
||
import com.tngtech.junit.dataprovider.DataProvider; | ||
import com.tngtech.junit.dataprovider.DisplayNameContext; | ||
import com.tngtech.junit.dataprovider.UseDataProvider; | ||
import com.tngtech.junit.dataprovider.UseDataProviderInvocationContextProvider; | ||
import com.tngtech.junit.dataprovider.convert.ConverterContext; | ||
import com.tngtech.junit.dataprovider.placeholder.BasePlaceholder; | ||
import com.tngtech.junit.dataprovider.resolver.DataProviderResolverContext; | ||
|
||
class AnnotationArgumentPlaceholderDataProviderExtension | ||
extends UseDataProviderInvocationContextProvider<UseDataProvider, DataProvider> { | ||
|
||
AnnotationArgumentPlaceholderDataProviderExtension() { | ||
super(UseDataProvider.class, DataProvider.class); | ||
} | ||
|
||
@Override | ||
protected DataProviderResolverContext getDataProviderResolverContext(ExtensionContext extensionContext, | ||
UseDataProvider testAnnotation) { | ||
return new DataProviderResolverContext(extensionContext.getRequiredTestMethod(), | ||
asList(testAnnotation.resolver()), testAnnotation.resolveStrategy(), asList(testAnnotation.location()), | ||
DataProvider.class, testAnnotation.value()); | ||
} | ||
|
||
@Override | ||
protected ConverterContext getConverterContext(DataProvider dataProvider) { | ||
return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()), | ||
ReflectionSupport.newInstance(dataProvider.singleArgConverter()), | ||
ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(), | ||
dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase()); | ||
} | ||
|
||
@Override | ||
protected DisplayNameContext getDisplayNameContext(DataProvider dataProvider) { | ||
@SuppressWarnings("unchecked") | ||
List<BasePlaceholder> defaultPlaceholders = (List<BasePlaceholder>) getDefaultPlaceholders(); | ||
defaultPlaceholders.add(0, new AnnotationArgumentPlaceholder()); | ||
return new DisplayNameContext(dataProvider.format(), defaultPlaceholders); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ntegTest/java/com/tngtech/test/junit/dataprovider/custom/argformat/ArgumentFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.tngtech.test.junit.dataprovider.custom.argformat; | ||
|
||
public interface ArgumentFormatter<T> { | ||
|
||
String format(T argument); | ||
} |
11 changes: 11 additions & 0 deletions
11
...piter/src/integTest/java/com/tngtech/test/junit/dataprovider/custom/argformat/Format.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.tngtech.test.junit.dataprovider.custom.argformat; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Format { | ||
|
||
Class<? extends ArgumentFormatter> value(); | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
.../integTest/java/com/tngtech/test/junit/dataprovider/custom/argformat/UnwrapFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.tngtech.test.junit.dataprovider.custom.argformat; | ||
|
||
public class UnwrapFormatter implements ArgumentFormatter<WrappedClass> { | ||
|
||
@Override | ||
public String format(WrappedClass argument) { | ||
return argument.getWrappedClazz().getSimpleName(); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...src/integTest/java/com/tngtech/test/junit/dataprovider/custom/argformat/WrappedClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.tngtech.test.junit.dataprovider.custom.argformat; | ||
|
||
class WrappedClass { | ||
private final Class<?> clazz; | ||
|
||
WrappedClass(Class<?> clazz) { | ||
this.clazz = clazz; | ||
} | ||
|
||
public Class<?> getWrappedClazz() { | ||
return clazz; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "WrappedClass [clazz=" + clazz + "]"; | ||
} | ||
} |