-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db67385
commit cc1c541
Showing
17 changed files
with
341 additions
and
87 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
core/src/main/java/io/cucumber/core/backend/DataTableTypeTypeDefinition.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 io.cucumber.core.backend; | ||
|
||
import io.cucumber.datatable.DataTableType; | ||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.EXPERIMENTAL) | ||
public interface DataTableTypeTypeDefinition { | ||
|
||
DataTableType dataTableType(); | ||
|
||
} |
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
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
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
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,23 @@ | ||
package io.cucumber.java; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Allows a DataTableType to be registered. | ||
* | ||
* Supports TableCellTransformer: String -> T | ||
* Supports TableEntryTransformer: Map<String, String> -> T | ||
* Supports TableRowTransformer: List<String> -> T | ||
* Supports TableTransformer: DataTable -> T | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
@API(status = API.Status.STABLE) | ||
public @interface DataTableType { | ||
|
||
} |
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
122 changes: 122 additions & 0 deletions
122
java/src/main/java/io/cucumber/java/JavaDataTableTypeDefinition.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,122 @@ | ||
package io.cucumber.java; | ||
|
||
import io.cucumber.core.backend.DataTableTypeTypeDefinition; | ||
import io.cucumber.core.backend.Lookup; | ||
import io.cucumber.core.exception.CucumberException; | ||
import io.cucumber.core.runtime.Invoker; | ||
import io.cucumber.datatable.DataTable; | ||
import io.cucumber.datatable.DataTableType; | ||
import io.cucumber.datatable.TableCellTransformer; | ||
import io.cucumber.datatable.TableEntryTransformer; | ||
import io.cucumber.datatable.TableRowTransformer; | ||
import io.cucumber.datatable.TableTransformer; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
class JavaDataTableTypeDefinition implements DataTableTypeTypeDefinition { | ||
|
||
private final Method method; | ||
private final Lookup lookup; | ||
private final DataTableType dataTableType; | ||
|
||
JavaDataTableTypeDefinition(Method method, Lookup lookup) { | ||
this.method = method; | ||
this.lookup = lookup; | ||
this.dataTableType = createDataTableType(method); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private DataTableType createDataTableType(Method method) { | ||
Class returnType = requireValidReturnType(method); | ||
Type parameterType = requireValidParameterType(method); | ||
|
||
if (DataTable.class.equals(parameterType)) { | ||
return new DataTableType( | ||
returnType, | ||
(TableTransformer<Object>) this::execute | ||
); | ||
} | ||
|
||
if (List.class.equals(parameterType)) { | ||
return new DataTableType( | ||
returnType, | ||
(TableRowTransformer<Object>) this::execute | ||
); | ||
} | ||
|
||
if (Map.class.equals(parameterType)) { | ||
return new DataTableType( | ||
returnType, | ||
(TableEntryTransformer<Object>) this::execute | ||
); | ||
} | ||
|
||
if (String.class.equals(parameterType)) { | ||
return new DataTableType( | ||
returnType, | ||
(TableCellTransformer<Object>) this::execute | ||
); | ||
} | ||
|
||
throw createInvalidSignatureException(); | ||
|
||
} | ||
|
||
private static CucumberException createInvalidSignatureException() { | ||
return new CucumberException("" + | ||
"A @DataTableType annotated method must have one of these signatures:\n" + | ||
" * public Author author(DataTable table)\n" + | ||
" * public Author author(List<String> row)\n" + | ||
" * public Author author(Map<String, String> entry)\n" + | ||
" * public Author author(String cell)\n" + | ||
"Note: Author is an example of the class you want to convert the table to" | ||
); | ||
} | ||
|
||
|
||
private static Type requireValidParameterType(Method method) { | ||
Type[] parameterTypes = method.getGenericParameterTypes(); | ||
if (parameterTypes.length != 1) { | ||
throw createInvalidSignatureException(); | ||
} | ||
|
||
Type parameterType = parameterTypes[0]; | ||
if (!(parameterType instanceof ParameterizedType)) { | ||
return parameterType; | ||
} | ||
|
||
ParameterizedType parameterizedType = (ParameterizedType) parameterType; | ||
Type[] typeParameters = parameterizedType.getActualTypeArguments(); | ||
for (Type typeParameter : typeParameters) { | ||
if (!String.class.equals(typeParameter)) { | ||
throw createInvalidSignatureException(); | ||
} | ||
} | ||
|
||
return parameterizedType.getRawType(); | ||
} | ||
|
||
private static Class requireValidReturnType(Method method) { | ||
Class returnType = method.getReturnType(); | ||
if (Void.class.equals(returnType)) { | ||
throw createInvalidSignatureException(); | ||
} | ||
return returnType; | ||
} | ||
|
||
|
||
@Override | ||
public DataTableType dataTableType() { | ||
return dataTableType; | ||
} | ||
|
||
|
||
private Object execute(Object arg) throws Throwable { | ||
return Invoker.invoke(lookup.getInstance(method.getDeclaringClass()), method, 0, arg); | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.