Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include SourceReferences in message output
Cucumber JVM can not reference files by URI. So it should use either java methods or stack trace elements as a source reference instead. For example: ``` { "hook": { "id": "a1839ec6-f75d-4029-9b75-4b8203d8b2e8", "sourceReference": { "javaMethod": { "className": "io.cucumber.compatibility.attachments.Attachments", "methodName": "before", "methodParameterTypes": [ "io.cucumber.java.Scenario" ] } } } } ``` See: cucumber/common#1119 Fixes: #2058
- Loading branch information
1 parent
41d45c5
commit 088ae40
Showing
12 changed files
with
223 additions
and
15 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
53 changes: 53 additions & 0 deletions
53
core/src/main/java/io/cucumber/core/backend/JavaMethodReference.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,53 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public final class JavaMethodReference implements SourceReference { | ||
|
||
private final String className; | ||
private final String methodName; | ||
private final List<String> methodParameterTypes; | ||
|
||
JavaMethodReference(Class<?> declaringClass, String methodName, Class<?>[] methodParameterTypes) { | ||
this.className = requireNonNull(declaringClass).getName(); | ||
this.methodName = requireNonNull(methodName); | ||
this.methodParameterTypes = new ArrayList<>(methodParameterTypes.length); | ||
for (Class<?> parameterType : methodParameterTypes) { | ||
this.methodParameterTypes.add(parameterType.getName()); | ||
} | ||
} | ||
|
||
public String className() { | ||
return className; | ||
} | ||
|
||
public String methodName() { | ||
return methodName; | ||
} | ||
|
||
public List<String> methodParameterTypes() { | ||
return methodParameterTypes; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
JavaMethodReference that = (JavaMethodReference) o; | ||
return className.equals(that.className) && | ||
methodName.equals(that.methodName) && | ||
methodParameterTypes.equals(that.methodParameterTypes); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(className, methodName, methodParameterTypes); | ||
} | ||
|
||
} |
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
22 changes: 22 additions & 0 deletions
22
core/src/main/java/io/cucumber/core/backend/SourceReference.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,22 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public interface SourceReference { | ||
|
||
static SourceReference fromMethod(Method method) { | ||
return new JavaMethodReference( | ||
method.getDeclaringClass(), | ||
method.getName(), | ||
method.getParameterTypes()); | ||
} | ||
|
||
static SourceReference fromStackTraceElement(StackTraceElement stackTraceElement) { | ||
return new StackTraceElementReference( | ||
stackTraceElement.getClassName(), | ||
stackTraceElement.getMethodName(), | ||
stackTraceElement.getFileName(), | ||
stackTraceElement.getLineNumber()); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
core/src/main/java/io/cucumber/core/backend/StackTraceElementReference.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,56 @@ | ||
package io.cucumber.core.backend; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class StackTraceElementReference implements SourceReference { | ||
|
||
private final String className; | ||
private final String methodName; | ||
private final String fileName; | ||
private final int lineNumber; | ||
|
||
StackTraceElementReference(String className, String methodName, String fileName, int lineNumber) { | ||
this.className = requireNonNull(className); | ||
this.methodName = requireNonNull(methodName); | ||
this.fileName = fileName; | ||
this.lineNumber = lineNumber; | ||
} | ||
|
||
public String className() { | ||
return className; | ||
} | ||
|
||
public String methodName() { | ||
return methodName; | ||
} | ||
|
||
public Optional<String> fileName() { | ||
return Optional.ofNullable(fileName); | ||
} | ||
|
||
public int lineNumber() { | ||
return lineNumber; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
StackTraceElementReference that = (StackTraceElementReference) o; | ||
return lineNumber == that.lineNumber && | ||
className.equals(that.className) && | ||
methodName.equals(that.methodName) && | ||
Objects.equals(fileName, that.fileName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(className, methodName, fileName, lineNumber); | ||
} | ||
|
||
} |
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
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